2015-09-14 05:39:57 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
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"
|
2015-09-14 05:39:57 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIPresentationDeviceManager.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();
|
|
|
|
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)
|
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)
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<PresentationAvailability>
|
2016-08-31 05:31:15 +03:00
|
|
|
PresentationAvailability::Create(nsPIDOMWindowInner* aWindow,
|
2016-09-05 02:17:00 +03:00
|
|
|
const nsTArray<nsString>& aUrls,
|
2016-08-31 05:31:15 +03:00
|
|
|
RefPtr<Promise>& aPromise)
|
2015-09-14 05:39:57 +03:00
|
|
|
{
|
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(nsPIDOMWindowInner* aWindow,
|
2016-09-05 02:17:00 +03:00
|
|
|
const nsTArray<nsString>& aUrls)
|
2015-09-14 05:39:57 +03:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
, mIsAvailable(false)
|
2016-09-05 02:17:00 +03:00
|
|
|
, mUrls(aUrls)
|
2015-09-14 05:39:57 +03:00
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-08-31 05:31:15 +03:00
|
|
|
PresentationAvailability::Init(RefPtr<Promise>& aPromise)
|
2015-09-14 05:39:57 +03:00
|
|
|
{
|
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 <<
|
2016-11-08 06:13:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(service->UnregisterAvailabilityListener(mUrls,
|
|
|
|
this)));
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
PresentationAvailability::DisconnectFromOwner()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2015-09-14 05:39:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ JSObject*
|
|
|
|
PresentationAvailability::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return PresentationAvailabilityBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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)
|
2015-09-10 11:29:08 +03:00
|
|
|
{
|
2016-11-08 06:13:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-08-31 05:31:15 +03:00
|
|
|
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 {
|
|
|
|
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());
|
|
|
|
|
2016-08-31 05:31:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isChanged) {
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused <<
|
|
|
|
NS_WARN_IF(NS_FAILED(DispatchTrustedEvent(NS_LITERAL_STRING("change"))));
|
2016-08-31 05:31:15 +03:00
|
|
|
}
|
2015-09-10 11:29:08 +03:00
|
|
|
}
|