2018-08-30 01:18:04 +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/. */
|
|
|
|
|
2020-06-25 20:50:51 +03:00
|
|
|
#include "mozilla/dom/InProcessParent.h"
|
|
|
|
#include "mozilla/dom/InProcessChild.h"
|
2020-06-25 23:35:18 +03:00
|
|
|
#include "mozilla/dom/JSProcessActorBinding.h"
|
2018-08-30 01:21:25 +03:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "mozilla/Services.h"
|
2018-08-30 01:18:04 +03:00
|
|
|
|
2020-06-25 20:50:51 +03:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
2018-08-30 01:18:04 +03:00
|
|
|
// This file contains the implementation of core InProcess lifecycle management
|
|
|
|
// facilities.
|
|
|
|
|
|
|
|
namespace mozilla {
|
2020-06-25 20:50:51 +03:00
|
|
|
namespace dom {
|
2018-08-30 01:18:04 +03:00
|
|
|
|
|
|
|
StaticRefPtr<InProcessParent> InProcessParent::sSingleton;
|
|
|
|
StaticRefPtr<InProcessChild> InProcessChild::sSingleton;
|
|
|
|
bool InProcessParent::sShutdown = false;
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// InProcess actor lifecycle management //
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
InProcessChild* InProcessChild::Singleton() {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sSingleton) {
|
|
|
|
InProcessParent::Startup();
|
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
InProcessParent* InProcessParent::Singleton() {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sSingleton) {
|
|
|
|
InProcessParent::Startup();
|
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
void InProcessParent::Startup() {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (sShutdown) {
|
|
|
|
NS_WARNING("Could not get in-process actor while shutting down!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
sShutdown = true;
|
|
|
|
NS_WARNING("Failed to get nsIObserverService for in-process actor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<InProcessParent> parent = new InProcessParent();
|
|
|
|
RefPtr<InProcessChild> child = new InProcessChild();
|
|
|
|
|
|
|
|
// Observe the shutdown event to close & clean up after ourselves.
|
|
|
|
nsresult rv = obs->AddObserver(parent, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the two actors
|
|
|
|
if (!child->OpenOnSameThread(parent->GetIPCChannel(), ChildSide)) {
|
|
|
|
MOZ_CRASH("Failed to open InProcessChild!");
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->SetOtherProcessId(base::GetCurrentProcId());
|
|
|
|
|
|
|
|
// Stash global references to fetch the other side of the reference.
|
2020-02-13 17:38:48 +03:00
|
|
|
InProcessParent::sSingleton = std::move(parent);
|
|
|
|
InProcessChild::sSingleton = std::move(child);
|
2018-08-30 01:18:04 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
void InProcessParent::Shutdown() {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sSingleton || sShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sShutdown = true;
|
|
|
|
|
|
|
|
RefPtr<InProcessParent> parent = sSingleton;
|
|
|
|
InProcessParent::sSingleton = nullptr;
|
|
|
|
InProcessChild::sSingleton = nullptr;
|
|
|
|
|
|
|
|
// Calling `Close` on the actor will cause the `Dealloc` methods to be called,
|
|
|
|
// freeing the remaining references.
|
|
|
|
parent->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessParent::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID));
|
|
|
|
InProcessParent::Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InProcessParent::ActorDestroy(ActorDestroyReason aWhy) {
|
2020-07-08 17:22:22 +03:00
|
|
|
JSActorDidDestroy();
|
2018-08-30 01:18:04 +03:00
|
|
|
InProcessParent::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InProcessChild::ActorDestroy(ActorDestroyReason aWhy) {
|
2020-07-08 17:22:22 +03:00
|
|
|
JSActorDidDestroy();
|
2018-08-30 01:18:04 +03:00
|
|
|
InProcessParent::Shutdown();
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:35:18 +03:00
|
|
|
/////////////////////////
|
|
|
|
// nsIDOMProcessParent //
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessParent::GetChildID(uint64_t* aChildID) {
|
|
|
|
*aChildID = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:09:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessParent::GetOsPid(int32_t* aOsPid) {
|
|
|
|
// InProcessParent always run in the parent process,
|
|
|
|
// so we can return the current process id.
|
|
|
|
*aOsPid = base::GetCurrentProcId();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:35:18 +03:00
|
|
|
NS_IMETHODIMP
|
2020-07-07 00:17:17 +03:00
|
|
|
InProcessParent::GetActor(const nsACString& aName, JSContext* aCx,
|
2020-06-25 23:35:18 +03:00
|
|
|
JSProcessActorParent** aActor) {
|
2020-07-08 17:22:22 +03:00
|
|
|
ErrorResult error;
|
|
|
|
RefPtr<JSProcessActorParent> actor =
|
|
|
|
JSActorManager::GetActor(aName, error).downcast<JSProcessActorParent>();
|
2020-07-07 00:17:17 +03:00
|
|
|
if (error.MaybeSetPendingException(aCx)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2020-07-08 04:09:27 +03:00
|
|
|
}
|
2020-07-08 17:22:22 +03:00
|
|
|
actor.forget(aActor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-07-08 04:09:27 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
already_AddRefed<JSActor> InProcessParent::InitJSActor(
|
|
|
|
JS::HandleObject aMaybeActor, const nsACString& aName, ErrorResult& aRv) {
|
2020-06-25 23:35:18 +03:00
|
|
|
RefPtr<JSProcessActorParent> actor;
|
2020-07-08 17:22:22 +03:00
|
|
|
if (aMaybeActor.get()) {
|
|
|
|
aRv = UNWRAP_OBJECT(JSProcessActorParent, aMaybeActor.get(), actor);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
actor = new JSProcessActorParent();
|
2020-06-25 23:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(!actor->Manager(),
|
|
|
|
"mManager was already initialized once!");
|
|
|
|
actor->Init(aName, this);
|
2020-07-08 17:22:22 +03:00
|
|
|
return actor.forget();
|
2020-06-25 23:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessParent::GetCanSend(bool* aCanSend) {
|
|
|
|
*aCanSend = CanSend();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentParent* InProcessParent::AsContentParent() { return nullptr; }
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
// nsIDOMProcessChild //
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessChild::GetChildID(uint64_t* aChildID) {
|
|
|
|
*aChildID = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-07-07 00:17:17 +03:00
|
|
|
InProcessChild::GetActor(const nsACString& aName, JSContext* aCx,
|
2020-06-25 23:35:18 +03:00
|
|
|
JSProcessActorChild** aActor) {
|
2020-07-08 17:22:22 +03:00
|
|
|
ErrorResult error;
|
|
|
|
RefPtr<JSProcessActorChild> actor =
|
|
|
|
JSActorManager::GetActor(aName, error).downcast<JSProcessActorChild>();
|
2020-07-07 00:17:17 +03:00
|
|
|
if (error.MaybeSetPendingException(aCx)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2020-07-08 04:09:27 +03:00
|
|
|
}
|
2020-07-08 17:22:22 +03:00
|
|
|
actor.forget(aActor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2020-07-08 04:09:27 +03:00
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
already_AddRefed<JSActor> InProcessChild::InitJSActor(
|
|
|
|
JS::HandleObject aMaybeActor, const nsACString& aName, ErrorResult& aRv) {
|
2020-06-25 23:35:18 +03:00
|
|
|
RefPtr<JSProcessActorChild> actor;
|
2020-07-08 17:22:22 +03:00
|
|
|
if (aMaybeActor.get()) {
|
|
|
|
aRv = UNWRAP_OBJECT(JSProcessActorChild, aMaybeActor.get(), actor);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
actor = new JSProcessActorChild();
|
2020-06-25 23:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(!actor->Manager(),
|
|
|
|
"mManager was already initialized once!");
|
|
|
|
actor->Init(aName, this);
|
2020-07-08 17:22:22 +03:00
|
|
|
return actor.forget();
|
2020-06-25 23:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InProcessChild::GetCanSend(bool* aCanSend) {
|
|
|
|
*aCanSend = CanSend();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentChild* InProcessChild::AsContentChild() { return nullptr; }
|
|
|
|
|
2018-08-30 01:18:04 +03:00
|
|
|
////////////////////////////////
|
|
|
|
// In-Process Actor Utilities //
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
// Helper method for implementing ParentActorFor and ChildActorFor.
|
|
|
|
static IProtocol* GetOtherInProcessActor(IProtocol* aActor) {
|
|
|
|
MOZ_ASSERT(aActor->GetSide() != UnknownSide, "bad unknown side");
|
|
|
|
|
|
|
|
// Discover the manager of aActor which is PInProcess.
|
|
|
|
IProtocol* current = aActor;
|
|
|
|
while (current) {
|
2019-05-24 23:21:35 +03:00
|
|
|
if (current->GetProtocolId() == PInProcessMsgStart) {
|
2018-08-30 01:18:04 +03:00
|
|
|
break; // Found the correct actor.
|
|
|
|
}
|
|
|
|
current = current->Manager();
|
|
|
|
}
|
|
|
|
if (!current) {
|
|
|
|
return nullptr; // Not a PInProcess actor, return |nullptr|
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(current->GetSide() == aActor->GetSide(), "side changed?");
|
|
|
|
MOZ_ASSERT_IF(aActor->GetSide() == ParentSide,
|
|
|
|
current == InProcessParent::Singleton());
|
|
|
|
MOZ_ASSERT_IF(aActor->GetSide() == ChildSide,
|
|
|
|
current == InProcessChild::Singleton());
|
|
|
|
|
|
|
|
// Check whether this is InProcessParent or InProcessChild, and get the other
|
|
|
|
// side's toplevel actor.
|
|
|
|
IProtocol* otherRoot = nullptr;
|
|
|
|
if (aActor->GetSide() == ParentSide) {
|
|
|
|
otherRoot = InProcessChild::Singleton();
|
|
|
|
} else {
|
|
|
|
otherRoot = InProcessParent::Singleton();
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!otherRoot)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the actor on the other side, and return it.
|
|
|
|
IProtocol* otherActor = otherRoot->Lookup(aActor->Id());
|
|
|
|
if (otherActor) {
|
|
|
|
MOZ_ASSERT(otherActor->GetSide() != UnknownSide, "bad unknown side");
|
|
|
|
MOZ_ASSERT(otherActor->GetSide() != aActor->GetSide(), "Wrong side!");
|
2019-05-24 23:21:35 +03:00
|
|
|
MOZ_ASSERT(otherActor->GetProtocolId() == aActor->GetProtocolId(),
|
2018-08-30 01:18:04 +03:00
|
|
|
"Wrong type of protocol!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return otherActor;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
IProtocol* InProcessParent::ChildActorFor(IProtocol* aActor) {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(aActor && aActor->GetSide() == ParentSide);
|
|
|
|
return GetOtherInProcessActor(aActor);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:08:36 +03:00
|
|
|
/* static */
|
|
|
|
IProtocol* InProcessChild::ParentActorFor(IProtocol* aActor) {
|
2018-08-30 01:18:04 +03:00
|
|
|
MOZ_ASSERT(aActor && aActor->GetSide() == ChildSide);
|
|
|
|
return GetOtherInProcessActor(aActor);
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:35:18 +03:00
|
|
|
NS_IMPL_ISUPPORTS(InProcessParent, nsIDOMProcessParent, nsIObserver)
|
|
|
|
NS_IMPL_ISUPPORTS(InProcessChild, nsIDOMProcessChild)
|
2019-08-08 19:46:20 +03:00
|
|
|
|
2020-06-25 20:50:51 +03:00
|
|
|
} // namespace dom
|
2018-08-30 01:18:04 +03:00
|
|
|
} // namespace mozilla
|