Files
palemoon27/dom/workers/ServiceWorkerRegistration.cpp
T
roytam1 18efcca64b import change from rmottola/Arctic-Fox:
- Bug 1134800 - Properly cache file matching patterns; r=glandium (0c65935e9)
- Bug 1136177 - Amount by mouse wheel scrolling is wrong. r=jimm (6cd8824a3)
- Bug 1055369 - Assertion failure: !handle || !handle->IsDoomed(). r=honzab (696f6d801)
- Bug 1132728 - Don't draw focus rings on b2g. r=fabrice,bz (0bf2719a2)
- Bug 1133201 - part 1 - treat null links in BrowserUtils.linkHasNoReferrer as specifying rel="noreferrer"; r=Gijs,mconley (97031e93c)
- Bug 906190 - Persist 'disable protection' option for Mixed Content Blocker in child tabs - tabbrowser part. r=gavin (7467fee83)
- Bug 947895 - [e10s] Null check browser.docShell in context-menu Open in new tab. r=gavin (5236b883e)
- Bug 1038604 - Rename disableMCB to allowMixedContent. r=dao (2df2b924b)
- Bug 1151349 - Make lldb ns(Int)Region summary handle the numRects==0 case. r=jrmuizel (19be5e2c0)
- Bug 1146585 - Add a test for Cache.delete; r=bkelly (db28bcc13)
- Bug 899222 - Make about:home work via message passing (r=felipe) Bug 900865: Make about:home call nsISearchEngine.getSubmission for all searches. r=gavin (15f8ae2f8)
- Bug 910523 - about:home now takes into account async startup of SessionRestore;r=felipe (ceaa0687c)
- Bug 927132 - Fix about:home in e10s (r=mdeboer) (53c586ee2)
- Bug 897062 - Handle special clicks in e10s. r=felipe,smaug (7ec4573c8)
- Bug 899348 - Implement about:tabcrashed page. r=jaws (547bb3e7c)
- Bug 899348 - Dispatch an event when an out-of-process browser crashes and display an error page when that occurs (missing parts) (88c87ce75)
- Bug 897066 - In e10s builds, some pages should be loaded in the chrome process (r=gavin) (4cc0f8ed5)
- Bug 1038811 - Push Notifications - WebIDL changes. r=nsm. sr=jst (2ed030bf3)
- Bug 1038811 - Push Notifications - ServiceWorker changes, push event implementation. r=nsm (6313c8c10)
- reinstantiate file as of 2015-02-05 (7f12724c8)
- Bug 1038811 - Push Notifications - Tests. r=nsm (cbac31308)
- Bug 1038811 - Push Notifications - Allow MOZ_DISABLE_NONLOCAL_CONNECTIONS for push subsuite. r=ahalberstadt/jgriffin (859e182ce)
- Bug 1038811 - Push Notifications - Push implementation changes. r=nsm (161a739f4)
- Bug 898170 - Avoid swapping docshells in e10s mode (r=felipe) (55e96d59a)
- Bug 862078 - Use an about:config preference to control multiprocess browsing, part 2 (r=felipe) [missing bits] (9fac04b17)
- Bug 666809 - Support SecurityUI in e10s mode. r=felipe f=gavin [missing bits] (0e7aa1368)
- Bug 691610 - e10s support for useDefaultIcon. r=felipe sr=smaug (cbf7e5341)
- Bug 897066 - Underline tab titles if the tab is remote (r=gavin) [+ followup fix] (2eda1d81e)
- Bug 899348 - Make reload of the about:tabcrashed work as expected. r=jaws (72843ef8f)
- Bug 1133846 - Add missing arguments to logging call in ActivateTimeoutTick. r=mcmanus (a784a7ce8)
- Bug 1135682 - Do not update the all MediaStreamGraph if it's not dirty, r=padenot (49eeeeef5)
- Bug 1135255 - Fix mozdevice tempfile handling on Windows. r=gbrown (53a2353bd)
- Bug 1134735 - Don't use tee->InitAsync in nsHttpChannel::InstallCacheListener when using cache2, r=michal (3fa833982)
- Bug 1132081 - Speed up ConvertHostARGBRow() in the PNG encoder. r=jmuizelaar (6b7890586)
- pointer style before patch (3336fab8f)
- Bug 1135100 - Don't update GC thing pointers that haven't changed after marking r=terrence (0df3ea820)
2019-08-30 23:22:17 +08:00

362 lines
9.5 KiB
C++

