11
11
#include < queue>
12
12
#include " base/basictypes.h"
13
13
#include " base/process.h"
14
+ #include " mozilla/EventTargetAndLockCapability.h"
14
15
#include " mozilla/UniquePtr.h"
15
16
#include " mozilla/UniquePtrExtensions.h"
16
17
#include " mozilla/WeakPtr.h"
@@ -29,10 +30,10 @@ class MessageWriter;
29
30
// ------------------------------------------------------------------------------
30
31
31
32
class Channel {
32
- // Security tests need access to the pipe handle.
33
- friend class ChannelTest ;
34
-
35
33
public:
34
+ NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_EVENT_TARGET (
35
+ Channel, IOThread().GetEventTarget());
36
+
36
37
// For channels which are created after initialization, handles to the pipe
37
38
// endpoints may be passed around directly using IPC messages.
38
39
using ChannelHandle = mozilla::UniqueFileHandle;
@@ -89,9 +90,11 @@ class Channel {
89
90
// The Channel must be created and destroyed on the IO thread, and all
90
91
// methods, unless otherwise noted, are only safe to call on the I/O thread.
91
92
//
92
- Channel (ChannelHandle pipe, Mode mode, base::ProcessId other_pid);
93
+ static already_AddRefed<Channel> Create (ChannelHandle pipe, Mode mode,
94
+ base::ProcessId other_pid);
93
95
94
- ~Channel ();
96
+ Channel (const Channel&) = delete ;
97
+ Channel& operator =(const Channel&) = delete ;
95
98
96
99
// Connect the pipe. On the server side, this will initiate
97
100
// waiting for connections. On the client, it attempts to
@@ -101,10 +104,10 @@ class Channel {
101
104
//
102
105
// |listener| will receive a callback on the current thread for each newly
103
106
// received message.
104
- bool Connect (Listener* listener);
107
+ virtual bool Connect (Listener* listener) MOZ_EXCLUDES(SendMutex()) = 0 ;
105
108
106
109
// Close this Channel explicitly. May be called multiple times.
107
- void Close ();
110
+ virtual void Close () MOZ_EXCLUDES(SendMutex()) = 0 ;
108
111
109
112
// Send a message over the Channel to the listener on the other end.
110
113
//
@@ -113,43 +116,60 @@ class Channel {
113
116
//
114
117
// If you Send() a message on a Close()'d channel, we delete the message
115
118
// immediately.
116
- bool Send (mozilla::UniquePtr<Message> message);
119
+ virtual bool Send (mozilla::UniquePtr<Message> message)
120
+ MOZ_EXCLUDES(SendMutex()) = 0;
117
121
118
122
// Explicitly set the pid expected for the other side of this channel. This
119
123
// will be used for logging, and on Windows may be used for transferring
120
124
// handles between processes.
121
125
//
122
126
// If it is set this way, the "hello" message will be checked to ensure that
123
127
// the same pid is reported.
124
- void SetOtherPid (base::ProcessId other_pid);
125
-
126
- // IsClosed() is safe to call from any thread, but the value returned may
127
- // be out of date.
128
- bool IsClosed () const ;
128
+ virtual void SetOtherPid (base::ProcessId other_pid)
129
+ MOZ_EXCLUDES(SendMutex()) = 0;
129
130
130
131
#if defined(XP_DARWIN)
131
132
// Configure the mach task_t for the peer task.
132
- void SetOtherMachTask (task_t task);
133
+ virtual void SetOtherMachTask (task_t task) MOZ_EXCLUDES(SendMutex()) = 0 ;
133
134
134
135
// Tell this pipe to accept mach ports. Exactly one side of the IPC connection
135
136
// must be set as `MODE_SERVER` and that side will be responsible for
136
137
// transferring the rights between processes.
137
- void StartAcceptingMachPorts (Mode mode);
138
+ virtual void StartAcceptingMachPorts (Mode mode) MOZ_EXCLUDES(SendMutex()) = 0 ;
138
139
#elif defined(XP_WIN)
139
140
// Tell this pipe to accept handles. Exactly one side of the IPC connection
140
141
// must be set as `MODE_SERVER`, and that side will be responsible for calling
141
142
// `DuplicateHandle` to transfer the handle between processes.
142
- void StartAcceptingHandles (Mode mode);
143
+ virtual void StartAcceptingHandles (Mode mode) MOZ_EXCLUDES(SendMutex()) = 0 ;
143
144
#endif
144
145
145
146
// Create a new pair of pipe endpoints which can be used to establish a
146
147
// native IPC::Channel connection.
147
148
static bool CreateRawPipe (ChannelHandle* server, ChannelHandle* client);
148
149
149
- private:
150
- // PIMPL to which all channel calls are delegated.
151
- class ChannelImpl ;
152
- RefPtr<ChannelImpl> channel_impl_;
150
+ protected:
151
+ Channel ();
152
+ virtual ~Channel ();
153
+
154
+ // Capability for members which may only be used on the IO thread. Generally
155
+ // used for state related to receiving IPC messages.
156
+ const mozilla::EventTargetCapability<nsISerialEventTarget>& IOThread () const
157
+ MOZ_RETURN_CAPABILITY(chan_cap_.Target()) {
158
+ return chan_cap_.Target ();
159
+ }
160
+
161
+ // Capability for members which may only be used with the send mutex held.
162
+ // Generally used for state related to sending IPC messages.
163
+ mozilla::Mutex& SendMutex () MOZ_RETURN_CAPABILITY(chan_cap_.Lock()) {
164
+ return chan_cap_.Lock ();
165
+ }
166
+
167
+ // Compound capability of the IO thread and a Mutex. This can be used for
168
+ // members which may be used immutably either on the IO thread or with the
169
+ // send mutex held, but may only be modified if both on the IO thread, and
170
+ // holding the send mutex.
171
+ mozilla::EventTargetAndLockCapability<nsISerialEventTarget, mozilla::Mutex>
172
+ chan_cap_;
153
173
154
174
enum {
155
175
#if defined(XP_DARWIN)
0 commit comments