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_backgroundparent_h__
8 #define mozilla_ipc_backgroundparent_h__
9 
10 #include "base/process.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/ipc/Transport.h"
13 
14 template <class> struct already_AddRefed;
15 
16 namespace mozilla {
17 namespace dom {
18 
19 class BlobImpl;
20 class ContentParent;
21 class PBlobParent;
22 
23 } // namespace dom
24 
25 namespace ipc {
26 
27 class PBackgroundParent;
28 
29 // This class is not designed for public consumption beyond the few static
30 // member functions.
31 class BackgroundParent final
32 {
33   friend class mozilla::dom::ContentParent;
34 
35   typedef base::ProcessId ProcessId;
36   typedef mozilla::dom::BlobImpl BlobImpl;
37   typedef mozilla::dom::ContentParent ContentParent;
38   typedef mozilla::ipc::Transport Transport;
39 
40 public:
41   // This function allows the caller to determine if the given parent actor
42   // corresponds to a child actor from another process or a child actor from a
43   // different thread in the same process.
44   // This function may only be called on the background thread.
45   static bool
46   IsOtherProcessActor(PBackgroundParent* aBackgroundActor);
47 
48   // This function returns the ContentParent associated with the parent actor if
49   // the parent actor corresponds to a child actor from another process. If the
50   // parent actor corresponds to a child actor from a different thread in the
51   // same process then this function returns null.
52   // This function may only be called on the background thread. However,
53   // ContentParent is not threadsafe and the returned pointer may not be used on
54   // any thread other than the main thread. Callers must take care to use (and
55   // release) the returned pointer appropriately.
56   static already_AddRefed<ContentParent>
57   GetContentParent(PBackgroundParent* aBackgroundActor);
58 
59   static mozilla::dom::PBlobParent*
60   GetOrCreateActorForBlobImpl(PBackgroundParent* aBackgroundActor,
61                               BlobImpl* aBlobImpl);
62 
63   // Get a value that represents the ContentParent associated with the parent
64   // actor for comparison. The value is not guaranteed to uniquely identify the
65   // ContentParent after the ContentParent has died. This function may only be
66   // called on the background thread.
67   static intptr_t
68   GetRawContentParentForComparison(PBackgroundParent* aBackgroundActor);
69 
70 private:
71   // Only called by ContentParent for cross-process actors.
72   static PBackgroundParent*
73   Alloc(ContentParent* aContent,
74         Transport* aTransport,
75         ProcessId aOtherProcess);
76 };
77 
78 // Implemented in BackgroundImpl.cpp.
79 bool
80 IsOnBackgroundThread();
81 
82 #ifdef DEBUG
83 
84 // Implemented in BackgroundImpl.cpp.
85 void
86 AssertIsOnBackgroundThread();
87 
88 #else
89 
90 inline void
AssertIsOnBackgroundThread()91 AssertIsOnBackgroundThread()
92 { }
93 
94 #endif // DEBUG
95 
96 inline void
AssertIsInMainProcess()97 AssertIsInMainProcess()
98 {
99   MOZ_ASSERT(XRE_IsParentProcess());
100 }
101 
102 } // namespace ipc
103 } // namespace mozilla
104 
105 #endif // mozilla_ipc_backgroundparent_h__
106