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/JSWindowActorChild.h"
9 #include "mozilla/dom/ContentChild.h"
10 #include "mozilla/dom/WindowGlobalChild.h"
11 #include "mozilla/dom/WindowGlobalParent.h"
12 #include "mozilla/dom/WindowProxyHolder.h"
13 #include "mozilla/dom/MessageManagerBinding.h"
14 #include "mozilla/dom/BrowsingContext.h"
15 #include "nsGlobalWindowInner.h"
17 namespace mozilla::dom
{
19 JSWindowActorChild::~JSWindowActorChild() { MOZ_ASSERT(!mManager
); }
21 JSObject
* JSWindowActorChild::WrapObject(JSContext
* aCx
,
22 JS::Handle
<JSObject
*> aGivenProto
) {
23 return JSWindowActorChild_Binding::Wrap(aCx
, this, aGivenProto
);
26 WindowGlobalChild
* JSWindowActorChild::GetManager() const { return mManager
; }
28 WindowContext
* JSWindowActorChild::GetWindowContext() const {
29 return mManager
? mManager
->WindowContext() : nullptr;
32 void JSWindowActorChild::Init(const nsACString
& aName
,
33 WindowGlobalChild
* aManager
) {
34 MOZ_ASSERT(!mManager
, "Cannot Init() a JSWindowActorChild twice!");
38 InvokeCallback(CallbackFunction::ActorCreated
);
41 void JSWindowActorChild::SendRawMessage(
42 const JSActorMessageMeta
& aMeta
, Maybe
<ipc::StructuredCloneData
>&& aData
,
43 Maybe
<ipc::StructuredCloneData
>&& aStack
, ErrorResult
& aRv
) {
44 if (!CanSend() || !mManager
|| !mManager
->CanSend()) {
45 aRv
.ThrowInvalidStateError("JSWindowActorChild cannot send at the moment");
49 if (mManager
->IsInProcess()) {
50 SendRawMessageInProcess(
51 aMeta
, std::move(aData
), std::move(aStack
),
52 [manager
{mManager
}]() { return manager
->GetParentActor(); });
56 // Cross-process case - send data over WindowGlobalChild to other side.
57 Maybe
<ClonedMessageData
> msgData
;
60 if (!aData
->BuildClonedMessageData(*msgData
)) {
61 aRv
.ThrowDataCloneError(
62 nsPrintfCString("JSWindowActorChild serialization error: cannot "
63 "clone, in actor '%s'",
64 PromiseFlatCString(aMeta
.actorName()).get()));
69 Maybe
<ClonedMessageData
> stackData
;
72 if (!aStack
->BuildClonedMessageData(*stackData
)) {
77 if (!mManager
->SendRawMessage(aMeta
, msgData
, stackData
)) {
78 aRv
.ThrowOperationError(
79 nsPrintfCString("JSWindowActorChild send error in actor '%s'",
80 PromiseFlatCString(aMeta
.actorName()).get()));
85 Document
* JSWindowActorChild::GetDocument(ErrorResult
& aRv
) {
87 ThrowStateErrorForGetter("document", aRv
);
91 nsGlobalWindowInner
* window
= mManager
->GetWindowGlobal();
92 return window
? window
->GetDocument() : nullptr;
95 BrowsingContext
* JSWindowActorChild::GetBrowsingContext(ErrorResult
& aRv
) {
97 ThrowStateErrorForGetter("browsingContext", aRv
);
101 return mManager
->BrowsingContext();
104 nsIDocShell
* JSWindowActorChild::GetDocShell(ErrorResult
& aRv
) {
106 ThrowStateErrorForGetter("docShell", aRv
);
110 return mManager
->BrowsingContext()->GetDocShell();
113 Nullable
<WindowProxyHolder
> JSWindowActorChild::GetContentWindow(
116 ThrowStateErrorForGetter("contentWindow", aRv
);
120 if (nsGlobalWindowInner
* window
= mManager
->GetWindowGlobal()) {
121 if (window
->IsCurrentInnerWindow()) {
122 return WindowProxyHolder(window
->GetBrowsingContext());
129 void JSWindowActorChild::ClearManager() { mManager
= nullptr; }
131 NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorChild
, JSActor
, mManager
)
133 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorChild
)
134 NS_INTERFACE_MAP_END_INHERITING(JSActor
)
136 NS_IMPL_ADDREF_INHERITED(JSWindowActorChild
, JSActor
)
137 NS_IMPL_RELEASE_INHERITED(JSWindowActorChild
, JSActor
)
139 } // namespace mozilla::dom