2015-05-03 22:32:37 +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: */
|
2013-11-20 03:14:07 +04: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/. */
|
|
|
|
|
|
|
|
#include "ServiceWorker.h"
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsIDocument.h"
|
2013-11-20 03:14:07 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "RemoteServiceWorkerImpl.h"
|
2018-06-25 17:03:18 +03:00
|
|
|
#include "ServiceWorkerCloneData.h"
|
2018-05-15 18:53:03 +03:00
|
|
|
#include "ServiceWorkerImpl.h"
|
2015-02-19 19:40:21 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
2015-10-01 02:11:03 +03:00
|
|
|
#include "ServiceWorkerPrivate.h"
|
2018-07-02 17:44:18 +03:00
|
|
|
#include "ServiceWorkerRegistration.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "ServiceWorkerUtils.h"
|
2013-11-20 03:14:07 +04:00
|
|
|
|
2018-01-08 16:05:04 +03:00
|
|
|
#include "mozilla/dom/DOMPrefs.h"
|
2017-12-12 23:44:47 +03:00
|
|
|
#include "mozilla/dom/ClientIPCTypes.h"
|
|
|
|
#include "mozilla/dom/ClientState.h"
|
2013-11-20 03:14:07 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-11-06 18:57:57 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2013-11-20 03:14:07 +04:00
|
|
|
|
2015-02-09 20:42:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using mozilla::ErrorResult;
|
2013-11-20 03:14:07 +04:00
|
|
|
using namespace mozilla::dom;
|
2014-11-06 18:57:57 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerVisible(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2018-01-08 16:05:04 +03:00
|
|
|
return DOMPrefs::ServiceWorkersEnabled();
|
2014-11-06 18:57:57 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
return IS_INSTANCE_OF(ServiceWorkerGlobalScope, aObj);
|
2014-11-06 18:57:57 +03:00
|
|
|
}
|
2013-11-20 03:14:07 +04:00
|
|
|
|
2018-01-31 20:10:25 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<ServiceWorker>
|
|
|
|
ServiceWorker::Create(nsIGlobalObject* aOwner,
|
|
|
|
const ServiceWorkerDescriptor& aDescriptor)
|
|
|
|
{
|
|
|
|
RefPtr<ServiceWorker> ref;
|
2018-07-10 02:02:39 +03:00
|
|
|
RefPtr<ServiceWorker::Inner> inner;
|
2018-01-31 20:10:25 +03:00
|
|
|
|
2018-07-10 02:02:39 +03:00
|
|
|
if (ServiceWorkerParentInterceptEnabled()) {
|
|
|
|
inner = new RemoteServiceWorkerImpl(aDescriptor);
|
|
|
|
} else {
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
NS_ENSURE_TRUE(swm, nullptr);
|
2018-01-31 20:10:25 +03:00
|
|
|
|
2018-07-10 02:02:39 +03:00
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> reg =
|
|
|
|
swm->GetRegistration(aDescriptor.PrincipalInfo(), aDescriptor.Scope());
|
|
|
|
NS_ENSURE_TRUE(reg, nullptr);
|
2018-01-31 20:10:25 +03:00
|
|
|
|
2018-07-10 02:02:39 +03:00
|
|
|
RefPtr<ServiceWorkerInfo> info = reg->GetByDescriptor(aDescriptor);
|
2018-07-11 22:06:21 +03:00
|
|
|
NS_ENSURE_TRUE(info, nullptr);
|
2018-07-10 02:02:39 +03:00
|
|
|
|
|
|
|
inner = new ServiceWorkerImpl(info, reg);
|
2018-01-31 20:10:25 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 02:02:39 +03:00
|
|
|
NS_ENSURE_TRUE(inner, nullptr);
|
|
|
|
|
2018-05-15 18:53:03 +03:00
|
|
|
ref = new ServiceWorker(aOwner, aDescriptor, inner);
|
2018-01-31 20:10:25 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorker::ServiceWorker(nsIGlobalObject* aGlobal,
|
2018-01-31 20:10:26 +03:00
|
|
|
const ServiceWorkerDescriptor& aDescriptor,
|
|
|
|
ServiceWorker::Inner* aInner)
|
2018-01-31 20:10:26 +03:00
|
|
|
: DOMEventTargetHelper(aGlobal)
|
|
|
|
, mDescriptor(aDescriptor)
|
2018-01-31 20:10:26 +03:00
|
|
|
, mInner(aInner)
|
2018-07-02 17:44:18 +03:00
|
|
|
, mLastNotifiedState(ServiceWorkerState::Installing)
|
2013-11-20 03:14:07 +04:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-01-31 20:10:26 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aGlobal);
|
2018-01-31 20:10:26 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInner);
|
2015-02-19 19:40:21 +03:00
|
|
|
|
2018-04-10 21:00:56 +03:00
|
|
|
KeepAliveIfHasListenersFor(NS_LITERAL_STRING("statechange"));
|
|
|
|
|
|
|
|
// The error event handler is required by the spec currently, but is not used
|
|
|
|
// anywhere. Don't keep the object alive in that case.
|
|
|
|
|
2015-02-19 19:40:21 +03:00
|
|
|
// This will update our state too.
|
2018-01-31 20:10:26 +03:00
|
|
|
mInner->AddServiceWorker(this);
|
2018-07-02 17:44:18 +03:00
|
|
|
|
|
|
|
// Attempt to get an existing binding object for the registration
|
|
|
|
// associated with this ServiceWorker.
|
|
|
|
RefPtr<ServiceWorkerRegistration> reg = aGlobal->GetServiceWorkerRegistration(
|
|
|
|
ServiceWorkerRegistrationDescriptor(mDescriptor.RegistrationId(),
|
2018-07-02 17:44:19 +03:00
|
|
|
mDescriptor.RegistrationVersion(),
|
2018-07-02 17:44:18 +03:00
|
|
|
mDescriptor.PrincipalInfo(),
|
|
|
|
mDescriptor.Scope(),
|
|
|
|
ServiceWorkerUpdateViaCache::Imports));
|
|
|
|
if (reg) {
|
|
|
|
MaybeAttachToRegistration(reg);
|
|
|
|
} else {
|
|
|
|
RefPtr<ServiceWorker> self = this;
|
|
|
|
|
|
|
|
mInner->GetRegistration(
|
|
|
|
[self = std::move(self)] (const ServiceWorkerRegistrationDescriptor& aDescriptor) {
|
|
|
|
nsIGlobalObject* global = self->GetParentObject();
|
|
|
|
NS_ENSURE_TRUE_VOID(global);
|
|
|
|
RefPtr<ServiceWorkerRegistration> reg =
|
|
|
|
global->GetOrCreateServiceWorkerRegistration(aDescriptor);
|
|
|
|
self->MaybeAttachToRegistration(reg);
|
|
|
|
}, [] (ErrorResult& aRv) {
|
|
|
|
// do nothing
|
|
|
|
aRv.SuppressException();
|
|
|
|
});
|
|
|
|
}
|
2013-11-20 03:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorker::~ServiceWorker()
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-04-10 21:00:56 +03:00
|
|
|
mInner->RemoveServiceWorker(this);
|
2013-11-20 03:14:07 +04:00
|
|
|
}
|
|
|
|
|
2018-07-02 17:44:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorker,
|
|
|
|
DOMEventTargetHelper,
|
|
|
|
mRegistration);
|
|
|
|
|
2013-11-20 03:14:07 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServiceWorker)
|
2018-02-21 21:53:52 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(ServiceWorker)
|
2013-11-20 03:14:07 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
ServiceWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-11-20 03:14:07 +04:00
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-11-20 03:14:07 +04:00
|
|
|
|
2018-06-26 00:20:54 +03:00
|
|
|
return ServiceWorker_Binding::Wrap(aCx, this, aGivenProto);
|
2013-11-20 03:14:07 +04:00
|
|
|
}
|
2013-11-20 03:15:02 +04:00
|
|
|
|
2018-01-31 20:10:26 +03:00
|
|
|
ServiceWorkerState
|
|
|
|
ServiceWorker::State() const
|
|
|
|
{
|
|
|
|
return mDescriptor.State();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorker::SetState(ServiceWorkerState aState)
|
|
|
|
{
|
2018-07-02 17:44:18 +03:00
|
|
|
NS_ENSURE_TRUE_VOID(aState >= mDescriptor.State());
|
2018-01-31 20:10:26 +03:00
|
|
|
mDescriptor.SetState(aState);
|
2018-07-02 17:44:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorker::MaybeDispatchStateChangeEvent()
|
|
|
|
{
|
|
|
|
if (mDescriptor.State() <= mLastNotifiedState || !GetParentObject()) {
|
2018-04-10 21:00:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-07-02 17:44:18 +03:00
|
|
|
mLastNotifiedState = mDescriptor.State();
|
2018-04-10 21:00:56 +03:00
|
|
|
|
|
|
|
DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("statechange"));
|
|
|
|
|
|
|
|
// Once we have transitioned to the redundant state then no
|
|
|
|
// more statechange events will occur. We can allow the DOM
|
|
|
|
// object to GC if script is not holding it alive.
|
2018-07-02 17:44:18 +03:00
|
|
|
if (mLastNotifiedState == ServiceWorkerState::Redundant) {
|
2018-04-10 21:00:56 +03:00
|
|
|
IgnoreKeepAliveIfHasListenersFor(NS_LITERAL_STRING("statechange"));
|
2018-01-31 20:10:26 +03:00
|
|
|
}
|
2018-01-31 20:10:26 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:40:21 +03:00
|
|
|
void
|
|
|
|
ServiceWorker::GetScriptURL(nsString& aURL) const
|
|
|
|
{
|
2018-01-31 20:10:26 +03:00
|
|
|
CopyUTF8toUTF16(mDescriptor.ScriptURL(), aURL);
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:42:00 +03:00
|
|
|
void
|
|
|
|
ServiceWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2017-02-03 13:00:38 +03:00
|
|
|
const Sequence<JSObject*>& aTransferable,
|
2015-02-09 20:42:00 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2018-04-10 21:00:56 +03:00
|
|
|
if (State() == ServiceWorkerState::Redundant) {
|
2015-08-13 23:35:10 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:02:15 +03:00
|
|
|
nsPIDOMWindowInner* window = GetOwner();
|
|
|
|
if (NS_WARN_IF(!window || !window->GetExtantDoc())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto storageAllowed = nsContentUtils::StorageAllowedForWindow(window);
|
|
|
|
if (storageAllowed != nsContentUtils::StorageAccess::eAllow) {
|
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
|
|
|
mDescriptor.Scope(), "ServiceWorkerPostMessageStorageError",
|
|
|
|
nsTArray<nsString> { NS_ConvertUTF8toUTF16(mDescriptor.Scope()) });
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<ClientInfo> clientInfo = window->GetClientInfo();
|
|
|
|
Maybe<ClientState> clientState = window->GetClientState();
|
|
|
|
if (NS_WARN_IF(clientInfo.isNothing() || clientState.isNothing())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx, aTransferable,
|
|
|
|
&transferable);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:03:18 +03:00
|
|
|
RefPtr<ServiceWorkerCloneData> data = new ServiceWorkerCloneData();
|
|
|
|
data->Write(aCx, aMessage, transferable, aRv);
|
2018-05-14 16:02:15 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mInner->PostMessage(std::move(data), clientInfo.ref(), clientState.ref());
|
2013-11-20 03:15:02 +04:00
|
|
|
}
|
2014-11-06 18:57:57 +03:00
|
|
|
|
2018-02-19 23:06:19 +03:00
|
|
|
|
|
|
|
const ServiceWorkerDescriptor&
|
|
|
|
ServiceWorker::Descriptor() const
|
2018-01-31 20:10:26 +03:00
|
|
|
{
|
2018-02-19 23:06:19 +03:00
|
|
|
return mDescriptor;
|
2018-01-31 20:10:26 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 20:10:26 +03:00
|
|
|
void
|
|
|
|
ServiceWorker::DisconnectFromOwner()
|
|
|
|
{
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:44:18 +03:00
|
|
|
void
|
|
|
|
ServiceWorker::MaybeAttachToRegistration(ServiceWorkerRegistration* aRegistration)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRegistration);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mRegistration);
|
|
|
|
|
2018-07-02 17:44:18 +03:00
|
|
|
// If the registration no longer actually references this ServiceWorker
|
|
|
|
// then we must be in the redundant state.
|
|
|
|
if (!aRegistration->Descriptor().HasWorker(mDescriptor)) {
|
|
|
|
SetState(ServiceWorkerState::Redundant);
|
|
|
|
MaybeDispatchStateChangeEvent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:44:18 +03:00
|
|
|
mRegistration = aRegistration;
|
|
|
|
}
|
|
|
|
|
2014-11-06 18:57:57 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|