2014-08-19 06:56:00 -07: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: */
|
|
|
|
/* 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 "ServiceWorkerRegistration.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
|
2014-08-20 10:30:00 -07:00
|
|
|
#include "mozilla/Services.h"
|
2014-08-19 06:56:00 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "nsNetUtil.h"
|
2014-08-19 06:56:00 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "ServiceWorker.h"
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "nsIDocument.h"
|
2014-08-19 06:56:00 -07:00
|
|
|
#include "nsIServiceWorkerManager.h"
|
2014-08-20 10:30:00 -07:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2014-08-19 06:56:00 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
using namespace mozilla::dom::workers;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistration)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServiceWorkerRegistration, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServiceWorkerRegistration, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistration,
|
|
|
|
DOMEventTargetHelper,
|
|
|
|
mInstallingWorker,
|
|
|
|
mWaitingWorker,
|
|
|
|
mActiveWorker)
|
|
|
|
|
|
|
|
ServiceWorkerRegistration::ServiceWorkerRegistration(nsPIDOMWindow* aWindow,
|
|
|
|
const nsAString& aScope)
|
2014-08-26 01:17:36 -07:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-08-19 06:56:00 -07:00
|
|
|
, mScope(aScope)
|
2014-08-27 13:33:20 -07:00
|
|
|
, mListeningForEvents(false)
|
2014-08-19 06:56:00 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
2014-08-20 10:30:00 -07:00
|
|
|
MOZ_ASSERT(aWindow->IsInnerWindow());
|
2014-08-19 06:56:00 -07:00
|
|
|
|
|
|
|
StartListeningForEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerRegistration::~ServiceWorkerRegistration()
|
2014-08-26 01:17:36 -07:00
|
|
|
{
|
2014-08-27 13:33:20 -07:00
|
|
|
StopListeningForEvents();
|
2014-08-26 01:17:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistration::DisconnectFromOwner()
|
2014-08-19 06:56:00 -07:00
|
|
|
{
|
|
|
|
StopListeningForEvents();
|
2014-08-26 01:17:36 -07:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-08-19 06:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
ServiceWorkerRegistration::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return ServiceWorkerRegistrationBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
ServiceWorkerRegistration::GetInstalling()
|
|
|
|
{
|
|
|
|
if (!mInstallingWorker) {
|
|
|
|
mInstallingWorker = GetWorkerReference(WhichServiceWorker::INSTALLING_WORKER);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> ret = mInstallingWorker;
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
ServiceWorkerRegistration::GetWaiting()
|
|
|
|
{
|
|
|
|
if (!mWaitingWorker) {
|
|
|
|
mWaitingWorker = GetWorkerReference(WhichServiceWorker::WAITING_WORKER);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> ret = mWaitingWorker;
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
ServiceWorkerRegistration::GetActive()
|
|
|
|
{
|
|
|
|
if (!mActiveWorker) {
|
|
|
|
mActiveWorker = GetWorkerReference(WhichServiceWorker::ACTIVE_WORKER);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> ret = mActiveWorker;
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UnregisterCallback MOZ_FINAL : public nsIServiceWorkerUnregisterCallback
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-09-11 12:46:45 -07:00
|
|
|
explicit UnregisterCallback(Promise* aPromise)
|
2014-09-10 08:21:32 -07:00
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UnregisterSucceeded(bool aState)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mPromise->MaybeResolve(aState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UnregisterFailed()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
AutoJSAPI api;
|
|
|
|
api.Init(mPromise->GetParentObject());
|
|
|
|
mPromise->MaybeReject(api.cx(), JS::UndefinedHandleValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~UnregisterCallback()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(UnregisterCallback, nsIServiceWorkerUnregisterCallback)
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-08-19 06:56:00 -07:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
ServiceWorkerRegistration::Unregister(ErrorResult& aRv)
|
|
|
|
{
|
2014-09-10 08:21:32 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(GetOwner());
|
|
|
|
if (!go) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Although the spec says that the same-origin checks should also be done
|
|
|
|
// asynchronously, we do them in sync because the Promise created by the
|
|
|
|
// WebIDL infrastructure due to a returned error will be resolved
|
|
|
|
// asynchronously. We aren't making any internal state changes in these
|
|
|
|
// checks, so ordering of multiple calls is not affected.
|
|
|
|
nsCOMPtr<nsIDocument> document = GetOwner()->GetExtantDoc();
|
|
|
|
if (!document) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scopeURI;
|
|
|
|
nsCOMPtr<nsIURI> baseURI = document->GetBaseURI();
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(scopeURI), mScope, nullptr, baseURI);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> documentPrincipal = document->NodePrincipal();
|
|
|
|
rv = documentPrincipal->CheckMayLoad(scopeURI, true /* report */,
|
|
|
|
false /* allowIfInheritsPrinciple */);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
aRv = scopeURI->GetSpec(uriSpec);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-19 06:56:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm =
|
|
|
|
do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
nsRefPtr<Promise> promise = Promise::Create(go, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<UnregisterCallback> cb = new UnregisterCallback(promise);
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 scope(uriSpec);
|
|
|
|
aRv = swm->Unregister(cb, scope);
|
2014-08-19 06:56:00 -07:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
return promise.forget();
|
2014-08-19 06:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<workers::ServiceWorker>
|
|
|
|
ServiceWorkerRegistration::GetWorkerReference(WhichServiceWorker aWhichOne)
|
|
|
|
{
|
2014-08-24 22:35:03 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetOwner();
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-19 06:56:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm =
|
|
|
|
do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> serviceWorker;
|
|
|
|
switch(aWhichOne) {
|
|
|
|
case WhichServiceWorker::INSTALLING_WORKER:
|
2014-08-24 22:35:03 -07:00
|
|
|
rv = swm->GetInstalling(window, mScope, getter_AddRefs(serviceWorker));
|
2014-08-19 06:56:00 -07:00
|
|
|
break;
|
|
|
|
case WhichServiceWorker::WAITING_WORKER:
|
2014-08-24 22:35:03 -07:00
|
|
|
rv = swm->GetWaiting(window, mScope, getter_AddRefs(serviceWorker));
|
2014-08-19 06:56:00 -07:00
|
|
|
break;
|
|
|
|
case WhichServiceWorker::ACTIVE_WORKER:
|
2014-08-24 22:35:03 -07:00
|
|
|
rv = swm->GetActive(window, mScope, getter_AddRefs(serviceWorker));
|
2014-08-19 06:56:00 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid enum value");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> ref =
|
|
|
|
static_cast<ServiceWorker*>(serviceWorker.get());
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistration::InvalidateWorkerReference(WhichServiceWorker aWhichOnes)
|
|
|
|
{
|
|
|
|
if (aWhichOnes & WhichServiceWorker::INSTALLING_WORKER) {
|
|
|
|
mInstallingWorker = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhichOnes & WhichServiceWorker::WAITING_WORKER) {
|
|
|
|
mWaitingWorker = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhichOnes & WhichServiceWorker::ACTIVE_WORKER) {
|
|
|
|
mActiveWorker = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXnsm, maybe this can be optimized to only add when a event handler is
|
|
|
|
// registered.
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistration::StartListeningForEvents()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
|
|
|
|
if (swm) {
|
|
|
|
swm->AddRegistrationEventListener(GetDocumentURI(), this);
|
2014-08-27 13:33:20 -07:00
|
|
|
mListeningForEvents = true;
|
2014-08-19 06:56:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerRegistration::StopListeningForEvents()
|
|
|
|
{
|
2014-08-27 13:33:20 -07:00
|
|
|
if (!mListeningForEvents) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-19 06:56:00 -07:00
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
|
2014-08-26 01:17:36 -07:00
|
|
|
if (swm) {
|
2014-08-19 06:56:00 -07:00
|
|
|
swm->RemoveRegistrationEventListener(GetDocumentURI(), this);
|
2014-08-27 13:33:20 -07:00
|
|
|
mListeningForEvents = false;
|
2014-08-19 06:56:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIURI*
|
|
|
|
ServiceWorkerRegistration::GetDocumentURI() const
|
|
|
|
{
|
2014-08-26 01:17:36 -07:00
|
|
|
MOZ_ASSERT(GetOwner());
|
|
|
|
return GetOwner()->GetDocumentURI();
|
2014-08-19 06:56:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|