2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-09-14 05:39:57 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
#include "PresentationAvailability.h"
|
|
|
|
|
2015-09-14 05:39:57 +03:00
|
|
|
#include "mozilla/dom/PresentationAvailabilityBinding.h"
|
2016-08-31 05:31:15 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2016-09-02 10:12:24 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-08-09 12:38:30 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-09-14 05:39:57 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIPresentationService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-08-17 11:15:32 +03:00
|
|
|
#include "PresentationLog.h"
|
2015-09-14 05:39:57 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PresentationAvailability)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PresentationAvailability,
|
|
|
|
DOMEventTargetHelper)
|
2016-08-17 11:15:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromises)
|
2015-09-14 05:39:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PresentationAvailability,
|
|
|
|
DOMEventTargetHelper)
|
2016-08-17 11:15:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromises);
|
2015-09-14 05:39:57 +03:00
|
|
|
tmp->Shutdown();
|
2020-02-25 22:44:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
|
2015-09-14 05:39:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PresentationAvailability, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PresentationAvailability, DOMEventTargetHelper)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PresentationAvailability)
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPresentationAvailabilityListener)
|
2015-09-14 05:39:57 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<PresentationAvailability> PresentationAvailability::Create(
|
|
|
|
nsPIDOMWindowInner* aWindow, const nsTArray<nsString>& aUrls,
|
|
|
|
RefPtr<Promise>& aPromise) {
|
2016-08-31 05:31:15 +03:00
|
|
|
RefPtr<PresentationAvailability> availability =
|
2016-09-05 02:17:00 +03:00
|
|
|
new PresentationAvailability(aWindow, aUrls);
|
2016-08-31 05:31:15 +03:00
|
|
|
return NS_WARN_IF(!availability->Init(aPromise)) ? nullptr
|
|
|
|
: availability.forget();
|
2015-09-14 05:39:57 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 11:15:32 +03:00
|
|
|
PresentationAvailability::PresentationAvailability(
|
2016-09-05 02:17:00 +03:00
|
|
|
nsPIDOMWindowInner* aWindow, const nsTArray<nsString>& aUrls)
|
2020-05-06 14:43:16 +03:00
|
|
|
: DOMEventTargetHelper(aWindow), mIsAvailable(false), mUrls(aUrls.Clone()) {
|
2016-11-08 06:13:00 +03:00
|
|
|
for (uint32_t i = 0; i < mUrls.Length(); ++i) {
|
|
|
|
mAvailabilityOfUrl.AppendElement(false);
|
|
|
|
}
|
2015-09-14 05:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PresentationAvailability::~PresentationAvailability() { Shutdown(); }
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
bool PresentationAvailability::Init(RefPtr<Promise>& aPromise) {
|
2015-09-10 11:29:08 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-08 06:13:00 +03:00
|
|
|
nsresult rv = service->RegisterAvailabilityListener(mUrls, this);
|
2015-09-10 11:29:08 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-08-31 05:31:15 +03:00
|
|
|
// If the user agent is unable to monitor available device,
|
|
|
|
// Resolve promise with |value| set to false.
|
|
|
|
mIsAvailable = false;
|
|
|
|
aPromise->MaybeResolve(this);
|
|
|
|
return true;
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|
2015-09-14 05:39:57 +03:00
|
|
|
|
2016-08-17 11:15:32 +03:00
|
|
|
EnqueuePromise(aPromise);
|
|
|
|
|
|
|
|
AvailabilityCollection* collection = AvailabilityCollection::GetSingleton();
|
|
|
|
if (collection) {
|
|
|
|
collection->Add(this);
|
|
|
|
}
|
2015-09-14 05:39:57 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PresentationAvailability::Shutdown() {
|
2016-08-17 11:15:32 +03:00
|
|
|
AvailabilityCollection* collection = AvailabilityCollection::GetSingleton();
|
|
|
|
if (collection) {
|
|
|
|
collection->Remove(this);
|
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(
|
2016-11-08 06:13:00 +03:00
|
|
|
NS_FAILED(service->UnregisterAvailabilityListener(mUrls, this)));
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
void PresentationAvailability::DisconnectFromOwner() {
|
2015-09-10 11:29:08 +03:00
|
|
|
Shutdown();
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2015-09-14 05:39:57 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* PresentationAvailability::WrapObject(
|
2015-09-14 05:39:57 +03:00
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PresentationAvailability_Binding::Wrap(aCx, this, aGivenProto);
|
2015-09-14 05:39:57 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 11:15:32 +03:00
|
|
|
bool PresentationAvailability::Equals(const uint64_t aWindowID,
|
2016-09-05 02:17:00 +03:00
|
|
|
const nsTArray<nsString>& aUrls) const {
|
2016-08-17 11:15:32 +03:00
|
|
|
if (GetOwner() && GetOwner()->WindowID() == aWindowID &&
|
2016-09-05 02:17:00 +03:00
|
|
|
mUrls.Length() == aUrls.Length()) {
|
|
|
|
for (const auto& url : aUrls) {
|
|
|
|
if (!mUrls.Contains(url)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 11:15:32 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:39:57 +03:00
|
|
|
bool PresentationAvailability::Value() const {
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-14 05:39:57 +03:00
|
|
|
return mIsAvailable;
|
|
|
|
}
|
2015-09-10 11:29:08 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-11-08 06:13:00 +03:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
return NS_DispatchToCurrentThread(NewRunnableMethod<bool>(
|
|
|
|
"dom::PresentationAvailability::UpdateAvailabilityAndDispatchEvent", this,
|
|
|
|
&PresentationAvailability::UpdateAvailabilityAndDispatchEvent,
|
|
|
|
available));
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
void PresentationAvailability::UpdateAvailabilityAndDispatchEvent(
|
|
|
|
bool aIsAvailable) {
|
2016-09-05 02:17:00 +03:00
|
|
|
PRES_DEBUG("%s\n", __func__);
|
2016-08-31 05:31:15 +03:00
|
|
|
bool isChanged = (aIsAvailable != mIsAvailable);
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
mIsAvailable = aIsAvailable;
|
|
|
|
|
2016-08-17 11:15:32 +03:00
|
|
|
if (!mPromises.IsEmpty()) {
|
|
|
|
// Use the first availability change notification to resolve promise.
|
|
|
|
do {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsTArray<RefPtr<Promise>> promises = std::move(mPromises);
|
2017-08-09 12:38:30 +03:00
|
|
|
|
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:15:32 +03:00
|
|
|
for (auto& promise : promises) {
|
|
|
|
promise->MaybeResolve(this);
|
|
|
|
}
|
|
|
|
// more promises may have been added to mPromises, at least in theory
|
|
|
|
} while (!mPromises.IsEmpty());
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
if (isChanged) {
|
2020-07-01 11:29:29 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(DispatchTrustedEvent(u"change"_ns)));
|
2016-08-31 05:31:15 +03:00
|
|
|
}
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|