/src/mozilla-central/dom/presentation/PresentationConnectionList.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "PresentationConnectionList.h" |
8 | | |
9 | | #include "nsContentUtils.h" |
10 | | #include "mozilla/AsyncEventDispatcher.h" |
11 | | #include "mozilla/dom/PresentationConnectionAvailableEvent.h" |
12 | | #include "mozilla/dom/PresentationConnectionListBinding.h" |
13 | | #include "mozilla/dom/Promise.h" |
14 | | #include "PresentationConnection.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | NS_IMPL_CYCLE_COLLECTION_INHERITED(PresentationConnectionList, DOMEventTargetHelper, |
20 | | mGetConnectionListPromise, |
21 | | mConnections) |
22 | | |
23 | | NS_IMPL_ADDREF_INHERITED(PresentationConnectionList, DOMEventTargetHelper) |
24 | | NS_IMPL_RELEASE_INHERITED(PresentationConnectionList, DOMEventTargetHelper) |
25 | | |
26 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PresentationConnectionList) |
27 | 0 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
28 | | |
29 | | PresentationConnectionList::PresentationConnectionList(nsPIDOMWindowInner* aWindow, |
30 | | Promise* aPromise) |
31 | | : DOMEventTargetHelper(aWindow) |
32 | | , mGetConnectionListPromise(aPromise) |
33 | 0 | { |
34 | 0 | MOZ_ASSERT(aWindow); |
35 | 0 | MOZ_ASSERT(aPromise); |
36 | 0 | } |
37 | | |
38 | | /* virtual */ JSObject* |
39 | | PresentationConnectionList::WrapObject(JSContext* aCx, |
40 | | JS::Handle<JSObject*> aGivenProto) |
41 | 0 | { |
42 | 0 | return PresentationConnectionList_Binding::Wrap(aCx, this, aGivenProto); |
43 | 0 | } |
44 | | |
45 | | void |
46 | | PresentationConnectionList::GetConnections( |
47 | | nsTArray<RefPtr<PresentationConnection>>& aConnections) const |
48 | 0 | { |
49 | 0 | if (nsContentUtils::ShouldResistFingerprinting()) { |
50 | 0 | aConnections.Clear(); |
51 | 0 | return; |
52 | 0 | } |
53 | 0 | |
54 | 0 | aConnections = mConnections; |
55 | 0 | } |
56 | | |
57 | | nsresult |
58 | | PresentationConnectionList::DispatchConnectionAvailableEvent( |
59 | | PresentationConnection* aConnection) |
60 | 0 | { |
61 | 0 | if (nsContentUtils::ShouldResistFingerprinting()) { |
62 | 0 | return NS_OK; |
63 | 0 | } |
64 | 0 | |
65 | 0 | PresentationConnectionAvailableEventInit init; |
66 | 0 | init.mConnection = aConnection; |
67 | 0 |
|
68 | 0 | RefPtr<PresentationConnectionAvailableEvent> event = |
69 | 0 | PresentationConnectionAvailableEvent::Constructor( |
70 | 0 | this, |
71 | 0 | NS_LITERAL_STRING("connectionavailable"), |
72 | 0 | init); |
73 | 0 |
|
74 | 0 | if (NS_WARN_IF(!event)) { |
75 | 0 | return NS_ERROR_FAILURE; |
76 | 0 | } |
77 | 0 | event->SetTrusted(true); |
78 | 0 |
|
79 | 0 | RefPtr<AsyncEventDispatcher> asyncDispatcher = |
80 | 0 | new AsyncEventDispatcher(this, event); |
81 | 0 | return asyncDispatcher->PostDOMEvent(); |
82 | 0 | } |
83 | | |
84 | | PresentationConnectionList::ConnectionArrayIndex |
85 | | PresentationConnectionList::FindConnectionById( |
86 | | const nsAString& aId) |
87 | 0 | { |
88 | 0 | for (ConnectionArrayIndex i = 0; i < mConnections.Length(); i++) { |
89 | 0 | nsAutoString id; |
90 | 0 | mConnections[i]->GetId(id); |
91 | 0 | if (id == nsAutoString(aId)) { |
92 | 0 | return i; |
93 | 0 | } |
94 | 0 | } |
95 | 0 |
|
96 | 0 | return ConnectionArray::NoIndex; |
97 | 0 | } |
98 | | |
99 | | void |
100 | | PresentationConnectionList::NotifyStateChange(const nsAString& aSessionId, |
101 | | PresentationConnection* aConnection) |
102 | 0 | { |
103 | 0 | if (!aConnection) { |
104 | 0 | MOZ_ASSERT(false, "PresentationConnection can not be null."); |
105 | 0 | return; |
106 | 0 | } |
107 | 0 |
|
108 | 0 | bool connectionFound = |
109 | 0 | FindConnectionById(aSessionId) != ConnectionArray::NoIndex ? true : false; |
110 | 0 |
|
111 | 0 | PresentationConnectionList_Binding::ClearCachedConnectionsValue(this); |
112 | 0 | switch (aConnection->State()) { |
113 | 0 | case PresentationConnectionState::Connected: |
114 | 0 | if (!connectionFound) { |
115 | 0 | mConnections.AppendElement(aConnection); |
116 | 0 | if (mGetConnectionListPromise) { |
117 | 0 | if (!nsContentUtils::ShouldResistFingerprinting()) { |
118 | 0 | mGetConnectionListPromise->MaybeResolve(this); |
119 | 0 | } |
120 | 0 | mGetConnectionListPromise = nullptr; |
121 | 0 | return; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | DispatchConnectionAvailableEvent(aConnection); |
125 | 0 | break; |
126 | 0 | case PresentationConnectionState::Terminated: |
127 | 0 | if (connectionFound) { |
128 | 0 | mConnections.RemoveElement(aConnection); |
129 | 0 | } |
130 | 0 | break; |
131 | 0 | default: |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | } // namespace dom |
137 | | } // namespace mozilla |