/src/mozilla-central/dom/presentation/ControllerConnectionCollection.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 "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 | | ControllerConnectionCollection::sSingleton; |
19 | | |
20 | | /* static */ ControllerConnectionCollection* |
21 | | ControllerConnectionCollection::GetSingleton() |
22 | 0 | { |
23 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
24 | 0 |
|
25 | 0 | if (!sSingleton) { |
26 | 0 | sSingleton = new ControllerConnectionCollection(); |
27 | 0 | ClearOnShutdown(&sSingleton); |
28 | 0 | } |
29 | 0 |
|
30 | 0 | return sSingleton; |
31 | 0 | } |
32 | | |
33 | | ControllerConnectionCollection::ControllerConnectionCollection() |
34 | 0 | { |
35 | 0 | MOZ_COUNT_CTOR(ControllerConnectionCollection); |
36 | 0 | } |
37 | | |
38 | | ControllerConnectionCollection::~ControllerConnectionCollection() |
39 | 0 | { |
40 | 0 | MOZ_COUNT_DTOR(ControllerConnectionCollection); |
41 | 0 | } |
42 | | |
43 | | void |
44 | | ControllerConnectionCollection::AddConnection( |
45 | | PresentationConnection* aConnection, |
46 | | const uint8_t aRole) |
47 | 0 | { |
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 | 0 | return; |
52 | 0 | } |
53 | 0 |
|
54 | 0 | if (!aConnection) { |
55 | 0 | return; |
56 | 0 | } |
57 | 0 | |
58 | 0 | WeakPtr<PresentationConnection> connection = aConnection; |
59 | 0 | if (mConnections.Contains(connection)) { |
60 | 0 | return; |
61 | 0 | } |
62 | 0 | |
63 | 0 | mConnections.AppendElement(connection); |
64 | 0 | } |
65 | | |
66 | | void |
67 | | ControllerConnectionCollection::RemoveConnection( |
68 | | PresentationConnection* aConnection, |
69 | | const uint8_t aRole) |
70 | 0 | { |
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 | 0 | return; |
75 | 0 | } |
76 | 0 |
|
77 | 0 | if (!aConnection) { |
78 | 0 | return; |
79 | 0 | } |
80 | 0 | |
81 | 0 | WeakPtr<PresentationConnection> connection = aConnection; |
82 | 0 | mConnections.RemoveElement(connection); |
83 | 0 | } |
84 | | |
85 | | already_AddRefed<PresentationConnection> |
86 | | ControllerConnectionCollection::FindConnection( |
87 | | uint64_t aWindowId, |
88 | | const nsAString& aId, |
89 | | const uint8_t aRole) |
90 | 0 | { |
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 | 0 | return nullptr; |
95 | 0 | } |
96 | 0 |
|
97 | 0 | // 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 | 0 | // The connection was destroyed. Remove it from the list. |
102 | 0 | mConnections.RemoveElementAt(i); |
103 | 0 | continue; |
104 | 0 | } |
105 | 0 | |
106 | 0 | if (connection->Equals(aWindowId, aId)) { |
107 | 0 | RefPtr<PresentationConnection> matchedConnection = connection.get(); |
108 | 0 | return matchedConnection.forget(); |
109 | 0 | } |
110 | 0 | } |
111 | 0 |
|
112 | 0 | return nullptr; |
113 | 0 | } |
114 | | |
115 | | } // namespace dom |
116 | | } // namespace mozilla |