/* -*- 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"
#include "mozilla/Services.h"
#include "nsCycleCollectionParticipant.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
#include "ServiceWorker.h"
#include "nsIDocument.h"
#include "nsIServiceWorkerManager.h"
#include "nsISupportsPrimitives.h"
#include "nsPIDOMWindow.h"
#ifndef MOZ_SIMPLEPUSH
#include "mozilla/dom/PushManagerBinding.h"
#endif
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)
#ifdef MOZ_SIMPLEPUSH
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistration,
DOMEventTargetHelper,
mInstallingWorker,
mWaitingWorker,
mActiveWorker)
#else
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistration,
DOMEventTargetHelper,
mInstallingWorker,
mWaitingWorker,
mActiveWorker,
mPushManager)
#endif
ServiceWorkerRegistration::ServiceWorkerRegistration(nsPIDOMWindow* aWindow,
const nsAString& aScope)
: DOMEventTargetHelper(aWindow)
, mScope(aScope)
, mListeningForEvents(false)
{
MOZ_ASSERT(aWindow);
MOZ_ASSERT(aWindow->IsInnerWindow());
StartListeningForEvents();
}
ServiceWorkerRegistration::~ServiceWorkerRegistration()
{
StopListeningForEvents();
}
void
ServiceWorkerRegistration::DisconnectFromOwner()
{
StopListeningForEvents();
DOMEventTargetHelper::DisconnectFromOwner();
}
JSObject*
ServiceWorkerRegistration::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return ServiceWorkerRegistrationBinding::Wrap(aCx, this, aGivenProto);
}
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();
}
namespace {
class UnregisterCallback final : public nsIServiceWorkerUnregisterCallback
{
nsRefPtr<Promise> mPromise;
public:
NS_DECL_ISUPPORTS
explicit UnregisterCallback(Promise* aPromise)
: mPromise(aPromise)
{
MOZ_ASSERT(mPromise);
}
NS_IMETHODIMP
UnregisterSucceeded(bool aState) override
{
AssertIsOnMainThread();
mPromise->MaybeResolve(aState);
return NS_OK;
}
NS_IMETHODIMP
UnregisterFailed() override
{
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
already_AddRefed<Promise>
ServiceWorkerRegistration::Unregister(ErrorResult& aRv)
{
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();
// "If the origin of scope is not client's origin..."
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->GetSpecIgnoringRef(uriSpec);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
nsCOMPtr<nsIServiceWorkerManager> swm =
do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return nullptr;
}
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(documentPrincipal, cb, scope);
if (aRv.Failed()) {
return nullptr;
}
return promise.forget();
}
already_AddRefed<workers::ServiceWorker>
ServiceWorkerRegistration::GetWorkerReference(WhichServiceWorker aWhichOne)
{
nsCOMPtr<nsPIDOMWindow> window = GetOwner();
if (!window) {
return nullptr;
}
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:
rv = swm->GetInstalling(window, mScope, getter_AddRefs(serviceWorker));
break;
case WhichServiceWorker::WAITING_WORKER:
rv = swm->GetWaiting(window, mScope, getter_AddRefs(serviceWorker));
break;
case WhichServiceWorker::ACTIVE_WORKER:
rv = swm->GetActive(window, mScope, getter_AddRefs(serviceWorker));
break;
default:
MOZ_CRASH("Invalid enum value");
}
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv) || rv == NS_ERROR_DOM_NOT_FOUND_ERR,
"Unexpected error getting service worker instance from ServiceWorkerManager");
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(mScope, this);
mListeningForEvents = true;
}
}
void
ServiceWorkerRegistration::StopListeningForEvents()
{
if (!mListeningForEvents) {
return;
}
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
if (swm) {
swm->RemoveRegistrationEventListener(mScope, this);
mListeningForEvents = false;
}
}
already_AddRefed<PushManager>
ServiceWorkerRegistration::GetPushManager(ErrorResult& aRv)
{
AssertIsOnMainThread();
#ifdef MOZ_SIMPLEPUSH
return nullptr;
#else
if (!mPushManager) {
nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(GetOwner());
if (!globalObject) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
JSContext* cx = jsapi.cx();
JS::RootedObject globalJs(cx, globalObject->GetGlobalJSObject());
GlobalObject global(cx, globalJs);
// TODO: bug 1148117. This will fail when swr is exposed on workers
JS::Rooted<JSObject*> jsImplObj(cx);
nsCOMPtr<nsIGlobalObject> unused = ConstructJSImplementation(cx, "@mozilla.org/push/PushManager;1",
global, &jsImplObj, aRv);
if (aRv.Failed()) {
return nullptr;
}
mPushManager = new PushManager(jsImplObj, globalObject);
mPushManager->SetScope(mScope, aRv);
if (aRv.Failed()) {
mPushManager = nullptr;
return nullptr;
}
}
nsRefPtr<PushManager> ret = mPushManager;
return ret.forget();
#endif /* ! MOZ_SIMPLEPUSH */
}
} // dom namespace
} // mozilla namespace