Coverage Report

Created: 2018-09-25 14:53

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