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
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
#include "ServiceWorkerManager.h"
|
2016-11-21 05:14:53 +03:00
|
|
|
|
#include "ServiceWorkerWindowClient.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
|
#include "nsIHttpHeaderVisitor.h"
|
|
|
|
|
#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 "WorkerRunnable.h"
|
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
#include "mozilla/Assertions.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"
|
2016-08-23 07:09:32 +03:00
|
|
|
|
#include "mozilla/Unused.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
|
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
2015-10-24 15:16:23 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ServiceWorkerPrivate)
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ServiceWorkerPrivate)
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(ServiceWorkerPrivate, mSupportsArray)
|
|
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServiceWorkerPrivate)
|
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2016-10-31 22:10:25 +03:00
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
2015-10-24 15:16:23 +03:00
|
|
|
|
NS_INTERFACE_MAP_END
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
// Tracks the "dom.disable_open_click_delay" preference. Modified on main
|
|
|
|
|
// thread, read on worker threads.
|
|
|
|
|
// 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);
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
// Used to keep track of pending waitUntil as well as in-flight extendable events.
|
|
|
|
|
// When the last token is released, we attempt to terminate the worker.
|
|
|
|
|
class KeepAliveToken final : public nsISupports
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
|
|
explicit KeepAliveToken(ServiceWorkerPrivate* aPrivate)
|
|
|
|
|
: mPrivate(aPrivate)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aPrivate);
|
|
|
|
|
mPrivate->AddToken();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~KeepAliveToken()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
mPrivate->ReleaseToken();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ServiceWorkerPrivate> mPrivate;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(KeepAliveToken)
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo)
|
|
|
|
|
: mInfo(aInfo)
|
2015-11-26 14:18:56 +03:00
|
|
|
|
, mDebuggerCount(0)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
, mTokenCount(0)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aInfo);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
mIdleWorkerTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
|
MOZ_ASSERT(mIdleWorkerTimer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServiceWorkerPrivate::~ServiceWorkerPrivate()
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(!mTokenCount);
|
|
|
|
|
MOZ_ASSERT(!mInfo);
|
2015-10-24 15:16:23 +03:00
|
|
|
|
MOZ_ASSERT(mSupportsArray.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 {
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
class CheckScriptEvaluationWithCallback final : public WorkerRunnable
|
|
|
|
|
{
|
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)
|
2016-06-28 20:28:13 +03:00
|
|
|
|
: WorkerRunnable(aWorkerPrivate)
|
2016-12-19 05:38:53 +03:00
|
|
|
|
, mServiceWorkerPrivate(new nsMainThreadPtrHolder<ServiceWorkerPrivate>(aServiceWorkerPrivate))
|
2015-10-01 02:11:03 +03:00
|
|
|
|
, mKeepAliveToken(new nsMainThreadPtrHolder<KeepAliveToken>(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
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<bool>(this,
|
|
|
|
|
&CheckScriptEvaluationWithCallback::ReportFetchFlag, fetchHandlerWasAdded);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
mServiceWorkerPrivate->SetHandlesFetch(aFetchHandlerWasAdded);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
2015-11-26 20:03:10 +03:00
|
|
|
|
Cancel() override
|
|
|
|
|
{
|
2016-12-19 05:38:53 +03:00
|
|
|
|
ReportScriptEvaluationResult(false);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void
|
2016-12-19 05:38:53 +03:00
|
|
|
|
ReportScriptEvaluationResult(bool aScriptEvaluationResult)
|
2015-11-26 20:03:10 +03:00
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
|
nsresult
|
2016-12-19 05:38:53 +03:00
|
|
|
|
ServiceWorkerPrivate::CheckScriptEvaluation(LifeCycleEventCallback* aScriptEvaluationCallback)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent, nullptr);
|
|
|
|
|
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(mWorkerPrivate,
|
2016-12-19 05:38:53 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
enum ExtendableEventResult {
|
|
|
|
|
Rejected = 0,
|
|
|
|
|
Resolved
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ExtendableEventCallback {
|
|
|
|
|
public:
|
|
|
|
|
virtual void
|
|
|
|
|
FinishedWithResult(ExtendableEventResult aResult) = 0;
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
|
|
|
|
AddRef() = 0;
|
|
|
|
|
|
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
|
|
|
|
Release() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class KeepAliveHandler final : public WorkerHolder
|
|
|
|
|
, public ExtendableEvent::ExtensionsHandler
|
|
|
|
|
, public PromiseNativeHandler
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2016-11-21 05:14:53 +03:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<KeepAliveToken> mKeepAliveToken;
|
|
|
|
|
WorkerPrivate* MOZ_NON_OWNING_REF mWorkerPrivate;
|
|
|
|
|
bool mWorkerHolderAdded;
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
: mKeepAliveToken(aKeepAliveToken)
|
|
|
|
|
, mWorkerPrivate(GetCurrentThreadWorkerPrivate())
|
|
|
|
|
, mWorkerHolderAdded(false)
|
|
|
|
|
, mCallback(aCallback)
|
|
|
|
|
, mPendingPromisesCount(0)
|
|
|
|
|
, mRejected(false)
|
2016-05-25 00:08:20 +03:00
|
|
|
|
{
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MOZ_ASSERT(mKeepAliveToken);
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
bool
|
|
|
|
|
UseWorkerHolder()
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_ASSERT(!mWorkerHolderAdded);
|
|
|
|
|
mWorkerHolderAdded = HoldWorker(mWorkerPrivate, Terminating);
|
|
|
|
|
return mWorkerHolderAdded;
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
bool
|
|
|
|
|
WaitOnPromise(Promise& aPromise) override
|
|
|
|
|
{
|
|
|
|
|
if (!mKeepAliveToken) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Notify(Status aStatus) override
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
if (aStatus < Terminating) {
|
2016-05-25 00:08:20 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MaybeCleanup();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MaybeDone()
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
|
|
if (mPendingPromisesCount) {
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
if (!mKeepAliveToken) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mWorkerHolderAdded) {
|
|
|
|
|
ReleaseWorker();
|
2016-05-25 00:08:20 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
mKeepAliveToken = nullptr;
|
|
|
|
|
mSelfRef = nullptr;
|
|
|
|
|
}
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
void
|
|
|
|
|
RemovePromise(ExtendableEventResult aResult)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2016-11-21 05:14:53 +03:00
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingPromisesCount > 0);
|
|
|
|
|
MOZ_ASSERT(mSelfRef);
|
|
|
|
|
MOZ_ASSERT(mKeepAliveToken);
|
|
|
|
|
|
|
|
|
|
mRejected |= (aResult == Rejected);
|
2016-05-25 00:08:20 +03:00
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
--mPendingPromisesCount;
|
|
|
|
|
if (mPendingPromisesCount) {
|
2016-05-25 00:08:20 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
CycleCollectedJSContext* cx = CycleCollectedJSContext::Get();
|
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
|
|
|
|
|
|
RefPtr<nsIRunnable> r = NewRunnableMethod(this, &KeepAliveHandler::MaybeDone);
|
|
|
|
|
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:
|
2015-12-11 22:53:11 +03:00
|
|
|
|
RegistrationUpdateRunnable(nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
|
|
|
|
|
bool aNeedTimeCheck)
|
2015-10-26 05:59:48 +03:00
|
|
|
|
: mRegistration(aRegistration)
|
2015-12-11 22:53:11 +03:00
|
|
|
|
, mNeedTimeCheck(aNeedTimeCheck)
|
2015-10-26 05:59:48 +03:00
|
|
|
|
{
|
2015-12-11 05:49:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-11 22:53:11 +03:00
|
|
|
|
NS_IMETHOD
|
|
|
|
|
Run() override
|
2015-12-11 05:49:48 +03:00
|
|
|
|
{
|
2015-12-11 22:53:11 +03:00
|
|
|
|
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)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(aKeepAliveToken);
|
|
|
|
|
|
|
|
|
|
mKeepAliveToken =
|
|
|
|
|
new nsMainThreadPtrHolder<KeepAliveToken>(aKeepAliveToken);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
nsresult
|
2015-10-01 02:11:03 +03:00
|
|
|
|
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);
|
|
|
|
|
if (NS_WARN_IF(!keepAliveHandler->UseWorkerHolder())) {
|
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;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
result = aWorkerScope->DispatchDOMEvent(nullptr, aEvent, nullptr, nullptr);
|
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
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
class SendMesssageEventRunnable final : public ExtendableEventWorkerRunnable
|
|
|
|
|
, public StructuredCloneHolder
|
|
|
|
|
{
|
|
|
|
|
UniquePtr<ServiceWorkerClientInfo> mEventSource;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SendMesssageEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
KeepAliveToken* aKeepAliveToken,
|
|
|
|
|
UniquePtr<ServiceWorkerClientInfo>&& aEventSource)
|
|
|
|
|
: ExtendableEventWorkerRunnable(aWorkerPrivate, aKeepAliveToken)
|
|
|
|
|
, StructuredCloneHolder(CloningSupported, TransferringSupported,
|
|
|
|
|
StructuredCloneScope::SameProcessDifferentThread)
|
|
|
|
|
, mEventSource(Move(aEventSource))
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mEventSource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
|
{
|
|
|
|
|
JS::Rooted<JS::Value> messageData(aCx);
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> sgo = aWorkerPrivate->GlobalScope();
|
|
|
|
|
ErrorResult rv;
|
|
|
|
|
Read(sgo, aCx, &messageData, rv);
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sequence<OwningNonNull<MessagePort>> ports;
|
|
|
|
|
if (!TakeTransferredPortsAsSequence(ports)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerClient> client = new ServiceWorkerWindowClient(sgo,
|
|
|
|
|
*mEventSource);
|
|
|
|
|
RootedDictionary<ExtendableMessageEventInit> init(aCx);
|
|
|
|
|
|
|
|
|
|
init.mBubbles = false;
|
|
|
|
|
init.mCancelable = false;
|
|
|
|
|
|
|
|
|
|
init.mData = messageData;
|
|
|
|
|
init.mPorts = ports;
|
|
|
|
|
init.mSource.SetValue().SetAsClient() = client;
|
|
|
|
|
|
|
|
|
|
RefPtr<EventTarget> target = aWorkerPrivate->GlobalScope();
|
|
|
|
|
RefPtr<ExtendableMessageEvent> extendableEvent =
|
|
|
|
|
ExtendableMessageEvent::Constructor(target, NS_LITERAL_STRING("message"),
|
|
|
|
|
init, rv);
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
|
rv.SuppressException();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ServiceWorkerPrivate::SendMessageEvent(JSContext* aCx,
|
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
|
UniquePtr<ServiceWorkerClientInfo>&& aClientInfo)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
ErrorResult rv(SpawnWorkerIfNeeded(MessageEvent, nullptr));
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
|
return rv.StealNSResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedHandleValue);
|
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
|
const Sequence<JS::Value>& value = aTransferable.Value();
|
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(value.Length(), value.Elements());
|
|
|
|
|
|
|
|
|
|
JSObject* array = JS_NewArrayObject(aCx, elements);
|
|
|
|
|
if (!array) {
|
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
transferable.setObject(*array);
|
|
|
|
|
}
|
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
|
|
|
|
RefPtr<SendMesssageEventRunnable> runnable =
|
|
|
|
|
new SendMesssageEventRunnable(mWorkerPrivate, token, Move(aClientInfo));
|
|
|
|
|
|
|
|
|
|
runnable->Write(aCx, aMessage, transferable, JS::CloneDataPolicy(), rv);
|
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
|
return rv.StealNSResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
|
{
|
2015-12-11 22:53:11 +03:00
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
|
new RegistrationUpdateRunnable(mRegistration, true /* time check */);
|
2016-09-14 06:14:02 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
return DispatchLifecycleEvent(aCx, aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
2015-11-26 20:03:10 +03:00
|
|
|
|
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
|
|
|
|
*/
|
2016-11-21 05:14:53 +03:00
|
|
|
|
class LifeCycleEventWatcher final : public ExtendableEventCallback,
|
2016-06-23 11:53:14 +03:00
|
|
|
|
public WorkerHolder
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2015-11-26 20:03:10 +03:00
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<LifeCycleEventCallback> mCallback;
|
2015-11-26 20:03:10 +03:00
|
|
|
|
bool mDone;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
~LifeCycleEventWatcher()
|
|
|
|
|
{
|
|
|
|
|
if (mDone) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(GetCurrentThreadWorkerPrivate() == mWorkerPrivate);
|
|
|
|
|
// 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
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
LifeCycleEventWatcher(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
LifeCycleEventCallback* aCallback)
|
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
|
, mCallback(aCallback)
|
|
|
|
|
, mDone(false)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2015-11-26 20:03:10 +03:00
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Init()
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
|
|
// 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.
|
2016-08-18 17:11:04 +03:00
|
|
|
|
if (NS_WARN_IF(!HoldWorker(mWorkerPrivate, Terminating))) {
|
2015-11-26 20:03:10 +03:00
|
|
|
|
NS_WARNING("LifeCycleEventWatcher failed to add feature.");
|
2016-02-29 22:52:42 +03:00
|
|
|
|
ReportResult(false);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2016-03-28 20:28:14 +03:00
|
|
|
|
Notify(Status aStatus) override
|
2015-11-26 20:03:10 +03:00
|
|
|
|
{
|
|
|
|
|
if (aStatus < Terminating) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(GetCurrentThreadWorkerPrivate() == mWorkerPrivate);
|
2016-02-29 22:52:42 +03:00
|
|
|
|
ReportResult(false);
|
2015-11-26 20:03:10 +03:00
|
|
|
|
|
|
|
|
|
return true;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-02-29 22:52:42 +03:00
|
|
|
|
ReportResult(bool aResult)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2015-11-26 20:03:10 +03:00
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
|
|
if (mDone) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mDone = true;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-11-26 20:03:10 +03:00
|
|
|
|
mCallback->SetResult(aResult);
|
2016-09-14 06:14:02 +03:00
|
|
|
|
nsresult rv = mWorkerPrivate->DispatchToMainThread(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
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
|
ReleaseWorker();
|
2015-11-26 20:03:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-11-21 05:14:53 +03:00
|
|
|
|
FinishedWithResult(ExtendableEventResult aResult) override
|
2015-11-26 20:03:10 +03:00
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(GetCurrentThreadWorkerPrivate() == mWorkerPrivate);
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
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.
|
|
|
|
|
RefPtr<LifeCycleEventWatcher> watcher =
|
|
|
|
|
new LifeCycleEventWatcher(aWorkerPrivate, mCallback);
|
|
|
|
|
|
|
|
|
|
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(const nsAString& aEventType,
|
|
|
|
|
LifeCycleEventCallback* aCallback,
|
|
|
|
|
nsIRunnable* aLoadFailure)
|
|
|
|
|
{
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(LifeCycleEvent, aLoadFailure);
|
|
|
|
|
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,
|
2016-07-28 03:36:10 +03:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
~PushErrorReporter()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2016-03-28 21:50:39 +03:00
|
|
|
|
{
|
2016-11-21 05:14:53 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2016-05-05 11:45:00 +03:00
|
|
|
|
NewRunnableMethod<uint16_t>(this,
|
2016-03-28 21:50:39 +03:00
|
|
|
|
&PushErrorReporter::ReportOnMainThread, aReason);
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
2016-09-14 06:14:02 +03:00
|
|
|
|
workerPrivate->DispatchToMainThread(runnable.forget())));
|
2016-03-28 21:50:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReportOnMainThread(uint16_t aReason)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
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(WorkerPrivate* aWorkerPrivate,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
KeepAliveToken* aKeepAliveToken,
|
2016-03-28 21:50:39 +03:00
|
|
|
|
const nsAString& aMessageId,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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 =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
PushEvent::Constructor(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
|
|
|
|
{
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
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)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
nsresult
|
2016-03-28 21:50:39 +03:00
|
|
|
|
ServiceWorkerPrivate::SendPushEvent(const nsAString& aMessageId,
|
|
|
|
|
const Maybe<nsTArray<uint8_t>>& aData,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
ServiceWorkerRegistrationInfo* aRegistration)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(PushEvent, nullptr);
|
|
|
|
|
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(
|
|
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(aRegistration, false));
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r = new SendPushEventRunnable(mWorkerPrivate,
|
2016-07-28 03:36:10 +03:00
|
|
|
|
token,
|
2016-03-28 21:50:39 +03:00
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(PushSubscriptionChangeEvent, nullptr);
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
DummyNotificationTimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
|
{
|
|
|
|
|
// Nothing.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AllowWindowInteractionHandler;
|
|
|
|
|
|
|
|
|
|
class ClearWindowAllowedRunnable final : public WorkerRunnable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ClearWindowAllowedRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
|
AllowWindowInteractionHandler* aHandler)
|
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
|
|
|
|
, mHandler(aHandler)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool
|
2016-02-26 00:05:39 +03:00
|
|
|
|
PreDispatch(WorkerPrivate* aWorkerPrivate) override
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
// WorkerRunnable asserts that the dispatch is from parent thread if
|
|
|
|
|
// the busy count modification is WorkerThreadUnchangedBusyCount.
|
|
|
|
|
// Since this runnable will be dispatched from the timer thread, we override
|
|
|
|
|
// PreDispatch and PostDispatch to skip the check.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-02-26 23:23:12 +03:00
|
|
|
|
PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
// Silence bad assertions.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
2016-04-08 23:12:52 +03:00
|
|
|
|
Cancel() override
|
|
|
|
|
{
|
|
|
|
|
// Always ensure the handler is released on the worker thread, even if we
|
|
|
|
|
// are cancelled.
|
|
|
|
|
mHandler = nullptr;
|
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<AllowWindowInteractionHandler> mHandler;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-21 05:14:53 +03:00
|
|
|
|
class AllowWindowInteractionHandler final : public ExtendableEventCallback
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
friend class ClearWindowAllowedRunnable;
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
|
|
|
|
|
~AllowWindowInteractionHandler()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-02-26 23:23:12 +03:00
|
|
|
|
MOZ_ALWAYS_TRUE(aWorkerPrivate->ModifyBusyCountFromWorker(false));
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
StartClearWindowTimer(WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
MOZ_ASSERT(!mTimer);
|
|
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ClearWindowAllowedRunnable> r =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
new ClearWindowAllowedRunnable(aWorkerPrivate, this);
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<TimerThreadEventTarget> target =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
new TimerThreadEventTarget(aWorkerPrivate, r);
|
|
|
|
|
|
|
|
|
|
rv = timer->SetTarget(target);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The important stuff that *has* to be reversed.
|
2016-02-26 23:23:12 +03:00
|
|
|
|
if (NS_WARN_IF(!aWorkerPrivate->ModifyBusyCountFromWorker(true))) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
rv = mTimer->InitWithFuncCallback(DummyNotificationTimerCallback, nullptr,
|
|
|
|
|
gDOMDisableOpenClickDelay,
|
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
ClearWindowAllowed(aWorkerPrivate);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2016-11-21 05:14:53 +03:00
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(AllowWindowInteractionHandler, override)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
explicit AllowWindowInteractionHandler(WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
StartClearWindowTimer(aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-11-21 05:14:53 +03:00
|
|
|
|
FinishedWithResult(ExtendableEventResult /* aResult */) override
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2016-11-21 05:14:53 +03:00
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
ClearWindowAllowed(workerPrivate);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
ClearWindowAllowedRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
|
{
|
|
|
|
|
mHandler->ClearWindowAllowed(aWorkerPrivate);
|
2016-04-08 23:12:52 +03:00
|
|
|
|
mHandler = nullptr;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
|
class SendNotificationEventRunnable final : public ExtendableEventWorkerRunnable
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2016-04-20 08:04:09 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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 =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
Notification::ConstructFromFields(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 =
|
2016-04-20 08:04:09 +03:00
|
|
|
|
NotificationEvent::Constructor(target, mEventName,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nei, result);
|
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
aWorkerPrivate->GlobalScope()->AllowWindowInteraction();
|
2016-06-06 17:38:40 +03:00
|
|
|
|
RefPtr<AllowWindowInteractionHandler> 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
|
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_XPC_JS_THREW_EXCEPTION) {
|
2016-11-21 05:14:53 +03:00
|
|
|
|
allowWindowInteraction->FinishedWithResult(Rejected);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
2016-06-06 17:38:40 +03:00
|
|
|
|
aWorkerPrivate->GlobalScope()->ConsumeWindowInteraction();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
|
|
|
|
|
nsresult
|
2016-04-20 08:04:09 +03:00
|
|
|
|
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)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2016-04-20 08:04:09 +03:00
|
|
|
|
WakeUpReason why;
|
|
|
|
|
if (aEventName.EqualsLiteral(NOTIFICATION_CLICK_EVENT_NAME)) {
|
|
|
|
|
why = NotificationClickEvent;
|
|
|
|
|
gDOMDisableOpenClickDelay = Preferences::GetInt("dom.disable_open_click_delay");
|
|
|
|
|
} 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
|
|
|
|
|
2016-04-20 08:04:09 +03:00
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(why, nullptr);
|
|
|
|
|
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();
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<WorkerRunnable> r =
|
2016-07-28 03:36:10 +03:00
|
|
|
|
new SendNotificationEventRunnable(mWorkerPrivate, token,
|
2016-04-20 08:04:09 +03:00
|
|
|
|
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;
|
2016-04-27 12:24:04 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
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;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
bool mIsReload;
|
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;
|
|
|
|
|
nsCString mReferrer;
|
2016-02-27 01:36:45 +03:00
|
|
|
|
ReferrerPolicy mReferrerPolicy;
|
2016-09-07 05:20:23 +03:00
|
|
|
|
nsString mIntegrity;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
public:
|
|
|
|
|
FetchEventRunnable(WorkerPrivate* aWorkerPrivate,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
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,
|
2015-11-26 02:05:34 +03:00
|
|
|
|
const nsAString& aDocumentId,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
bool aIsReload)
|
2015-10-26 05:59:48 +03:00
|
|
|
|
: ExtendableFunctionalEventWorkerRunnable(
|
|
|
|
|
aWorkerPrivate, aKeepAliveToken, aRegistration)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
, mInterceptedChannel(aChannel)
|
|
|
|
|
, mScriptSpec(aScriptSpec)
|
2016-04-27 12:24:04 +03:00
|
|
|
|
, mRegistration(aRegistration)
|
2015-11-26 02:05:34 +03:00
|
|
|
|
, mClientId(aDocumentId)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
, mIsReload(aIsReload)
|
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)
|
|
|
|
|
, mReferrer(kFETCH_CLIENT_REFERRER_STR)
|
2016-02-27 01:36:45 +03:00
|
|
|
|
, mReferrerPolicy(ReferrerPolicy::_empty)
|
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()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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;
|
|
|
|
|
rv = uri->CloneIgnoringRef(getter_AddRefs(uriNoFragment));
|
|
|
|
|
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);
|
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo;
|
|
|
|
|
rv = channel->GetLoadInfo(getter_AddRefs(loadInfo));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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
|
|
|
|
|
2016-02-27 01:36:45 +03:00
|
|
|
|
nsAutoCString referrer;
|
|
|
|
|
// Ignore the return value since the Referer header may not exist.
|
|
|
|
|
httpChannel->GetRequestHeader(NS_LITERAL_CSTRING("Referer"), referrer);
|
|
|
|
|
if (!referrer.IsEmpty()) {
|
|
|
|
|
mReferrer = referrer;
|
2016-11-14 10:15:32 +03:00
|
|
|
|
} else {
|
|
|
|
|
// If there's no referrer Header, means the header was omitted for
|
|
|
|
|
// security/privacy reason.
|
|
|
|
|
mReferrer = EmptyCString();
|
2016-02-27 01:36:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t referrerPolicy = 0;
|
|
|
|
|
rv = httpChannel->GetReferrerPolicy(&referrerPolicy);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
switch (referrerPolicy) {
|
2016-11-14 10:15:32 +03:00
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_UNSET:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::_empty;
|
|
|
|
|
break;
|
2016-02-27 01:36:45 +03:00
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_NO_REFERRER:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::No_referrer;
|
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_ORIGIN:
|
2016-04-20 03:16:39 +03:00
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Origin;
|
2016-02-27 01:36:45 +03:00
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::No_referrer_when_downgrade;
|
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_ORIGIN_WHEN_XORIGIN:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Origin_when_cross_origin;
|
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_UNSAFE_URL:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Unsafe_url;
|
|
|
|
|
break;
|
2016-11-14 10:15:32 +03:00
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_SAME_ORIGIN:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Same_origin;
|
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Strict_origin_when_cross_origin;
|
|
|
|
|
break;
|
|
|
|
|
case nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN:
|
|
|
|
|
mReferrerPolicy = ReferrerPolicy::Strict_origin;
|
|
|
|
|
break;
|
2016-02-27 01:36:45 +03:00
|
|
|
|
default:
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid Referrer Policy enum value?");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
internalChannel->GetRedirectMode(&redirectMode);
|
|
|
|
|
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;
|
|
|
|
|
internalChannel->GetFetchCacheMode(&cacheMode);
|
|
|
|
|
mCacheMode = static_cast<RequestCache>(cacheMode);
|
|
|
|
|
|
2016-09-07 05:20:23 +03:00
|
|
|
|
internalChannel->GetIntegrityMetadata(mIntegrity);
|
|
|
|
|
|
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);
|
|
|
|
|
bool bodyHasHeaders = false;
|
|
|
|
|
rv = uploadChannel->GetUploadStreamHasHeaders(&bodyHasHeaders);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
nsCOMPtr<nsIInputStream> uploadStream;
|
|
|
|
|
rv = uploadChannel->CloneUploadStream(getter_AddRefs(uploadStream));
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
if (bodyHasHeaders) {
|
|
|
|
|
HandleBodyWithHeaders(uploadStream);
|
|
|
|
|
} else {
|
|
|
|
|
mUploadStream = uploadStream;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
|
{
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
return DispatchFetchEvent(aCx, aWorkerPrivate);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
|
nsresult
|
2015-11-24 18:47:59 +03:00
|
|
|
|
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:
|
|
|
|
|
~FetchEventRunnable() {}
|
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
|
class ResumeRequest final : public Runnable {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
|
|
|
|
|
public:
|
|
|
|
|
explicit ResumeRequest(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel)
|
|
|
|
|
: mChannel(aChannel)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
|
NS_IMETHOD Run() override
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
nsresult rv = mChannel->ResetInterception();
|
2016-09-01 08:01:16 +03:00
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
|
"Failed to resume intercepted network request");
|
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,
|
2016-11-07 05:16:34 +03:00
|
|
|
|
mFragment,
|
2016-01-14 02:14:38 +03:00
|
|
|
|
mMethod,
|
|
|
|
|
internalHeaders.forget(),
|
2016-02-29 02:05:40 +03:00
|
|
|
|
mCacheMode,
|
2016-01-14 02:14:38 +03:00
|
|
|
|
mRequestMode,
|
|
|
|
|
mRequestRedirect,
|
|
|
|
|
mRequestCredentials,
|
|
|
|
|
NS_ConvertUTF8toUTF16(mReferrer),
|
2016-02-27 01:36:45 +03:00
|
|
|
|
mReferrerPolicy,
|
2016-09-07 05:20:23 +03:00
|
|
|
|
mContentPolicyType,
|
|
|
|
|
mIntegrity);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
internalReq->SetBody(mUploadStream);
|
2016-01-14 02:14:38 +03:00
|
|
|
|
// For Telemetry, note that this Request object was created by a Fetch event.
|
|
|
|
|
internalReq->SetCreatedByFetchEvent();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
2016-01-14 02:14:38 +03:00
|
|
|
|
RefPtr<Request> request = new Request(global, internalReq);
|
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;
|
2016-01-08 23:53:38 +03:00
|
|
|
|
if (!mClientId.IsEmpty()) {
|
|
|
|
|
init.mClientId = mClientId;
|
|
|
|
|
}
|
2015-10-25 06:29:43 +03:00
|
|
|
|
init.mIsReload = mIsReload;
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<FetchEvent> event =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init, result);
|
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
|
result.SuppressException();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 12:24:04 +03:00
|
|
|
|
event->PostInit(mInterceptedChannel, mRegistration, mScriptSpec);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
2016-11-10 12:53:24 +03:00
|
|
|
|
nsresult rv2 =
|
2016-11-21 05:14:53 +03:00
|
|
|
|
DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
|
|
|
|
|
event, nullptr);
|
2016-11-10 12:53:24 +03:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv2)) || !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,
|
2016-04-27 12:24:04 +03:00
|
|
|
|
mRegistration,
|
2015-12-11 22:53:11 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
2015-10-25 01:22:46 +03:00
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
HandleBodyWithHeaders(nsIInputStream* aUploadStream)
|
|
|
|
|
{
|
|
|
|
|
// We are dealing with an nsMIMEInputStream which uses string input streams
|
|
|
|
|
// under the hood, so all of the data is available synchronously.
|
|
|
|
|
bool nonBlocking = false;
|
|
|
|
|
nsresult rv = aUploadStream->IsNonBlocking(&nonBlocking);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
if (NS_WARN_IF(!nonBlocking)) {
|
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
}
|
|
|
|
|
nsAutoCString body;
|
|
|
|
|
rv = NS_ConsumeStream(aUploadStream, UINT32_MAX, body);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
// Extract the headers in the beginning of the buffer
|
|
|
|
|
nsAutoCString::const_iterator begin, end;
|
|
|
|
|
body.BeginReading(begin);
|
|
|
|
|
body.EndReading(end);
|
|
|
|
|
const nsAutoCString::const_iterator body_end = end;
|
|
|
|
|
nsAutoCString headerName, headerValue;
|
|
|
|
|
bool emptyHeader = false;
|
|
|
|
|
while (FetchUtil::ExtractHeader(begin, end, headerName,
|
|
|
|
|
headerValue, &emptyHeader) &&
|
|
|
|
|
!emptyHeader) {
|
|
|
|
|
mHeaderNames.AppendElement(headerName);
|
|
|
|
|
mHeaderValues.AppendElement(headerValue);
|
|
|
|
|
headerName.Truncate();
|
|
|
|
|
headerValue.Truncate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace the upload stream with one only containing the body text.
|
|
|
|
|
nsCOMPtr<nsIStringInputStream> strStream =
|
|
|
|
|
do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
// Skip past the "\r\n" that separates the headers and the body.
|
|
|
|
|
++begin;
|
|
|
|
|
++begin;
|
|
|
|
|
body.Assign(Substring(begin, body_end));
|
|
|
|
|
rv = strStream->SetData(body.BeginReading(), body.Length());
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
mUploadStream = strStream;
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(FetchEventRunnable, WorkerRunnable, nsIHttpHeaderVisitor)
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ServiceWorkerPrivate::SendFetchEvent(nsIInterceptedChannel* aChannel,
|
|
|
|
|
nsILoadGroup* aLoadGroup,
|
2015-11-26 02:05:34 +03:00
|
|
|
|
const nsAString& aDocumentId,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
bool aIsReload)
|
|
|
|
|
{
|
2015-11-24 18:47:59 +03:00
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
if (NS_WARN_IF(!mInfo)) {
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
|
MOZ_ASSERT(swm);
|
|
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> registration =
|
|
|
|
|
swm->GetRegistration(mInfo->GetPrincipal(), mInfo->Scope());
|
|
|
|
|
|
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) {
|
|
|
|
|
aChannel->ResetInterception();
|
|
|
|
|
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()) {
|
|
|
|
|
aChannel->ResetInterception();
|
|
|
|
|
|
|
|
|
|
// Trigger soft updates if necessary.
|
|
|
|
|
registration->MaybeScheduleTimeCheckAndUpdate();
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
// if the ServiceWorker script fails to load for some reason, just resume
|
|
|
|
|
// the original channel.
|
|
|
|
|
nsCOMPtr<nsIRunnable> failRunnable =
|
2016-05-05 11:45:00 +03:00
|
|
|
|
NewRunnableMethod(aChannel, &nsIInterceptedChannel::ResetInterception);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(FetchEvent, failRunnable, aLoadGroup);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<nsIInterceptedChannel> handle(
|
|
|
|
|
new nsMainThreadPtrHolder<nsIInterceptedChannel>(aChannel, false));
|
|
|
|
|
|
2015-10-26 05:59:48 +03:00
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
|
|
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(registration, false));
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<FetchEventRunnable> r =
|
2016-07-28 03:36:10 +03:00
|
|
|
|
new FetchEventRunnable(mWorkerPrivate, token, handle,
|
2015-10-26 05:59:48 +03:00
|
|
|
|
mInfo->ScriptSpec(), regInfo,
|
2015-11-26 02:05:34 +03:00
|
|
|
|
aDocumentId, aIsReload);
|
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,
|
|
|
|
|
nsIRunnable* aLoadFailedRunnable,
|
|
|
|
|
nsILoadGroup* aLoadGroup)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
// XXXcatalinb: We need to have a separate load group that's linked to
|
|
|
|
|
// an existing tab child to pass security checks on b2g.
|
|
|
|
|
// This should be fixed in bug 1125961, but for now we enforce updating
|
|
|
|
|
// the overriden load group when intercepting a fetch.
|
|
|
|
|
MOZ_ASSERT_IF(aWhy == FetchEvent, aLoadGroup);
|
|
|
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(info.mBaseURI), mInfo->ScriptSpec(),
|
|
|
|
|
nullptr, nullptr);
|
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.mResolvedScriptURI = info.mBaseURI;
|
|
|
|
|
MOZ_ASSERT(!mInfo->CacheName().IsEmpty());
|
|
|
|
|
info.mServiceWorkerCacheName = mInfo->CacheName();
|
|
|
|
|
info.mServiceWorkerID = mInfo->ID();
|
|
|
|
|
info.mLoadGroup = aLoadGroup;
|
|
|
|
|
info.mLoadFailedAsyncRunnable = aLoadFailedRunnable;
|
|
|
|
|
|
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.
|
|
|
|
|
info.mLoadFlags = mInfo->GetLoadFlags() |
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.mPrincipal = mInfo->GetPrincipal();
|
|
|
|
|
|
|
|
|
|
nsContentUtils::StorageAccess access =
|
|
|
|
|
nsContentUtils::StorageAllowedForPrincipal(info.mPrincipal);
|
|
|
|
|
info.mStorageAllowed = access > nsContentUtils::StorageAccess::ePrivateBrowsing;
|
2016-09-20 04:13:00 +03:00
|
|
|
|
info.mOriginAttributes = mInfo->GetOriginAttributes();
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
|
rv = info.mPrincipal->GetCsp(getter_AddRefs(csp));
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.mCSP = csp;
|
|
|
|
|
if (info.mCSP) {
|
|
|
|
|
rv = info.mCSP->GetAllowsEval(&info.mReportCSPViolations,
|
|
|
|
|
&info.mEvalAllowed);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
info.mEvalAllowed = true;
|
|
|
|
|
info.mReportCSPViolations = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WorkerPrivate::OverrideLoadInfoLoadGroup(info);
|
|
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
jsapi.Init();
|
|
|
|
|
ErrorResult error;
|
|
|
|
|
NS_ConvertUTF8toUTF16 scriptSpec(mInfo->ScriptSpec());
|
|
|
|
|
|
|
|
|
|
mWorkerPrivate = WorkerPrivate::Constructor(jsapi.cx(),
|
|
|
|
|
scriptSpec,
|
|
|
|
|
false, WorkerTypeService,
|
|
|
|
|
mInfo->Scope(), &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
|
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-24 15:16:23 +03:00
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::StoreISupports(nsISupports* aSupports)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
MOZ_ASSERT(!mSupportsArray.Contains(aSupports));
|
|
|
|
|
|
|
|
|
|
mSupportsArray.AppendElement(aSupports);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::RemoveISupports(nsISupports* aSupports)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
mSupportsArray.RemoveElement(aSupports);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::TerminateWorker()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
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) {
|
2015-10-01 02:11:03 +03:00
|
|
|
|
if (Preferences::GetBool("dom.serviceWorkers.testing.enabled")) {
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
|
if (os) {
|
|
|
|
|
os->NotifyObservers(this, "service-worker-shutdown", nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
|
Unused << NS_WARN_IF(!mWorkerPrivate->Terminate());
|
2015-10-01 02:11:03 +03:00
|
|
|
|
mWorkerPrivate = nullptr;
|
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()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
mInfo = nullptr;
|
|
|
|
|
TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-24 18:47:59 +03:00
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::Activated()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
// If we had to queue up events due to the worker activating, that means
|
|
|
|
|
// the worker must be currently running. We should be called synchronously
|
|
|
|
|
// when the worker becomes activated.
|
|
|
|
|
MOZ_ASSERT_IF(!mPendingFunctionalEvents.IsEmpty(), mWorkerPrivate);
|
|
|
|
|
|
|
|
|
|
nsTArray<RefPtr<WorkerRunnable>> pendingEvents;
|
|
|
|
|
mPendingFunctionalEvents.SwapElements(pendingEvents);
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pendingEvents.Length(); ++i) {
|
|
|
|
|
RefPtr<WorkerRunnable> r = pendingEvents[i].forget();
|
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)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
|
|
|
|
|
if (!mDebuggerCount) {
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWorkerDebugger> debugger = do_QueryInterface(mWorkerPrivate->Debugger());
|
|
|
|
|
debugger.forget(aResult);
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ServiceWorkerPrivate::AttachDebugger()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
nsresult rv = SpawnWorkerIfNeeded(AttachEvent, nullptr);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
|
mIdleWorkerTimer->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++mDebuggerCount;
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ServiceWorkerPrivate::DetachDebugger()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
return mTokenCount == 0 || (mTokenCount == 1 && mIdleKeepAliveToken);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
/* static */ void
|
|
|
|
|
ServiceWorkerPrivate::NoteIdleWorkerCallback(nsITimer* aTimer, void* aPrivate)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aPrivate);
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ServiceWorkerPrivate> swp = static_cast<ServiceWorkerPrivate*>(aPrivate);
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(aTimer == swp->mIdleWorkerTimer, "Invalid timer!");
|
|
|
|
|
|
|
|
|
|
// Release ServiceWorkerPrivate's token, since the grace period has ended.
|
2016-07-28 03:36:10 +03:00
|
|
|
|
swp->mIdleKeepAliveToken = nullptr;
|
2015-10-01 02:11:03 +03:00
|
|
|
|
|
|
|
|
|
if (swp->mWorkerPrivate) {
|
|
|
|
|
// If we still have a workerPrivate at this point it means there are pending
|
|
|
|
|
// waitUntil promises. 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");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
|
swp->mIdleWorkerTimer->InitWithFuncCallback(ServiceWorkerPrivate::TerminateWorkerCallback,
|
|
|
|
|
aPrivate,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
timeout,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
|
ServiceWorkerPrivate::TerminateWorkerCallback(nsITimer* aTimer, void *aPrivate)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(aPrivate);
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
|
RefPtr<ServiceWorkerPrivate> serviceWorkerPrivate =
|
2015-10-01 02:11:03 +03:00
|
|
|
|
static_cast<ServiceWorkerPrivate*>(aPrivate);
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(aTimer == serviceWorkerPrivate->mIdleWorkerTimer,
|
|
|
|
|
"Invalid timer!");
|
|
|
|
|
|
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(
|
|
|
|
|
serviceWorkerPrivate->mInfo->Scope(),
|
|
|
|
|
"ServiceWorkerGraceTimeoutTermination",
|
|
|
|
|
nsTArray<nsString> { NS_ConvertUTF8toUTF16(serviceWorkerPrivate->mInfo->Scope()) });
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
serviceWorkerPrivate->TerminateWorker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-11-26 14:18:56 +03:00
|
|
|
|
ServiceWorkerPrivate::RenewKeepAliveToken(WakeUpReason aWhy)
|
2015-10-01 02:11:03 +03:00
|
|
|
|
{
|
2015-11-26 14:18:56 +03:00
|
|
|
|
// We should have an active worker if we're renewing the keep alive token.
|
2015-10-01 02:11:03 +03:00
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
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");
|
2015-10-01 02:11:03 +03:00
|
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
|
mIdleWorkerTimer->InitWithFuncCallback(ServiceWorkerPrivate::NoteIdleWorkerCallback,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
this, timeout,
|
2015-10-01 02:11:03 +03:00
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::AddToken()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
++mTokenCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::ReleaseToken()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(mTokenCount > 0);
|
|
|
|
|
--mTokenCount;
|
|
|
|
|
if (!mTokenCount) {
|
|
|
|
|
TerminateWorker();
|
2016-07-28 03:36:10 +03:00
|
|
|
|
} else if (IsIdle()) {
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
|
if (swm) {
|
|
|
|
|
swm->WorkerIsIdle(mInfo);
|
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 03:36:10 +03:00
|
|
|
|
already_AddRefed<KeepAliveToken>
|
|
|
|
|
ServiceWorkerPrivate::CreateEventKeepAliveToken()
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2016-07-28 03:36:10 +03:00
|
|
|
|
MOZ_ASSERT(mIdleKeepAliveToken);
|
2016-07-28 03:36:10 +03:00
|
|
|
|
RefPtr<KeepAliveToken> ref = new KeepAliveToken(this);
|
|
|
|
|
return ref.forget();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 22:10:25 +03:00
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::AddPendingWindow(Runnable* aPendingWindow)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
pendingWindows.AppendElement(aPendingWindow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
|
ServiceWorkerPrivate::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
nsCString topic(aTopic);
|
|
|
|
|
if (!topic.Equals(NS_LITERAL_CSTRING("BrowserChrome:Ready"))) {
|
|
|
|
|
MOZ_ASSERT(false, "Unexpected topic.");
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
|
NS_ENSURE_STATE(os);
|
|
|
|
|
os->RemoveObserver(static_cast<nsIObserver*>(this), "BrowserChrome:Ready");
|
|
|
|
|
|
|
|
|
|
size_t len = pendingWindows.Length();
|
|
|
|
|
for (int i = len-1; i >= 0; i--) {
|
|
|
|
|
RefPtr<Runnable> runnable = pendingWindows[i];
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
|
|
|
|
pendingWindows.RemoveElementAt(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 05:38:53 +03:00
|
|
|
|
void
|
|
|
|
|
ServiceWorkerPrivate::SetHandlesFetch(bool aValue)
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mInfo)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mInfo->SetHandlesFetch(aValue);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
|
END_WORKERS_NAMESPACE
|