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 #ifndef mozilla_ipc_backgroundchildimpl_h__
8 #define mozilla_ipc_backgroundchildimpl_h__
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ipc/InputStreamUtils.h"
12 #include "mozilla/ipc/PBackgroundChild.h"
13 #include "mozilla/UniquePtr.h"
14 #include "nsRefPtrHashtable.h"
15 
16 namespace mozilla {
17 namespace dom {
18 
19 class IDBFileHandle;
20 
21 namespace indexedDB {
22 
23 class ThreadLocal;
24 
25 }  // namespace indexedDB
26 }  // namespace dom
27 
28 namespace ipc {
29 
30 // Instances of this class should never be created directly. This class is meant
31 // to be inherited in BackgroundImpl.
32 class BackgroundChildImpl : public PBackgroundChild,
33                             public ChildToParentStreamActorManager {
34  public:
35   class ThreadLocal;
36 
37   // Get the ThreadLocal for the current thread if
38   // BackgroundChild::GetOrCreateForCurrentThread() has been called and true was
39   // returned (e.g. a valid PBackgroundChild actor has been created or is in the
40   // process of being created). Otherwise this function returns null.
41   // This functions is implemented in BackgroundImpl.cpp.
42   static ThreadLocal* GetThreadLocalForCurrentThread();
43 
44   PChildToParentStreamChild* SendPChildToParentStreamConstructor(
45       PChildToParentStreamChild* aActor) override;
46   PFileDescriptorSetChild* SendPFileDescriptorSetConstructor(
47       const FileDescriptor& aFD) override;
48 
49  protected:
50   BackgroundChildImpl();
51   virtual ~BackgroundChildImpl();
52 
53   virtual void ProcessingError(Result aCode, const char* aReason) override;
54 
55   virtual void ActorDestroy(ActorDestroyReason aWhy) override;
56 
57   virtual PBackgroundTestChild* AllocPBackgroundTestChild(
58       const nsCString& aTestArg) override;
59 
60   virtual bool DeallocPBackgroundTestChild(
61       PBackgroundTestChild* aActor) override;
62 
63   virtual PBackgroundIndexedDBUtilsChild* AllocPBackgroundIndexedDBUtilsChild()
64       override;
65 
66   virtual bool DeallocPBackgroundIndexedDBUtilsChild(
67       PBackgroundIndexedDBUtilsChild* aActor) override;
68 
69   virtual PBackgroundSDBConnectionChild* AllocPBackgroundSDBConnectionChild(
70       const PersistenceType& aPersistenceType,
71       const PrincipalInfo& aPrincipalInfo) override;
72 
73   virtual bool DeallocPBackgroundSDBConnectionChild(
74       PBackgroundSDBConnectionChild* aActor) override;
75 
76   virtual PBackgroundLSDatabaseChild* AllocPBackgroundLSDatabaseChild(
77       const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId,
78       const uint64_t& aDatastoreId) override;
79 
80   virtual bool DeallocPBackgroundLSDatabaseChild(
81       PBackgroundLSDatabaseChild* aActor) override;
82 
83   virtual PBackgroundLSObserverChild* AllocPBackgroundLSObserverChild(
84       const uint64_t& aObserverId) override;
85 
86   virtual bool DeallocPBackgroundLSObserverChild(
87       PBackgroundLSObserverChild* aActor) override;
88 
89   virtual PBackgroundLSRequestChild* AllocPBackgroundLSRequestChild(
90       const LSRequestParams& aParams) override;
91 
92   virtual bool DeallocPBackgroundLSRequestChild(
93       PBackgroundLSRequestChild* aActor) override;
94 
95   virtual PBackgroundLSSimpleRequestChild* AllocPBackgroundLSSimpleRequestChild(
96       const LSSimpleRequestParams& aParams) override;
97 
98   virtual bool DeallocPBackgroundLSSimpleRequestChild(
99       PBackgroundLSSimpleRequestChild* aActor) override;
100 
101   virtual PBackgroundLocalStorageCacheChild*
102   AllocPBackgroundLocalStorageCacheChild(
103       const PrincipalInfo& aPrincipalInfo, const nsCString& aOriginKey,
104       const uint32_t& aPrivateBrowsingId) override;
105 
106   virtual bool DeallocPBackgroundLocalStorageCacheChild(
107       PBackgroundLocalStorageCacheChild* aActor) override;
108 
109   virtual PBackgroundStorageChild* AllocPBackgroundStorageChild(
110       const nsString& aProfilePath,
111       const uint32_t& aPrivateBrowsingId) override;
112 
113   virtual bool DeallocPBackgroundStorageChild(
114       PBackgroundStorageChild* aActor) override;
115 
116   virtual already_AddRefed<PRemoteLazyInputStreamChild>
117   AllocPRemoteLazyInputStreamChild(const nsID& aID,
118                                    const uint64_t& aSize) override;
119 
120   virtual PTemporaryIPCBlobChild* AllocPTemporaryIPCBlobChild() override;
121 
122   virtual bool DeallocPTemporaryIPCBlobChild(
123       PTemporaryIPCBlobChild* aActor) override;
124 
125   virtual PFileCreatorChild* AllocPFileCreatorChild(
126       const nsString& aFullPath, const nsString& aType, const nsString& aName,
127       const Maybe<int64_t>& aLastModified, const bool& aExistenceCheck,
128       const bool& aIsFromNsIFile) override;
129 
130   virtual bool DeallocPFileCreatorChild(PFileCreatorChild* aActor) override;
131 
132   virtual mozilla::dom::PRemoteWorkerChild* AllocPRemoteWorkerChild(
133       const RemoteWorkerData& aData) override;
134 
135   virtual mozilla::ipc::IPCResult RecvPRemoteWorkerConstructor(
136       PRemoteWorkerChild* aActor, const RemoteWorkerData& aData) override;
137 
138   virtual bool DeallocPRemoteWorkerChild(
139       mozilla::dom::PRemoteWorkerChild* aActor) override;
140 
141   virtual mozilla::dom::PRemoteWorkerControllerChild*
142   AllocPRemoteWorkerControllerChild(
143       const mozilla::dom::RemoteWorkerData& aRemoteWorkerData) override;
144 
145   virtual bool DeallocPRemoteWorkerControllerChild(
146       mozilla::dom::PRemoteWorkerControllerChild* aActor) override;
147 
148   virtual mozilla::dom::PRemoteWorkerServiceChild*
149   AllocPRemoteWorkerServiceChild() override;
150 
151   virtual bool DeallocPRemoteWorkerServiceChild(
152       mozilla::dom::PRemoteWorkerServiceChild* aActor) override;
153 
154   virtual mozilla::dom::PSharedWorkerChild* AllocPSharedWorkerChild(
155       const mozilla::dom::RemoteWorkerData& aData, const uint64_t& aWindowID,
156       const mozilla::dom::MessagePortIdentifier& aPortIdentifier) override;
157 
158   virtual bool DeallocPSharedWorkerChild(
159       mozilla::dom::PSharedWorkerChild* aActor) override;
160 
161   virtual PFileDescriptorSetChild* AllocPFileDescriptorSetChild(
162       const FileDescriptor& aFileDescriptor) override;
163 
164   virtual bool DeallocPFileDescriptorSetChild(
165       PFileDescriptorSetChild* aActor) override;
166 
167   virtual PCamerasChild* AllocPCamerasChild() override;
168 
169   virtual bool DeallocPCamerasChild(PCamerasChild* aActor) override;
170 
171   virtual PUDPSocketChild* AllocPUDPSocketChild(
172       const Maybe<PrincipalInfo>& aPrincipalInfo,
173       const nsCString& aFilter) override;
174   virtual bool DeallocPUDPSocketChild(PUDPSocketChild* aActor) override;
175 
176   virtual PBroadcastChannelChild* AllocPBroadcastChannelChild(
177       const PrincipalInfo& aPrincipalInfo, const nsCString& aOrigin,
178       const nsString& aChannel) override;
179 
180   virtual bool DeallocPBroadcastChannelChild(
181       PBroadcastChannelChild* aActor) override;
182 
183   virtual PServiceWorkerManagerChild* AllocPServiceWorkerManagerChild()
184       override;
185 
186   virtual bool DeallocPServiceWorkerManagerChild(
187       PServiceWorkerManagerChild* aActor) override;
188 
189   virtual dom::cache::PCacheStorageChild* AllocPCacheStorageChild(
190       const dom::cache::Namespace& aNamespace,
191       const PrincipalInfo& aPrincipalInfo) override;
192 
193   virtual bool DeallocPCacheStorageChild(
194       dom::cache::PCacheStorageChild* aActor) override;
195 
196   virtual dom::cache::PCacheChild* AllocPCacheChild() override;
197 
198   virtual bool DeallocPCacheChild(dom::cache::PCacheChild* aActor) override;
199 
200   virtual already_AddRefed<dom::cache::PCacheStreamControlChild>
201   AllocPCacheStreamControlChild() override;
202 
203   virtual PMessagePortChild* AllocPMessagePortChild(
204       const nsID& aUUID, const nsID& aDestinationUUID,
205       const uint32_t& aSequenceID) override;
206 
207   virtual bool DeallocPMessagePortChild(PMessagePortChild* aActor) override;
208 
209   virtual PChildToParentStreamChild* AllocPChildToParentStreamChild() override;
210 
211   virtual bool DeallocPChildToParentStreamChild(
212       PChildToParentStreamChild* aActor) override;
213 
214   virtual PParentToChildStreamChild* AllocPParentToChildStreamChild() override;
215 
216   virtual bool DeallocPParentToChildStreamChild(
217       PParentToChildStreamChild* aActor) override;
218 
219   virtual PQuotaChild* AllocPQuotaChild() override;
220 
221   virtual bool DeallocPQuotaChild(PQuotaChild* aActor) override;
222 
223   virtual PClientManagerChild* AllocPClientManagerChild() override;
224 
225   virtual bool DeallocPClientManagerChild(PClientManagerChild* aActor) override;
226 
227 #ifdef EARLY_BETA_OR_EARLIER
228   virtual void OnChannelReceivedMessage(const Message& aMsg) override;
229 #endif
230 
231   virtual PWebAuthnTransactionChild* AllocPWebAuthnTransactionChild() override;
232 
233   virtual bool DeallocPWebAuthnTransactionChild(
234       PWebAuthnTransactionChild* aActor) override;
235 
236   virtual PMIDIPortChild* AllocPMIDIPortChild(
237       const MIDIPortInfo& aPortInfo, const bool& aSysexEnabled) override;
238   virtual bool DeallocPMIDIPortChild(PMIDIPortChild*) override;
239 
240   virtual PMIDIManagerChild* AllocPMIDIManagerChild() override;
241   virtual bool DeallocPMIDIManagerChild(PMIDIManagerChild*) override;
242 
243   already_AddRefed<PServiceWorkerChild> AllocPServiceWorkerChild(
244       const IPCServiceWorkerDescriptor&);
245 
246   already_AddRefed<PServiceWorkerContainerChild>
247   AllocPServiceWorkerContainerChild();
248 
249   already_AddRefed<PServiceWorkerRegistrationChild>
250   AllocPServiceWorkerRegistrationChild(
251       const IPCServiceWorkerRegistrationDescriptor&);
252 
253   virtual PEndpointForReportChild* AllocPEndpointForReportChild(
254       const nsString& aGroupName, const PrincipalInfo& aPrincipalInfo) override;
255 
256   virtual bool DeallocPEndpointForReportChild(
257       PEndpointForReportChild* aActor) override;
258 
259   virtual dom::PMediaTransportChild* AllocPMediaTransportChild() override;
260 
261   virtual bool DeallocPMediaTransportChild(
262       dom::PMediaTransportChild* aActor) override;
263 };
264 
265 class BackgroundChildImpl::ThreadLocal final {
266   friend class mozilla::DefaultDelete<ThreadLocal>;
267 
268  public:
269   mozilla::UniquePtr<mozilla::dom::indexedDB::ThreadLocal>
270       mIndexedDBThreadLocal;
271   mozilla::dom::IDBFileHandle* mCurrentFileHandle;
272 
273  public:
274   ThreadLocal();
275 
276  private:
277   // Only destroyed by UniquePtr<ThreadLocal>.
278   ~ThreadLocal();
279 };
280 
281 }  // namespace ipc
282 }  // namespace mozilla
283 
284 #endif  // mozilla_ipc_backgroundchildimpl_h__
285