1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef mozilla_net_SocketProcessChild_h
7 #define mozilla_net_SocketProcessChild_h
8 
9 #include "mozilla/net/PSocketProcessChild.h"
10 #include "mozilla/ipc/InputStreamUtils.h"
11 #include "mozilla/Mutex.h"
12 #include "nsRefPtrHashtable.h"
13 
14 namespace mozilla {
15 class ChildProfilerController;
16 }
17 
18 namespace mozilla {
19 namespace net {
20 
21 class SocketProcessBridgeParent;
22 class BackgroundDataBridgeParent;
23 
24 // The IPC actor implements PSocketProcessChild in child process.
25 // This is allocated and kept alive by SocketProcessImpl.
26 class SocketProcessChild final
27     : public PSocketProcessChild,
28       public mozilla::ipc::ChildToParentStreamActorManager {
29  public:
30   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessChild)
31 
32   SocketProcessChild();
33 
34   static SocketProcessChild* GetSingleton();
35 
36   bool Init(base::ProcessId aParentPid, const char* aParentBuildID,
37             MessageLoop* aIOLoop, UniquePtr<IPC::Channel> aChannel);
38 
39   void ActorDestroy(ActorDestroyReason aWhy) override;
40 
41   mozilla::ipc::IPCResult RecvPreferenceUpdate(const Pref& aPref);
42   mozilla::ipc::IPCResult RecvRequestMemoryReport(
43       const uint32_t& generation, const bool& anonymize,
44       const bool& minimizeMemoryUsage,
45       const Maybe<mozilla::ipc::FileDescriptor>& DMDFile);
46   mozilla::ipc::IPCResult RecvSetOffline(const bool& aOffline);
47   mozilla::ipc::IPCResult RecvInitLinuxSandbox(
48       const Maybe<ipc::FileDescriptor>& aBrokerFd);
49   mozilla::ipc::IPCResult RecvInitSocketProcessBridgeParent(
50       const ProcessId& aContentProcessId,
51       Endpoint<mozilla::net::PSocketProcessBridgeParent>&& aEndpoint);
52   mozilla::ipc::IPCResult RecvInitProfiler(
53       Endpoint<mozilla::PProfilerChild>&& aEndpoint);
54   mozilla::ipc::IPCResult RecvSocketProcessTelemetryPing();
55 
56   PWebrtcTCPSocketChild* AllocPWebrtcTCPSocketChild(const Maybe<TabId>& tabId);
57   bool DeallocPWebrtcTCPSocketChild(PWebrtcTCPSocketChild* aActor);
58 
59   already_AddRefed<PHttpTransactionChild> AllocPHttpTransactionChild();
60 
61   PFileDescriptorSetChild* AllocPFileDescriptorSetChild(
62       const FileDescriptor& fd);
63   bool DeallocPFileDescriptorSetChild(PFileDescriptorSetChild* aActor);
64 
65   PChildToParentStreamChild* AllocPChildToParentStreamChild();
66   bool DeallocPChildToParentStreamChild(PChildToParentStreamChild* aActor);
67   PParentToChildStreamChild* AllocPParentToChildStreamChild();
68   bool DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor);
69 
70   void CleanUp();
71   void DestroySocketProcessBridgeParent(ProcessId aId);
72 
73   PChildToParentStreamChild* SendPChildToParentStreamConstructor(
74       PChildToParentStreamChild* aActor) override;
75   PFileDescriptorSetChild* SendPFileDescriptorSetConstructor(
76       const FileDescriptor& aFD) override;
77   already_AddRefed<PHttpConnectionMgrChild> AllocPHttpConnectionMgrChild();
78 
79   mozilla::ipc::IPCResult RecvOnHttpActivityDistributorActivated(
80       const bool& aIsActivated);
81 
82   already_AddRefed<PInputChannelThrottleQueueChild>
83   AllocPInputChannelThrottleQueueChild(const uint32_t& aMeanBytesPerSecond,
84                                        const uint32_t& aMaxBytesPerSecond);
85 
86   already_AddRefed<PAltSvcTransactionChild> AllocPAltSvcTransactionChild(
87       const HttpConnectionInfoCloneArgs& aConnInfo, const uint32_t& aCaps);
88 
IsShuttingDown()89   bool IsShuttingDown() { return mShuttingDown; }
90 
91   already_AddRefed<PDNSRequestChild> AllocPDNSRequestChild(
92       const nsCString& aHost, const nsCString& aTrrServer,
93       const uint16_t& aType, const OriginAttributes& aOriginAttributes,
94       const uint32_t& aFlags);
95   mozilla::ipc::IPCResult RecvPDNSRequestConstructor(
96       PDNSRequestChild* aActor, const nsCString& aHost,
97       const nsCString& aTrrServer, const uint16_t& aType,
98       const OriginAttributes& aOriginAttributes,
99       const uint32_t& aFlags) override;
100 
101   void AddDataBridgeToMap(uint64_t aChannelId,
102                           BackgroundDataBridgeParent* aActor);
103   void RemoveDataBridgeFromMap(uint64_t aChannelId);
104   Maybe<RefPtr<BackgroundDataBridgeParent>> GetAndRemoveDataBridge(
105       uint64_t aChannelId);
106 
107   mozilla::ipc::IPCResult RecvClearSessionCache();
108 
109   already_AddRefed<PTRRServiceChild> AllocPTRRServiceChild(
110       const bool& aCaptiveIsPassed, const bool& aParentalControlEnabled,
111       const nsTArray<nsCString>& aDNSSuffixList);
112   mozilla::ipc::IPCResult RecvPTRRServiceConstructor(
113       PTRRServiceChild* aActor, const bool& aCaptiveIsPassed,
114       const bool& aParentalControlEnabled,
115       nsTArray<nsCString>&& aDNSSuffixList) override;
116 
117  protected:
118   friend class SocketProcessImpl;
119   ~SocketProcessChild();
120 
121  private:
122   // Mapping of content process id and the SocketProcessBridgeParent.
123   // This table keeps SocketProcessBridgeParent alive in socket process.
124   nsRefPtrHashtable<nsUint32HashKey, SocketProcessBridgeParent>
125       mSocketProcessBridgeParentMap;
126 
127 #ifdef MOZ_GECKO_PROFILER
128   RefPtr<ChildProfilerController> mProfilerController;
129 #endif
130 
131   bool mShuttingDown;
132   // Protect the table below.
133   Mutex mMutex;
134   nsDataHashtable<nsUint64HashKey, RefPtr<BackgroundDataBridgeParent>>
135       mBackgroundDataBridgeMap;
136 };
137 
138 }  // namespace net
139 }  // namespace mozilla
140 
141 #endif  // mozilla_net_SocketProcessChild_h
142