2013-11-19 15:15:02 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ServiceWorkerManager.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-06-11 09:12:56 -07:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
|
2013-11-19 15:15:02 -08:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCxPusher.h"
|
|
|
|
#include "nsNetUtil.h"
|
2014-06-11 09:12:56 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2013-11-19 15:15:02 -08:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ServiceWorker.h"
|
|
|
|
#include "WorkerInlines.h"
|
2014-06-11 09:12:56 -07:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
2013-11-19 15:15:02 -08:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
NS_IMPL_ISUPPORTS0(ServiceWorkerRegistration)
|
|
|
|
|
|
|
|
UpdatePromise::UpdatePromise()
|
|
|
|
: mState(Pending)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(UpdatePromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdatePromise::~UpdatePromise()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(UpdatePromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdatePromise::AddPromise(Promise* aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
mPromises.AppendElement(aPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdatePromise::ResolveAllPromises(const nsACString& aScriptSpec, const nsACString& aScope)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
mState = Resolved;
|
|
|
|
RuntimeService* rs = RuntimeService::GetOrCreateService();
|
|
|
|
MOZ_ASSERT(rs);
|
|
|
|
|
|
|
|
nsTArray<nsTWeakRef<Promise>> array;
|
|
|
|
array.SwapElements(mPromises);
|
|
|
|
for (uint32_t i = 0; i < array.Length(); ++i) {
|
|
|
|
nsTWeakRef<Promise>& pendingPromise = array.ElementAt(i);
|
|
|
|
if (pendingPromise) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> go =
|
|
|
|
do_QueryInterface(pendingPromise->GetParentObject());
|
|
|
|
MOZ_ASSERT(go);
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> global(cx, go->GetGlobalJSObject());
|
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
|
|
|
GlobalObject domGlobal(cx, global);
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> serviceWorker;
|
|
|
|
nsresult rv = rs->CreateServiceWorker(domGlobal,
|
|
|
|
NS_ConvertUTF8toUTF16(aScriptSpec),
|
|
|
|
aScope,
|
|
|
|
getter_AddRefs(serviceWorker));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
pendingPromise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingPromise->MaybeResolve(serviceWorker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdatePromise::RejectAllPromises(nsresult aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
mState = Rejected;
|
|
|
|
|
|
|
|
nsTArray<nsTWeakRef<Promise>> array;
|
|
|
|
array.SwapElements(mPromises);
|
|
|
|
for (uint32_t i = 0; i < array.Length(); ++i) {
|
|
|
|
nsTWeakRef<Promise>& pendingPromise = array.ElementAt(i);
|
|
|
|
if (pendingPromise) {
|
|
|
|
// Since ServiceWorkerContainer is only exposed to windows we can be
|
|
|
|
// certain about this cast.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window =
|
|
|
|
do_QueryInterface(pendingPromise->GetParentObject());
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
nsRefPtr<DOMError> domError = new DOMError(window, aRv);
|
|
|
|
pendingPromise->MaybeRejectBrokenly(domError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FinishFetchOnMainThreadRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerUpdateInstance> mUpdateInstance;
|
|
|
|
public:
|
|
|
|
FinishFetchOnMainThreadRunnable
|
|
|
|
(const nsMainThreadPtrHandle<ServiceWorkerUpdateInstance>& aUpdateInstance)
|
|
|
|
: mUpdateInstance(aUpdateInstance)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() MOZ_OVERRIDE;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FinishSuccessfulFetchWorkerRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerUpdateInstance> mUpdateInstance;
|
|
|
|
public:
|
|
|
|
FinishSuccessfulFetchWorkerRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsMainThreadPtrHandle<ServiceWorkerUpdateInstance>& aUpdateInstance)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
|
|
|
mUpdateInstance(aUpdateInstance)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (!aWorkerPrivate->WorkerScriptExecutedSuccessfully()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<FinishFetchOnMainThreadRunnable> r =
|
|
|
|
new FinishFetchOnMainThreadRunnable(mUpdateInstance);
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allows newer calls to Update() to 'abort' older calls.
|
|
|
|
// Each call to Update() creates the instance which handles creating the
|
|
|
|
// worker and queues up a runnable to resolve the update promise once the
|
|
|
|
// worker has successfully been parsed.
|
|
|
|
class ServiceWorkerUpdateInstance MOZ_FINAL : public nsISupports
|
|
|
|
{
|
|
|
|
// Owner of this instance.
|
|
|
|
ServiceWorkerRegistration* mRegistration;
|
|
|
|
nsCString mScriptSpec;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
|
|
|
|
bool mAborted;
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
ServiceWorkerUpdateInstance(ServiceWorkerRegistration *aRegistration,
|
|
|
|
nsPIDOMWindow* aWindow)
|
|
|
|
: mRegistration(aRegistration),
|
|
|
|
// Capture the current script spec in case register() gets called.
|
|
|
|
mScriptSpec(aRegistration->mScriptSpec),
|
|
|
|
mWindow(aWindow),
|
|
|
|
mAborted(false)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Abort()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mAborted);
|
|
|
|
mAborted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Update()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
MOZ_ASSERT(swm);
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> serviceWorker;
|
|
|
|
nsresult rv = swm->CreateServiceWorkerForWindow(mWindow,
|
|
|
|
mScriptSpec,
|
|
|
|
mRegistration->mScope,
|
|
|
|
getter_AddRefs(serviceWorker));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
swm->RejectUpdatePromiseObservers(mRegistration, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<ServiceWorkerUpdateInstance> handle =
|
|
|
|
new nsMainThreadPtrHolder<ServiceWorkerUpdateInstance>(this);
|
|
|
|
// FIXME(nsm): Deal with error case (worker failed to download, redirect,
|
|
|
|
// parse) in error handler patch.
|
|
|
|
nsRefPtr<FinishSuccessfulFetchWorkerRunnable> r =
|
|
|
|
new FinishSuccessfulFetchWorkerRunnable(serviceWorker->GetWorkerPrivate(), handle);
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
if (!r->Dispatch(cx)) {
|
|
|
|
swm->RejectUpdatePromiseObservers(mRegistration, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FetchDone()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (mAborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
MOZ_ASSERT(swm);
|
|
|
|
swm->FinishFetch(mRegistration, mWindow);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(ServiceWorkerUpdateInstance)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FinishFetchOnMainThreadRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mUpdateInstance->FetchDone();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerRegistration::ServiceWorkerRegistration(const nsACString& aScope)
|
|
|
|
: mScope(aScope),
|
|
|
|
mPendingUninstall(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ServiceWorkerRegistration::~ServiceWorkerRegistration()
|
|
|
|
{ }
|
|
|
|
|
2013-11-19 15:15:02 -08:00
|
|
|
//////////////////////////
|
|
|
|
// ServiceWorkerManager //
|
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(ServiceWorkerManager)
|
|
|
|
NS_IMPL_RELEASE(ServiceWorkerManager)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ServiceWorkerManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIServiceWorkerManager)
|
|
|
|
if (aIID.Equals(NS_GET_IID(ServiceWorkerManager)))
|
|
|
|
foundInterface = static_cast<nsIServiceWorkerManager*>(this);
|
|
|
|
else
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIServiceWorkerManager)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
ServiceWorkerManager::ServiceWorkerManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerManager::~ServiceWorkerManager()
|
|
|
|
{
|
|
|
|
// The map will assert if it is not empty when destroyed.
|
|
|
|
mDomainMap.EnumerateRead(CleanupServiceWorkerInformation, nullptr);
|
|
|
|
mDomainMap.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ PLDHashOperator
|
|
|
|
ServiceWorkerManager::CleanupServiceWorkerInformation(const nsACString& aDomain,
|
|
|
|
ServiceWorkerDomainInfo* aDomainInfo,
|
|
|
|
void *aUnused)
|
|
|
|
{
|
|
|
|
aDomainInfo->mServiceWorkerRegistrations.Clear();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements the async aspects of the register algorithm.
|
|
|
|
*/
|
|
|
|
class RegisterRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
const nsCString mScope;
|
|
|
|
nsCOMPtr<nsIURI> mScriptURI;
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
public:
|
|
|
|
RegisterRunnable(nsPIDOMWindow* aWindow, const nsCString aScope,
|
|
|
|
nsIURI* aScriptURI, Promise* aPromise)
|
|
|
|
: mWindow(aWindow), mScope(aScope), mScriptURI(aScriptURI),
|
|
|
|
mPromise(aPromise)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
nsCString domain;
|
|
|
|
nsresult rv = mScriptURI->GetHost(domain);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mPromise->MaybeReject(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
ServiceWorkerManager::ServiceWorkerDomainInfo* domainInfo =
|
|
|
|
swm->mDomainMap.Get(domain);
|
2014-06-11 09:12:56 -07:00
|
|
|
// XXXnsm: This pattern can be refactored if we end up using it
|
|
|
|
// often enough.
|
2013-11-19 15:15:02 -08:00
|
|
|
if (!swm->mDomainMap.Get(domain, &domainInfo)) {
|
|
|
|
domainInfo = new ServiceWorkerManager::ServiceWorkerDomainInfo;
|
|
|
|
swm->mDomainMap.Put(domain, domainInfo);
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
nsRefPtr<ServiceWorkerRegistration> registration =
|
|
|
|
domainInfo->GetRegistration(mScope);
|
2013-11-19 15:15:02 -08:00
|
|
|
|
|
|
|
nsCString spec;
|
|
|
|
rv = mScriptURI->GetSpec(spec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mPromise->MaybeReject(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (registration) {
|
|
|
|
registration->mPendingUninstall = false;
|
|
|
|
if (spec.Equals(registration->mScriptSpec)) {
|
|
|
|
// There is an existing update in progress. Resolve with whatever it
|
|
|
|
// results in.
|
|
|
|
if (registration->HasUpdatePromise()) {
|
|
|
|
registration->AddUpdatePromiseObserver(mPromise);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
// There is no update in progress and since SW updating is upto the UA,
|
|
|
|
// we will not update right now. Simply resolve with whatever worker we
|
2013-11-19 15:15:02 -08:00
|
|
|
// have.
|
|
|
|
ServiceWorkerInfo info = registration->Newest();
|
|
|
|
if (info.IsValid()) {
|
|
|
|
nsRefPtr<ServiceWorker> serviceWorker;
|
|
|
|
nsresult rv =
|
|
|
|
swm->CreateServiceWorkerForWindow(mWindow,
|
|
|
|
info.GetScriptSpec(),
|
|
|
|
registration->mScope,
|
|
|
|
getter_AddRefs(serviceWorker));
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPromise->MaybeResolve(serviceWorker);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registration = domainInfo->CreateNewRegistration(mScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
registration->mScriptSpec = spec;
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
rv = swm->Update(registration, mWindow);
|
|
|
|
MOZ_ASSERT(registration->HasUpdatePromise());
|
|
|
|
|
|
|
|
// We append this register() call's promise after calling Update() because
|
|
|
|
// we don't want this one to be aborted when the others (existing updates
|
|
|
|
// for the same registration) are aborted. Update() sets a new
|
|
|
|
// UpdatePromise on the registration.
|
|
|
|
registration->mUpdatePromise->AddPromise(mPromise);
|
|
|
|
|
|
|
|
return rv;
|
2013-11-19 15:15:02 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we return an error code here, the ServiceWorkerContainer will
|
|
|
|
// automatically reject the Promise.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerManager::Register(nsIDOMWindow* aWindow, const nsAString& aScope,
|
2014-06-11 09:12:56 -07:00
|
|
|
const nsAString& aScriptURL,
|
|
|
|
nsISupports** aPromise)
|
2013-11-19 15:15:02 -08:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
// XXXnsm Don't allow chrome callers for now, we don't support chrome
|
|
|
|
// ServiceWorkers.
|
|
|
|
MOZ_ASSERT(!nsContentUtils::IsCallerChrome());
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow);
|
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> sgo = do_QueryInterface(window);
|
|
|
|
nsRefPtr<Promise> promise = new Promise(sgo);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> documentURI = window->GetDocumentURI();
|
|
|
|
if (!documentURI) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Although the spec says that the same-origin checks should also be done
|
|
|
|
// asynchronously, we do them in sync because the Promise created by the
|
|
|
|
// WebIDL infrastructure due to a returned error will be resolved
|
|
|
|
// asynchronously. We aren't making any internal state changes in these
|
|
|
|
// checks, so ordering of multiple calls is not affected.
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
// FIXME(nsm): Bug 1003991. Disable check when devtools are open.
|
|
|
|
if (!Preferences::GetBool("dom.serviceWorkers.testing.enabled")) {
|
|
|
|
bool isHttps;
|
|
|
|
rv = documentURI->SchemeIs("https", &isHttps);
|
|
|
|
if (NS_FAILED(rv) || !isHttps) {
|
|
|
|
NS_WARNING("ServiceWorker registration from insecure websites is not allowed.");
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> documentPrincipal;
|
|
|
|
if (window->GetExtantDoc()) {
|
|
|
|
documentPrincipal = window->GetExtantDoc()->NodePrincipal();
|
|
|
|
} else {
|
|
|
|
documentPrincipal = do_CreateInstance("@mozilla.org/nullprincipal;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scriptURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(scriptURI), aScriptURL, nullptr, documentURI);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
// Data URLs are not allowed.
|
2013-11-19 15:15:02 -08:00
|
|
|
rv = documentPrincipal->CheckMayLoad(scriptURI, true /* report */,
|
2014-06-11 09:12:56 -07:00
|
|
|
false /* allowIfInheritsPrincipal */);
|
2013-11-19 15:15:02 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scopeURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(scopeURI), aScope, nullptr, documentURI);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = documentPrincipal->CheckMayLoad(scopeURI, true /* report */,
|
|
|
|
false /* allowIfInheritsPrinciple */);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString cleanedScope;
|
2014-06-17 13:03:11 -07:00
|
|
|
rv = scopeURI->GetSpecIgnoringRef(cleanedScope);
|
2013-11-19 15:15:02 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> registerRunnable =
|
|
|
|
new RegisterRunnable(window, cleanedScope, scriptURI, promise);
|
|
|
|
promise.forget(aPromise);
|
|
|
|
return NS_DispatchToCurrentThread(registerRunnable);
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
void
|
|
|
|
ServiceWorkerManager::RejectUpdatePromiseObservers(ServiceWorkerRegistration* aRegistration,
|
|
|
|
nsresult aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aRegistration->HasUpdatePromise());
|
|
|
|
aRegistration->mUpdatePromise->RejectAllPromises(aRv);
|
|
|
|
aRegistration->mUpdatePromise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update() does not return the Promise that the spec says it should. Callers
|
|
|
|
* may access the registration's (new) Promise after calling this method.
|
|
|
|
*/
|
2013-11-19 15:15:02 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerManager::Update(ServiceWorkerRegistration* aRegistration,
|
|
|
|
nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2014-06-11 09:12:56 -07:00
|
|
|
if (aRegistration->HasUpdatePromise()) {
|
|
|
|
NS_WARNING("Already had a UpdatePromise. Aborting that one!");
|
|
|
|
RejectUpdatePromiseObservers(aRegistration, NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
MOZ_ASSERT(aRegistration->mUpdateInstance);
|
|
|
|
aRegistration->mUpdateInstance->Abort();
|
|
|
|
aRegistration->mUpdateInstance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRegistration->mInstallingWorker.IsValid()) {
|
|
|
|
// FIXME(nsm): Terminate the worker. We still haven't figured out worker
|
|
|
|
// instance ownership when not associated with a window, so let's wait on
|
|
|
|
// this.
|
|
|
|
// FIXME(nsm): We should be setting the state on the actual worker
|
|
|
|
// instance.
|
|
|
|
// FIXME(nsm): Fire "statechange" on installing worker instance.
|
|
|
|
aRegistration->mInstallingWorker.Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
aRegistration->mUpdatePromise = new UpdatePromise();
|
|
|
|
// FIXME(nsm): Bug 931249. If we don't need to fetch & install, resolve
|
|
|
|
// promise and skip this.
|
|
|
|
// FIXME(nsm): Bug 931249. Force cache update if > 1 day.
|
|
|
|
|
|
|
|
aRegistration->mUpdateInstance =
|
|
|
|
new ServiceWorkerUpdateInstance(aRegistration, aWindow);
|
|
|
|
aRegistration->mUpdateInstance->Update();
|
|
|
|
|
2013-11-19 15:15:02 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we return an error, ServiceWorkerContainer will reject the Promise.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerManager::Unregister(nsIDOMWindow* aWindow, const nsAString& aScope,
|
|
|
|
nsISupports** aPromise)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
// XXXnsm Don't allow chrome callers for now.
|
|
|
|
MOZ_ASSERT(!nsContentUtils::IsCallerChrome());
|
|
|
|
|
|
|
|
// FIXME(nsm): Same bug, different patch.
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<ServiceWorkerManager>
|
|
|
|
ServiceWorkerManager::GetInstance()
|
|
|
|
{
|
2014-06-11 09:12:56 -07:00
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm =
|
|
|
|
do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
|
2013-11-19 15:15:02 -08:00
|
|
|
nsRefPtr<ServiceWorkerManager> concrete = do_QueryObject(swm);
|
|
|
|
return concrete.forget();
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:12:56 -07:00
|
|
|
void
|
|
|
|
ServiceWorkerManager::ResolveRegisterPromises(ServiceWorkerRegistration* aRegistration,
|
|
|
|
const nsACString& aWorkerScriptSpec)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aRegistration->HasUpdatePromise());
|
|
|
|
if (aRegistration->mUpdatePromise->IsRejected()) {
|
|
|
|
aRegistration->mUpdatePromise = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRegistration->mUpdatePromise->ResolveAllPromises(aWorkerScriptSpec,
|
|
|
|
aRegistration->mScope);
|
|
|
|
aRegistration->mUpdatePromise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must NS_Free() aString
|
|
|
|
void
|
|
|
|
ServiceWorkerManager::FinishFetch(ServiceWorkerRegistration* aRegistration,
|
|
|
|
nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(aRegistration->HasUpdatePromise());
|
|
|
|
MOZ_ASSERT(aRegistration->mUpdateInstance);
|
|
|
|
aRegistration->mUpdateInstance = nullptr;
|
|
|
|
if (aRegistration->mUpdatePromise->IsRejected()) {
|
|
|
|
aRegistration->mUpdatePromise = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have skipped Steps 3-8.3 of the Update algorithm here!
|
|
|
|
|
|
|
|
nsRefPtr<ServiceWorker> worker;
|
|
|
|
nsresult rv = CreateServiceWorkerForWindow(aWindow,
|
|
|
|
aRegistration->mScriptSpec,
|
|
|
|
aRegistration->mScope,
|
|
|
|
getter_AddRefs(worker));
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
RejectUpdatePromiseObservers(aRegistration, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveRegisterPromises(aRegistration, aRegistration->mScriptSpec);
|
|
|
|
|
|
|
|
ServiceWorkerInfo info(aRegistration->mScriptSpec);
|
|
|
|
Install(aRegistration, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerManager::Install(ServiceWorkerRegistration* aRegistration,
|
|
|
|
ServiceWorkerInfo aServiceWorkerInfo)
|
|
|
|
{
|
2014-07-01 20:29:08 -07:00
|
|
|
// FIXME(nsm): Same bug, different patch.
|
2014-06-11 09:12:56 -07:00
|
|
|
}
|
|
|
|
|
2013-11-19 15:15:02 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ServiceWorkerManager::CreateServiceWorkerForWindow(nsPIDOMWindow* aWindow,
|
|
|
|
const nsACString& aScriptSpec,
|
|
|
|
const nsACString& aScope,
|
|
|
|
ServiceWorker** aServiceWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
RuntimeService* rs = RuntimeService::GetOrCreateService();
|
|
|
|
nsRefPtr<ServiceWorker> serviceWorker;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> sgo = do_QueryInterface(aWindow);
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> jsGlobal(cx, sgo->GetGlobalJSObject());
|
|
|
|
JSAutoCompartment ac(cx, jsGlobal);
|
|
|
|
|
|
|
|
GlobalObject global(cx, jsGlobal);
|
|
|
|
nsresult rv = rs->CreateServiceWorker(global,
|
|
|
|
NS_ConvertUTF8toUTF16(aScriptSpec),
|
|
|
|
aScope,
|
|
|
|
getter_AddRefs(serviceWorker));
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceWorker.forget(aServiceWorker);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|