OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_EDK_SYSTEM_DISPATCHER_H_ | |
6 #define MOJO_EDK_SYSTEM_DISPATCHER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/synchronization/lock.h" | |
17 #include "mojo/edk/embedder/platform_handle_vector.h" | |
18 #include "mojo/edk/system/handle_signals_state.h" | |
19 #include "mojo/edk/system/memory.h" | |
20 #include "mojo/edk/system/system_impl_export.h" | |
21 #include "mojo/public/c/system/buffer.h" | |
22 #include "mojo/public/c/system/data_pipe.h" | |
23 #include "mojo/public/c/system/message_pipe.h" | |
24 #include "mojo/public/c/system/types.h" | |
25 | |
26 namespace mojo { | |
27 | |
28 namespace embedder { | |
29 class PlatformSharedBufferMapping; | |
30 } | |
31 | |
32 namespace system { | |
33 | |
34 class Channel; | |
35 class Core; | |
36 class Dispatcher; | |
37 class DispatcherTransport; | |
38 class HandleTable; | |
39 class LocalMessagePipeEndpoint; | |
40 class ProxyMessagePipeEndpoint; | |
41 class TransportData; | |
42 class Awakable; | |
43 | |
44 typedef std::vector<scoped_refptr<Dispatcher>> DispatcherVector; | |
45 | |
46 namespace test { | |
47 | |
48 // Test helper. We need to declare it here so we can friend it. | |
49 MOJO_SYSTEM_IMPL_EXPORT DispatcherTransport | |
50 DispatcherTryStartTransport(Dispatcher* dispatcher); | |
51 | |
52 } // namespace test | |
53 | |
54 // A |Dispatcher| implements Mojo primitives that are "attached" to a particular | |
55 // handle. This includes most (all?) primitives except for |MojoWait...()|. This | |
56 // object is thread-safe, with its state being protected by a single lock | |
57 // |lock_|, which is also made available to implementation subclasses (via the | |
58 // |lock()| method). | |
59 class MOJO_SYSTEM_IMPL_EXPORT Dispatcher | |
60 : public base::RefCountedThreadSafe<Dispatcher> { | |
61 public: | |
62 enum Type { | |
63 kTypeUnknown = 0, | |
64 kTypeMessagePipe, | |
65 kTypeDataPipeProducer, | |
66 kTypeDataPipeConsumer, | |
67 kTypeSharedBuffer, | |
68 | |
69 // "Private" types (not exposed via the public interface): | |
70 kTypePlatformHandle = -1 | |
71 }; | |
72 virtual Type GetType() const = 0; | |
73 | |
74 // These methods implement the various primitives named |Mojo...()|. These | |
75 // take |lock_| and handle races with |Close()|. Then they call out to | |
76 // subclasses' |...ImplNoLock()| methods (still under |lock_|), which actually | |
77 // implement the primitives. | |
78 // NOTE(vtl): This puts a big lock around each dispatcher (i.e., handle), and | |
79 // prevents the various |...ImplNoLock()|s from releasing the lock as soon as | |
80 // possible. If this becomes an issue, we can rethink this. | |
81 MojoResult Close(); | |
82 | |
83 // |transports| may be non-null if and only if there are handles to be | |
84 // written; not that |this| must not be in |transports|. On success, all the | |
85 // dispatchers in |transports| must have been moved to a closed state; on | |
86 // failure, they should remain in their original state. | |
87 MojoResult WriteMessage(UserPointer<const void> bytes, | |
88 uint32_t num_bytes, | |
89 std::vector<DispatcherTransport>* transports, | |
90 MojoWriteMessageFlags flags); | |
91 // |dispatchers| must be non-null but empty, if |num_dispatchers| is non-null | |
92 // and nonzero. On success, it will be set to the dispatchers to be received | |
93 // (and assigned handles) as part of the message. | |
94 MojoResult ReadMessage(UserPointer<void> bytes, | |
95 UserPointer<uint32_t> num_bytes, | |
96 DispatcherVector* dispatchers, | |
97 uint32_t* num_dispatchers, | |
98 MojoReadMessageFlags flags); | |
99 MojoResult WriteData(UserPointer<const void> elements, | |
100 UserPointer<uint32_t> elements_num_bytes, | |
101 MojoWriteDataFlags flags); | |
102 MojoResult BeginWriteData(UserPointer<void*> buffer, | |
103 UserPointer<uint32_t> buffer_num_bytes, | |
104 MojoWriteDataFlags flags); | |
105 MojoResult EndWriteData(uint32_t num_bytes_written); | |
106 MojoResult ReadData(UserPointer<void> elements, | |
107 UserPointer<uint32_t> num_bytes, | |
108 MojoReadDataFlags flags); | |
109 MojoResult BeginReadData(UserPointer<const void*> buffer, | |
110 UserPointer<uint32_t> buffer_num_bytes, | |
111 MojoReadDataFlags flags); | |
112 MojoResult EndReadData(uint32_t num_bytes_read); | |
113 // |options| may be null. |new_dispatcher| must not be null, but | |
114 // |*new_dispatcher| should be null (and will contain the dispatcher for the | |
115 // new handle on success). | |
116 MojoResult DuplicateBufferHandle( | |
117 UserPointer<const MojoDuplicateBufferHandleOptions> options, | |
118 scoped_refptr<Dispatcher>* new_dispatcher); | |
119 MojoResult MapBuffer( | |
120 uint64_t offset, | |
121 uint64_t num_bytes, | |
122 MojoMapBufferFlags flags, | |
123 scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping); | |
124 | |
125 // Gets the current handle signals state. (The default implementation simply | |
126 // returns a default-constructed |HandleSignalsState|, i.e., no signals | |
127 // satisfied or satisfiable.) Note: The state is subject to change from other | |
128 // threads. | |
129 HandleSignalsState GetHandleSignalsState() const; | |
130 | |
131 // Adds an awakable to this dispatcher, which will be woken up when this | |
132 // object changes state to satisfy |signals| with context |context|. It will | |
133 // also be woken up when it becomes impossible for the object to ever satisfy | |
134 // |signals| with a suitable error status. | |
135 // | |
136 // If |signals_state| is non-null, on *failure* |*signals_state| will be set | |
137 // to the current handle signals state (on success, it is left untouched). | |
138 // | |
139 // Returns: | |
140 // - |MOJO_RESULT_OK| if the awakable was added; | |
141 // - |MOJO_RESULT_ALREADY_EXISTS| if |signals| is already satisfied; | |
142 // - |MOJO_RESULT_INVALID_ARGUMENT| if the dispatcher has been closed; and | |
143 // - |MOJO_RESULT_FAILED_PRECONDITION| if it is not (or no longer) possible | |
144 // that |signals| will ever be satisfied. | |
145 MojoResult AddAwakable(Awakable* awakable, | |
146 MojoHandleSignals signals, | |
147 uint32_t context, | |
148 HandleSignalsState* signals_state); | |
149 // Removes an awakable from this dispatcher. (It is valid to call this | |
150 // multiple times for the same |awakable| on the same object, so long as | |
151 // |AddAwakable()| was called at most once.) If |signals_state| is non-null, | |
152 // |*signals_state| will be set to the current handle signals state. | |
153 void RemoveAwakable(Awakable* awakable, HandleSignalsState* signals_state); | |
154 | |
155 // A dispatcher must be put into a special state in order to be sent across a | |
156 // message pipe. Outside of tests, only |HandleTableAccess| is allowed to do | |
157 // this, since there are requirements on the handle table (see below). | |
158 // | |
159 // In this special state, only a restricted set of operations is allowed. | |
160 // These are the ones available as |DispatcherTransport| methods. Other | |
161 // |Dispatcher| methods must not be called until |DispatcherTransport::End()| | |
162 // has been called. | |
163 class HandleTableAccess { | |
164 private: | |
165 friend class Core; | |
166 friend class HandleTable; | |
167 // Tests also need this, to avoid needing |Core|. | |
168 friend DispatcherTransport test::DispatcherTryStartTransport(Dispatcher*); | |
169 | |
170 // This must be called under the handle table lock and only if the handle | |
171 // table entry is not marked busy. The caller must maintain a reference to | |
172 // |dispatcher| until |DispatcherTransport::End()| is called. | |
173 static DispatcherTransport TryStartTransport(Dispatcher* dispatcher); | |
174 }; | |
175 | |
176 // A |TransportData| may serialize dispatchers that are given to it (and which | |
177 // were previously attached to the |MessageInTransit| that is creating it) to | |
178 // a given |Channel| and then (probably in a different process) deserialize. | |
179 // Note that the |MessageInTransit| "owns" (i.e., has the only ref to) these | |
180 // dispatchers, so there are no locking issues. (There's no lock ordering | |
181 // issue, and in fact no need to take dispatcher locks at all.) | |
182 // TODO(vtl): Consider making another wrapper similar to |DispatcherTransport| | |
183 // (but with an owning, unique reference), and having | |
184 // |CreateEquivalentDispatcherAndCloseImplNoLock()| return that wrapper (and | |
185 // |MessageInTransit|, etc. only holding on to such wrappers). | |
186 class TransportDataAccess { | |
187 private: | |
188 friend class TransportData; | |
189 | |
190 // Serialization API. These functions may only be called on such | |
191 // dispatchers. (|channel| is the |Channel| to which the dispatcher is to be | |
192 // serialized.) See the |Dispatcher| methods of the same names for more | |
193 // details. | |
194 static void StartSerialize(Dispatcher* dispatcher, | |
195 Channel* channel, | |
196 size_t* max_size, | |
197 size_t* max_platform_handles); | |
198 static bool EndSerializeAndClose( | |
199 Dispatcher* dispatcher, | |
200 Channel* channel, | |
201 void* destination, | |
202 size_t* actual_size, | |
203 embedder::PlatformHandleVector* platform_handles); | |
204 | |
205 // Deserialization API. | |
206 // Note: This "clears" (i.e., reset to the invalid handle) any platform | |
207 // handles that it takes ownership of. | |
208 static scoped_refptr<Dispatcher> Deserialize( | |
209 Channel* channel, | |
210 int32_t type, | |
211 const void* source, | |
212 size_t size, | |
213 embedder::PlatformHandleVector* platform_handles); | |
214 }; | |
215 | |
216 protected: | |
217 friend class base::RefCountedThreadSafe<Dispatcher>; | |
218 | |
219 Dispatcher(); | |
220 virtual ~Dispatcher(); | |
221 | |
222 // These are to be overridden by subclasses (if necessary). They are called | |
223 // exactly once -- first |CancelAllAwakablesNoLock()|, then | |
224 // |CloseImplNoLock()|, | |
225 // when the dispatcher is being closed. They are called under |lock_|. | |
226 virtual void CancelAllAwakablesNoLock(); | |
227 virtual void CloseImplNoLock(); | |
228 virtual scoped_refptr<Dispatcher> | |
229 CreateEquivalentDispatcherAndCloseImplNoLock() = 0; | |
230 | |
231 // These are to be overridden by subclasses (if necessary). They are never | |
232 // called after the dispatcher has been closed. They are called under |lock_|. | |
233 // See the descriptions of the methods without the "ImplNoLock" for more | |
234 // information. | |
235 virtual MojoResult WriteMessageImplNoLock( | |
236 UserPointer<const void> bytes, | |
237 uint32_t num_bytes, | |
238 std::vector<DispatcherTransport>* transports, | |
239 MojoWriteMessageFlags flags); | |
240 virtual MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, | |
241 UserPointer<uint32_t> num_bytes, | |
242 DispatcherVector* dispatchers, | |
243 uint32_t* num_dispatchers, | |
244 MojoReadMessageFlags flags); | |
245 virtual MojoResult WriteDataImplNoLock(UserPointer<const void> elements, | |
246 UserPointer<uint32_t> num_bytes, | |
247 MojoWriteDataFlags flags); | |
248 virtual MojoResult BeginWriteDataImplNoLock( | |
249 UserPointer<void*> buffer, | |
250 UserPointer<uint32_t> buffer_num_bytes, | |
251 MojoWriteDataFlags flags); | |
252 virtual MojoResult EndWriteDataImplNoLock(uint32_t num_bytes_written); | |
253 virtual MojoResult ReadDataImplNoLock(UserPointer<void> elements, | |
254 UserPointer<uint32_t> num_bytes, | |
255 MojoReadDataFlags flags); | |
256 virtual MojoResult BeginReadDataImplNoLock( | |
257 UserPointer<const void*> buffer, | |
258 UserPointer<uint32_t> buffer_num_bytes, | |
259 MojoReadDataFlags flags); | |
260 virtual MojoResult EndReadDataImplNoLock(uint32_t num_bytes_read); | |
261 virtual MojoResult DuplicateBufferHandleImplNoLock( | |
262 UserPointer<const MojoDuplicateBufferHandleOptions> options, | |
263 scoped_refptr<Dispatcher>* new_dispatcher); | |
264 virtual MojoResult MapBufferImplNoLock( | |
265 uint64_t offset, | |
266 uint64_t num_bytes, | |
267 MojoMapBufferFlags flags, | |
268 scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping); | |
269 virtual HandleSignalsState GetHandleSignalsStateImplNoLock() const; | |
270 virtual MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
271 MojoHandleSignals signals, | |
272 uint32_t context, | |
273 HandleSignalsState* signals_state); | |
274 virtual void RemoveAwakableImplNoLock(Awakable* awakable, | |
275 HandleSignalsState* signals_state); | |
276 | |
277 // These implement the API used to serialize dispatchers to a |Channel| | |
278 // (described below). They will only be called on a dispatcher that's attached | |
279 // to and "owned" by a |MessageInTransit|. See the non-"impl" versions for | |
280 // more information. | |
281 // | |
282 // Note: |StartSerializeImplNoLock()| is actually called with |lock_| NOT | |
283 // held, since the dispatcher should only be accessible to the calling thread. | |
284 // On Debug builds, |EndSerializeAndCloseImplNoLock()| is called with |lock_| | |
285 // held, to satisfy any |lock_.AssertAcquired()| (e.g., in |CloseImplNoLock()| | |
286 // -- and anything it calls); disentangling those assertions is | |
287 // difficult/fragile, and would weaken our general checking of invariants. | |
288 // | |
289 // TODO(vtl): Consider making these pure virtual once most things support | |
290 // being passed over a message pipe. | |
291 virtual void StartSerializeImplNoLock(Channel* channel, | |
292 size_t* max_size, | |
293 size_t* max_platform_handles); | |
294 virtual bool EndSerializeAndCloseImplNoLock( | |
295 Channel* channel, | |
296 void* destination, | |
297 size_t* actual_size, | |
298 embedder::PlatformHandleVector* platform_handles); | |
299 | |
300 // Available to subclasses. (Note: Returns a non-const reference, just like | |
301 // |base::AutoLock|'s constructor takes a non-const reference.) | |
302 base::Lock& lock() const { return lock_; } | |
303 | |
304 private: | |
305 friend class DispatcherTransport; | |
306 | |
307 // This should be overridden to return true if/when there's an ongoing | |
308 // operation (e.g., two-phase read/writes on data pipes) that should prevent a | |
309 // handle from being sent over a message pipe (with status "busy"). | |
310 virtual bool IsBusyNoLock() const; | |
311 | |
312 // Closes the dispatcher. This must be done under lock, and unlike |Close()|, | |
313 // the dispatcher must not be closed already. (This is the "equivalent" of | |
314 // |CreateEquivalentDispatcherAndCloseNoLock()|, for situations where the | |
315 // dispatcher must be disposed of instead of "transferred".) | |
316 void CloseNoLock(); | |
317 | |
318 // Creates an equivalent dispatcher -- representing the same resource as this | |
319 // dispatcher -- and close (i.e., disable) this dispatcher. I.e., this | |
320 // dispatcher will look as though it was closed, but the resource it | |
321 // represents will be assigned to the new dispatcher. This must be called | |
322 // under the dispatcher's lock. | |
323 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseNoLock(); | |
324 | |
325 // API to serialize dispatchers to a |Channel|, exposed to only | |
326 // |TransportData| (via |TransportData|). They may only be called on a | |
327 // dispatcher attached to a |MessageInTransit| (and in particular not in | |
328 // |CoreImpl|'s handle table). | |
329 // | |
330 // Starts the serialization. Returns (via the two "out" parameters) the | |
331 // maximum amount of space that may be needed to serialize this dispatcher to | |
332 // the given |Channel| (no more than | |
333 // |TransportData::kMaxSerializedDispatcherSize|) and the maximum number of | |
334 // |PlatformHandle|s that may need to be attached (no more than | |
335 // |TransportData::kMaxSerializedDispatcherPlatformHandles|). If this | |
336 // dispatcher cannot be serialized to the given |Channel|, |*max_size| and | |
337 // |*max_platform_handles| should be set to zero. A call to this method will | |
338 // ALWAYS be followed by a call to |EndSerializeAndClose()| (even if this | |
339 // dispatcher cannot be serialized to the given |Channel|). | |
340 void StartSerialize(Channel* channel, | |
341 size_t* max_size, | |
342 size_t* max_platform_handles); | |
343 // Completes the serialization of this dispatcher to the given |Channel| and | |
344 // closes it. (This call will always follow an earlier call to | |
345 // |StartSerialize()|, with the same |Channel|.) This does so by writing to | |
346 // |destination| and appending any |PlatformHandle|s needed to | |
347 // |platform_handles| (which may be null if no platform handles were indicated | |
348 // to be required to |StartSerialize()|). This may write no more than the | |
349 // amount indicated by |StartSerialize()|. (WARNING: Beware of races, e.g., if | |
350 // something can be mutated between the two calls!) Returns true on success, | |
351 // in which case |*actual_size| is set to the amount it actually wrote to | |
352 // |destination|. On failure, |*actual_size| should not be modified; however, | |
353 // the dispatcher will still be closed. | |
354 bool EndSerializeAndClose(Channel* channel, | |
355 void* destination, | |
356 size_t* actual_size, | |
357 embedder::PlatformHandleVector* platform_handles); | |
358 | |
359 // This protects the following members as well as any state added by | |
360 // subclasses. | |
361 mutable base::Lock lock_; | |
362 bool is_closed_; | |
363 | |
364 DISALLOW_COPY_AND_ASSIGN(Dispatcher); | |
365 }; | |
366 | |
367 // Wrapper around a |Dispatcher| pointer, while it's being processed to be | |
368 // passed in a message pipe. See the comment about | |
369 // |Dispatcher::HandleTableAccess| for more details. | |
370 // | |
371 // Note: This class is deliberately "thin" -- no more expensive than a | |
372 // |Dispatcher*|. | |
373 class MOJO_SYSTEM_IMPL_EXPORT DispatcherTransport { | |
374 public: | |
375 DispatcherTransport() : dispatcher_(nullptr) {} | |
376 | |
377 void End(); | |
378 | |
379 Dispatcher::Type GetType() const { return dispatcher_->GetType(); } | |
380 bool IsBusy() const { return dispatcher_->IsBusyNoLock(); } | |
381 void Close() { dispatcher_->CloseNoLock(); } | |
382 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndClose() { | |
383 return dispatcher_->CreateEquivalentDispatcherAndCloseNoLock(); | |
384 } | |
385 | |
386 bool is_valid() const { return !!dispatcher_; } | |
387 | |
388 protected: | |
389 Dispatcher* dispatcher() { return dispatcher_; } | |
390 | |
391 private: | |
392 friend class Dispatcher::HandleTableAccess; | |
393 | |
394 explicit DispatcherTransport(Dispatcher* dispatcher) | |
395 : dispatcher_(dispatcher) {} | |
396 | |
397 Dispatcher* dispatcher_; | |
398 | |
399 // Copy and assign allowed. | |
400 }; | |
401 | |
402 } // namespace system | |
403 } // namespace mojo | |
404 | |
405 #endif // MOJO_EDK_SYSTEM_DISPATCHER_H_ | |
OLD | NEW |