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_backgroundchild_h__
8 #define mozilla_ipc_backgroundchild_h__
9 
10 #include "mozilla/Attributes.h"
11 
12 class nsIEventTarget;
13 
14 namespace mozilla {
15 namespace dom {
16 
17 class BlobImpl;
18 class ContentChild;
19 class ContentParent;
20 class ContentProcess;
21 
22 }  // namespace dom
23 
24 namespace net {
25 
26 class SocketProcessChild;
27 class SocketProcessBridgeChild;
28 
29 }  // namespace net
30 
31 namespace ipc {
32 
33 class PBackgroundChild;
34 class PBackgroundStarterChild;
35 
36 // This class allows access to the PBackground protocol. PBackground allows
37 // communication between any thread (in the parent or a child process) and a
38 // single background thread in the parent process. Each PBackgroundChild
39 // instance is tied to the thread on which it is created and must not be shared
40 // across threads. Each PBackgroundChild is unique and valid as long as its
41 // designated thread lives.
42 //
43 // Creation of PBackground is synchronous. GetOrCreateForCurrentThread will
44 // create the actor if it doesn't exist yet. Thereafter (assuming success)
45 // GetForCurrentThread() will return the same actor every time.
46 //
47 // GetOrCreateSocketActorForCurrentThread, which is like
48 // GetOrCreateForCurrentThread, is used to get or create PBackground actor
49 // between child process and socket process.
50 //
51 // CloseForCurrentThread() will close the current PBackground actor.  Subsequent
52 // calls to GetForCurrentThread will return null.  CloseForCurrentThread() may
53 // only be called exactly once for each thread-specific actor.  Currently it is
54 // illegal to call this before the PBackground actor has been created.
55 //
56 // The PBackgroundChild actor and all its sub-protocol actors will be
57 // automatically destroyed when its designated thread completes.
58 //
59 // Init{Content,Socket,SocketBridge}Starter must be called on the main thread
60 // with an actor bridging to the relevant target process type before these
61 // methods can be used.
62 class BackgroundChild final {
63   friend class mozilla::dom::ContentParent;
64   friend class mozilla::dom::ContentProcess;
65   friend class mozilla::net::SocketProcessChild;
66 
67  public:
68   // See above.
69   static PBackgroundChild* GetForCurrentThread();
70 
71   // See above.
72   static PBackgroundChild* GetOrCreateForCurrentThread();
73 
74   // See above.
75   static PBackgroundChild* GetOrCreateSocketActorForCurrentThread();
76 
77   // See above.
78   static PBackgroundChild* GetOrCreateForSocketParentBridgeForCurrentThread();
79 
80   // See above.
81   static void CloseForCurrentThread();
82 
83   // See above.
84   static void InitContentStarter(mozilla::dom::ContentChild* aContent);
85 
86   // See above.
87   static void InitSocketStarter(mozilla::net::SocketProcessChild* aSocket);
88 
89   // See above.
90   static void InitSocketBridgeStarter(
91       mozilla::net::SocketProcessBridgeChild* aSocketBridge);
92 
93  private:
94   // Only called by this class's friends.
95   static void Startup();
96 };
97 
98 }  // namespace ipc
99 }  // namespace mozilla
100 
101 #endif  // mozilla_ipc_backgroundchild_h__
102