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 "AvailabilityCollection.h"
8 :
9 : #include "mozilla/ClearOnShutdown.h"
10 : #include "PresentationAvailability.h"
11 :
12 : namespace mozilla {
13 : namespace dom {
14 :
15 : /* static */
16 : StaticAutoPtr<AvailabilityCollection>
17 3 : AvailabilityCollection::sSingleton;
18 : static bool gOnceAliveNowDead = false;
19 :
20 : /* static */ AvailabilityCollection*
21 0 : AvailabilityCollection::GetSingleton()
22 : {
23 0 : MOZ_ASSERT(NS_IsMainThread());
24 :
25 0 : if (!sSingleton && !gOnceAliveNowDead) {
26 0 : sSingleton = new AvailabilityCollection();
27 0 : ClearOnShutdown(&sSingleton);
28 : }
29 :
30 0 : return sSingleton;
31 : }
32 :
33 0 : AvailabilityCollection::AvailabilityCollection()
34 : {
35 0 : MOZ_COUNT_CTOR(AvailabilityCollection);
36 0 : }
37 :
38 0 : AvailabilityCollection::~AvailabilityCollection()
39 : {
40 0 : MOZ_COUNT_DTOR(AvailabilityCollection);
41 0 : gOnceAliveNowDead = true;
42 0 : }
43 :
44 : void
45 0 : AvailabilityCollection::Add(PresentationAvailability* aAvailability)
46 : {
47 0 : MOZ_ASSERT(NS_IsMainThread());
48 :
49 0 : if (!aAvailability) {
50 0 : return;
51 : }
52 :
53 0 : WeakPtr<PresentationAvailability> availability = aAvailability;
54 0 : if (mAvailabilities.Contains(aAvailability)) {
55 0 : return;
56 : }
57 :
58 0 : mAvailabilities.AppendElement(aAvailability);
59 : }
60 :
61 : void
62 0 : AvailabilityCollection::Remove(PresentationAvailability* aAvailability)
63 : {
64 0 : MOZ_ASSERT(NS_IsMainThread());
65 :
66 0 : if (!aAvailability) {
67 0 : return;
68 : }
69 :
70 0 : WeakPtr<PresentationAvailability> availability = aAvailability;
71 0 : mAvailabilities.RemoveElement(availability);
72 : }
73 :
74 : already_AddRefed<PresentationAvailability>
75 0 : AvailabilityCollection::Find(const uint64_t aWindowId, const nsTArray<nsString>& aUrls)
76 : {
77 0 : MOZ_ASSERT(NS_IsMainThread());
78 :
79 : // Loop backwards to allow removing elements in the loop.
80 0 : for (int i = mAvailabilities.Length() - 1; i >= 0; --i) {
81 0 : WeakPtr<PresentationAvailability> availability = mAvailabilities[i];
82 0 : if (!availability) {
83 : // The availability object was destroyed. Remove it from the list.
84 0 : mAvailabilities.RemoveElementAt(i);
85 0 : continue;
86 : }
87 :
88 0 : if (availability->Equals(aWindowId, aUrls)) {
89 0 : RefPtr<PresentationAvailability> matchedAvailability = availability.get();
90 0 : return matchedAvailability.forget();
91 : }
92 : }
93 :
94 :
95 0 : return nullptr;
96 : }
97 :
98 : } // namespace dom
99 : } // namespace mozilla
|