Bug 1858921 - Part 2: Move WasmStructObject inlinable allocation methods to new inlin...
[gecko.git] / netwerk / ipc / NeckoChild.cpp
blobfe2b3df0bff84adf9dfef001298abf51585259a2
2 /* vim: set sw=2 ts=8 et tw=80 : */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsHttp.h"
9 #include "mozilla/net/NeckoChild.h"
10 #include "mozilla/dom/ContentChild.h"
11 #include "mozilla/dom/BrowserChild.h"
12 #include "mozilla/net/HttpChannelChild.h"
13 #include "mozilla/net/ChildDNSService.h"
14 #include "mozilla/net/CookieServiceChild.h"
15 #include "mozilla/net/DataChannelChild.h"
16 #ifdef MOZ_WIDGET_GTK
17 # include "mozilla/net/GIOChannelChild.h"
18 #endif
19 #include "mozilla/net/FileChannelChild.h"
20 #include "mozilla/net/WebSocketChannelChild.h"
21 #include "mozilla/net/WebSocketEventListenerChild.h"
22 #include "mozilla/net/DNSRequestChild.h"
23 #include "mozilla/net/IPCTransportProvider.h"
24 #include "mozilla/dom/network/TCPSocketChild.h"
25 #include "mozilla/dom/network/TCPServerSocketChild.h"
26 #include "mozilla/dom/network/UDPSocketChild.h"
27 #include "mozilla/net/AltDataOutputStreamChild.h"
28 #include "mozilla/net/SocketProcessBridgeChild.h"
29 #ifdef MOZ_WEBRTC
30 # include "mozilla/net/StunAddrsRequestChild.h"
31 # include "mozilla/net/WebrtcTCPSocketChild.h"
32 #endif
34 #include "SerializedLoadContext.h"
35 #include "nsGlobalWindowInner.h"
36 #include "nsIOService.h"
37 #include "nsINetworkPredictor.h"
38 #include "nsINetworkPredictorVerifier.h"
39 #include "nsINetworkLinkService.h"
40 #include "nsQueryObject.h"
41 #include "mozilla/ipc/URIUtils.h"
42 #include "nsNetUtil.h"
43 #include "SimpleChannel.h"
45 using mozilla::dom::TCPServerSocketChild;
46 using mozilla::dom::TCPSocketChild;
47 using mozilla::dom::UDPSocketChild;
49 namespace mozilla {
50 namespace net {
52 PNeckoChild* gNeckoChild = nullptr;
54 // C++ file contents
56 NeckoChild::~NeckoChild() {
57 // Send__delete__(gNeckoChild);
58 gNeckoChild = nullptr;
61 void NeckoChild::InitNeckoChild() {
62 if (!IsNeckoChild()) {
63 MOZ_ASSERT(false, "InitNeckoChild called by non-child!");
64 return;
67 if (!gNeckoChild) {
68 mozilla::dom::ContentChild* cpc =
69 mozilla::dom::ContentChild::GetSingleton();
70 NS_ASSERTION(cpc, "Content Protocol is NULL!");
71 if (NS_WARN_IF(cpc->IsShuttingDown())) {
72 return;
74 RefPtr<NeckoChild> child = new NeckoChild();
75 gNeckoChild = cpc->SendPNeckoConstructor(child);
76 NS_ASSERTION(gNeckoChild, "PNecko Protocol init failed!");
80 PStunAddrsRequestChild* NeckoChild::AllocPStunAddrsRequestChild() {
81 // We don't allocate here: instead we always use IPDL constructor that takes
82 // an existing object
83 MOZ_ASSERT_UNREACHABLE(
84 "AllocPStunAddrsRequestChild should not be called "
85 "on child");
86 return nullptr;
89 bool NeckoChild::DeallocPStunAddrsRequestChild(PStunAddrsRequestChild* aActor) {
90 #ifdef MOZ_WEBRTC
91 StunAddrsRequestChild* p = static_cast<StunAddrsRequestChild*>(aActor);
92 p->ReleaseIPDLReference();
93 #endif
94 return true;
97 PWebrtcTCPSocketChild* NeckoChild::AllocPWebrtcTCPSocketChild(
98 const Maybe<TabId>& tabId) {
99 // We don't allocate here: instead we always use IPDL constructor that takes
100 // an existing object
101 MOZ_ASSERT_UNREACHABLE(
102 "AllocPWebrtcTCPSocketChild should not be called on"
103 " child");
104 return nullptr;
107 bool NeckoChild::DeallocPWebrtcTCPSocketChild(PWebrtcTCPSocketChild* aActor) {
108 #ifdef MOZ_WEBRTC
109 WebrtcTCPSocketChild* child = static_cast<WebrtcTCPSocketChild*>(aActor);
110 child->ReleaseIPDLReference();
111 #endif
112 return true;
115 PAltDataOutputStreamChild* NeckoChild::AllocPAltDataOutputStreamChild(
116 const nsACString& type, const int64_t& predictedSize,
117 PHttpChannelChild* channel) {
118 // We don't allocate here: see HttpChannelChild::OpenAlternativeOutputStream()
119 MOZ_ASSERT_UNREACHABLE("AllocPAltDataOutputStreamChild should not be called");
120 return nullptr;
123 bool NeckoChild::DeallocPAltDataOutputStreamChild(
124 PAltDataOutputStreamChild* aActor) {
125 AltDataOutputStreamChild* child =
126 static_cast<AltDataOutputStreamChild*>(aActor);
127 child->ReleaseIPDLReference();
128 return true;
131 #ifdef MOZ_WIDGET_GTK
132 PGIOChannelChild* NeckoChild::AllocPGIOChannelChild(
133 PBrowserChild* aBrowser, const SerializedLoadContext& aSerialized,
134 const GIOChannelCreationArgs& aOpenArgs) {
135 // We don't allocate here: see GIOChannelChild::AsyncOpen()
136 MOZ_CRASH("AllocPGIOChannelChild should not be called");
137 return nullptr;
140 bool NeckoChild::DeallocPGIOChannelChild(PGIOChannelChild* channel) {
141 MOZ_ASSERT(IsNeckoChild(), "DeallocPGIOChannelChild called by non-child!");
143 GIOChannelChild* child = static_cast<GIOChannelChild*>(channel);
144 child->ReleaseIPDLReference();
145 return true;
147 #endif
149 PCookieServiceChild* NeckoChild::AllocPCookieServiceChild() {
150 // We don't allocate here: see CookieService::GetSingleton()
151 MOZ_ASSERT_UNREACHABLE("AllocPCookieServiceChild should not be called");
152 return nullptr;
155 bool NeckoChild::DeallocPCookieServiceChild(PCookieServiceChild* cs) {
156 NS_ASSERTION(IsNeckoChild(),
157 "DeallocPCookieServiceChild called by non-child!");
159 CookieServiceChild* p = static_cast<CookieServiceChild*>(cs);
160 p->Release();
161 return true;
164 PWebSocketChild* NeckoChild::AllocPWebSocketChild(
165 PBrowserChild* browser, const SerializedLoadContext& aSerialized,
166 const uint32_t& aSerial) {
167 MOZ_ASSERT_UNREACHABLE("AllocPWebSocketChild should not be called");
168 return nullptr;
171 bool NeckoChild::DeallocPWebSocketChild(PWebSocketChild* child) {
172 WebSocketChannelChild* p = static_cast<WebSocketChannelChild*>(child);
173 p->ReleaseIPDLReference();
174 return true;
177 PWebSocketEventListenerChild* NeckoChild::AllocPWebSocketEventListenerChild(
178 const uint64_t& aInnerWindowID) {
179 RefPtr<WebSocketEventListenerChild> c = new WebSocketEventListenerChild(
180 aInnerWindowID, GetMainThreadSerialEventTarget());
181 return c.forget().take();
184 bool NeckoChild::DeallocPWebSocketEventListenerChild(
185 PWebSocketEventListenerChild* aActor) {
186 RefPtr<WebSocketEventListenerChild> c =
187 dont_AddRef(static_cast<WebSocketEventListenerChild*>(aActor));
188 MOZ_ASSERT(c);
189 return true;
192 PSimpleChannelChild* NeckoChild::AllocPSimpleChannelChild(
193 const uint32_t& channelId) {
194 MOZ_ASSERT_UNREACHABLE("Should never get here");
195 return nullptr;
198 bool NeckoChild::DeallocPSimpleChannelChild(PSimpleChannelChild* child) {
199 static_cast<SimpleChannelChild*>(child)->Release();
200 return true;
203 PTCPSocketChild* NeckoChild::AllocPTCPSocketChild(const nsAString& host,
204 const uint16_t& port) {
205 TCPSocketChild* p = new TCPSocketChild(host, port, nullptr);
206 p->AddIPDLReference();
207 return p;
210 bool NeckoChild::DeallocPTCPSocketChild(PTCPSocketChild* child) {
211 TCPSocketChild* p = static_cast<TCPSocketChild*>(child);
212 p->ReleaseIPDLReference();
213 return true;
216 PTCPServerSocketChild* NeckoChild::AllocPTCPServerSocketChild(
217 const uint16_t& aLocalPort, const uint16_t& aBacklog,
218 const bool& aUseArrayBuffers) {
219 MOZ_ASSERT_UNREACHABLE("AllocPTCPServerSocket should not be called");
220 return nullptr;
223 bool NeckoChild::DeallocPTCPServerSocketChild(PTCPServerSocketChild* child) {
224 TCPServerSocketChild* p = static_cast<TCPServerSocketChild*>(child);
225 p->ReleaseIPDLReference();
226 return true;
229 PUDPSocketChild* NeckoChild::AllocPUDPSocketChild(nsIPrincipal* aPrincipal,
230 const nsACString& aFilter) {
231 MOZ_ASSERT_UNREACHABLE("AllocPUDPSocket should not be called");
232 return nullptr;
235 bool NeckoChild::DeallocPUDPSocketChild(PUDPSocketChild* child) {
236 UDPSocketChild* p = static_cast<UDPSocketChild*>(child);
237 p->ReleaseIPDLReference();
238 return true;
241 PTransportProviderChild* NeckoChild::AllocPTransportProviderChild() {
242 // This refcount is transferred to the receiver of the message that
243 // includes the PTransportProviderChild actor.
244 RefPtr<TransportProviderChild> res = new TransportProviderChild();
246 return res.forget().take();
249 bool NeckoChild::DeallocPTransportProviderChild(
250 PTransportProviderChild* aActor) {
251 return true;
254 /* Predictor Messages */
255 mozilla::ipc::IPCResult NeckoChild::RecvPredOnPredictPrefetch(
256 nsIURI* aURI, const uint32_t& aHttpStatus) {
257 MOZ_ASSERT(NS_IsMainThread(),
258 "PredictorChild::RecvOnPredictPrefetch "
259 "off main thread.");
260 if (!aURI) {
261 return IPC_FAIL(this, "aURI is null");
264 // Get the current predictor
265 nsresult rv = NS_OK;
266 nsCOMPtr<nsINetworkPredictorVerifier> predictor =
267 do_GetService("@mozilla.org/network/predictor;1", &rv);
268 NS_ENSURE_SUCCESS(rv, IPC_FAIL_NO_REASON(this));
270 predictor->OnPredictPrefetch(aURI, aHttpStatus);
271 return IPC_OK();
274 mozilla::ipc::IPCResult NeckoChild::RecvPredOnPredictPreconnect(nsIURI* aURI) {
275 MOZ_ASSERT(NS_IsMainThread(),
276 "PredictorChild::RecvOnPredictPreconnect "
277 "off main thread.");
278 if (!aURI) {
279 return IPC_FAIL(this, "aURI is null");
281 // Get the current predictor
282 nsresult rv = NS_OK;
283 nsCOMPtr<nsINetworkPredictorVerifier> predictor =
284 do_GetService("@mozilla.org/network/predictor;1", &rv);
285 NS_ENSURE_SUCCESS(rv, IPC_FAIL_NO_REASON(this));
287 predictor->OnPredictPreconnect(aURI);
288 return IPC_OK();
291 mozilla::ipc::IPCResult NeckoChild::RecvPredOnPredictDNS(nsIURI* aURI) {
292 MOZ_ASSERT(NS_IsMainThread(),
293 "PredictorChild::RecvOnPredictDNS off "
294 "main thread.");
295 if (!aURI) {
296 return IPC_FAIL(this, "aURI is null");
298 // Get the current predictor
299 nsresult rv = NS_OK;
300 nsCOMPtr<nsINetworkPredictorVerifier> predictor =
301 do_GetService("@mozilla.org/network/predictor;1", &rv);
302 NS_ENSURE_SUCCESS(rv, IPC_FAIL_NO_REASON(this));
304 predictor->OnPredictDNS(aURI);
305 return IPC_OK();
308 mozilla::ipc::IPCResult NeckoChild::RecvSpeculativeConnectRequest() {
309 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
310 if (obsService) {
311 obsService->NotifyObservers(nullptr, "speculative-connect-request",
312 nullptr);
314 return IPC_OK();
317 mozilla::ipc::IPCResult NeckoChild::RecvNetworkChangeNotification(
318 nsCString const& type) {
319 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
320 if (obsService) {
321 obsService->NotifyObservers(nullptr, NS_NETWORK_LINK_TOPIC,
322 NS_ConvertUTF8toUTF16(type).get());
324 return IPC_OK();
327 mozilla::ipc::IPCResult NeckoChild::RecvSetTRRDomain(const nsCString& domain) {
328 RefPtr<net::ChildDNSService> dnsServiceChild =
329 dont_AddRef(net::ChildDNSService::GetSingleton());
330 if (dnsServiceChild) {
331 dnsServiceChild->SetTRRDomain(domain);
333 return IPC_OK();
336 } // namespace net
337 } // namespace mozilla