2015-10-01 02:11:03 +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: */
|
|
|
|
|
/* 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 "ServiceWorkerPrivate.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
#include <utility>
|
|
|
|
|
|
2018-06-25 17:03:18 +03:00
|
|
|
|
#include "ServiceWorkerCloneData.h"
|
2015-10-01 02:11:03 +03:00
|
|
|
|
#include "ServiceWorkerManager.h"
|
2019-08-15 20:27:37 +03:00
|
|
|
|
#include "ServiceWorkerPrivateImpl.h"
|
|
|
|
|
#include "ServiceWorkerUtils.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsContentUtils.h"
|
2017-12-04 09:39:10 +03:00
|
|
|
|
#include "nsICacheInfoChannel.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
|
#include "nsIHttpHeaderVisitor.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
|
#include "nsINamed.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsINetworkInterceptController.h"
|
2016-09-01 21:17:03 +03:00
|
|
|
|
#include "nsIPushErrorReporter.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
|
#include "nsIUploadChannel2.h"
|
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
|
#include "nsQueryObject.h"
|
2015-10-25 01:22:46 +03:00
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
#include "nsStringStream.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "mozilla/Assertions.h"
|
2018-05-10 08:04:12 +03:00
|
|
|
|
#include "mozilla/CycleCollectedJSContext.h" // for MicroTaskRunnable
|
2018-05-14 16:02:15 +03:00
|
|
|
|
#include "mozilla/JSObjectHolder.h"
|
2017-12-12 23:44:47 +03:00
|
|
|
|
#include "mozilla/dom/Client.h"
|
|
|
|
|
#include "mozilla/dom/ClientIPCTypes.h"
|
2015-10-25 01:22:46 +03:00
|
|
|
|
#include "mozilla/dom/FetchUtil.h"
|
2016-02-17 00:46:08 +03:00
|
|
|
|
#include "mozilla/dom/IndexedDatabaseManager.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "mozilla/dom/InternalHeaders.h"
|
|
|
|
|
#include "mozilla/dom/NotificationEvent.h"
|
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2016-09-01 21:17:03 +03:00
|
|
|
|
#include "mozilla/dom/PushEventBinding.h"
|
2016-03-28 21:50:39 +03:00
|
|
|
|
#include "mozilla/dom/RequestBinding.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
|
#include "mozilla/dom/WorkerDebugger.h"
|
2018-03-16 17:46:21 +03:00
|
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2018-05-14 16:02:15 +03:00
|
|
|
|
#include "mozilla/dom/ipc/StructuredCloneData.h"
|
2019-08-15 20:27:37 +03:00
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2020-03-04 11:59:08 +03:00
|
|
|
|
#include "mozilla/net/CookieJarSettings.h"
|
2019-02-20 13:05:42 +03:00
|
|
|
|
#include "mozilla/net/NeckoChannelParams.h"
|
2019-08-15 20:27:37 +03:00
|
|
|
|
#include "mozilla/Services.h"
|
2019-08-17 04:14:08 +03:00
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
|
#include "mozilla/Unused.h"
|
2019-05-02 15:33:55 +03:00
|
|
|
|
#include "nsIReferrerInfo.h"
|
2016-03-28 21:50:39 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
using namespace mozilla;
|
|
|
|
|
using namespace mozilla::dom;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
|
namespace mozilla {
|
|
|
|
|
namespace dom {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-12-12 23:44:47 +03:00
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
|
|
2017-01-31 17:21:52 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(ServiceWorkerPrivate)
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(ServiceWorkerPrivate)
|
2015-10-24 15:16:23 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(ServiceWorkerPrivate, mSupportsArray)
|
2017-01-31 17:21:52 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ServiceWorkerPrivate, AddRef)
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ServiceWorkerPrivate, Release)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-10-23 21:24:31 +03:00
|
|
|
|
// Tracks the "dom.serviceWorkers.disable_open_click_delay" preference. Modified
|
|
|
|
|
// on main thread, read on worker threads.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
// It is updated every time a "notificationclick" event is dispatched. While
|
|
|
|
|
// this is done without synchronization, at the worst, the thread will just get
|
|
|
|
|
// an older value within which a popup is allowed to be displayed, which will
|
|
|
|
|
// still be a valid value since it was set prior to dispatching the runnable.
|
|
|
|
|
Atomic<uint32_t> gDOMDisableOpenClickDelay(0);
|
|
|
|
|
|
2019-08-15 20:27:07 +03:00
|
|
|
|
KeepAliveToken::KeepAliveToken(ServiceWorkerPrivate* aPrivate)
|
|
|
|
|
: mPrivate(aPrivate) {
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
MOZ_ASSERT(aPrivate);
|
|
|
|
|
mPrivate->AddToken();
|
|
|
|
|
}
|
2019-08-15 01:04:46 +03:00
|
|
|
|
|
2019-08-15 20:27:07 +03:00
|
|
|
|
KeepAliveToken::~KeepAliveToken() {
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
mPrivate->ReleaseToken();
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(KeepAliveToken)
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo)
|
2015-11-26 14:18:56 +03:00
|
|
|
|
: mInfo(aInfo), mDebuggerCount(0), mTokenCount(0) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aInfo);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-10-16 09:15:40 +03:00
|
|
|
|
mIdleWorkerTimer = NS_NewTimer();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(mIdleWorkerTimer);
|
2019-08-15 20:27:37 +03:00
|
|
|
|
|
|
|
|
|
if (ServiceWorkerParentInterceptEnabled()) {
|
|
|
|
|
RefPtr<ServiceWorkerPrivateImpl> inner = new ServiceWorkerPrivateImpl(this);
|
2019-08-17 04:14:08 +03:00
|
|
|
|
|
|
|
|
|
// Assert in all debug builds as well as non-debug Nightly and Dev Edition.
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(inner->Initialize()));
|
|
|
|
|
#else
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(inner->Initialize());
|
|
|
|
|
#endif
|
2019-08-15 20:27:37 +03:00
|
|
|
|
|
2020-02-13 17:38:48 +03:00
|
|
|
|
mInner = std::move(inner);
|
2019-08-15 20:27:37 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServiceWorkerPrivate::~ServiceWorkerPrivate() {
|
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(!mTokenCount);
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(!mInner);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(!mInfo);
|
2015-10-24 15:16:23 +03:00
|
|
|
|
MOZ_ASSERT(mSupportsArray.IsEmpty());
|
2019-07-26 21:41:48 +03:00
|
|
|
|
MOZ_ASSERT(mIdlePromiseHolder.IsEmpty());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
mIdleWorkerTimer->Cancel();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 17:32:12 +03:00
|
|
|
|
namespace {
|
|
|
|
|
|
2019-11-06 21:26:17 +03:00
|
|
|
|
class CheckScriptEvaluationWithCallback final : public WorkerDebuggeeRunnable {
|
2016-12-19 05:38:53 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerPrivate> mServiceWorkerPrivate;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
|
2016-12-19 05:38:53 +03:00
|
|
|
|
|
|
|
|
|
// The script evaluation result must be reported even if the runnable
|
|
|
|
|
// is cancelled.
|
|
|
|
|
RefPtr<LifeCycleEventCallback> mScriptEvaluationCallback;
|
|
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
bool mDone;
|
|
|
|
|
#endif
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
CheckScriptEvaluationWithCallback(
|
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2016-12-19 05:38:53 +03:00
|
|
|
|
ServiceWorkerPrivate* aServiceWorkerPrivate,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
KeepAliveToken* aKeepAliveToken,
|
2016-12-19 05:38:53 +03:00
|
|
|
|
LifeCycleEventCallback* aScriptEvaluationCallback)
|
2019-11-06 21:26:17 +03:00
|
|
|
|
: WorkerDebuggeeRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
2017-06-14 04:27:17 +03:00
|
|
|
|
mServiceWorkerPrivate(new nsMainThreadPtrHolder<ServiceWorkerPrivate>(
|
|
|
|
|
"CheckScriptEvaluationWithCallback::mServiceWorkerPrivate",
|
|
|
|
|
aServiceWorkerPrivate)),
|
|
|
|
|
mKeepAliveToken(new nsMainThreadPtrHolder<KeepAliveToken>(
|
|
|
|
|
"CheckScriptEvaluationWithCallback::mKeepAliveToken",
|
|
|
|
|
aKeepAliveToken)),
|
2016-12-19 05:38:53 +03:00
|
|
|
|
mScriptEvaluationCallback(aScriptEvaluationCallback)
|
2016-02-26 18:52:06 +03:00
|
|
|
|
#ifdef DEBUG
|
2015-11-26 20:03:10 +03:00
|
|
|
|
,
|
|
|
|
|
mDone(false)
|
2016-02-26 18:52:06 +03:00
|
|
|
|
#endif
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
~CheckScriptEvaluationWithCallback() { MOZ_ASSERT(mDone); }
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2016-12-19 05:38:53 +03:00
|
|
|
|
|
|
|
|
|
bool fetchHandlerWasAdded = aWorkerPrivate->FetchHandlerWasAdded();
|
2017-06-12 22:34:10 +03:00
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<bool>(
|
2018-01-27 00:08:59 +03:00
|
|
|
|
"dom::CheckScriptEvaluationWithCallback::ReportFetchFlag", this,
|
2017-06-12 22:34:10 +03:00
|
|
|
|
&CheckScriptEvaluationWithCallback::ReportFetchFlag,
|
|
|
|
|
fetchHandlerWasAdded);
|
2016-12-19 05:38:53 +03:00
|
|
|
|
aWorkerPrivate->DispatchToMainThread(runnable.forget());
|
|
|
|
|
|
|
|
|
|
ReportScriptEvaluationResult(
|
|
|
|
|
aWorkerPrivate->WorkerScriptExecutedSuccessfully());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
void ReportFetchFlag(bool aFetchHandlerWasAdded) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-12-19 05:38:53 +03:00
|
|
|
|
mServiceWorkerPrivate->SetHandlesFetch(aFetchHandlerWasAdded);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
nsresult Cancel() override {
|
2016-12-19 05:38:53 +03:00
|
|
|
|
ReportScriptEvaluationResult(false);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2016-12-19 05:38:53 +03:00
|
|
|
|
void ReportScriptEvaluationResult(bool aScriptEvaluationResult) {
|
2016-02-26 18:52:06 +03:00
|
|
|
|
#ifdef DEBUG
|
2015-11-26 20:03:10 +03:00
|
|
|
|
mDone = true;
|
2016-02-26 18:52:06 +03:00
|
|
|
|
#endif
|
2016-12-19 05:38:53 +03:00
|
|
|
|
mScriptEvaluationCallback->SetResult(aScriptEvaluationResult);
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
|
mWorkerPrivate->DispatchToMainThread(mScriptEvaluationCallback));
|
2015-11-26 20:03:10 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
nsresult ServiceWorkerPrivate::CheckScriptEvaluation(
|
|
|
|
|
LifeCycleEventCallback* aScriptEvaluationCallback) {
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->CheckScriptEvaluation(aScriptEvaluationCallback);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 17:50:23 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r = new CheckScriptEvaluationWithCallback(
|
2016-12-19 05:38:53 +03:00
|
|
|
|
mWorkerPrivate, this, token, aScriptEvaluationCallback);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
2018-05-14 16:02:15 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
namespace {
|
|
|
|
|
|
2018-07-13 19:26:29 +03:00
|
|
|
|
class KeepAliveHandler final : public ExtendableEvent::ExtensionsHandler,
|
2016-11-21 05:14:53 +03:00
|
|
|
|
public PromiseNativeHandler {
|
|
|
|
|
// This class manages lifetime extensions added by calling WaitUntil()
|
|
|
|
|
// or RespondWith(). We allow new extensions as long as we still hold
|
|
|
|
|
// |mKeepAliveToken|. Once the last promise was settled, we queue a microtask
|
|
|
|
|
// which releases the token and prevents further extensions. By doing this,
|
|
|
|
|
// we give other pending microtasks a chance to continue adding extensions.
|
|
|
|
|
|
2018-07-13 19:26:29 +03:00
|
|
|
|
RefPtr<StrongWorkerRef> mWorkerRef;
|
2016-11-21 05:14:53 +03:00
|
|
|
|
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
|
|
|
|
|
|
|
|
|
|
// We start holding a self reference when the first extension promise is
|
|
|
|
|
// added. As far as I can tell, the only case where this is useful is when
|
|
|
|
|
// we're waiting indefinitely on a promise that's no longer reachable
|
|
|
|
|
// and will never be settled.
|
|
|
|
|
// The cycle is broken when the last promise was settled or when the
|
|
|
|
|
// worker is shutting down.
|
|
|
|
|
RefPtr<KeepAliveHandler> mSelfRef;
|
|
|
|
|
|
|
|
|
|
// Called when the last promise was settled.
|
|
|
|
|
RefPtr<ExtendableEventCallback> mCallback;
|
|
|
|
|
|
|
|
|
|
uint32_t mPendingPromisesCount;
|
|
|
|
|
|
|
|
|
|
// We don't actually care what values the promises resolve to, only whether
|
|
|
|
|
// any of them were rejected.
|
|
|
|
|
bool mRejected;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
|
|
explicit KeepAliveHandler(
|
|
|
|
|
const nsMainThreadPtrHandle<KeepAliveToken>& aKeepAliveToken,
|
|
|
|
|
ExtendableEventCallback* aCallback)
|
2018-07-13 19:26:29 +03:00
|
|
|
|
: mKeepAliveToken(aKeepAliveToken),
|
2016-11-21 05:14:53 +03:00
|
|
|
|
mCallback(aCallback),
|
|
|
|
|
mPendingPromisesCount(0),
|
|
|
|
|
mRejected(false) {
|
|
|
|
|
MOZ_ASSERT(mKeepAliveToken);
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2018-07-13 19:26:29 +03:00
|
|
|
|
bool Init() {
|
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
|
|
|
|
|
|
|
|
|
RefPtr<KeepAliveHandler> self = this;
|
|
|
|
|
mWorkerRef = StrongWorkerRef::Create(GetCurrentThreadWorkerPrivate(),
|
|
|
|
|
"KeepAliveHandler",
|
|
|
|
|
[self]() { self->MaybeCleanup(); });
|
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mWorkerRef)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-11-21 05:14:53 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
bool WaitOnPromise(Promise& aPromise) override {
|
|
|
|
|
if (!mKeepAliveToken) {
|
2020-01-15 18:49:26 +03:00
|
|
|
|
MOZ_ASSERT(!GetDispatchFlag());
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MOZ_ASSERT(!mSelfRef, "We shouldn't be holding a self reference!");
|
|
|
|
|
return false;
|
2016-05-25 00:08:20 +03:00
|
|
|
|
}
|
2016-11-21 05:14:53 +03:00
|
|
|
|
if (!mSelfRef) {
|
|
|
|
|
MOZ_ASSERT(!mPendingPromisesCount);
|
|
|
|
|
mSelfRef = this;
|
2016-05-25 00:08:20 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
++mPendingPromisesCount;
|
|
|
|
|
aPromise.AppendNativeHandler(this);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
|
|
|
|
RemovePromise(Resolved);
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
|
|
|
|
RemovePromise(Rejected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MaybeDone() {
|
2018-07-13 19:26:29 +03:00
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2020-01-15 18:49:26 +03:00
|
|
|
|
MOZ_ASSERT(!GetDispatchFlag());
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
|
if (mPendingPromisesCount || !mKeepAliveToken) {
|
2016-11-21 05:14:53 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mCallback) {
|
|
|
|
|
mCallback->FinishedWithResult(mRejected ? Rejected : Resolved);
|
2016-05-25 00:08:20 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MaybeCleanup();
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
private:
|
|
|
|
|
~KeepAliveHandler() { MaybeCleanup(); }
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void MaybeCleanup() {
|
2018-07-13 19:26:29 +03:00
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
if (!mKeepAliveToken) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2018-07-13 19:26:29 +03:00
|
|
|
|
mWorkerRef = nullptr;
|
2016-11-21 05:14:53 +03:00
|
|
|
|
mKeepAliveToken = nullptr;
|
|
|
|
|
mSelfRef = nullptr;
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
|
class MaybeDoneRunner : public MicroTaskRunnable {
|
|
|
|
|
public:
|
|
|
|
|
explicit MaybeDoneRunner(KeepAliveHandler* aHandler) : mHandler(aHandler) {}
|
|
|
|
|
virtual void Run(AutoSlowOperation& aAso) override {
|
|
|
|
|
mHandler->MaybeDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefPtr<KeepAliveHandler> mHandler;
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void RemovePromise(ExtendableEventResult aResult) {
|
2018-07-13 19:26:29 +03:00
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingPromisesCount > 0);
|
2017-06-01 16:20:12 +03:00
|
|
|
|
|
|
|
|
|
// Note: mSelfRef and mKeepAliveToken can be nullptr here
|
|
|
|
|
// if MaybeCleanup() was called just before a promise
|
|
|
|
|
// settled. This can happen, for example, if the
|
|
|
|
|
// worker thread is being terminated for running too
|
|
|
|
|
// long, browser shutdown, etc.
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
|
|
|
|
mRejected |= (aResult == Rejected);
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
--mPendingPromisesCount;
|
2020-01-15 18:49:26 +03:00
|
|
|
|
if (mPendingPromisesCount || GetDispatchFlag()) {
|
2016-05-25 00:08:20 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
CycleCollectedJSContext* cx = CycleCollectedJSContext::Get();
|
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
|
RefPtr<MaybeDoneRunner> r = new MaybeDoneRunner(this);
|
2016-11-21 05:14:53 +03:00
|
|
|
|
cx->DispatchToMicroTask(r.forget());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS0(KeepAliveHandler)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
|
class RegistrationUpdateRunnable : public Runnable {
|
2015-10-26 05:59:48 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
2015-12-11 22:53:11 +03:00
|
|
|
|
const bool mNeedTimeCheck;
|
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
|
RegistrationUpdateRunnable(
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
|
|
|
|
bool aNeedTimeCheck)
|
2018-01-27 00:08:59 +03:00
|
|
|
|
: Runnable("dom::RegistrationUpdateRunnable"),
|
2017-06-12 22:34:10 +03:00
|
|
|
|
mRegistration(aRegistration),
|
2015-12-11 22:53:11 +03:00
|
|
|
|
mNeedTimeCheck(aNeedTimeCheck) {
|
2017-01-27 23:04:24 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mRegistration);
|
2015-12-11 05:49:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-11 22:53:11 +03:00
|
|
|
|
NS_IMETHOD
|
|
|
|
|
Run() override {
|
|
|
|
|
if (mNeedTimeCheck) {
|
|
|
|
|
mRegistration->MaybeScheduleTimeCheckAndUpdate();
|
|
|
|
|
} else {
|
|
|
|
|
mRegistration->MaybeScheduleUpdate();
|
2015-10-26 05:59:48 +03:00
|
|
|
|
}
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
class ExtendableEventWorkerRunnable : public WorkerRunnable {
|
|
|
|
|
protected:
|
|
|
|
|
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ExtendableEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
KeepAliveToken* aKeepAliveToken)
|
2016-06-28 20:28:13 +03:00
|
|
|
|
: WorkerRunnable(aWorkerPrivate) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aKeepAliveToken);
|
|
|
|
|
|
2017-06-14 04:27:17 +03:00
|
|
|
|
mKeepAliveToken = new nsMainThreadPtrHolder<KeepAliveToken>(
|
|
|
|
|
"ExtendableEventWorkerRunnable::mKeepAliveToken", aKeepAliveToken);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
JSContext* aCx, WorkerGlobalScope* aWorkerScope, ExtendableEvent* aEvent,
|
2016-11-21 05:14:53 +03:00
|
|
|
|
ExtendableEventCallback* aCallback) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerScope);
|
|
|
|
|
MOZ_ASSERT(aEvent);
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> sgo = aWorkerScope;
|
2016-02-12 18:40:07 +03:00
|
|
|
|
WidgetEvent* internalEvent = aEvent->WidgetEventPtr();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
RefPtr<KeepAliveHandler> keepAliveHandler =
|
|
|
|
|
new KeepAliveHandler(mKeepAliveToken, aCallback);
|
2018-07-13 19:26:29 +03:00
|
|
|
|
if (NS_WARN_IF(!keepAliveHandler->Init())) {
|
2016-11-10 12:53:24 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
2016-11-21 05:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This must always be set *before* dispatching the event, otherwise
|
|
|
|
|
// waitUntil calls will fail.
|
|
|
|
|
aEvent->SetKeepAliveHandler(keepAliveHandler);
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
ErrorResult result;
|
2018-04-05 20:42:41 +03:00
|
|
|
|
aWorkerScope->DispatchEvent(*aEvent, result);
|
2016-11-10 12:53:24 +03:00
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
result.SuppressException();
|
2016-11-10 12:53:24 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
// [[ If e’s extend lifetime promises is empty, unset e’s extensions allowed
|
|
|
|
|
// flag and abort these steps. ]]
|
|
|
|
|
keepAliveHandler->MaybeDone();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
// We don't block the event when getting an exception but still report the
|
|
|
|
|
// error message.
|
|
|
|
|
// Report exception message. Note: This will not stop the event.
|
|
|
|
|
if (internalEvent->mFlags.mExceptionWasRaised) {
|
|
|
|
|
result.SuppressException();
|
|
|
|
|
return NS_ERROR_XPC_JS_THREW_EXCEPTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-12-12 23:44:47 +03:00
|
|
|
|
class SendMessageEventRunnable final : public ExtendableEventWorkerRunnable {
|
|
|
|
|
const ClientInfoAndState mClientInfoAndState;
|
2018-06-25 17:03:18 +03:00
|
|
|
|
RefPtr<ServiceWorkerCloneData> mData;
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
|
|
|
|
public:
|
2017-12-12 23:44:47 +03:00
|
|
|
|
SendMessageEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
KeepAliveToken* aKeepAliveToken,
|
2018-06-25 17:03:18 +03:00
|
|
|
|
const ClientInfoAndState& aClientInfoAndState,
|
|
|
|
|
RefPtr<ServiceWorkerCloneData>&& aData)
|
2016-11-21 05:14:53 +03:00
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
|
2017-12-12 23:44:47 +03:00
|
|
|
|
mClientInfoAndState(aClientInfoAndState),
|
2018-06-25 17:03:18 +03:00
|
|
|
|
mData(std::move(aData)) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-06-25 17:03:18 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mData);
|
2016-11-21 05:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
JS::Rooted<JS::Value> messageData(aCx);
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> sgo = aWorkerPrivate->GlobalScope();
|
|
|
|
|
ErrorResult rv;
|
2018-06-25 17:03:18 +03:00
|
|
|
|
mData->Read(aCx, &messageData, rv);
|
2018-10-23 00:22:25 +03:00
|
|
|
|
|
|
|
|
|
// If deserialization fails, we will fire a messageerror event
|
2020-02-15 00:23:28 +03:00
|
|
|
|
bool deserializationFailed = rv.Failed();
|
2018-10-23 00:22:25 +03:00
|
|
|
|
|
|
|
|
|
if (!deserializationFailed && NS_WARN_IF(rv.Failed())) {
|
2019-09-11 17:35:28 +03:00
|
|
|
|
rv.SuppressException();
|
2016-11-21 05:14:53 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence<OwningNonNull<MessagePort>> ports;
|
2018-06-25 17:03:18 +03:00
|
|
|
|
if (!mData->TakeTransferredPortsAsSequence(ports)) {
|
2016-11-21 05:14:53 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RootedDictionary<ExtendableMessageEventInit> init(aCx);
|
|
|
|
|
|
|
|
|
|
init.mBubbles = false;
|
|
|
|
|
init.mCancelable = false;
|
|
|
|
|
|
2018-10-23 00:22:25 +03:00
|
|
|
|
// On a messageerror event, we disregard ports:
|
|
|
|
|
// https://w3c.github.io/ServiceWorker/#service-worker-postmessage
|
|
|
|
|
if (!deserializationFailed) {
|
|
|
|
|
init.mData = messageData;
|
|
|
|
|
init.mPorts = ports;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 23:44:47 +03:00
|
|
|
|
init.mSource.SetValue().SetAsClient() =
|
|
|
|
|
new Client(sgo, mClientInfoAndState);
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
2019-09-11 17:35:28 +03:00
|
|
|
|
rv.SuppressException();
|
2016-11-21 05:14:53 +03:00
|
|
|
|
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
|
|
|
|
|
RefPtr<ExtendableMessageEvent> extendableEvent =
|
2018-10-23 00:22:25 +03:00
|
|
|
|
ExtendableMessageEvent::Constructor(
|
|
|
|
|
target,
|
|
|
|
|
deserializationFailed ? NS_LITERAL_STRING("messageerror")
|
|
|
|
|
: NS_LITERAL_STRING("message"),
|
2019-09-11 17:35:28 +03:00
|
|
|
|
init);
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
|
|
|
|
extendableEvent->SetTrusted(true);
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
return NS_SUCCEEDED(DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
aCx, aWorkerPrivate->GlobalScope(), extendableEvent, nullptr));
|
2016-11-21 05:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2018-06-25 17:03:18 +03:00
|
|
|
|
nsresult ServiceWorkerPrivate::SendMessageEvent(
|
|
|
|
|
RefPtr<ServiceWorkerCloneData>&& aData,
|
2017-12-12 23:44:47 +03:00
|
|
|
|
const ClientInfoAndState& aClientInfoAndState) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendMessageEvent(std::move(aData), aClientInfoAndState);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 17:03:18 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(MessageEvent);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-11-21 05:14:53 +03:00
|
|
|
|
|
2018-05-14 16:02:15 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
|
|
|
|
RefPtr<SendMessageEventRunnable> runnable = new SendMessageEventRunnable(
|
2018-06-25 17:03:18 +03:00
|
|
|
|
mWorkerPrivate, token, aClientInfoAndState, std::move(aData));
|
2018-05-15 21:19:46 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
if (!runnable->Dispatch()) {
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
// Handle functional event
|
|
|
|
|
// 9.9.7 If the time difference in seconds calculated by the current time minus
|
|
|
|
|
// registration's last update check time is greater than 86400, invoke Soft
|
|
|
|
|
// Update algorithm.
|
|
|
|
|
class ExtendableFunctionalEventWorkerRunnable
|
|
|
|
|
: public ExtendableEventWorkerRunnable {
|
|
|
|
|
protected:
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
public:
|
|
|
|
|
ExtendableFunctionalEventWorkerRunnable(
|
|
|
|
|
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration)
|
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
|
|
|
|
|
mRegistration(aRegistration) {
|
2017-01-10 00:40:46 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRegistration);
|
2015-10-26 05:59:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
bool aRunResult) override {
|
2017-01-27 23:04:24 +03:00
|
|
|
|
// Sub-class PreRun() or WorkerRun() methods could clear our mRegistration.
|
|
|
|
|
if (mRegistration) {
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
|
new RegistrationUpdateRunnable(mRegistration, true /* time check */);
|
|
|
|
|
aWorkerPrivate->DispatchToMainThread(runnable.forget());
|
|
|
|
|
}
|
2016-02-19 02:02:51 +03:00
|
|
|
|
|
|
|
|
|
ExtendableEventWorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
2015-10-26 05:59:48 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
/*
|
|
|
|
|
* Fires 'install' event on the ServiceWorkerGlobalScope. Modifies busy count
|
|
|
|
|
* since it fires the event. This is ok since there can't be nested
|
|
|
|
|
* ServiceWorkers, so the parent thread -> worker thread requirement for
|
|
|
|
|
* runnables is satisfied.
|
|
|
|
|
*/
|
2015-10-01 02:11:03 +03:00
|
|
|
|
class LifecycleEventWorkerRunnable : public ExtendableEventWorkerRunnable {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsString mEventName;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<LifeCycleEventCallback> mCallback;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
LifecycleEventWorkerRunnable(WorkerPrivate* aWorkerPrivate,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
KeepAliveToken* aToken,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
const nsAString& aEventName,
|
|
|
|
|
LifeCycleEventCallback* aCallback)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aToken),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mEventName(aEventName),
|
|
|
|
|
mCallback(aCallback) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
return DispatchLifecycleEvent(aCx, aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
nsresult Cancel() override {
|
|
|
|
|
mCallback->SetResult(false);
|
2016-09-14 06:14:02 +03:00
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback));
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
private:
|
|
|
|
|
bool DispatchLifecycleEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
2015-11-26 20:03:10 +03:00
|
|
|
|
* Used to handle ExtendableEvent::waitUntil() and catch abnormal worker
|
|
|
|
|
* termination during the execution of life cycle events. It is responsible
|
|
|
|
|
* with advancing the job queue for install/activate tasks.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
*/
|
2018-07-13 19:25:58 +03:00
|
|
|
|
class LifeCycleEventWatcher final : public ExtendableEventCallback {
|
|
|
|
|
RefPtr<StrongWorkerRef> mWorkerRef;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<LifeCycleEventCallback> mCallback;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
~LifeCycleEventWatcher() {
|
|
|
|
|
// XXXcatalinb: If all the promises passed to waitUntil go out of scope,
|
|
|
|
|
// the resulting Promise.all will be cycle collected and it will drop its
|
|
|
|
|
// native handlers (including this object). Instead of waiting for a timeout
|
|
|
|
|
// we report the failure now.
|
2016-02-29 22:52:42 +03:00
|
|
|
|
ReportResult(false);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
public:
|
2016-11-21 05:14:53 +03:00
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(LifeCycleEventWatcher, override)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2018-07-13 19:25:58 +03:00
|
|
|
|
explicit LifeCycleEventWatcher(LifeCycleEventCallback* aCallback)
|
|
|
|
|
: mCallback(aCallback) {
|
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2015-11-26 20:03:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Init() {
|
2018-07-13 19:25:58 +03:00
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
|
|
|
|
// We need to listen for worker termination in case the event handler
|
|
|
|
|
// never completes or never resolves the waitUntil promise. There are
|
|
|
|
|
// two possible scenarios:
|
|
|
|
|
// 1. The keepAlive token expires and the worker is terminated, in which
|
|
|
|
|
// case the registration/update promise will be rejected
|
|
|
|
|
// 2. A new service worker is registered which will terminate the current
|
|
|
|
|
// installing worker.
|
2018-07-13 19:25:58 +03:00
|
|
|
|
RefPtr<LifeCycleEventWatcher> self = this;
|
|
|
|
|
mWorkerRef =
|
|
|
|
|
StrongWorkerRef::Create(workerPrivate, "LifeCycleEventWatcher",
|
|
|
|
|
[self]() { self->ReportResult(false); });
|
|
|
|
|
if (NS_WARN_IF(!mWorkerRef)) {
|
|
|
|
|
mCallback->SetResult(false);
|
2019-03-05 19:47:05 +03:00
|
|
|
|
// Using DispatchToMainThreadForMessaging so that state update on
|
|
|
|
|
// the main thread doesn't happen too soon.
|
|
|
|
|
nsresult rv = workerPrivate->DispatchToMainThreadForMessaging(mCallback);
|
2018-07-13 19:25:58 +03:00
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2015-11-26 20:03:10 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
|
void ReportResult(bool aResult) {
|
2018-07-13 19:25:58 +03:00
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
2018-07-13 19:25:58 +03:00
|
|
|
|
if (!mWorkerRef) {
|
2015-11-26 20:03:10 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
mCallback->SetResult(aResult);
|
2019-03-05 19:47:05 +03:00
|
|
|
|
// Using DispatchToMainThreadForMessaging so that state update on
|
|
|
|
|
// the main thread doesn't happen too soon.
|
|
|
|
|
nsresult rv =
|
|
|
|
|
mWorkerRef->Private()->DispatchToMainThreadForMessaging(mCallback);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-12-03 00:46:53 +03:00
|
|
|
|
MOZ_CRASH("Failed to dispatch life cycle event handler.");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
2018-07-13 19:25:58 +03:00
|
|
|
|
mWorkerRef = nullptr;
|
2015-11-26 20:03:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void FinishedWithResult(ExtendableEventResult aResult) override {
|
2018-07-13 19:25:58 +03:00
|
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2016-11-21 05:14:53 +03:00
|
|
|
|
ReportResult(aResult == Resolved);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-20 00:15:17 +03:00
|
|
|
|
// Note, all WaitUntil() rejections are reported to client consoles
|
|
|
|
|
// by the WaitUntilHandler in ServiceWorkerEvents. This ensures that
|
|
|
|
|
// errors in non-lifecycle events like FetchEvent and PushEvent are
|
|
|
|
|
// reported properly.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool LifecycleEventWorkerRunnable::DispatchLifecycleEvent(
|
|
|
|
|
JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ExtendableEvent> event;
|
|
|
|
|
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
if (mEventName.EqualsASCII("install") || mEventName.EqualsASCII("activate")) {
|
|
|
|
|
ExtendableEventInit init;
|
|
|
|
|
init.mBubbles = false;
|
|
|
|
|
init.mCancelable = false;
|
|
|
|
|
event = ExtendableEvent::Constructor(target, mEventName, init);
|
|
|
|
|
} else {
|
|
|
|
|
MOZ_CRASH("Unexpected lifecycle event");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
// It is important to initialize the watcher before actually dispatching
|
|
|
|
|
// the event in order to catch worker termination while the event handler
|
|
|
|
|
// is still executing. This can happen with infinite loops, for example.
|
2018-07-13 19:25:58 +03:00
|
|
|
|
RefPtr<LifeCycleEventWatcher> watcher = new LifeCycleEventWatcher(mCallback);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
|
|
|
|
if (!watcher->Init()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
nsresult rv = DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
aCx, aWorkerPrivate->GlobalScope(), event, watcher);
|
|
|
|
|
// Do not fail event processing when an exception is thrown.
|
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION) {
|
2016-02-29 22:52:42 +03:00
|
|
|
|
watcher->ReportResult(false);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::SendLifeCycleEvent(
|
2018-04-02 17:50:23 +03:00
|
|
|
|
const nsAString& aEventType, LifeCycleEventCallback* aCallback) {
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendLifeCycleEvent(aEventType, aCallback);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 17:50:23 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r = new LifecycleEventWorkerRunnable(
|
|
|
|
|
mWorkerPrivate, token, aEventType, aCallback);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
class PushErrorReporter final : public ExtendableEventCallback {
|
2016-03-28 21:50:39 +03:00
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
nsString mMessageId;
|
|
|
|
|
|
2020-02-12 13:52:29 +03:00
|
|
|
|
~PushErrorReporter() = default;
|
2016-03-28 21:50:39 +03:00
|
|
|
|
|
|
|
|
|
public:
|
2016-11-21 05:14:53 +03:00
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PushErrorReporter, override)
|
2016-03-28 21:50:39 +03:00
|
|
|
|
|
|
|
|
|
PushErrorReporter(WorkerPrivate* aWorkerPrivate, const nsAString& aMessageId)
|
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mMessageId(aMessageId) {
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void FinishedWithResult(ExtendableEventResult aResult) override {
|
|
|
|
|
if (aResult == Rejected) {
|
|
|
|
|
Report(nsIPushErrorReporter::DELIVERY_UNHANDLED_REJECTION);
|
|
|
|
|
}
|
2016-03-28 21:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Report(
|
|
|
|
|
uint16_t aReason = nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) {
|
2016-09-14 06:14:02 +03:00
|
|
|
|
WorkerPrivate* workerPrivate = mWorkerPrivate;
|
2016-03-28 21:50:39 +03:00
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aReason > nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) ||
|
|
|
|
|
mMessageId.IsEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-12 22:34:10 +03:00
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<uint16_t>(
|
2018-01-27 00:08:59 +03:00
|
|
|
|
"dom::PushErrorReporter::ReportOnMainThread", this,
|
2017-06-12 22:34:10 +03:00
|
|
|
|
&PushErrorReporter::ReportOnMainThread, aReason);
|
2016-03-28 21:50:39 +03:00
|
|
|
|
MOZ_ALWAYS_TRUE(
|
2016-09-14 06:14:02 +03:00
|
|
|
|
NS_SUCCEEDED(workerPrivate->DispatchToMainThread(runnable.forget())));
|
2016-03-28 21:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReportOnMainThread(uint16_t aReason) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-28 21:50:39 +03:00
|
|
|
|
nsCOMPtr<nsIPushErrorReporter> reporter =
|
|
|
|
|
do_GetService("@mozilla.org/push/Service;1");
|
|
|
|
|
if (reporter) {
|
|
|
|
|
nsresult rv = reporter->ReportDeliveryError(mMessageId, aReason);
|
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
class SendPushEventRunnable final
|
|
|
|
|
: public ExtendableFunctionalEventWorkerRunnable {
|
2016-03-28 21:50:39 +03:00
|
|
|
|
nsString mMessageId;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
Maybe<nsTArray<uint8_t>> mData;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SendPushEventRunnable(
|
2015-10-01 02:11:03 +03:00
|
|
|
|
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
const nsAString& aMessageId, const Maybe<nsTArray<uint8_t>>& aData,
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> aRegistration)
|
|
|
|
|
: ExtendableFunctionalEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken,
|
|
|
|
|
aRegistration),
|
2016-03-28 21:50:39 +03:00
|
|
|
|
mMessageId(aMessageId),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mData(aData) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
|
|
|
|
|
|
2016-03-28 21:50:39 +03:00
|
|
|
|
RefPtr<PushErrorReporter> errorReporter =
|
|
|
|
|
new PushErrorReporter(aWorkerPrivate, mMessageId);
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
PushEventInit pei;
|
|
|
|
|
if (mData) {
|
|
|
|
|
const nsTArray<uint8_t>& bytes = mData.ref();
|
|
|
|
|
JSObject* data =
|
|
|
|
|
Uint8Array::Create(aCx, bytes.Length(), bytes.Elements());
|
|
|
|
|
if (!data) {
|
2016-03-28 21:50:39 +03:00
|
|
|
|
errorReporter->Report();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pei.mData.Construct().SetAsArrayBufferView().Init(data);
|
|
|
|
|
}
|
|
|
|
|
pei.mBubbles = false;
|
|
|
|
|
pei.mCancelable = false;
|
|
|
|
|
|
|
|
|
|
ErrorResult result;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<PushEvent> event = PushEvent::Constructor(
|
2015-10-01 02:11:03 +03:00
|
|
|
|
globalObj, NS_LITERAL_STRING("push"), pei, result);
|
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
result.SuppressException();
|
2016-03-28 21:50:39 +03:00
|
|
|
|
errorReporter->Report();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
nsresult rv = DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
aCx, aWorkerPrivate->GlobalScope(), event, errorReporter);
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
// We don't cancel WorkerPrivate when catching an excetpion.
|
2016-03-28 21:50:39 +03:00
|
|
|
|
errorReporter->Report(nsIPushErrorReporter::DELIVERY_UNCAUGHT_EXCEPTION);
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
class SendPushSubscriptionChangeEventRunnable final
|
|
|
|
|
: public ExtendableEventWorkerRunnable {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public:
|
|
|
|
|
explicit SendPushSubscriptionChangeEventRunnable(
|
2015-10-01 02:11:03 +03:00
|
|
|
|
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken)
|
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
2015-10-31 04:13:48 +03:00
|
|
|
|
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-31 04:13:48 +03:00
|
|
|
|
ExtendableEventInit init;
|
|
|
|
|
init.mBubbles = false;
|
|
|
|
|
init.mCancelable = false;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-31 04:13:48 +03:00
|
|
|
|
RefPtr<ExtendableEvent> event = ExtendableEvent::Constructor(
|
|
|
|
|
target, NS_LITERAL_STRING("pushsubscriptionchange"), init);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
2015-10-31 04:13:48 +03:00
|
|
|
|
DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
|
|
|
|
|
event, nullptr);
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2016-03-28 21:50:39 +03:00
|
|
|
|
nsresult ServiceWorkerPrivate::SendPushEvent(
|
|
|
|
|
const nsAString& aMessageId, const Maybe<nsTArray<uint8_t>>& aData,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
ServiceWorkerRegistrationInfo* aRegistration) {
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendPushEvent(aRegistration, aMessageId, aData);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 17:50:23 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(PushEvent);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
2015-10-26 05:59:48 +03:00
|
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
|
2017-06-14 04:27:17 +03:00
|
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
|
2017-10-27 20:45:52 +03:00
|
|
|
|
"ServiceWorkerRegistrationInfoProxy", aRegistration, false));
|
2015-10-26 05:59:48 +03:00
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r = new SendPushEventRunnable(
|
|
|
|
|
mWorkerPrivate, token, aMessageId, aData, regInfo);
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
|
|
|
|
if (mInfo->State() == ServiceWorkerState::Activating) {
|
|
|
|
|
mPendingFunctionalEvents.AppendElement(r.forget());
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
|
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::SendPushSubscriptionChangeEvent() {
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendPushSubscriptionChangeEvent();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 17:50:23 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(PushSubscriptionChangeEvent);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r =
|
2016-07-28 03:36:10 +03:00
|
|
|
|
new SendPushSubscriptionChangeEventRunnable(mWorkerPrivate, token);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
class AllowWindowInteractionHandler final : public ExtendableEventCallback,
|
2017-01-26 19:01:33 +03:00
|
|
|
|
public nsITimerCallback,
|
2017-07-26 21:18:20 +03:00
|
|
|
|
public nsINamed {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2018-03-16 17:46:21 +03:00
|
|
|
|
RefPtr<StrongWorkerRef> mWorkerRef;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
~AllowWindowInteractionHandler() {
|
2017-01-26 19:01:33 +03:00
|
|
|
|
// We must either fail to initialize or call ClearWindowAllowed.
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mTimer);
|
2018-03-16 17:46:21 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mWorkerRef);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClearWindowAllowed(WorkerPrivate* aWorkerPrivate) {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (!mTimer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// XXXcatalinb: This *might* be executed after the global was unrooted, in
|
|
|
|
|
// which case GlobalScope() will return null. Making the check here just
|
|
|
|
|
// to be safe.
|
|
|
|
|
WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
|
|
|
|
|
if (!globalScope) {
|
|
|
|
|
return;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
globalScope->ConsumeWindowInteraction();
|
|
|
|
|
mTimer->Cancel();
|
|
|
|
|
mTimer = nullptr;
|
2017-01-26 19:01:33 +03:00
|
|
|
|
|
2018-03-16 17:46:21 +03:00
|
|
|
|
mWorkerRef = nullptr;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartClearWindowTimer(WorkerPrivate* aWorkerPrivate) {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_ASSERT(!mTimer);
|
|
|
|
|
|
|
|
|
|
nsresult rv;
|
2017-10-16 09:15:40 +03:00
|
|
|
|
nsCOMPtr<nsITimer> timer =
|
|
|
|
|
NS_NewTimer(aWorkerPrivate->ControlEventTarget());
|
|
|
|
|
if (NS_WARN_IF(!timer)) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 17:46:21 +03:00
|
|
|
|
MOZ_ASSERT(!mWorkerRef);
|
|
|
|
|
RefPtr<AllowWindowInteractionHandler> self = this;
|
|
|
|
|
mWorkerRef = StrongWorkerRef::Create(
|
|
|
|
|
aWorkerPrivate, "AllowWindowInteractionHandler", [self]() {
|
|
|
|
|
// We could try to hold the worker alive until the timer fires, but
|
|
|
|
|
// other APIs are not likely to work in this partially shutdown state.
|
|
|
|
|
// We might as well let the worker thread exit.
|
|
|
|
|
self->ClearWindowAllowed(self->mWorkerRef->Private());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!mWorkerRef) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-01-26 19:01:33 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
aWorkerPrivate->GlobalScope()->AllowWindowInteraction();
|
|
|
|
|
timer.swap(mTimer);
|
|
|
|
|
|
|
|
|
|
// We swap first and then initialize the timer so that even if initializing
|
|
|
|
|
// fails, we still clean the busy count and interaction count correctly.
|
|
|
|
|
// The timer can't be initialized before modifying the busy count since the
|
|
|
|
|
// timer thread could run and call the timeout but the worker may
|
|
|
|
|
// already be terminating and modifying the busy count could fail.
|
2017-01-26 19:01:33 +03:00
|
|
|
|
rv = mTimer->InitWithCallback(this, gDOMDisableOpenClickDelay,
|
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
ClearWindowAllowed(aWorkerPrivate);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-26 19:01:33 +03:00
|
|
|
|
// nsITimerCallback virtual methods
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
Notify(nsITimer* aTimer) override {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mTimer == aTimer);
|
2018-03-16 17:46:21 +03:00
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
|
ClearWindowAllowed(workerPrivate);
|
2017-01-26 19:01:33 +03:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
|
// nsINamed virtual methods
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
GetName(nsACString& aName) override {
|
|
|
|
|
aName.AssignLiteral("AllowWindowInteractionHandler");
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public:
|
2017-01-26 19:01:33 +03:00
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
explicit AllowWindowInteractionHandler(WorkerPrivate* aWorkerPrivate) {
|
|
|
|
|
StartClearWindowTimer(aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void FinishedWithResult(ExtendableEventResult /* aResult */) override {
|
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
ClearWindowAllowed(workerPrivate);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS(AllowWindowInteractionHandler, nsITimerCallback, nsINamed)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
|
class SendNotificationEventRunnable final
|
|
|
|
|
: public ExtendableEventWorkerRunnable {
|
|
|
|
|
const nsString mEventName;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
const nsString mID;
|
|
|
|
|
const nsString mTitle;
|
|
|
|
|
const nsString mDir;
|
|
|
|
|
const nsString mLang;
|
|
|
|
|
const nsString mBody;
|
|
|
|
|
const nsString mTag;
|
|
|
|
|
const nsString mIcon;
|
|
|
|
|
const nsString mData;
|
|
|
|
|
const nsString mBehavior;
|
|
|
|
|
const nsString mScope;
|
|
|
|
|
|
|
|
|
|
public:
|
2016-04-20 08:04:09 +03:00
|
|
|
|
SendNotificationEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
KeepAliveToken* aKeepAliveToken,
|
|
|
|
|
const nsAString& aEventName,
|
|
|
|
|
const nsAString& aID, const nsAString& aTitle,
|
|
|
|
|
const nsAString& aDir, const nsAString& aLang,
|
|
|
|
|
const nsAString& aBody, const nsAString& aTag,
|
|
|
|
|
const nsAString& aIcon, const nsAString& aData,
|
|
|
|
|
const nsAString& aBehavior,
|
|
|
|
|
const nsAString& aScope)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken),
|
2016-04-20 08:04:09 +03:00
|
|
|
|
mEventName(aEventName),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mID(aID),
|
|
|
|
|
mTitle(aTitle),
|
|
|
|
|
mDir(aDir),
|
|
|
|
|
mLang(aLang),
|
|
|
|
|
mBody(aBody),
|
|
|
|
|
mTag(aTag),
|
|
|
|
|
mIcon(aIcon),
|
|
|
|
|
mData(aData),
|
|
|
|
|
mBehavior(aBehavior),
|
|
|
|
|
mScope(aScope) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<EventTarget> target = do_QueryObject(aWorkerPrivate->GlobalScope());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
ErrorResult result;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<Notification> notification = Notification::ConstructFromFields(
|
2015-10-01 02:11:03 +03:00
|
|
|
|
aWorkerPrivate->GlobalScope(), mID, mTitle, mDir, mLang, mBody, mTag,
|
|
|
|
|
mIcon, mData, mScope, result);
|
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NotificationEventInit nei;
|
|
|
|
|
nei.mNotification = notification;
|
|
|
|
|
nei.mBubbles = false;
|
|
|
|
|
nei.mCancelable = false;
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<NotificationEvent> event =
|
2019-09-11 17:35:28 +03:00
|
|
|
|
NotificationEvent::Constructor(target, mEventName, nei);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2018-01-10 13:05:07 +03:00
|
|
|
|
|
|
|
|
|
RefPtr<AllowWindowInteractionHandler> allowWindowInteraction;
|
|
|
|
|
if (mEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
|
|
|
|
|
allowWindowInteraction =
|
|
|
|
|
new AllowWindowInteractionHandler(aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
nsresult rv = DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
aCx, aWorkerPrivate->GlobalScope(), event, allowWindowInteraction);
|
|
|
|
|
// Don't reject when catching an exception
|
2018-01-10 13:05:07 +03:00
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION &&
|
|
|
|
|
allowWindowInteraction) {
|
2016-11-21 05:14:53 +03:00
|
|
|
|
allowWindowInteraction->FinishedWithResult(Rejected);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
|
nsresult ServiceWorkerPrivate::SendNotificationEvent(
|
|
|
|
|
const nsAString& aEventName, const nsAString& aID, const nsAString& aTitle,
|
|
|
|
|
const nsAString& aDir, const nsAString& aLang, const nsAString& aBody,
|
|
|
|
|
const nsAString& aTag, const nsAString& aIcon, const nsAString& aData,
|
|
|
|
|
const nsAString& aBehavior, const nsAString& aScope) {
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
|
WakeUpReason why;
|
|
|
|
|
if (aEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
|
|
|
|
|
why = NotificationClickEvent;
|
2017-10-23 21:24:31 +03:00
|
|
|
|
gDOMDisableOpenClickDelay =
|
|
|
|
|
Preferences::GetInt("dom.serviceWorkers.disable_open_click_delay");
|
2016-04-20 08:04:09 +03:00
|
|
|
|
} else if (aEventName.EqualsLiteral(NOTIFICATION_CLOSE_EVENT_NAME)) {
|
|
|
|
|
why = NotificationCloseEvent;
|
|
|
|
|
} else {
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid notification event name");
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendNotificationEvent(aEventName, aID, aTitle, aDir, aLang,
|
|
|
|
|
aBody, aTag, aIcon, aData, aBehavior,
|
|
|
|
|
aScope, gDOMDisableOpenClickDelay);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 17:50:23 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(why);
|
2016-04-20 08:04:09 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
|
|
|
|
|
|
|
|
|
RefPtr<WorkerRunnable> r = new SendNotificationEventRunnable(
|
2016-04-20 08:04:09 +03:00
|
|
|
|
mWorkerPrivate, token, aEventName, aID, aTitle, aDir, aLang, aBody, aTag,
|
|
|
|
|
aIcon, aData, aBehavior, aScope);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
// Inheriting ExtendableEventWorkerRunnable so that the worker is not terminated
|
|
|
|
|
// while handling the fetch event, though that's very unlikely.
|
2015-10-26 05:59:48 +03:00
|
|
|
|
class FetchEventRunnable : public ExtendableFunctionalEventWorkerRunnable,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public nsIHttpHeaderVisitor {
|
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
|
|
|
|
|
const nsCString mScriptSpec;
|
|
|
|
|
nsTArray<nsCString> mHeaderNames;
|
|
|
|
|
nsTArray<nsCString> mHeaderValues;
|
|
|
|
|
nsCString mSpec;
|
2016-11-07 05:16:34 +03:00
|
|
|
|
nsCString mFragment;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCString mMethod;
|
2015-11-26 02:05:34 +03:00
|
|
|
|
nsString mClientId;
|
2018-11-15 11:28:23 +03:00
|
|
|
|
nsString mResultingClientId;
|
2017-04-10 16:13:21 +03:00
|
|
|
|
bool mMarkLaunchServiceWorkerEnd;
|
2016-02-29 02:05:40 +03:00
|
|
|
|
RequestCache mCacheMode;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
RequestMode mRequestMode;
|
|
|
|
|
RequestRedirect mRequestRedirect;
|
|
|
|
|
RequestCredentials mRequestCredentials;
|
|
|
|
|
nsContentPolicyType mContentPolicyType;
|
|
|
|
|
nsCOMPtr<nsIInputStream> mUploadStream;
|
2017-12-13 03:38:19 +03:00
|
|
|
|
int64_t mUploadStreamContentLength;
|
2019-08-20 19:22:44 +03:00
|
|
|
|
nsString mReferrer;
|
2016-02-27 01:36:45 +03:00
|
|
|
|
ReferrerPolicy mReferrerPolicy;
|
2016-09-07 05:20:23 +03:00
|
|
|
|
nsString mIntegrity;
|
2018-11-15 11:28:23 +03:00
|
|
|
|
const bool mIsNonSubresourceRequest;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public:
|
|
|
|
|
FetchEventRunnable(
|
2015-10-01 02:11:03 +03:00
|
|
|
|
WorkerPrivate* aWorkerPrivate, KeepAliveToken* aKeepAliveToken,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
|
|
|
|
|
// CSP checks might require the worker script spec
|
|
|
|
|
// later on.
|
|
|
|
|
const nsACString& aScriptSpec,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
2018-11-15 11:28:23 +03:00
|
|
|
|
const nsAString& aClientId, const nsAString& aResultingClientId,
|
2020-01-25 03:05:12 +03:00
|
|
|
|
bool aMarkLaunchServiceWorkerEnd, bool aIsNonSubresourceRequest)
|
2015-10-26 05:59:48 +03:00
|
|
|
|
: ExtendableFunctionalEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken,
|
|
|
|
|
aRegistration),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mInterceptedChannel(aChannel),
|
|
|
|
|
mScriptSpec(aScriptSpec),
|
2017-12-12 23:44:48 +03:00
|
|
|
|
mClientId(aClientId),
|
2018-11-15 11:28:23 +03:00
|
|
|
|
mResultingClientId(aResultingClientId),
|
2017-04-10 16:13:21 +03:00
|
|
|
|
mMarkLaunchServiceWorkerEnd(aMarkLaunchServiceWorkerEnd),
|
2016-02-29 02:05:40 +03:00
|
|
|
|
mCacheMode(RequestCache::Default),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mRequestMode(RequestMode::No_cors),
|
|
|
|
|
mRequestRedirect(RequestRedirect::Follow)
|
|
|
|
|
// By default we set it to same-origin since normal HTTP fetches always
|
|
|
|
|
// send credentials to same-origin websites unless explicitly forbidden.
|
|
|
|
|
,
|
|
|
|
|
mRequestCredentials(RequestCredentials::Same_origin),
|
|
|
|
|
mContentPolicyType(nsIContentPolicy::TYPE_INVALID),
|
2017-12-13 03:38:19 +03:00
|
|
|
|
mUploadStreamContentLength(-1),
|
2019-08-20 19:22:44 +03:00
|
|
|
|
mReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR)),
|
2016-02-27 01:36:45 +03:00
|
|
|
|
mReferrerPolicy(ReferrerPolicy::_empty),
|
2018-11-15 11:28:23 +03:00
|
|
|
|
mIsNonSubresourceRequest(aIsNonSubresourceRequest) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
VisitHeader(const nsACString& aHeader, const nsACString& aValue) override {
|
|
|
|
|
mHeaderNames.AppendElement(aHeader);
|
|
|
|
|
mHeaderValues.AppendElement(aValue);
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult Init() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
|
nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2015-11-02 19:27:00 +03:00
|
|
|
|
rv = mInterceptedChannel->GetSecureUpgradedChannelURI(getter_AddRefs(uri));
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2016-02-29 21:21:19 +03:00
|
|
|
|
// Normally we rely on the Request constructor to strip the fragment, but
|
|
|
|
|
// when creating the FetchEvent we bypass the constructor. So strip the
|
|
|
|
|
// fragment manually here instead. We can't do it later when we create
|
|
|
|
|
// the Request because that code executes off the main thread.
|
|
|
|
|
nsCOMPtr<nsIURI> uriNoFragment;
|
2018-07-23 14:28:47 +03:00
|
|
|
|
rv = NS_GetURIWithoutRef(uri, getter_AddRefs(uriNoFragment));
|
2016-02-29 21:21:19 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
rv = uriNoFragment->GetSpec(mSpec);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-11-07 05:16:34 +03:00
|
|
|
|
rv = uri->GetRef(mFragment);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
uint32_t loadFlags;
|
|
|
|
|
rv = channel->GetLoadFlags(&loadFlags);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2019-04-25 15:16:35 +03:00
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mContentPolicyType = loadInfo->InternalContentPolicyType();
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel);
|
2016-01-25 22:03:13 +03:00
|
|
|
|
MOZ_ASSERT(httpChannel, "How come we don't have an HTTP channel?");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-08-21 16:28:23 +03:00
|
|
|
|
mReferrerPolicy = ReferrerPolicy::_empty;
|
2019-08-20 19:22:44 +03:00
|
|
|
|
mReferrer = EmptyString();
|
2019-05-02 15:33:55 +03:00
|
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo();
|
|
|
|
|
if (referrerInfo) {
|
2019-08-21 16:28:23 +03:00
|
|
|
|
mReferrerPolicy = referrerInfo->ReferrerPolicy();
|
2019-08-20 19:22:44 +03:00
|
|
|
|
Unused << referrerInfo->GetComputedReferrerSpec(mReferrer);
|
2019-05-02 15:33:55 +03:00
|
|
|
|
}
|
2016-02-27 01:36:45 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
rv = httpChannel->GetRequestMethod(mMethod);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
nsCOMPtr<nsIHttpChannelInternal> internalChannel =
|
|
|
|
|
do_QueryInterface(httpChannel);
|
|
|
|
|
NS_ENSURE_TRUE(internalChannel, NS_ERROR_NOT_AVAILABLE);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
mRequestMode = InternalRequest::MapChannelToRequestMode(channel);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-02-29 02:05:40 +03:00
|
|
|
|
// This is safe due to static_asserts in ServiceWorkerManager.cpp.
|
2016-01-25 22:03:13 +03:00
|
|
|
|
uint32_t redirectMode;
|
2016-12-20 06:49:32 +03:00
|
|
|
|
rv = internalChannel->GetRedirectMode(&redirectMode);
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-01-25 22:03:13 +03:00
|
|
|
|
mRequestRedirect = static_cast<RequestRedirect>(redirectMode);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-02-29 02:05:40 +03:00
|
|
|
|
// This is safe due to static_asserts in ServiceWorkerManager.cpp.
|
|
|
|
|
uint32_t cacheMode;
|
2016-12-20 06:49:32 +03:00
|
|
|
|
rv = internalChannel->GetFetchCacheMode(&cacheMode);
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-02-29 02:05:40 +03:00
|
|
|
|
mCacheMode = static_cast<RequestCache>(cacheMode);
|
|
|
|
|
|
2016-12-20 06:49:32 +03:00
|
|
|
|
rv = internalChannel->GetIntegrityMetadata(mIntegrity);
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-09-07 05:20:23 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
mRequestCredentials =
|
|
|
|
|
InternalRequest::MapChannelToRequestCredentials(channel);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
rv = httpChannel->VisitNonDefaultRequestHeaders(this);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
nsCOMPtr<nsIUploadChannel2> uploadChannel = do_QueryInterface(httpChannel);
|
|
|
|
|
if (uploadChannel) {
|
|
|
|
|
MOZ_ASSERT(!mUploadStream);
|
|
|
|
|
nsCOMPtr<nsIInputStream> uploadStream;
|
2017-12-13 03:38:19 +03:00
|
|
|
|
rv = uploadChannel->CloneUploadStream(&mUploadStreamContentLength,
|
|
|
|
|
getter_AddRefs(uploadStream));
|
2016-01-25 22:03:13 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-02-10 17:10:59 +03:00
|
|
|
|
mUploadStream = uploadStream;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2017-04-10 16:13:21 +03:00
|
|
|
|
|
|
|
|
|
if (mMarkLaunchServiceWorkerEnd) {
|
|
|
|
|
mInterceptedChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
|
2017-08-17 13:32:36 +03:00
|
|
|
|
|
|
|
|
|
// A probe to measure sw launch time for telemetry.
|
|
|
|
|
TimeStamp launchStartTime = TimeStamp();
|
|
|
|
|
mInterceptedChannel->GetLaunchServiceWorkerStart(&launchStartTime);
|
|
|
|
|
|
|
|
|
|
TimeStamp launchEndTime = TimeStamp();
|
|
|
|
|
mInterceptedChannel->GetLaunchServiceWorkerEnd(&launchEndTime);
|
|
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::SERVICE_WORKER_LAUNCH_TIME,
|
|
|
|
|
launchStartTime, launchEndTime);
|
2017-04-10 16:13:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mInterceptedChannel->SetDispatchFetchEventEnd(TimeStamp::Now());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return DispatchFetchEvent(aCx, aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-24 18:47:59 +03:00
|
|
|
|
nsresult Cancel() override {
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ResumeRequest(mInterceptedChannel);
|
2016-09-14 06:14:02 +03:00
|
|
|
|
if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable))) {
|
2015-11-24 18:47:59 +03:00
|
|
|
|
NS_WARNING("Failed to resume channel on FetchEventRunnable::Cancel()!\n");
|
|
|
|
|
}
|
|
|
|
|
WorkerRunnable::Cancel();
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
private:
|
2020-02-12 13:52:29 +03:00
|
|
|
|
~FetchEventRunnable() = default;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
|
class ResumeRequest final : public Runnable {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
|
explicit ResumeRequest(
|
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel)
|
2018-01-27 00:08:59 +03:00
|
|
|
|
: Runnable("dom::FetchEventRunnable::ResumeRequest"),
|
2017-06-12 22:34:10 +03:00
|
|
|
|
mChannel(aChannel) {
|
2017-04-26 01:16:57 +03:00
|
|
|
|
mChannel->SetFinishResponseStart(TimeStamp::Now());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
|
NS_IMETHOD Run() override {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-04-10 16:13:21 +03:00
|
|
|
|
|
2017-04-26 01:16:57 +03:00
|
|
|
|
TimeStamp timeStamp = TimeStamp::Now();
|
|
|
|
|
mChannel->SetHandleFetchEventEnd(timeStamp);
|
|
|
|
|
mChannel->SetChannelResetEnd(timeStamp);
|
|
|
|
|
mChannel->SaveTimeStamps();
|
2017-04-10 16:13:21 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsresult rv = mChannel->ResetInterception();
|
2017-10-30 17:30:01 +03:00
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
NS_WARNING("Failed to resume intercepted network request");
|
|
|
|
|
mChannel->CancelInterception(rv);
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool DispatchFetchEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
|
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
|
|
|
|
|
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<InternalHeaders> internalHeaders =
|
|
|
|
|
new InternalHeaders(HeadersGuardEnum::Request);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(mHeaderNames.Length() == mHeaderValues.Length());
|
|
|
|
|
for (uint32_t i = 0; i < mHeaderNames.Length(); i++) {
|
|
|
|
|
ErrorResult result;
|
|
|
|
|
internalHeaders->Set(mHeaderNames[i], mHeaderValues[i], result);
|
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
result.SuppressException();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorResult result;
|
2016-01-14 02:14:38 +03:00
|
|
|
|
internalHeaders->SetGuard(HeadersGuardEnum::Immutable, result);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
result.SuppressException();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-01-14 02:14:38 +03:00
|
|
|
|
RefPtr<InternalRequest> internalReq = new InternalRequest(
|
|
|
|
|
mSpec, mFragment, mMethod, internalHeaders.forget(), mCacheMode,
|
2019-08-20 19:22:44 +03:00
|
|
|
|
mRequestMode, mRequestRedirect, mRequestCredentials, mReferrer,
|
|
|
|
|
mReferrerPolicy, mContentPolicyType, mIntegrity);
|
2017-12-13 03:38:19 +03:00
|
|
|
|
internalReq->SetBody(mUploadStream, mUploadStreamContentLength);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-12-04 09:39:10 +03:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
|
nsresult rv = mInterceptedChannel->GetChannel(getter_AddRefs(channel));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheInfoChannel> cic = do_QueryInterface(channel);
|
Bug 1487100 - Allow calling nsICacheInfoChannel.preferAlternativeDataType(altDataType, contentType) multiple times r=michal,luke
This patch changes the way we set and handle the preferred alternate data type.
It is no longer just one choice, but a set of preferences, each conditional
on the contentType of the resource.
For example:
var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
cc.preferAlternativeDataType("js-bytecode", "text/javascript");
cc.preferAlternativeDataType("ammended-text", "text/plain");
cc.preferAlternativeDataType("something-else", "");
When loaded from the cache, the available alt-data type will be checked against
"js-bytecode" if the contentType is "text/javascript", "ammended-text" if the contentType is "text/plain" or "something-else" for all contentTypes.
Note that the alt-data type could be "something-else" even if the contentType is "text/javascript".
The preferences are saved as an nsTArray<mozilla::Tuple<nsCString, nsCString>>.
Differential Revision: https://phabricator.services.mozilla.com/D8071
--HG--
extra : rebase_source : eb4961f05a52e557e7d2d986d59e0a2cf18a3447
extra : source : dd1c31ea78c2b15d14750d137037a54d50719997
2018-10-17 16:58:30 +03:00
|
|
|
|
if (cic && !cic->PreferredAlternativeDataTypes().IsEmpty()) {
|
|
|
|
|
// TODO: the internal request probably needs all the preferred types.
|
|
|
|
|
nsAutoCString alternativeDataType;
|
|
|
|
|
alternativeDataType.Assign(
|
2019-02-20 13:05:42 +03:00
|
|
|
|
cic->PreferredAlternativeDataTypes()[0].type());
|
2017-12-04 09:39:10 +03:00
|
|
|
|
internalReq->SetPreferredAlternativeDataType(alternativeDataType);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 02:14:38 +03:00
|
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
|
do_QueryInterface(globalObj.GetAsSupports());
|
|
|
|
|
if (NS_WARN_IF(!global)) {
|
2015-10-19 21:23:28 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-29 12:31:06 +03:00
|
|
|
|
|
|
|
|
|
// TODO This request object should be created with a AbortSignal object
|
|
|
|
|
// which should be aborted if the loading is aborted. See bug 1394102.
|
|
|
|
|
RefPtr<Request> request = new Request(global, internalReq, nullptr);
|
2015-10-19 21:23:28 +03:00
|
|
|
|
|
2016-01-25 22:03:13 +03:00
|
|
|
|
MOZ_ASSERT_IF(internalReq->IsNavigationRequest(),
|
2015-10-01 02:11:03 +03:00
|
|
|
|
request->Redirect() == RequestRedirect::Manual);
|
|
|
|
|
|
|
|
|
|
RootedDictionary<FetchEventInit> init(aCx);
|
2016-01-09 06:35:22 +03:00
|
|
|
|
init.mRequest = request;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
init.mBubbles = false;
|
|
|
|
|
init.mCancelable = true;
|
2017-12-12 23:44:48 +03:00
|
|
|
|
// Only expose the FetchEvent.clientId on subresource requests for now.
|
2018-11-15 11:28:23 +03:00
|
|
|
|
// Once we implement .targetClientId we can then start exposing .clientId
|
|
|
|
|
// on non-subresource requests as well. See bug 1487534.
|
2017-12-12 23:44:48 +03:00
|
|
|
|
if (!mClientId.IsEmpty() && !internalReq->IsNavigationRequest()) {
|
2016-01-08 23:53:38 +03:00
|
|
|
|
init.mClientId = mClientId;
|
|
|
|
|
}
|
2018-11-15 11:28:23 +03:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* https://w3c.github.io/ServiceWorker/#on-fetch-request-algorithm
|
|
|
|
|
*
|
|
|
|
|
* "If request is a non-subresource request and request’s
|
|
|
|
|
* destination is not "report", initialize e’s resultingClientId attribute
|
|
|
|
|
* to reservedClient’s [resultingClient's] id, and to the empty string
|
|
|
|
|
* otherwise." (Step 18.8)
|
|
|
|
|
*/
|
|
|
|
|
if (!mResultingClientId.IsEmpty() && mIsNonSubresourceRequest &&
|
|
|
|
|
internalReq->Destination() != RequestDestination::Report) {
|
|
|
|
|
init.mResultingClientId = mResultingClientId;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 17:35:28 +03:00
|
|
|
|
RefPtr<FetchEvent> event =
|
|
|
|
|
FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-04-27 12:24:04 +03:00
|
|
|
|
event->PostInit(mInterceptedChannel, mRegistration, mScriptSpec);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
2017-04-10 16:13:21 +03:00
|
|
|
|
mInterceptedChannel->SetHandleFetchEventStart(TimeStamp::Now());
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
nsresult rv2 = DispatchExtendableEventOnWorkerScope(
|
|
|
|
|
aCx, aWorkerPrivate->GlobalScope(), event, nullptr);
|
2017-03-03 04:51:06 +03:00
|
|
|
|
if ((NS_WARN_IF(NS_FAILED(rv2)) &&
|
|
|
|
|
rv2 != NS_ERROR_XPC_JS_THREW_EXCEPTION) ||
|
|
|
|
|
!event->WaitToRespond()) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCOMPtr<nsIRunnable> runnable;
|
2016-11-16 22:10:22 +03:00
|
|
|
|
MOZ_ASSERT(!aWorkerPrivate->UsesSystemPrincipal(),
|
|
|
|
|
"We don't support system-principal serviceworkers");
|
|
|
|
|
if (event->DefaultPrevented(CallerType::NonSystem)) {
|
2015-12-11 22:53:11 +03:00
|
|
|
|
runnable = new CancelChannelRunnable(mInterceptedChannel, mRegistration,
|
|
|
|
|
NS_ERROR_INTERCEPTION_FAILED);
|
2016-11-10 12:53:24 +03:00
|
|
|
|
} else {
|
|
|
|
|
runnable = new ResumeRequest(mInterceptedChannel);
|
2015-12-11 22:53:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 06:14:02 +03:00
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
|
mWorkerPrivate->DispatchToMainThread(runnable.forget()));
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(FetchEventRunnable, WorkerRunnable,
|
|
|
|
|
nsIHttpHeaderVisitor)
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::SendFetchEvent(
|
|
|
|
|
nsIInterceptedChannel* aChannel, nsILoadGroup* aLoadGroup,
|
2020-01-25 03:05:12 +03:00
|
|
|
|
const nsAString& aClientId, const nsAString& aResultingClientId) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
2017-01-13 01:00:36 +03:00
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
|
if (NS_WARN_IF(!mInfo || !swm)) {
|
2016-12-19 05:38:53 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 21:41:48 +03:00
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
|
nsresult rv = aChannel->GetChannel(getter_AddRefs(channel));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
bool isNonSubresourceRequest =
|
|
|
|
|
nsContentUtils::IsNonSubresourceRequest(channel);
|
|
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> registration;
|
|
|
|
|
if (isNonSubresourceRequest) {
|
|
|
|
|
registration = swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
|
|
|
|
|
} else {
|
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo;
|
|
|
|
|
channel->GetLoadInfo(getter_AddRefs(loadInfo));
|
|
|
|
|
|
|
|
|
|
// We'll check for a null registration below rather than an error code here.
|
|
|
|
|
Unused << swm->GetClientRegistration(loadInfo->GetClientInfo().ref(),
|
|
|
|
|
getter_AddRefs(registration));
|
|
|
|
|
}
|
2016-12-19 05:38:53 +03:00
|
|
|
|
|
2017-01-10 00:40:46 +03:00
|
|
|
|
// Its possible the registration is removed between starting the interception
|
|
|
|
|
// and actually dispatching the fetch event. In these cases we simply
|
|
|
|
|
// want to restart the original network request. Since this is a normal
|
|
|
|
|
// condition we handle the reset here instead of returning an error which
|
|
|
|
|
// would in turn trigger a console report.
|
|
|
|
|
if (!registration) {
|
2017-10-30 17:30:01 +03:00
|
|
|
|
nsresult rv = aChannel->ResetInterception();
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
NS_WARNING("Failed to resume intercepted network request");
|
|
|
|
|
aChannel->CancelInterception(rv);
|
|
|
|
|
}
|
2017-01-10 00:40:46 +03:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
// Handle Fetch algorithm - step 16. If the service worker didn't register
|
|
|
|
|
// any fetch event handlers, then abort the interception and maybe trigger
|
|
|
|
|
// the soft update algorithm.
|
|
|
|
|
if (!mInfo->HandlesFetch()) {
|
2017-10-30 17:30:01 +03:00
|
|
|
|
nsresult rv = aChannel->ResetInterception();
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
NS_WARNING("Failed to resume intercepted network request");
|
|
|
|
|
aChannel->CancelInterception(rv);
|
|
|
|
|
}
|
2016-12-19 05:38:53 +03:00
|
|
|
|
|
|
|
|
|
// Trigger soft updates if necessary.
|
|
|
|
|
registration->MaybeScheduleTimeCheckAndUpdate();
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->SendFetchEvent(std::move(registration), aChannel, aClientId,
|
2020-01-25 03:05:12 +03:00
|
|
|
|
aResultingClientId);
|
2019-08-15 20:27:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-10 16:13:21 +03:00
|
|
|
|
aChannel->SetLaunchServiceWorkerStart(TimeStamp::Now());
|
|
|
|
|
aChannel->SetDispatchFetchEventStart(TimeStamp::Now());
|
|
|
|
|
|
|
|
|
|
bool newWorkerCreated = false;
|
2019-07-26 21:41:48 +03:00
|
|
|
|
rv = SpawnWorkerIfNeeded(FetchEvent, &newWorkerCreated, aLoadGroup);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2017-04-10 16:13:21 +03:00
|
|
|
|
if (!newWorkerCreated) {
|
|
|
|
|
aChannel->SetLaunchServiceWorkerEnd(TimeStamp::Now());
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> handle(
|
2017-06-14 04:27:17 +03:00
|
|
|
|
new nsMainThreadPtrHolder<nsIInterceptedChannel>("nsIInterceptedChannel",
|
|
|
|
|
aChannel, false));
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
|
2017-06-14 04:27:17 +03:00
|
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(
|
2017-10-27 20:45:52 +03:00
|
|
|
|
"ServiceWorkerRegistrationInfoProxy", registration, false));
|
2015-10-26 05:59:48 +03:00
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<FetchEventRunnable> r = new FetchEventRunnable(
|
2016-07-28 03:36:10 +03:00
|
|
|
|
mWorkerPrivate, token, handle, mInfo->ScriptSpec(), regInfo, aClientId,
|
2020-01-25 03:05:12 +03:00
|
|
|
|
aResultingClientId, newWorkerCreated, isNonSubresourceRequest);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
rv = r->Init();
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-24 18:47:59 +03:00
|
|
|
|
if (mInfo->State() == ServiceWorkerState::Activating) {
|
|
|
|
|
mPendingFunctionalEvents.AppendElement(r.forget());
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
|
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
|
2017-04-10 16:13:21 +03:00
|
|
|
|
bool* aNewWorkerCreated,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsILoadGroup* aLoadGroup) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(!mInner);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-04-10 16:13:21 +03:00
|
|
|
|
// Defaults to no new worker created, but if there is one, we'll set the value
|
|
|
|
|
// to true at the end of this function.
|
|
|
|
|
if (aNewWorkerCreated) {
|
|
|
|
|
*aNewWorkerCreated = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 05:53:21 +03:00
|
|
|
|
// If the worker started shutting down on itself we may have a stale
|
|
|
|
|
// reference here. Invoke our termination code to clean it out.
|
|
|
|
|
if (mWorkerPrivate && mWorkerPrivate->ParentStatusProtected() > Running) {
|
|
|
|
|
TerminateWorker();
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (mWorkerPrivate) {
|
2018-01-24 20:47:16 +03:00
|
|
|
|
// If we have a load group here then use it to update the service worker
|
|
|
|
|
// load group. This was added when we needed the load group's tab child
|
|
|
|
|
// to pass some security checks. Those security checks are gone, though,
|
|
|
|
|
// and we could possibly remove this now. For now we just do it
|
|
|
|
|
// opportunistically. When the service worker is running in a separate
|
|
|
|
|
// process from the client that initiated the intercepted channel, then
|
|
|
|
|
// the load group will be nullptr. UpdateOverrideLoadGroup ignores nullptr
|
|
|
|
|
// load groups.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mWorkerPrivate->UpdateOverridenLoadGroup(aLoadGroup);
|
2015-11-26 14:18:56 +03:00
|
|
|
|
RenewKeepAliveToken(aWhy);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-24 15:16:23 +03:00
|
|
|
|
// Sanity check: mSupportsArray should be empty if we're about to
|
|
|
|
|
// spin up a new worker.
|
|
|
|
|
MOZ_ASSERT(mSupportsArray.IsEmpty());
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (NS_WARN_IF(!mInfo)) {
|
|
|
|
|
NS_WARNING("Trying to wake up a dead service worker.");
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 22:23:15 +03:00
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
|
NS_ENSURE_TRUE(swm, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> reg =
|
|
|
|
|
swm->GetRegistration(mInfo->Principal(), mInfo->Scope());
|
|
|
|
|
NS_ENSURE_TRUE(reg, NS_ERROR_FAILURE);
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
// TODO(catalinb): Bug 1192138 - Add telemetry for service worker wake-ups.
|
|
|
|
|
|
|
|
|
|
// Ensure that the IndexedDatabaseManager is initialized
|
2016-09-02 10:12:24 +03:00
|
|
|
|
Unused << NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
WorkerLoadInfo info;
|
2019-07-15 16:39:51 +03:00
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(info.mBaseURI), mInfo->ScriptSpec());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.mResolvedScriptURI = info.mBaseURI;
|
|
|
|
|
MOZ_ASSERT(!mInfo->CacheName().IsEmpty());
|
|
|
|
|
info.mServiceWorkerCacheName = mInfo->CacheName();
|
2017-10-30 20:35:07 +03:00
|
|
|
|
|
2018-01-31 19:29:49 +03:00
|
|
|
|
info.mServiceWorkerDescriptor.emplace(mInfo->Descriptor());
|
2018-02-14 22:23:15 +03:00
|
|
|
|
info.mServiceWorkerRegistrationDescriptor.emplace(reg->Descriptor());
|
2017-10-30 20:35:07 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
info.mLoadGroup = aLoadGroup;
|
|
|
|
|
|
2017-01-04 12:09:00 +03:00
|
|
|
|
// If we are loading a script for a ServiceWorker then we must not
|
|
|
|
|
// try to intercept it. If the interception matches the current
|
|
|
|
|
// ServiceWorker's scope then we could deadlock the load.
|
2017-08-16 09:18:52 +03:00
|
|
|
|
info.mLoadFlags =
|
2017-01-04 12:09:00 +03:00
|
|
|
|
mInfo->GetImportsLoadFlags() | nsIChannel::LOAD_BYPASS_SERVICE_WORKER;
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
rv = info.mBaseURI->GetHost(info.mDomain);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-06 23:19:56 +03:00
|
|
|
|
info.mPrincipal = mInfo->Principal();
|
2018-01-06 03:50:09 +03:00
|
|
|
|
info.mLoadingPrincipal = info.mPrincipal;
|
2019-04-12 08:30:36 +03:00
|
|
|
|
// StoragePrincipal for ServiceWorkers is equal to mPrincipal because, at the
|
|
|
|
|
// moment, ServiceWorkers are not exposed in partitioned contexts.
|
|
|
|
|
info.mStoragePrincipal = info.mPrincipal;
|
|
|
|
|
|
2020-03-04 11:59:08 +03:00
|
|
|
|
info.mCookieJarSettings = mozilla::net::CookieJarSettings::Create();
|
|
|
|
|
MOZ_ASSERT(info.mCookieJarSettings);
|
2019-03-08 12:04:11 +03:00
|
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
|
info.mStorageAccess =
|
2020-03-04 11:59:08 +03:00
|
|
|
|
StorageAllowedForServiceWorker(info.mPrincipal, info.mCookieJarSettings);
|
2019-04-30 17:27:43 +03:00
|
|
|
|
|
2016-09-20 04:13:00 +03:00
|
|
|
|
info.mOriginAttributes = mInfo->GetOriginAttributes();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-05-22 02:14:50 +03:00
|
|
|
|
// Verify that we don't have any CSP on pristine client.
|
2017-05-26 19:53:35 +03:00
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
2019-05-22 02:14:50 +03:00
|
|
|
|
if (info.mChannel) {
|
|
|
|
|
nsCOMPtr<nsILoadInfo> loadinfo = info.mChannel->LoadInfo();
|
|
|
|
|
csp = loadinfo->GetCsp();
|
|
|
|
|
}
|
2017-02-14 18:06:38 +03:00
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!csp);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Default CSP permissions for now. These will be overrided if necessary
|
|
|
|
|
// based on the script CSP headers during load in ScriptLoader.
|
|
|
|
|
info.mEvalAllowed = true;
|
|
|
|
|
info.mReportCSPViolations = false;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
|
WorkerPrivate::OverrideLoadInfoLoadGroup(info, info.mPrincipal);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-05-22 02:14:50 +03:00
|
|
|
|
rv = info.SetPrincipalsAndCSPOnMainThread(
|
|
|
|
|
info.mPrincipal, info.mStoragePrincipal, info.mLoadGroup, nullptr);
|
2017-02-14 18:06:39 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 12:57:23 +03:00
|
|
|
|
info.mAgentClusterId = reg->AgentClusterId();
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
ErrorResult error;
|
|
|
|
|
NS_ConvertUTF8toUTF16 scriptSpec(mInfo->ScriptSpec());
|
|
|
|
|
|
|
|
|
|
mWorkerPrivate = WorkerPrivate::Constructor(jsapi.cx(), scriptSpec, false,
|
|
|
|
|
WorkerTypeService, VoidString(),
|
2017-12-12 23:44:48 +03:00
|
|
|
|
EmptyCString(), &info, error);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
|
return error.StealNSResult();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
RenewKeepAliveToken(aWhy);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-04-10 16:13:21 +03:00
|
|
|
|
if (aNewWorkerCreated) {
|
|
|
|
|
*aNewWorkerCreated = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return NS_OK;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 14:45:00 +03:00
|
|
|
|
bool ServiceWorkerPrivate::MaybeStoreISupports(nsISupports* aSupports) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-24 15:16:23 +03:00
|
|
|
|
|
2018-03-22 14:45:00 +03:00
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSupportsArray.IsEmpty());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mSupportsArray.Contains(aSupports));
|
2015-10-24 15:16:23 +03:00
|
|
|
|
mSupportsArray.AppendElement(aSupports);
|
2018-03-22 14:45:00 +03:00
|
|
|
|
return true;
|
2015-10-24 15:16:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::RemoveISupports(nsISupports* aSupports) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-24 15:16:23 +03:00
|
|
|
|
mSupportsArray.RemoveElement(aSupports);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
void ServiceWorkerPrivate::TerminateWorker() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mIdleWorkerTimer->Cancel();
|
2016-07-28 03:36:10 +03:00
|
|
|
|
mIdleKeepAliveToken = nullptr;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (mWorkerPrivate) {
|
2018-09-10 21:36:16 +03:00
|
|
|
|
if (StaticPrefs::dom_serviceWorkers_testing_enabled()) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
|
if (os) {
|
2017-01-31 17:21:52 +03:00
|
|
|
|
os->NotifyObservers(nullptr, "service-worker-shutdown", nullptr);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
|
Unused << NS_WARN_IF(!mWorkerPrivate->Cancel());
|
2020-02-13 17:38:48 +03:00
|
|
|
|
RefPtr<WorkerPrivate> workerPrivate = std::move(mWorkerPrivate);
|
|
|
|
|
mozilla::Unused << workerPrivate;
|
2015-10-24 15:16:23 +03:00
|
|
|
|
mSupportsArray.Clear();
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
|
|
|
|
// Any pending events are never going to fire on this worker. Cancel
|
|
|
|
|
// them so that intercepted channels can be reset and other resources
|
|
|
|
|
// cleaned up.
|
|
|
|
|
nsTArray<RefPtr<WorkerRunnable>> pendingEvents;
|
|
|
|
|
mPendingFunctionalEvents.SwapElements(pendingEvents);
|
|
|
|
|
for (uint32_t i = 0; i < pendingEvents.Length(); ++i) {
|
|
|
|
|
pendingEvents[i]->Cancel();
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::NoteDeadServiceWorkerInfo() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-08-15 20:27:37 +03:00
|
|
|
|
|
|
|
|
|
if (mInner) {
|
|
|
|
|
mInner->NoteDeadOuter();
|
|
|
|
|
mInner = nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mInfo = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 20:35:07 +03:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class UpdateStateControlRunnable final
|
|
|
|
|
: public MainThreadWorkerControlRunnable {
|
|
|
|
|
const ServiceWorkerState mState;
|
|
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
|
|
|
|
|
aWorkerPrivate->UpdateServiceWorkerState(mState);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
UpdateStateControlRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
ServiceWorkerState aState)
|
|
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate), mState(aState) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::UpdateState(ServiceWorkerState aState) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
return mInner->UpdateState(aState);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 20:35:07 +03:00
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingFunctionalEvents.IsEmpty());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefPtr<WorkerRunnable> r =
|
|
|
|
|
new UpdateStateControlRunnable(mWorkerPrivate, aState);
|
|
|
|
|
Unused << r->Dispatch();
|
|
|
|
|
|
|
|
|
|
if (aState != ServiceWorkerState::Activated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-24 18:47:59 +03:00
|
|
|
|
|
|
|
|
|
nsTArray<RefPtr<WorkerRunnable>> pendingEvents;
|
|
|
|
|
mPendingFunctionalEvents.SwapElements(pendingEvents);
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pendingEvents.Length(); ++i) {
|
2020-02-13 17:38:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r = std::move(pendingEvents[i]);
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!r->Dispatch())) {
|
2015-11-24 18:47:59 +03:00
|
|
|
|
NS_WARNING("Failed to dispatch pending functional event!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
nsresult ServiceWorkerPrivate::GetDebugger(nsIWorkerDebugger** aResult) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-26 14:18:56 +03:00
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mInner) {
|
|
|
|
|
*aResult = nullptr;
|
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
if (!mDebuggerCount) {
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
2018-10-02 00:38:01 +03:00
|
|
|
|
nsCOMPtr<nsIWorkerDebugger> debugger = mWorkerPrivate->Debugger();
|
2015-11-26 14:18:56 +03:00
|
|
|
|
debugger.forget(aResult);
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::AttachDebugger() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-26 14:18:56 +03:00
|
|
|
|
|
|
|
|
|
// When the first debugger attaches to a worker, we spawn a worker if needed,
|
|
|
|
|
// and cancel the idle timeout. The idle timeout should not be reset until
|
|
|
|
|
// the last debugger detached from the worker.
|
|
|
|
|
if (!mDebuggerCount) {
|
2019-08-20 18:21:11 +03:00
|
|
|
|
nsresult rv = mInner ? mInner->SpawnWorkerIfNeeded()
|
|
|
|
|
: SpawnWorkerIfNeeded(AttachEvent);
|
2015-11-26 14:18:56 +03:00
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
2019-08-20 18:21:11 +03:00
|
|
|
|
/**
|
|
|
|
|
* Under parent-intercept mode (i.e. non-null `mInner`), renewing the idle
|
|
|
|
|
* KeepAliveToken for spawning workers happens asynchronously, rather than
|
|
|
|
|
* synchronously without parent-intercept (see
|
|
|
|
|
* `ServiceWorkerPrivate::SpawnWorkerIfNeeded`). The asynchronous renewal is
|
|
|
|
|
* because the actual spawning of workers under parent-intercept occurs in a
|
|
|
|
|
* content process, so we will only renew once notified that the worker has
|
|
|
|
|
* been successfully created
|
|
|
|
|
* (see `ServiceWorkerPrivateImpl::CreationSucceeded`).
|
|
|
|
|
*
|
|
|
|
|
* This means that the DevTools way of starting up a worker by calling
|
|
|
|
|
* `AttachDebugger` immediately followed by `DetachDebugger` will spawn and
|
|
|
|
|
* immediately terminate a worker (because `mTokenCount` is possibly 0
|
|
|
|
|
* due to the idle KeepAliveToken being created asynchronously). So, just
|
|
|
|
|
* renew the KeepAliveToken right now.
|
|
|
|
|
*/
|
|
|
|
|
if (mInner) {
|
|
|
|
|
RenewKeepAliveToken(AttachEvent);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
mIdleWorkerTimer->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++mDebuggerCount;
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult ServiceWorkerPrivate::DetachDebugger() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-26 14:18:56 +03:00
|
|
|
|
|
|
|
|
|
if (!mDebuggerCount) {
|
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
--mDebuggerCount;
|
|
|
|
|
|
|
|
|
|
// When the last debugger detaches from a worker, we either reset the idle
|
|
|
|
|
// timeout, or terminate the worker if there are no more active tokens.
|
|
|
|
|
if (!mDebuggerCount) {
|
|
|
|
|
if (mTokenCount) {
|
|
|
|
|
ResetIdleTimeout();
|
|
|
|
|
} else {
|
|
|
|
|
TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
bool ServiceWorkerPrivate::IsIdle() const {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-07-28 03:36:10 +03:00
|
|
|
|
return mTokenCount == 0 || (mTokenCount == 1 && mIdleKeepAliveToken);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 21:41:48 +03:00
|
|
|
|
RefPtr<GenericPromise> ServiceWorkerPrivate::GetIdlePromise() {
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
MOZ_ASSERT(!IsIdle());
|
|
|
|
|
MOZ_ASSERT(!mIdlePromiseObtained, "Idle promise may only be obtained once!");
|
|
|
|
|
mIdlePromiseObtained = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return mIdlePromiseHolder.Ensure(__func__);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class ServiceWorkerPrivateTimerCallback final : public nsITimerCallback,
|
2017-07-26 21:18:20 +03:00
|
|
|
|
public nsINamed {
|
2017-02-14 16:49:44 +03:00
|
|
|
|
public:
|
|
|
|
|
typedef void (ServiceWorkerPrivate::*Method)(nsITimer*);
|
|
|
|
|
|
|
|
|
|
ServiceWorkerPrivateTimerCallback(ServiceWorkerPrivate* aServiceWorkerPrivate,
|
|
|
|
|
Method aMethod)
|
|
|
|
|
: mServiceWorkerPrivate(aServiceWorkerPrivate), mMethod(aMethod) {}
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
|
Notify(nsITimer* aTimer) override {
|
|
|
|
|
(mServiceWorkerPrivate->*mMethod)(aTimer);
|
|
|
|
|
mServiceWorkerPrivate = nullptr;
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
|
NS_IMETHOD
|
|
|
|
|
GetName(nsACString& aName) override {
|
|
|
|
|
aName.AssignLiteral("ServiceWorkerPrivateTimerCallback");
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
private:
|
|
|
|
|
~ServiceWorkerPrivateTimerCallback() = default;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
|
|
|
|
|
Method mMethod;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
|
NS_IMPL_ISUPPORTS(ServiceWorkerPrivateTimerCallback, nsITimerCallback,
|
|
|
|
|
nsINamed);
|
2017-02-14 16:49:44 +03:00
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::NoteIdleWorkerCallback(nsITimer* aTimer) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-02-14 16:49:44 +03:00
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(aTimer == mIdleWorkerTimer, "Invalid timer!");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
// Release ServiceWorkerPrivate's token, since the grace period has ended.
|
2017-02-14 16:49:44 +03:00
|
|
|
|
mIdleKeepAliveToken = nullptr;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2019-08-15 20:27:37 +03:00
|
|
|
|
if (mWorkerPrivate || (mInner && !mInner->WorkerIsDead())) {
|
|
|
|
|
// There sould only be EITHER mWorkerPrivate or mInner (but not both).
|
|
|
|
|
MOZ_ASSERT(!(mWorkerPrivate && mInner));
|
|
|
|
|
|
2020-01-09 03:30:27 +03:00
|
|
|
|
// If we still have a living worker at this point it means that either there
|
|
|
|
|
// are pending waitUntil promises or the worker is doing some long-running
|
|
|
|
|
// computation. Wait a bit more until we forcibly terminate the worker.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
uint32_t timeout =
|
|
|
|
|
Preferences::GetInt("dom.serviceWorkers.idle_extended_timeout");
|
2017-02-14 16:49:44 +03:00
|
|
|
|
nsCOMPtr<nsITimerCallback> cb = new ServiceWorkerPrivateTimerCallback(
|
|
|
|
|
this, &ServiceWorkerPrivate::TerminateWorkerCallback);
|
|
|
|
|
DebugOnly<nsresult> rv = mIdleWorkerTimer->InitWithCallback(
|
|
|
|
|
cb, timeout, nsITimer::TYPE_ONE_SHOT);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
void ServiceWorkerPrivate::TerminateWorkerCallback(nsITimer* aTimer) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
MOZ_ASSERT(aTimer == this->mIdleWorkerTimer, "Invalid timer!");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-07-06 23:34:19 +03:00
|
|
|
|
// mInfo must be non-null at this point because NoteDeadServiceWorkerInfo
|
|
|
|
|
// which zeroes it calls TerminateWorker which cancels our timer which will
|
|
|
|
|
// ensure we don't get invoked even if the nsTimerEvent is in the event queue.
|
|
|
|
|
ServiceWorkerManager::LocalizeAndReportToAllClients(
|
|
|
|
|
mInfo->Scope(), "ServiceWorkerGraceTimeoutTermination",
|
2017-02-14 16:49:44 +03:00
|
|
|
|
nsTArray<nsString>{NS_ConvertUTF8toUTF16(mInfo->Scope())});
|
2016-07-06 23:34:19 +03:00
|
|
|
|
|
2017-02-14 16:49:44 +03:00
|
|
|
|
TerminateWorker();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
void ServiceWorkerPrivate::RenewKeepAliveToken(WakeUpReason aWhy) {
|
|
|
|
|
// We should have an active worker if we're renewing the keep alive token.
|
2019-08-15 20:27:37 +03:00
|
|
|
|
MOZ_ASSERT(mWorkerPrivate || (mInner && !mInner->WorkerIsDead()));
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-26 14:18:56 +03:00
|
|
|
|
// If there is at least one debugger attached to the worker, the idle worker
|
|
|
|
|
// timeout was canceled when the first debugger attached to the worker. It
|
|
|
|
|
// should not be reset until the last debugger detaches from the worker.
|
|
|
|
|
if (!mDebuggerCount) {
|
|
|
|
|
ResetIdleTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
if (!mIdleKeepAliveToken) {
|
|
|
|
|
mIdleKeepAliveToken = new KeepAliveToken(this);
|
2015-11-26 14:18:56 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::ResetIdleTimeout() {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
uint32_t timeout = Preferences::GetInt("dom.serviceWorkers.idle_timeout");
|
2017-02-14 16:49:44 +03:00
|
|
|
|
nsCOMPtr<nsITimerCallback> cb = new ServiceWorkerPrivateTimerCallback(
|
|
|
|
|
this, &ServiceWorkerPrivate::NoteIdleWorkerCallback);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
DebugOnly<nsresult> rv =
|
2017-02-14 16:49:44 +03:00
|
|
|
|
mIdleWorkerTimer->InitWithCallback(cb, timeout, nsITimer::TYPE_ONE_SHOT);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::AddToken() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
++mTokenCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServiceWorkerPrivate::ReleaseToken() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mTokenCount > 0);
|
|
|
|
|
--mTokenCount;
|
2017-02-03 22:36:27 +03:00
|
|
|
|
|
2019-07-26 21:41:48 +03:00
|
|
|
|
if (IsIdle()) {
|
|
|
|
|
mIdlePromiseHolder.ResolveIfExists(true, __func__);
|
|
|
|
|
|
|
|
|
|
if (!mTokenCount) {
|
|
|
|
|
TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mInfo can be nullptr here if NoteDeadServiceWorkerInfo() is called while
|
|
|
|
|
// the KeepAliveToken is being proxy released as a runnable.
|
|
|
|
|
else if (mInfo) {
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
|
if (swm) {
|
|
|
|
|
swm->WorkerIsIdle(mInfo);
|
|
|
|
|
}
|
2016-07-28 03:36:10 +03:00
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
already_AddRefed<KeepAliveToken>
|
|
|
|
|
ServiceWorkerPrivate::CreateEventKeepAliveToken() {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-08-15 20:27:37 +03:00
|
|
|
|
|
|
|
|
|
// When the WorkerPrivate is in a separate process, we first hold a normal
|
|
|
|
|
// KeepAliveToken. Then, after we're notified that the worker is alive, we
|
|
|
|
|
// create the idle KeepAliveToken.
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate || (mInner && !mInner->WorkerIsDead()));
|
|
|
|
|
MOZ_ASSERT(mIdleKeepAliveToken || (mInner && !mInner->WorkerIsDead()));
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> ref = new KeepAliveToken(this);
|
|
|
|
|
return ref.forget();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
void ServiceWorkerPrivate::SetHandlesFetch(bool aValue) {
|
2018-01-27 00:08:59 +03:00
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-12-19 05:38:53 +03:00
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mInfo)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mInfo->SetHandlesFetch(aValue);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 00:08:59 +03:00
|
|
|
|
} // namespace dom
|
|
|
|
|
} // namespace mozilla
|