Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "mozilla/AsyncEventDispatcher.h"
10 : #include "mozilla/dom/PresentationConnectionAvailableEvent.h"
11 : #include "mozilla/dom/PresentationConnectionListBinding.h"
12 : #include "mozilla/dom/Promise.h"
13 : #include "PresentationConnection.h"
14 :
15 : namespace mozilla {
16 : namespace dom {
17 :
18 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(PresentationConnectionList, DOMEventTargetHelper,
19 : mGetConnectionListPromise,
20 : mConnections)
21 :
22 0 : NS_IMPL_ADDREF_INHERITED(PresentationConnectionList, DOMEventTargetHelper)
23 0 : NS_IMPL_RELEASE_INHERITED(PresentationConnectionList, DOMEventTargetHelper)
24 :
25 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(PresentationConnectionList)
26 0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
27 :
28 0 : PresentationConnectionList::PresentationConnectionList(nsPIDOMWindowInner* aWindow,
29 0 : Promise* aPromise)
30 : : DOMEventTargetHelper(aWindow)
31 0 : , mGetConnectionListPromise(aPromise)
32 : {
33 0 : MOZ_ASSERT(aWindow);
34 0 : MOZ_ASSERT(aPromise);
35 0 : }
36 :
37 : /* virtual */ JSObject*
38 0 : PresentationConnectionList::WrapObject(JSContext* aCx,
39 : JS::Handle<JSObject*> aGivenProto)
40 : {
41 0 : return PresentationConnectionListBinding::Wrap(aCx, this, aGivenProto);
42 : }
43 :
44 : void
45 0 : PresentationConnectionList::GetConnections(
46 : nsTArray<RefPtr<PresentationConnection>>& aConnections) const
47 : {
48 0 : aConnections = mConnections;
49 0 : }
50 :
51 : nsresult
52 0 : PresentationConnectionList::DispatchConnectionAvailableEvent(
53 : PresentationConnection* aConnection)
54 : {
55 0 : PresentationConnectionAvailableEventInit init;
56 0 : init.mConnection = aConnection;
57 :
58 : RefPtr<PresentationConnectionAvailableEvent> event =
59 0 : PresentationConnectionAvailableEvent::Constructor(
60 : this,
61 0 : NS_LITERAL_STRING("connectionavailable"),
62 0 : init);
63 :
64 0 : if (NS_WARN_IF(!event)) {
65 0 : return NS_ERROR_FAILURE;
66 : }
67 0 : event->SetTrusted(true);
68 :
69 : RefPtr<AsyncEventDispatcher> asyncDispatcher =
70 0 : new AsyncEventDispatcher(this, event);
71 0 : return asyncDispatcher->PostDOMEvent();
72 : }
73 :
74 : PresentationConnectionList::ConnectionArrayIndex
75 0 : PresentationConnectionList::FindConnectionById(
76 : const nsAString& aId)
77 : {
78 0 : for (ConnectionArrayIndex i = 0; i < mConnections.Length(); i++) {
79 0 : nsAutoString id;
80 0 : mConnections[i]->GetId(id);
81 0 : if (id == nsAutoString(aId)) {
82 0 : return i;
83 : }
84 : }
85 :
86 0 : return mConnections.NoIndex;
87 : }
88 :
89 : void
90 0 : PresentationConnectionList::NotifyStateChange(const nsAString& aSessionId,
91 : PresentationConnection* aConnection)
92 : {
93 0 : if (!aConnection) {
94 0 : MOZ_ASSERT(false, "PresentationConnection can not be null.");
95 : return;
96 : }
97 :
98 : bool connectionFound =
99 0 : FindConnectionById(aSessionId) != mConnections.NoIndex ? true : false;
100 :
101 0 : PresentationConnectionListBinding::ClearCachedConnectionsValue(this);
102 0 : switch (aConnection->State()) {
103 : case PresentationConnectionState::Connected:
104 0 : if (!connectionFound) {
105 0 : mConnections.AppendElement(aConnection);
106 0 : if (mGetConnectionListPromise) {
107 0 : mGetConnectionListPromise->MaybeResolve(this);
108 0 : mGetConnectionListPromise = nullptr;
109 0 : return;
110 : }
111 : }
112 0 : DispatchConnectionAvailableEvent(aConnection);
113 0 : break;
114 : case PresentationConnectionState::Terminated:
115 0 : if (connectionFound) {
116 0 : mConnections.RemoveElement(aConnection);
117 : }
118 0 : break;
119 : default:
120 0 : break;
121 : }
122 : }
123 :
124 : } // namespace dom
125 : } // namespace mozilla
|