2016-04-06 23:27:22 +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 "ServiceWorkerJob.h"
|
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "nsIPrincipal.h"
|
2016-04-06 23:27:22 +03:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2018-07-10 02:02:39 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJob::Type ServiceWorkerJob::GetType() const { return mType; }
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJob::State ServiceWorkerJob::GetState() const { return mState; }
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
bool ServiceWorkerJob::Canceled() const { return mCanceled; }
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
bool ServiceWorkerJob::ResultCallbacksInvoked() const {
|
2016-04-06 23:27:23 +03:00
|
|
|
return mResultCallbacksInvoked;
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
bool ServiceWorkerJob::IsEquivalentTo(ServiceWorkerJob* aJob) const {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(aJob);
|
|
|
|
return mType == aJob->mType && mScope.Equals(aJob->mScope) &&
|
|
|
|
mScriptSpec.Equals(aJob->mScriptSpec) &&
|
|
|
|
mPrincipal->Equals(aJob->mPrincipal);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::AppendResultCallback(Callback* aCallback) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != State::Finished);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aCallback);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mFinalCallback != aCallback);
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(!mResultCallbackList.Contains(aCallback));
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mResultCallbacksInvoked);
|
2016-04-06 23:27:22 +03:00
|
|
|
mResultCallbackList.AppendElement(aCallback);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::StealResultCallbacksFrom(ServiceWorkerJob* aJob) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(aJob);
|
|
|
|
MOZ_ASSERT(aJob->mState == State::Initial);
|
|
|
|
|
|
|
|
// Take the callbacks from the other job immediately to avoid the
|
|
|
|
// any possibility of them existing on both jobs at once.
|
|
|
|
nsTArray<RefPtr<Callback>> callbackList;
|
|
|
|
callbackList.SwapElements(aJob->mResultCallbackList);
|
|
|
|
|
|
|
|
for (RefPtr<Callback>& callback : callbackList) {
|
|
|
|
// Use AppendResultCallback() so that assertion checking is performed on
|
|
|
|
// each callback.
|
|
|
|
AppendResultCallback(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::Start(Callback* aFinalCallback) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mCanceled);
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFinalCallback);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mFinalCallback);
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(!mResultCallbackList.Contains(aFinalCallback));
|
|
|
|
mFinalCallback = aFinalCallback;
|
|
|
|
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == State::Initial);
|
2016-04-06 23:27:22 +03:00
|
|
|
mState = State::Started;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod(
|
2017-05-10 02:19:37 +03:00
|
|
|
"ServiceWorkerJob::AsyncExecute", this, &ServiceWorkerJob::AsyncExecute);
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
// We may have to wait for the PBackground actor to be initialized
|
|
|
|
// before proceeding. We should always be able to get a ServiceWorkerManager,
|
|
|
|
// however, since Start() should not be called during shutdown.
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2017-01-13 01:00:36 +03:00
|
|
|
if (!swm) {
|
|
|
|
// browser shutdown
|
|
|
|
return;
|
|
|
|
}
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
// Otherwise start asynchronously. We should never run a job synchronously.
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(runnable.forget())));
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::Cancel() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(!mCanceled);
|
|
|
|
mCanceled = true;
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJob::ServiceWorkerJob(Type aType, nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aScope,
|
|
|
|
const nsACString& aScriptSpec)
|
2016-04-06 23:27:22 +03:00
|
|
|
: mType(aType),
|
|
|
|
mPrincipal(aPrincipal),
|
|
|
|
mScope(aScope),
|
|
|
|
mScriptSpec(aScriptSpec),
|
|
|
|
mState(State::Initial),
|
|
|
|
mCanceled(false),
|
2016-04-06 23:27:23 +03:00
|
|
|
mResultCallbacksInvoked(false) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
MOZ_ASSERT(mPrincipal);
|
|
|
|
MOZ_ASSERT(!mScope.IsEmpty());
|
|
|
|
// Some job types may have an empty script spec
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
ServiceWorkerJob::~ServiceWorkerJob() {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-06 23:27:22 +03:00
|
|
|
// Jobs must finish or never be started. Destroying an actively running
|
|
|
|
// job is an error.
|
|
|
|
MOZ_ASSERT(mState != State::Started);
|
2016-04-06 23:27:23 +03:00
|
|
|
MOZ_ASSERT_IF(mState == State::Finished, mResultCallbacksInvoked);
|
2016-04-06 23:27:22 +03:00
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::InvokeResultCallbacks(ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == State::Started);
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mResultCallbacksInvoked);
|
2016-04-06 23:27:23 +03:00
|
|
|
mResultCallbacksInvoked = true;
|
|
|
|
|
2016-04-06 23:27:22 +03:00
|
|
|
nsTArray<RefPtr<Callback>> callbackList;
|
|
|
|
callbackList.SwapElements(mResultCallbackList);
|
|
|
|
|
|
|
|
for (RefPtr<Callback>& callback : callbackList) {
|
|
|
|
// The callback might consume an exception on the ErrorResult, so we need
|
|
|
|
// to clone in order to maintain the error for the next callback.
|
|
|
|
ErrorResult rv;
|
|
|
|
aRv.CloneTo(rv);
|
|
|
|
|
|
|
|
callback->JobFinished(this, rv);
|
|
|
|
|
|
|
|
// The callback might not consume the error.
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::InvokeResultCallbacks(nsresult aRv) {
|
2016-04-06 23:27:22 +03:00
|
|
|
ErrorResult converted(aRv);
|
|
|
|
InvokeResultCallbacks(converted);
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::Finish(ErrorResult& aRv) {
|
2018-01-27 00:08:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-01-16 17:17:15 +03:00
|
|
|
|
|
|
|
// Avoid double-completion because it can result on operating on cleaned
|
|
|
|
// up data. This should not happen, though, so also assert to try to
|
|
|
|
// narrow down the causes.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == State::Started);
|
|
|
|
if (mState != State::Started) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
// Ensure that we only surface SecurityErr, TypeErr or InvalidStateErr to
|
|
|
|
// script.
|
|
|
|
if (aRv.Failed() && !aRv.ErrorCodeIs(NS_ERROR_DOM_SECURITY_ERR) &&
|
|
|
|
!aRv.ErrorCodeIs(NS_ERROR_DOM_TYPE_ERR) &&
|
|
|
|
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR)) {
|
|
|
|
// Remove the old error code so we can replace it with a TypeError.
|
|
|
|
aRv.SuppressException();
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 scriptSpec(mScriptSpec);
|
|
|
|
NS_ConvertUTF8toUTF16 scope(mScope);
|
|
|
|
|
|
|
|
// Throw the type error with a generic error message.
|
|
|
|
aRv.ThrowTypeError<MSG_SW_INSTALL_ERROR>(scriptSpec, scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final callback may drop the last ref to this object.
|
2016-04-06 23:27:23 +03:00
|
|
|
RefPtr<ServiceWorkerJob> kungFuDeathGrip = this;
|
2016-04-06 23:27:22 +03:00
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
if (!mResultCallbacksInvoked) {
|
|
|
|
InvokeResultCallbacks(aRv);
|
|
|
|
}
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
mState = State::Finished;
|
|
|
|
|
2017-01-16 17:17:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mFinalCallback);
|
|
|
|
if (mFinalCallback) {
|
|
|
|
mFinalCallback->JobFinished(this, aRv);
|
|
|
|
mFinalCallback = nullptr;
|
|
|
|
}
|
2016-04-06 23:27:22 +03:00
|
|
|
|
|
|
|
// The callback might not consume the error.
|
|
|
|
aRv.SuppressException();
|
|
|
|
|
|
|
|
// Async release this object to ensure that our caller methods complete
|
|
|
|
// as well.
|
2017-10-27 20:45:52 +03:00
|
|
|
NS_ReleaseOnMainThreadSystemGroup("ServiceWorkerJobProxyRunnable",
|
2017-06-14 04:27:17 +03:00
|
|
|
kungFuDeathGrip.forget(),
|
|
|
|
true /* always proxy */);
|
2016-04-06 23:27:22 +03:00
|
|
|
}
|
|
|
|
|
2016-04-06 23:27:23 +03:00
|
|
|
void ServiceWorkerJob::Finish(nsresult aRv) {
|
2016-04-06 23:27:22 +03:00
|
|
|
ErrorResult converted(aRv);
|
|
|
|
Finish(converted);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|