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 "ControllerConnectionCollection.h"
8 :
9 : #include "mozilla/ClearOnShutdown.h"
10 : #include "nsIPresentationService.h"
11 : #include "PresentationConnection.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : /* static */
17 : StaticAutoPtr<ControllerConnectionCollection>
18 3 : ControllerConnectionCollection::sSingleton;
19 :
20 : /* static */ ControllerConnectionCollection*
21 0 : ControllerConnectionCollection::GetSingleton()
22 : {
23 0 : MOZ_ASSERT(NS_IsMainThread());
24 :
25 0 : if (!sSingleton) {
26 0 : sSingleton = new ControllerConnectionCollection();
27 0 : ClearOnShutdown(&sSingleton);
28 : }
29 :
30 0 : return sSingleton;
31 : }
32 :
33 0 : ControllerConnectionCollection::ControllerConnectionCollection()
34 : {
35 0 : MOZ_COUNT_CTOR(ControllerConnectionCollection);
36 0 : }
37 :
38 0 : ControllerConnectionCollection::~ControllerConnectionCollection()
39 : {
40 0 : MOZ_COUNT_DTOR(ControllerConnectionCollection);
41 0 : }
42 :
43 : void
44 0 : ControllerConnectionCollection::AddConnection(
45 : PresentationConnection* aConnection,
46 : const uint8_t aRole)
47 : {
48 0 : MOZ_ASSERT(NS_IsMainThread());
49 0 : if (aRole != nsIPresentationService::ROLE_CONTROLLER) {
50 0 : MOZ_ASSERT(false, "This is allowed only to be called at controller side.");
51 : return;
52 : }
53 :
54 0 : if (!aConnection) {
55 0 : return;
56 : }
57 :
58 0 : WeakPtr<PresentationConnection> connection = aConnection;
59 0 : if (mConnections.Contains(connection)) {
60 0 : return;
61 : }
62 :
63 0 : mConnections.AppendElement(connection);
64 : }
65 :
66 : void
67 0 : ControllerConnectionCollection::RemoveConnection(
68 : PresentationConnection* aConnection,
69 : const uint8_t aRole)
70 : {
71 0 : MOZ_ASSERT(NS_IsMainThread());
72 0 : if (aRole != nsIPresentationService::ROLE_CONTROLLER) {
73 0 : MOZ_ASSERT(false, "This is allowed only to be called at controller side.");
74 : return;
75 : }
76 :
77 0 : if (!aConnection) {
78 0 : return;
79 : }
80 :
81 0 : WeakPtr<PresentationConnection> connection = aConnection;
82 0 : mConnections.RemoveElement(connection);
83 : }
84 :
85 : already_AddRefed<PresentationConnection>
86 0 : ControllerConnectionCollection::FindConnection(
87 : uint64_t aWindowId,
88 : const nsAString& aId,
89 : const uint8_t aRole)
90 : {
91 0 : MOZ_ASSERT(NS_IsMainThread());
92 0 : if (aRole != nsIPresentationService::ROLE_CONTROLLER) {
93 0 : MOZ_ASSERT(false, "This is allowed only to be called at controller side.");
94 : return nullptr;
95 : }
96 :
97 : // Loop backwards to allow removing elements in the loop.
98 0 : for (int i = mConnections.Length() - 1; i >= 0; --i) {
99 0 : WeakPtr<PresentationConnection> connection = mConnections[i];
100 0 : if (!connection) {
101 : // The connection was destroyed. Remove it from the list.
102 0 : mConnections.RemoveElementAt(i);
103 0 : continue;
104 : }
105 :
106 0 : if (connection->Equals(aWindowId, aId)) {
107 0 : RefPtr<PresentationConnection> matchedConnection = connection.get();
108 0 : return matchedConnection.forget();
109 : }
110 : }
111 :
112 0 : return nullptr;
113 : }
114 :
115 : } // namespace dom
116 : } // namespace mozilla
|