2015-06-04 21:51:57 +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 "ServiceWorkerManagerParent.h"
|
|
|
|
#include "ServiceWorkerManagerService.h"
|
|
|
|
#include "mozilla/AppProcessChecker.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerRegistrar.h"
|
2015-06-04 21:51:57 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-06-04 21:51:57 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
namespace workers {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint64_t sServiceWorkerManagerParentID = 0;
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class RegisterServiceWorkerCallback final : public Runnable
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
RegisterServiceWorkerCallback(const ServiceWorkerRegistrationData& aData,
|
|
|
|
uint64_t aParentID)
|
|
|
|
: mData(aData)
|
|
|
|
, mParentID(aParentID)
|
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::ServiceWorkerRegistrar> service =
|
2015-06-04 21:51:57 +03:00
|
|
|
dom::ServiceWorkerRegistrar::Get();
|
|
|
|
MOZ_ASSERT(service);
|
|
|
|
|
|
|
|
service->RegisterServiceWorker(mData);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerManagerService> managerService =
|
2015-06-04 21:51:57 +03:00
|
|
|
ServiceWorkerManagerService::Get();
|
|
|
|
if (managerService) {
|
|
|
|
managerService->PropagateRegistration(mParentID, mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ServiceWorkerRegistrationData mData;
|
|
|
|
const uint64_t mParentID;
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class UnregisterServiceWorkerCallback final : public Runnable
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnregisterServiceWorkerCallback(const PrincipalInfo& aPrincipalInfo,
|
2016-01-25 09:49:00 +03:00
|
|
|
const nsString& aScope,
|
|
|
|
uint64_t aParentID)
|
2015-06-04 21:51:57 +03:00
|
|
|
: mPrincipalInfo(aPrincipalInfo)
|
|
|
|
, mScope(aScope)
|
2016-01-25 09:49:00 +03:00
|
|
|
, mParentID(aParentID)
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::ServiceWorkerRegistrar> service =
|
2015-06-04 21:51:57 +03:00
|
|
|
dom::ServiceWorkerRegistrar::Get();
|
|
|
|
MOZ_ASSERT(service);
|
|
|
|
|
|
|
|
service->UnregisterServiceWorker(mPrincipalInfo,
|
|
|
|
NS_ConvertUTF16toUTF8(mScope));
|
2016-01-25 09:49:00 +03:00
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManagerService> managerService =
|
|
|
|
ServiceWorkerManagerService::Get();
|
|
|
|
if (managerService) {
|
|
|
|
managerService->PropagateUnregister(mParentID, mPrincipalInfo,
|
|
|
|
mScope);
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const PrincipalInfo mPrincipalInfo;
|
|
|
|
nsString mScope;
|
2016-01-25 09:49:00 +03:00
|
|
|
uint64_t mParentID;
|
2015-06-04 21:51:57 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class CheckPrincipalWithCallbackRunnable final : public Runnable
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CheckPrincipalWithCallbackRunnable(already_AddRefed<ContentParent> aParent,
|
|
|
|
const PrincipalInfo& aPrincipalInfo,
|
2016-04-26 03:23:21 +03:00
|
|
|
Runnable* aCallback)
|
2015-06-04 21:51:57 +03:00
|
|
|
: mContentParent(aParent)
|
|
|
|
, mPrincipalInfo(aPrincipalInfo)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
, mBackgroundThread(NS_GetCurrentThread())
|
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mContentParent);
|
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
MOZ_ASSERT(mBackgroundThread);
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-06-04 21:51:57 +03:00
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(mPrincipalInfo);
|
|
|
|
AssertAppPrincipal(mContentParent, principal);
|
|
|
|
mContentParent = nullptr;
|
|
|
|
|
|
|
|
mBackgroundThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mCallback->Run();
|
|
|
|
mCallback = nullptr;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentParent> mContentParent;
|
2015-06-04 21:51:57 +03:00
|
|
|
PrincipalInfo mPrincipalInfo;
|
2016-04-26 03:23:21 +03:00
|
|
|
RefPtr<Runnable> mCallback;
|
2015-06-04 21:51:57 +03:00
|
|
|
nsCOMPtr<nsIThread> mBackgroundThread;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-04 21:51:57 +03:00
|
|
|
|
|
|
|
ServiceWorkerManagerParent::ServiceWorkerManagerParent()
|
|
|
|
: mService(ServiceWorkerManagerService::GetOrCreate())
|
|
|
|
, mID(++sServiceWorkerManagerParentID)
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mService->RegisterActor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerManagerParent::~ServiceWorkerManagerParent()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvRegister(
|
|
|
|
const ServiceWorkerRegistrationData& aData)
|
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// Basic validation.
|
|
|
|
if (aData.scope().IsEmpty() ||
|
|
|
|
aData.principal().type() == PrincipalInfo::TNullPrincipalInfo ||
|
|
|
|
aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RegisterServiceWorkerCallback> callback =
|
2015-06-04 21:51:57 +03:00
|
|
|
new RegisterServiceWorkerCallback(aData, mID);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentParent> parent =
|
2015-06-04 21:51:57 +03:00
|
|
|
BackgroundParent::GetContentParent(Manager());
|
|
|
|
|
|
|
|
// If the ContentParent is null we are dealing with a same-process actor.
|
|
|
|
if (!parent) {
|
|
|
|
callback->Run();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
|
2015-06-04 21:51:57 +03:00
|
|
|
new CheckPrincipalWithCallbackRunnable(parent.forget(), aData.principal(),
|
|
|
|
callback);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
2015-06-04 21:51:57 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvUnregister(const PrincipalInfo& aPrincipalInfo,
|
|
|
|
const nsString& aScope)
|
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// Basic validation.
|
|
|
|
if (aScope.IsEmpty() ||
|
|
|
|
aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo ||
|
|
|
|
aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnregisterServiceWorkerCallback> callback =
|
2016-01-25 09:49:00 +03:00
|
|
|
new UnregisterServiceWorkerCallback(aPrincipalInfo, aScope, mID);
|
2015-06-04 21:51:57 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentParent> parent =
|
2015-06-04 21:51:57 +03:00
|
|
|
BackgroundParent::GetContentParent(Manager());
|
|
|
|
|
|
|
|
// If the ContentParent is null we are dealing with a same-process actor.
|
|
|
|
if (!parent) {
|
|
|
|
callback->Run();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
|
2015-06-04 21:51:57 +03:00
|
|
|
new CheckPrincipalWithCallbackRunnable(parent.forget(), aPrincipalInfo,
|
|
|
|
callback);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
2015-06-04 21:51:57 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-11-03 04:50:54 +03:00
|
|
|
ServiceWorkerManagerParent::RecvPropagateSoftUpdate(const PrincipalOriginAttributes& aOriginAttributes,
|
2015-06-04 21:51:57 +03:00
|
|
|
const nsString& aScope)
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateSoftUpdate(mID, aOriginAttributes, aScope);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvPropagateUnregister(const PrincipalInfo& aPrincipalInfo,
|
|
|
|
const nsString& aScope)
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateUnregister(mID, aPrincipalInfo, aScope);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-21 14:19:07 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvPropagateRemove(const nsCString& aHost)
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateRemove(mID, aHost);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-21 14:17:58 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvPropagateRemoveAll()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->PropagateRemoveAll(mID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:51:57 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerManagerParent::RecvShutdown()
|
|
|
|
{
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->UnregisterActor(this);
|
|
|
|
mService = nullptr;
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this);
|
2015-06-04 21:51:57 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 18:25:42 +03:00
|
|
|
} // namespace workers
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|