gecko-dev/dom/ipc/JSWindowActorChild.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 строки
4.8 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "mozilla/dom/JSWindowActorBinding.h"
#include "mozilla/dom/JSWindowActorChild.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/dom/WindowProxyHolder.h"
#include "mozilla/dom/MessageManagerBinding.h"
#include "mozilla/dom/BrowsingContext.h"
#include "nsGlobalWindowInner.h"
namespace mozilla {
namespace dom {
Bug 1541557: Part 1 - Use correct globals for JSWindowActors not in the shared JSM global. r=nika The current JSWindowActor code assumes that all actors will be created in the shared JSM global. This has a few problems: 1) For actors in other scopes, it enters the wrong compartment before decoding messages, which leads to a compartment checker error when trying to call message handlers. That could be fixed by just wrapping the result for the caller, but that would lead to other problems. Aside from the efficiency concerns of having to deal with cross-compartment wrappers, SpecialPowers in particular would not be able to pass the resulting objects to unprivileged scopes, since only SpecialPowers compartments have permissive CCWs enabled. 2) It also leads to the prototype objects for the actor instances being created in the shared JSM scope, even when the actors themselves are defined in other compartments. Again, aside from CCW efficiency issues, this prevents the SpecialPowers instances from being accessed by the unprivileged scopes that they're exposed to, since the prototype objects live in privileged scopes which don't have permissive CCWs enabled. This patch changes child actors to always create their objects in the global of their constructors. The parent objects are still created in the shared JSM global, but they now wrap values for the appropriate compartment before trying to call message handlers. Differential Revision: https://phabricator.services.mozilla.com/D35051 --HG-- extra : rebase_source : 436ce516080812680d1433bf3ecbd12f91d88165 extra : source : 61fa2745733f3631488a3ecccc144823683b7b6d
2019-06-13 02:06:40 +03:00
JSWindowActorChild::JSWindowActorChild(nsIGlobalObject* aGlobal)
: mGlobal(aGlobal ? aGlobal
: xpc::NativeGlobal(xpc::PrivilegedJunkScope())) {}
JSWindowActorChild::~JSWindowActorChild() { MOZ_ASSERT(!mManager); }
JSObject* JSWindowActorChild::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return JSWindowActorChild_Binding::Wrap(aCx, this, aGivenProto);
}
WindowGlobalChild* JSWindowActorChild::GetManager() const { return mManager; }
void JSWindowActorChild::Init(const nsAString& aName,
WindowGlobalChild* aManager) {
MOZ_ASSERT(!mManager, "Cannot Init() a JSWindowActorChild twice!");
SetName(aName);
mManager = aManager;
InvokeCallback(CallbackFunction::ActorCreated);
}
namespace {
class AsyncMessageToParent : public Runnable {
public:
AsyncMessageToParent(const JSWindowActorMessageMeta& aMetadata,
ipc::StructuredCloneData&& aData,
WindowGlobalChild* aManager)
: mozilla::Runnable("WindowGlobalParent::HandleAsyncMessage"),
mMetadata(aMetadata),
mData(std::move(aData)),
mManager(aManager) {}
NS_IMETHOD Run() override {
MOZ_ASSERT(NS_IsMainThread(), "Should be called on the main thread.");
RefPtr<WindowGlobalParent> parent = mManager->GetParentActor();
if (parent) {
parent->ReceiveRawMessage(mMetadata, std::move(mData));
}
return NS_OK;
}
private:
JSWindowActorMessageMeta mMetadata;
ipc::StructuredCloneData mData;
RefPtr<WindowGlobalChild> mManager;
};
} // anonymous namespace
void JSWindowActorChild::SendRawMessage(const JSWindowActorMessageMeta& aMeta,
ipc::StructuredCloneData&& aData,
ErrorResult& aRv) {
if (NS_WARN_IF(!mCanSend || !mManager || !mManager->CanSend())) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
if (mManager->IsInProcess()) {
nsCOMPtr<nsIRunnable> runnable =
new AsyncMessageToParent(aMeta, std::move(aData), mManager);
NS_DispatchToMainThread(runnable.forget());
return;
}
// Cross-process case - send data over WindowGlobalChild to other side.
ClonedMessageData msgData;
ContentChild* cc = ContentChild::GetSingleton();
if (NS_WARN_IF(!aData.BuildClonedMessageDataForChild(cc, msgData))) {
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
return;
}
if (NS_WARN_IF(!mManager->SendRawMessage(aMeta, msgData))) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return;
}
}
Document* JSWindowActorChild::GetDocument(ErrorResult& aRv) {
if (!mManager) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;
}
nsGlobalWindowInner* window = mManager->WindowGlobal();
return window ? window->GetDocument() : nullptr;
}
BrowsingContext* JSWindowActorChild::GetBrowsingContext(ErrorResult& aRv) {
if (!mManager) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;
}
return mManager->BrowsingContext();
}
nsIDocShell* JSWindowActorChild::GetDocShell(ErrorResult& aRv) {
if (BrowsingContext* bc = GetBrowsingContext(aRv)) {
return bc->GetDocShell();
}
return nullptr;
}
Nullable<WindowProxyHolder> JSWindowActorChild::GetContentWindow(
ErrorResult& aRv) {
if (BrowsingContext* bc = GetBrowsingContext(aRv)) {
return WindowProxyHolder(bc);
}
return nullptr;
}
void JSWindowActorChild::StartDestroy() {
JSWindowActor::StartDestroy();
mCanSend = false;
}
void JSWindowActorChild::AfterDestroy() {
JSWindowActor::AfterDestroy();
mManager = nullptr;
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorChild, JSWindowActor, mManager)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(JSWindowActorChild,
JSWindowActor)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorChild)
NS_INTERFACE_MAP_END_INHERITING(JSWindowActor)
NS_IMPL_ADDREF_INHERITED(JSWindowActorChild, JSWindowActor)
NS_IMPL_RELEASE_INHERITED(JSWindowActorChild, JSWindowActor)
} // namespace dom
} // namespace mozilla