Backed out changeset e95ff11012c5 (bug 1910360) as requested for causing Android...
[gecko.git] / dom / ipc / jsactor / JSWindowActorParent.cpp
blob0bb82a9b8ec68f29b08cded32ec7c65d49c0c06f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/JSWindowActorBinding.h"
8 #include "mozilla/dom/JSWindowActorParent.h"
9 #include "mozilla/dom/BrowserParent.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/WindowGlobalChild.h"
12 #include "mozilla/dom/WindowGlobalParent.h"
13 #include "mozilla/dom/MessageManagerBinding.h"
15 namespace mozilla::dom {
17 JSWindowActorParent::~JSWindowActorParent() { MOZ_ASSERT(!mManager); }
19 JSObject* JSWindowActorParent::WrapObject(JSContext* aCx,
20 JS::Handle<JSObject*> aGivenProto) {
21 return JSWindowActorParent_Binding::Wrap(aCx, this, aGivenProto);
24 WindowGlobalParent* JSWindowActorParent::GetManager() const { return mManager; }
26 WindowContext* JSWindowActorParent::GetWindowContext() const {
27 return mManager;
30 void JSWindowActorParent::Init(const nsACString& aName,
31 WindowGlobalParent* aManager) {
32 MOZ_ASSERT(!mManager, "Cannot Init() a JSWindowActorParent twice!");
33 SetName(aName);
34 mManager = aManager;
36 InvokeCallback(CallbackFunction::ActorCreated);
39 void JSWindowActorParent::SendRawMessage(
40 const JSActorMessageMeta& aMeta, Maybe<ipc::StructuredCloneData>&& aData,
41 Maybe<ipc::StructuredCloneData>&& aStack, ErrorResult& aRv) {
42 if (NS_WARN_IF(!CanSend() || !mManager || !mManager->CanSend())) {
43 aRv.ThrowInvalidStateError("JSWindowActorParent cannot send at the moment");
44 return;
47 if (mManager->IsInProcess()) {
48 SendRawMessageInProcess(
49 aMeta, std::move(aData), std::move(aStack),
50 [manager{mManager}]() { return manager->GetChildActor(); });
51 return;
54 Maybe<ClonedMessageData> msgData;
55 if (aData) {
56 msgData.emplace();
57 if (NS_WARN_IF(!aData->BuildClonedMessageData(*msgData))) {
58 aRv.ThrowDataCloneError(
59 nsPrintfCString("JSWindowActorParent serialization error: cannot "
60 "clone, in actor '%s'",
61 PromiseFlatCString(aMeta.actorName()).get()));
62 return;
66 Maybe<ClonedMessageData> stackData;
67 if (aStack) {
68 stackData.emplace();
69 if (!aStack->BuildClonedMessageData(*stackData)) {
70 stackData.reset();
74 if (NS_WARN_IF(!mManager->SendRawMessage(aMeta, msgData, stackData))) {
75 aRv.ThrowOperationError(
76 nsPrintfCString("JSWindowActorParent send error in actor '%s'",
77 PromiseFlatCString(aMeta.actorName()).get()));
78 return;
82 CanonicalBrowsingContext* JSWindowActorParent::GetBrowsingContext(
83 ErrorResult& aRv) {
84 if (!mManager) {
85 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
86 return nullptr;
89 return mManager->BrowsingContext();
92 void JSWindowActorParent::ClearManager() { mManager = nullptr; }
94 NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorParent, JSActor, mManager)
96 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorParent)
97 NS_INTERFACE_MAP_END_INHERITING(JSActor)
99 NS_IMPL_ADDREF_INHERITED(JSWindowActorParent, JSActor)
100 NS_IMPL_RELEASE_INHERITED(JSWindowActorParent, JSActor)
102 } // namespace mozilla::dom