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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = service->RegisterAvailabilityListener(this);
|
|
|
|
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(NS_FAILED(service->UnregisterAvailabilityListener(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
|
|
|
|
PresentationAvailability::NotifyAvailableChange(bool aIsAvailable)
|
|
|
|
{
|
2016-05-05 11:45:00 +03:00
|
|
|
return NS_DispatchToCurrentThread(NewRunnableMethod
|
|
|
|
<bool>(this,
|
|
|
|
&PresentationAvailability::UpdateAvailabilityAndDispatchEvent,
|
|
|
|
aIsAvailable));
|
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
|
|
|
}
|