Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsIContentChild.h"
8 :
9 : #include "mozilla/dom/ContentChild.h"
10 : #include "mozilla/dom/DOMTypes.h"
11 : #include "mozilla/dom/File.h"
12 : #include "mozilla/dom/PermissionMessageUtils.h"
13 : #include "mozilla/dom/TabChild.h"
14 : #include "mozilla/dom/TabGroup.h"
15 : #include "mozilla/dom/ipc/StructuredCloneData.h"
16 : #include "mozilla/ipc/FileDescriptorSetChild.h"
17 : #include "mozilla/ipc/InputStreamUtils.h"
18 : #include "mozilla/ipc/IPCStreamAlloc.h"
19 : #include "mozilla/ipc/IPCStreamDestination.h"
20 : #include "mozilla/ipc/IPCStreamSource.h"
21 : #include "mozilla/ipc/PChildToParentStreamChild.h"
22 : #include "mozilla/ipc/PParentToChildStreamChild.h"
23 : #include "mozilla/dom/ipc/IPCBlobInputStreamChild.h"
24 :
25 : #include "nsPrintfCString.h"
26 : #include "xpcpublic.h"
27 :
28 : using namespace mozilla::ipc;
29 : using namespace mozilla::jsipc;
30 :
31 : namespace mozilla {
32 : namespace dom {
33 :
34 : PJavaScriptChild*
35 2 : nsIContentChild::AllocPJavaScriptChild()
36 : {
37 2 : return NewJavaScriptChild();
38 : }
39 :
40 : bool
41 0 : nsIContentChild::DeallocPJavaScriptChild(PJavaScriptChild* aChild)
42 : {
43 0 : ReleaseJavaScriptChild(aChild);
44 0 : return true;
45 : }
46 :
47 : PBrowserChild*
48 1 : nsIContentChild::AllocPBrowserChild(const TabId& aTabId,
49 : const TabId& aSameTabGroupAs,
50 : const IPCTabContext& aContext,
51 : const uint32_t& aChromeFlags,
52 : const ContentParentId& aCpID,
53 : const bool& aIsForBrowser)
54 : {
55 : // We'll happily accept any kind of IPCTabContext here; we don't need to
56 : // check that it's of a certain type for security purposes, because we
57 : // believe whatever the parent process tells us.
58 :
59 2 : MaybeInvalidTabContext tc(aContext);
60 1 : if (!tc.IsValid()) {
61 0 : NS_ERROR(nsPrintfCString("Received an invalid TabContext from "
62 : "the parent process. (%s) Crashing...",
63 : tc.GetInvalidReason()).get());
64 0 : MOZ_CRASH("Invalid TabContext received from the parent process.");
65 : }
66 :
67 : RefPtr<TabChild> child =
68 2 : TabChild::Create(this, aTabId, aSameTabGroupAs,
69 2 : tc.GetTabContext(), aChromeFlags);
70 :
71 : // The ref here is released in DeallocPBrowserChild.
72 2 : return child.forget().take();
73 : }
74 :
75 : bool
76 0 : nsIContentChild::DeallocPBrowserChild(PBrowserChild* aIframe)
77 : {
78 0 : TabChild* child = static_cast<TabChild*>(aIframe);
79 0 : NS_RELEASE(child);
80 0 : return true;
81 : }
82 :
83 : mozilla::ipc::IPCResult
84 1 : nsIContentChild::RecvPBrowserConstructor(PBrowserChild* aActor,
85 : const TabId& aTabId,
86 : const TabId& aSameTabGroupAs,
87 : const IPCTabContext& aContext,
88 : const uint32_t& aChromeFlags,
89 : const ContentParentId& aCpID,
90 : const bool& aIsForBrowser)
91 : {
92 : // This runs after AllocPBrowserChild() returns and the IPC machinery for this
93 : // PBrowserChild has been set up.
94 :
95 1 : auto tabChild = static_cast<TabChild*>(static_cast<TabChild*>(aActor));
96 :
97 1 : if (NS_WARN_IF(NS_FAILED(tabChild->Init()))) {
98 0 : return IPC_FAIL(tabChild, "TabChild::Init failed");
99 : }
100 :
101 2 : nsCOMPtr<nsIObserverService> os = services::GetObserverService();
102 1 : if (os) {
103 1 : os->NotifyObservers(static_cast<nsITabChild*>(tabChild), "tab-child-created", nullptr);
104 : }
105 :
106 1 : return IPC_OK();
107 : }
108 :
109 : PIPCBlobInputStreamChild*
110 0 : nsIContentChild::AllocPIPCBlobInputStreamChild(const nsID& aID,
111 : const uint64_t& aSize)
112 : {
113 : // IPCBlobInputStreamChild is refcounted. Here it's created and in
114 : // DeallocPIPCBlobInputStreamChild is released.
115 :
116 : RefPtr<IPCBlobInputStreamChild> actor =
117 0 : new IPCBlobInputStreamChild(aID, aSize);
118 0 : return actor.forget().take();
119 : }
120 :
121 : bool
122 0 : nsIContentChild::DeallocPIPCBlobInputStreamChild(PIPCBlobInputStreamChild* aActor)
123 : {
124 : RefPtr<IPCBlobInputStreamChild> actor =
125 0 : dont_AddRef(static_cast<IPCBlobInputStreamChild*>(aActor));
126 0 : return true;
127 : }
128 :
129 : PChildToParentStreamChild*
130 0 : nsIContentChild::AllocPChildToParentStreamChild()
131 : {
132 0 : MOZ_CRASH("PChildToParentStreamChild actors should be manually constructed!");
133 : }
134 :
135 : bool
136 0 : nsIContentChild::DeallocPChildToParentStreamChild(PChildToParentStreamChild* aActor)
137 : {
138 0 : delete aActor;
139 0 : return true;
140 : }
141 :
142 : PParentToChildStreamChild*
143 0 : nsIContentChild::AllocPParentToChildStreamChild()
144 : {
145 0 : return mozilla::ipc::AllocPParentToChildStreamChild();
146 : }
147 :
148 : bool
149 0 : nsIContentChild::DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor)
150 : {
151 0 : delete aActor;
152 0 : return true;
153 : }
154 :
155 : PFileDescriptorSetChild*
156 0 : nsIContentChild::AllocPFileDescriptorSetChild(const FileDescriptor& aFD)
157 : {
158 0 : return new FileDescriptorSetChild(aFD);
159 : }
160 :
161 : bool
162 0 : nsIContentChild::DeallocPFileDescriptorSetChild(PFileDescriptorSetChild* aActor)
163 : {
164 0 : delete static_cast<FileDescriptorSetChild*>(aActor);
165 0 : return true;
166 : }
167 :
168 : mozilla::ipc::IPCResult
169 0 : nsIContentChild::RecvAsyncMessage(const nsString& aMsg,
170 : InfallibleTArray<CpowEntry>&& aCpows,
171 : const IPC::Principal& aPrincipal,
172 : const ClonedMessageData& aData)
173 : {
174 0 : NS_LossyConvertUTF16toASCII messageNameCStr(aMsg);
175 0 : AUTO_PROFILER_LABEL_DYNAMIC("nsIContentChild::RecvAsyncMessage", EVENTS,
176 : messageNameCStr.get());
177 :
178 0 : CrossProcessCpowHolder cpows(this, aCpows);
179 0 : RefPtr<nsFrameMessageManager> cpm = nsFrameMessageManager::GetChildProcessManager();
180 0 : if (cpm) {
181 0 : ipc::StructuredCloneData data;
182 0 : ipc::UnpackClonedMessageDataForChild(aData, data);
183 :
184 0 : cpm->ReceiveMessage(static_cast<nsIContentFrameMessageManager*>(cpm.get()), nullptr,
185 0 : aMsg, false, &data, &cpows, aPrincipal, nullptr);
186 : }
187 0 : return IPC_OK();
188 : }
189 :
190 : /* static */
191 : already_AddRefed<nsIEventTarget>
192 1 : nsIContentChild::GetConstructedEventTarget(const IPC::Message& aMsg)
193 : {
194 : ActorHandle handle;
195 1 : TabId tabId, sameTabGroupAs;
196 1 : PickleIterator iter(aMsg);
197 1 : if (!IPC::ReadParam(&aMsg, &iter, &handle)) {
198 0 : return nullptr;
199 : }
200 1 : aMsg.IgnoreSentinel(&iter);
201 1 : if (!IPC::ReadParam(&aMsg, &iter, &tabId)) {
202 0 : return nullptr;
203 : }
204 1 : aMsg.IgnoreSentinel(&iter);
205 1 : if (!IPC::ReadParam(&aMsg, &iter, &sameTabGroupAs)) {
206 0 : return nullptr;
207 : }
208 :
209 : // If sameTabGroupAs is non-zero, then the new tab will be in the same
210 : // TabGroup as a previously created tab. Rather than try to find the
211 : // previously created tab (whose constructor message may not even have been
212 : // processed yet, in theory) and look up its event target, we just use the
213 : // default event target. This means that runnables for this tab will not be
214 : // labeled. However, this path is only taken for print preview and view
215 : // source, which are not performance-sensitive.
216 1 : if (sameTabGroupAs) {
217 0 : return nullptr;
218 : }
219 :
220 : // If the request for a new TabChild is coming from the parent process, then
221 : // there is no opener. Therefore, we create a fresh TabGroup.
222 2 : RefPtr<TabGroup> tabGroup = new TabGroup();
223 2 : nsCOMPtr<nsIEventTarget> target = tabGroup->EventTargetFor(TaskCategory::Other);
224 1 : return target.forget();
225 : }
226 :
227 :
228 : } // namespace dom
229 : } // namespace mozilla
|