summaryrefslogtreecommitdiff
path: root/dom/presentation/PresentationAvailability.cpp
blob: 93f27dfbfc5c87190de376a1d927c6e3ccfb62ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "PresentationAvailability.h"

#include "mozilla/dom/PresentationAvailabilityBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/Unused.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIPresentationDeviceManager.h"
#include "nsIPresentationService.h"
#include "nsServiceManagerUtils.h"
#include "PresentationLog.h"

using namespace mozilla;
using namespace mozilla::dom;

NS_IMPL_CYCLE_COLLECTION_CLASS(PresentationAvailability)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PresentationAvailability, DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromises)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PresentationAvailability, DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromises);
  tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_ADDREF_INHERITED(PresentationAvailability, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(PresentationAvailability, DOMEventTargetHelper)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(PresentationAvailability)
  NS_INTERFACE_MAP_ENTRY(nsIPresentationAvailabilityListener)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

/* static */ already_AddRefed<PresentationAvailability>
PresentationAvailability::Create(nsPIDOMWindowInner* aWindow,
                                 const nsTArray<nsString>& aUrls,
                                 RefPtr<Promise>& aPromise)
{
  RefPtr<PresentationAvailability> availability =
    new PresentationAvailability(aWindow, aUrls);
  return NS_WARN_IF(!availability->Init(aPromise)) ? nullptr
                                                   : availability.forget();
}

PresentationAvailability::PresentationAvailability(nsPIDOMWindowInner* aWindow,
                                                   const nsTArray<nsString>& aUrls)
  : DOMEventTargetHelper(aWindow)
  , mIsAvailable(false)
  , mUrls(aUrls)
{
  for (uint32_t i = 0; i < mUrls.Length(); ++i) {
    mAvailabilityOfUrl.AppendElement(false);
  }
}

PresentationAvailability::~PresentationAvailability()
{
  Shutdown();
}

bool
PresentationAvailability::Init(RefPtr<Promise>& aPromise)
{
  nsCOMPtr<nsIPresentationService> service =
    do_GetService(PRESENTATION_SERVICE_CONTRACTID);
  if (NS_WARN_IF(!service)) {
    return false;
  }

  nsresult rv = service->RegisterAvailabilityListener(mUrls, this);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    // If the user agent is unable to monitor available device,
    // Resolve promise with |value| set to false.
    mIsAvailable = false;
    aPromise->MaybeResolve(this);
    return true;
  }

  EnqueuePromise(aPromise);

  AvailabilityCollection* collection = AvailabilityCollection::GetSingleton();
  if (collection) {
    collection->Add(this);
  }

  return true;
}

void PresentationAvailability::Shutdown()
{
  AvailabilityCollection* collection = AvailabilityCollection::GetSingleton();
  if (collection ) {
    collection->Remove(this);
  }

  nsCOMPtr<nsIPresentationService> service =
    do_GetService(PRESENTATION_SERVICE_CONTRACTID);
  if (NS_WARN_IF(!service)) {
    return;
  }

  Unused <<
    NS_WARN_IF(NS_FAILED(service->UnregisterAvailabilityListener(mUrls,
                                                                 this)));
}

/* virtual */ void
PresentationAvailability::DisconnectFromOwner()
{
  Shutdown();
  DOMEventTargetHelper::DisconnectFromOwner();
}

/* virtual */ JSObject*
PresentationAvailability::WrapObject(JSContext* aCx,
                                     JS::Handle<JSObject*> aGivenProto)
{
  return PresentationAvailabilityBinding::Wrap(aCx, this, aGivenProto);
}

bool
PresentationAvailability::Equals(const uint64_t aWindowID,
                                 const nsTArray<nsString>& aUrls) const
{
  if (GetOwner() && GetOwner()->WindowID() == aWindowID &&
      mUrls.Length() == aUrls.Length()) {
    for (const auto& url : aUrls) {
      if (!mUrls.Contains(url)) {
        return false;
      }
    }
    return true;
  }

  return false;
}

bool
PresentationAvailability::IsCachedValueReady()
{
  // All pending promises will be solved when cached value is ready and
  // no promise should be enqueued afterward.
  return mPromises.IsEmpty();
}

void
PresentationAvailability::EnqueuePromise(RefPtr<Promise>& aPromise)
{
  mPromises.AppendElement(aPromise);
}

bool
PresentationAvailability::Value() const
{
  return mIsAvailable;
}

NS_IMETHODIMP
PresentationAvailability::NotifyAvailableChange(const nsTArray<nsString>& aAvailabilityUrls,
                                                bool aIsAvailable)
{
  bool available = false;
  for (uint32_t i = 0; i < mUrls.Length(); ++i) {
    if (aAvailabilityUrls.Contains(mUrls[i])) {
      mAvailabilityOfUrl[i] = aIsAvailable;
    }
    available |= mAvailabilityOfUrl[i];
  }

  return NS_DispatchToCurrentThread(NewRunnableMethod
                                    <bool>(this,
                                           &PresentationAvailability::UpdateAvailabilityAndDispatchEvent,
                                           available));
}

void
PresentationAvailability::UpdateAvailabilityAndDispatchEvent(bool aIsAvailable)
{
  PRES_DEBUG("%s\n", __func__);
  bool isChanged = (aIsAvailable != mIsAvailable);

  mIsAvailable = aIsAvailable;

  if (!mPromises.IsEmpty()) {
    // Use the first availability change notification to resolve promise.
    do {
      nsTArray<RefPtr<Promise>> promises = Move(mPromises);
      for (auto& promise : promises) {
        promise->MaybeResolve(this);
      }
      // more promises may have been added to mPromises, at least in theory
    } while (!mPromises.IsEmpty());

    return;
  }

  if (isChanged) {
    Unused <<
      NS_WARN_IF(NS_FAILED(DispatchTrustedEvent(NS_LITERAL_STRING("change"))));
  }
}