2015-06-04 18:51:57 +00: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 "ServiceWorkerManagerParent.h"
|
|
|
|
#include "ServiceWorkerManagerService.h"
|
2017-03-28 09:48:38 +00:00
|
|
|
#include "ServiceWorkerUpdaterParent.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerRegistrar.h"
|
2015-06-04 18:51:57 +00:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2016-08-23 04:09:32 +00:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2015-06-04 18:51:57 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint64_t sServiceWorkerManagerParentID = 0;
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class RegisterServiceWorkerCallback final : public Runnable {
|
2015-06-04 18:51:57 +00:00
|
|
|
public:
|
|
|
|
RegisterServiceWorkerCallback(const ServiceWorkerRegistrationData& aData,
|
|
|
|
uint64_t aParentID)
|
2018-01-26 21:08:59 +00:00
|
|
|
: Runnable("dom::RegisterServiceWorkerCallback"),
|
2017-06-12 19:34:10 +00:00
|
|
|
mData(aData),
|
2015-06-04 18:51:57 +00:00
|
|
|
mParentID(aParentID) {
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2015-06-04 18:51:57 +00:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<dom::ServiceWorkerRegistrar> service =
|
2015-06-04 18:51:57 +00:00
|
|
|
dom::ServiceWorkerRegistrar::Get();
|
2017-01-13 15:39:28 +00:00
|
|
|
|
|
|
|
// Shutdown during the process of trying to update the registrar. Give
|
|
|
|
// up on this modification.
|
|
|
|
if (!service) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-06-04 18:51:57 +00:00
|
|
|
|
|
|
|
service->RegisterServiceWorker(mData);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ServiceWorkerManagerService> managerService =
|
2015-06-04 18:51:57 +00:00
|
|
|
ServiceWorkerManagerService::Get();
|
|
|
|
if (managerService) {
|
|
|
|
managerService->PropagateRegistration(mParentID, mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ServiceWorkerRegistrationData mData;
|
|
|
|
const uint64_t mParentID;
|
|
|
|
};
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class UnregisterServiceWorkerCallback final : public Runnable {
|
2015-06-04 18:51:57 +00:00
|
|
|
public:
|
|
|
|
UnregisterServiceWorkerCallback(const PrincipalInfo& aPrincipalInfo,
|
2016-01-25 06:49:00 +00:00
|
|
|
const nsString& aScope, uint64_t aParentID)
|
2018-01-26 21:08:59 +00:00
|
|
|
: Runnable("dom::UnregisterServiceWorkerCallback"),
|
2017-06-12 19:34:10 +00:00
|
|
|
mPrincipalInfo(aPrincipalInfo),
|
2015-06-04 18:51:57 +00:00
|
|
|
mScope(aScope),
|
2016-01-25 06:49:00 +00:00
|
|
|
mParentID(aParentID) {
|
2015-06-04 18:51:57 +00:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2015-06-04 18:51:57 +00:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<dom::ServiceWorkerRegistrar> service =
|
2015-06-04 18:51:57 +00:00
|
|
|
dom::ServiceWorkerRegistrar::Get();
|
2017-01-13 15:39:28 +00:00
|
|
|
|
|
|
|
// Shutdown during the process of trying to update the registrar. Give
|
|
|
|
// up on this modification.
|
|
|
|
if (!service) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-06-04 18:51:57 +00:00
|
|
|
|
|
|
|
service->UnregisterServiceWorker(mPrincipalInfo,
|
|
|
|
NS_ConvertUTF16toUTF8(mScope));
|
2016-01-25 06:49:00 +00:00
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManagerService> managerService =
|
|
|
|
ServiceWorkerManagerService::Get();
|
|
|
|
if (managerService) {
|
|
|
|
managerService->PropagateUnregister(mParentID, mPrincipalInfo, mScope);
|
|
|
|
}
|
|
|
|
|
2015-06-04 18:51:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const PrincipalInfo mPrincipalInfo;
|
|
|
|
nsString mScope;
|
2016-01-25 06:49:00 +00:00
|
|
|
uint64_t mParentID;
|
2015-06-04 18:51:57 +00:00
|
|
|
};
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class CheckPrincipalWithCallbackRunnable final : public Runnable {
|
2015-06-04 18:51:57 +00:00
|
|
|
public:
|
|
|
|
CheckPrincipalWithCallbackRunnable(already_AddRefed<ContentParent> aParent,
|
|
|
|
const PrincipalInfo& aPrincipalInfo,
|
2016-04-26 00:23:21 +00:00
|
|
|
Runnable* aCallback)
|
2018-01-26 21:08:59 +00:00
|
|
|
: Runnable("dom::CheckPrincipalWithCallbackRunnable"),
|
2017-06-12 19:34:10 +00:00
|
|
|
mContentParent(aParent),
|
2015-06-04 18:51:57 +00:00
|
|
|
mPrincipalInfo(aPrincipalInfo),
|
|
|
|
mCallback(aCallback),
|
2017-06-01 20:42:05 +00:00
|
|
|
mBackgroundEventTarget(GetCurrentThreadEventTarget()) {
|
2015-06-04 18:51:57 +00:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mContentParent);
|
|
|
|
MOZ_ASSERT(mCallback);
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mBackgroundEventTarget);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override {
|
2015-06-04 18:51:57 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mContentParent = nullptr;
|
|
|
|
|
2017-06-01 20:42:05 +00:00
|
|
|
mBackgroundEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
2015-06-04 18:51:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mCallback->Run();
|
|
|
|
mCallback = nullptr;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ContentParent> mContentParent;
|
2015-06-04 18:51:57 +00:00
|
|
|
PrincipalInfo mPrincipalInfo;
|
2016-04-26 00:23:21 +00:00
|
|
|
RefPtr<Runnable> mCallback;
|
2017-06-01 20:42:05 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mBackgroundEventTarget;
|
2015-06-04 18:51:57 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2015-06-04 18:51:57 +00:00
|
|
|
|
|
|
|
ServiceWorkerManagerParent::ServiceWorkerManagerParent()
|
|
|
|
: mService(ServiceWorkerManagerService::GetOrCreate()),
|
|
|
|
mID(++sServiceWorkerManagerParentID) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mService->RegisterActor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerManagerParent::~ServiceWorkerManagerParent() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvRegister(
|
|
|
|
const ServiceWorkerRegistrationData& aData) {
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// Basic validation.
|
|
|
|
if (aData.scope().IsEmpty() ||
|
|
|
|
aData.principal().type() == PrincipalInfo::TNullPrincipalInfo ||
|
|
|
|
aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<RegisterServiceWorkerCallback> callback =
|
2015-06-04 18:51:57 +00:00
|
|
|
new RegisterServiceWorkerCallback(aData, mID);
|
|
|
|
|
|
|
|
RefPtr<ContentParent> parent = BackgroundParent::GetContentParent(Manager());
|
|
|
|
|
|
|
|
// If the ContentParent is null we are dealing with a same-process actor.
|
|
|
|
if (!parent) {
|
|
|
|
callback->Run();
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
|
2015-06-04 18:51:57 +00:00
|
|
|
new CheckPrincipalWithCallbackRunnable(parent.forget(), aData.principal(),
|
|
|
|
callback);
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
2015-06-04 18:51:57 +00:00
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvUnregister(
|
|
|
|
const PrincipalInfo& aPrincipalInfo, const nsString& aScope) {
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// Basic validation.
|
|
|
|
if (aScope.IsEmpty() ||
|
|
|
|
aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo ||
|
|
|
|
aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<UnregisterServiceWorkerCallback> callback =
|
2016-01-25 06:49:00 +00:00
|
|
|
new UnregisterServiceWorkerCallback(aPrincipalInfo, aScope, mID);
|
2015-06-04 18:51:57 +00:00
|
|
|
|
|
|
|
RefPtr<ContentParent> parent = BackgroundParent::GetContentParent(Manager());
|
|
|
|
|
|
|
|
// If the ContentParent is null we are dealing with a same-process actor.
|
|
|
|
if (!parent) {
|
|
|
|
callback->Run();
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
|
2015-06-04 18:51:57 +00:00
|
|
|
new CheckPrincipalWithCallbackRunnable(parent.forget(), aPrincipalInfo,
|
|
|
|
callback);
|
2016-03-28 17:28:15 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
2015-06-04 18:51:57 +00:00
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 16:38:48 +00:00
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateSoftUpdate(
|
|
|
|
const OriginAttributes& aOriginAttributes, const nsString& aScope) {
|
2015-06-04 18:51:57 +00:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateSoftUpdate(mID, aOriginAttributes, aScope);
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateUnregister(
|
|
|
|
const PrincipalInfo& aPrincipalInfo, const nsString& aScope) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateUnregister(mID, aPrincipalInfo, aScope);
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:19:07 +00:00
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateRemove(
|
|
|
|
const nsCString& aHost) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-21 11:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateRemove(mID, aHost);
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-21 11:19:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:17:58 +00:00
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateRemoveAll() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-21 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateRemoveAll(mID);
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-21 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 18:51:57 +00:00
|
|
|
mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvShutdown() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mService->UnregisterActor(this);
|
|
|
|
mService = nullptr;
|
|
|
|
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << Send__delete__(this);
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2015-06-04 18:51:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 09:48:38 +00:00
|
|
|
PServiceWorkerUpdaterParent*
|
|
|
|
ServiceWorkerManagerParent::AllocPServiceWorkerUpdaterParent(
|
|
|
|
const OriginAttributes& aOriginAttributes, const nsCString& aScope) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
return new ServiceWorkerUpdaterParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult
|
|
|
|
ServiceWorkerManagerParent::RecvPServiceWorkerUpdaterConstructor(
|
|
|
|
PServiceWorkerUpdaterParent* aActor,
|
|
|
|
const OriginAttributes& aOriginAttributes, const nsCString& aScope) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return IPC_FAIL_NO_REASON(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->ProcessUpdaterActor(
|
|
|
|
static_cast<ServiceWorkerUpdaterParent*>(aActor), aOriginAttributes,
|
|
|
|
aScope, mID);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceWorkerManagerParent::DeallocPServiceWorkerUpdaterParent(
|
|
|
|
PServiceWorkerUpdaterParent* aActor) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
delete aActor;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-04 18:51:57 +00:00
|
|
|
void ServiceWorkerManagerParent::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (mService) {
|
|
|
|
// This object is about to be released and with it, also mService will be
|
|
|
|
// released too.
|
|
|
|
mService->UnregisterActor(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|