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/. */
6 
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"
16 
17 namespace mozilla::dom {
18 
~JSWindowActorChild()19 JSWindowActorChild::~JSWindowActorChild() { MOZ_ASSERT(!mManager); }
20 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)21 JSObject* JSWindowActorChild::WrapObject(JSContext* aCx,
22                                          JS::Handle<JSObject*> aGivenProto) {
23   return JSWindowActorChild_Binding::Wrap(aCx, this, aGivenProto);
24 }
25 
GetManager() const26 WindowGlobalChild* JSWindowActorChild::GetManager() const { return mManager; }
27 
GetWindowContext() const28 WindowContext* JSWindowActorChild::GetWindowContext() const {
29   return mManager ? mManager->WindowContext() : nullptr;
30 }
31 
Init(const nsACString & aName,WindowGlobalChild * aManager)32 void JSWindowActorChild::Init(const nsACString& aName,
33                               WindowGlobalChild* aManager) {
34   MOZ_ASSERT(!mManager, "Cannot Init() a JSWindowActorChild twice!");
35   SetName(aName);
36   mManager = aManager;
37 
38   InvokeCallback(CallbackFunction::ActorCreated);
39 }
40 
SendRawMessage(const JSActorMessageMeta & aMeta,Maybe<ipc::StructuredCloneData> && aData,Maybe<ipc::StructuredCloneData> && aStack,ErrorResult & aRv)41 void JSWindowActorChild::SendRawMessage(
42     const JSActorMessageMeta& aMeta, Maybe<ipc::StructuredCloneData>&& aData,
43     Maybe<ipc::StructuredCloneData>&& aStack, ErrorResult& aRv) {
44   if (NS_WARN_IF(!CanSend() || !mManager || !mManager->CanSend())) {
45     aRv.ThrowInvalidStateError("JSWindowActorChild cannot send at the moment");
46     return;
47   }
48 
49   if (mManager->IsInProcess()) {
50     SendRawMessageInProcess(
51         aMeta, std::move(aData), std::move(aStack),
52         [manager{mManager}]() { return manager->GetParentActor(); });
53     return;
54   }
55 
56   size_t length = 0;
57   if (aData) {
58     length += aData->DataLength();
59   }
60   if (aStack) {
61     length += aStack->DataLength();
62   }
63 
64   if (NS_WARN_IF(!AllowMessage(aMeta, length))) {
65     aRv.ThrowDataCloneError(
66         nsPrintfCString("JSWindowActorChild serialization error: data too "
67                         "large, in actor '%s'",
68                         PromiseFlatCString(aMeta.actorName()).get()));
69     return;
70   }
71 
72   // Cross-process case - send data over WindowGlobalChild to other side.
73   ContentChild* cc = ContentChild::GetSingleton();
74   Maybe<ClonedMessageData> msgData;
75   if (aData) {
76     msgData.emplace();
77     if (NS_WARN_IF(!aData->BuildClonedMessageDataForChild(cc, *msgData))) {
78       aRv.ThrowDataCloneError(
79           nsPrintfCString("JSWindowActorChild serialization error: cannot "
80                           "clone, in actor '%s'",
81                           PromiseFlatCString(aMeta.actorName()).get()));
82       return;
83     }
84   }
85 
86   Maybe<ClonedMessageData> stackData;
87   if (aStack) {
88     stackData.emplace();
89     if (!aStack->BuildClonedMessageDataForChild(cc, *stackData)) {
90       stackData.reset();
91     }
92   }
93 
94   if (NS_WARN_IF(!mManager->SendRawMessage(aMeta, msgData, stackData))) {
95     aRv.ThrowOperationError(
96         nsPrintfCString("JSWindowActorChild send error in actor '%s'",
97                         PromiseFlatCString(aMeta.actorName()).get()));
98     return;
99   }
100 }
101 
GetDocument(ErrorResult & aRv)102 Document* JSWindowActorChild::GetDocument(ErrorResult& aRv) {
103   if (!mManager) {
104     ThrowStateErrorForGetter("document", aRv);
105     return nullptr;
106   }
107 
108   nsGlobalWindowInner* window = mManager->GetWindowGlobal();
109   return window ? window->GetDocument() : nullptr;
110 }
111 
GetBrowsingContext(ErrorResult & aRv)112 BrowsingContext* JSWindowActorChild::GetBrowsingContext(ErrorResult& aRv) {
113   if (!mManager) {
114     ThrowStateErrorForGetter("browsingContext", aRv);
115     return nullptr;
116   }
117 
118   return mManager->BrowsingContext();
119 }
120 
GetDocShell(ErrorResult & aRv)121 nsIDocShell* JSWindowActorChild::GetDocShell(ErrorResult& aRv) {
122   if (!mManager) {
123     ThrowStateErrorForGetter("docShell", aRv);
124     return nullptr;
125   }
126 
127   return mManager->BrowsingContext()->GetDocShell();
128 }
129 
GetContentWindow(ErrorResult & aRv)130 Nullable<WindowProxyHolder> JSWindowActorChild::GetContentWindow(
131     ErrorResult& aRv) {
132   if (!mManager) {
133     ThrowStateErrorForGetter("contentWindow", aRv);
134     return nullptr;
135   }
136 
137   if (nsGlobalWindowInner* window = mManager->GetWindowGlobal()) {
138     if (window->IsCurrentInnerWindow()) {
139       return WindowProxyHolder(window->GetBrowsingContext());
140     }
141   }
142 
143   return nullptr;
144 }
145 
ClearManager()146 void JSWindowActorChild::ClearManager() { mManager = nullptr; }
147 
148 NS_IMPL_CYCLE_COLLECTION_INHERITED(JSWindowActorChild, JSActor, mManager)
149 
150 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(JSWindowActorChild, JSActor)
151 NS_IMPL_CYCLE_COLLECTION_TRACE_END
152 
153 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorChild)
154 NS_INTERFACE_MAP_END_INHERITING(JSActor)
155 
156 NS_IMPL_ADDREF_INHERITED(JSWindowActorChild, JSActor)
157 NS_IMPL_RELEASE_INHERITED(JSWindowActorChild, JSActor)
158 
159 }  // namespace mozilla::dom
160