OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_EDK_SYSTEM_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_DISPATCHER_H_ |
6 #define MOJO_EDK_SYSTEM_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_DISPATCHER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <ostream> | 11 #include <ostream> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | |
15 #include "base/containers/stack_container.h" | |
14 #include "base/macros.h" | 16 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
17 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
18 #include "mojo/edk/embedder/platform_handle.h" | 20 #include "mojo/edk/embedder/platform_handle.h" |
19 #include "mojo/edk/embedder/platform_shared_buffer.h" | 21 #include "mojo/edk/embedder/platform_shared_buffer.h" |
20 #include "mojo/edk/system/handle_signals_state.h" | 22 #include "mojo/edk/system/handle_signals_state.h" |
21 #include "mojo/edk/system/ports/name.h" | 23 #include "mojo/edk/system/ports/name.h" |
22 #include "mojo/edk/system/system_impl_export.h" | 24 #include "mojo/edk/system/system_impl_export.h" |
23 #include "mojo/public/c/system/buffer.h" | 25 #include "mojo/public/c/system/buffer.h" |
24 #include "mojo/public/c/system/data_pipe.h" | 26 #include "mojo/public/c/system/data_pipe.h" |
25 #include "mojo/public/c/system/message_pipe.h" | 27 #include "mojo/public/c/system/message_pipe.h" |
26 #include "mojo/public/c/system/types.h" | 28 #include "mojo/public/c/system/types.h" |
27 | 29 |
28 namespace mojo { | 30 namespace mojo { |
29 namespace edk { | 31 namespace edk { |
30 | 32 |
31 class Awakable; | 33 class Awakable; |
32 class Dispatcher; | 34 class Dispatcher; |
33 | 35 |
34 using DispatcherVector = std::vector<scoped_refptr<Dispatcher>>; | 36 using DispatcherVector = std::vector<scoped_refptr<Dispatcher>>; |
35 | 37 |
36 // A |Dispatcher| implements Mojo EDK calls that are associated with a | 38 // A |Dispatcher| implements Mojo EDK calls that are associated with a |
37 // particular MojoHandle, with the exception of MojoWait and MojoWaitMany ( | 39 // particular MojoHandle, with the exception of MojoWait and MojoWaitMany ( |
38 // which are implemented directly in Core.). | 40 // which are implemented directly in Core.). |
39 class MOJO_SYSTEM_IMPL_EXPORT Dispatcher | 41 class MOJO_SYSTEM_IMPL_EXPORT Dispatcher |
40 : public base::RefCountedThreadSafe<Dispatcher> { | 42 : public base::RefCountedThreadSafe<Dispatcher> { |
41 public: | 43 public: |
44 using WatchCallback = | |
45 base::Callback<void(MojoResult, MojoHandleSignalsState)>; | |
46 | |
47 // Context associated with a single EDK request. | |
48 class RequestContext { | |
49 public: | |
50 static const size_t kFinalizersStackBufferSize = 4; | |
Anand Mistry (off Chromium)
2016/02/29 12:03:35
Shouldn't these be private?
Ken Rockot(use gerrit already)
2016/02/29 20:59:06
Done
| |
51 using FinalizerList = | |
52 base::StackVector<base::Closure, kFinalizersStackBufferSize>; | |
53 | |
54 RequestContext(); | |
55 ~RequestContext(); | |
56 | |
57 // Adds |callback| to the context's list of finalizers. These are called | |
58 // when the context is destroyed, at a time when no EDK locks are held. | |
59 void AddFinalizer(const base::Closure& callback); | |
Anand Mistry (off Chromium)
2016/02/29 12:03:35
Hmm... Since you're already making an effort to av
Ken Rockot(use gerrit already)
2016/02/29 20:59:06
I agree, base::Callback is overkill. To support re
| |
60 | |
61 private: | |
62 FinalizerList finalizers_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(RequestContext); | |
65 }; | |
66 | |
42 struct DispatcherInTransit { | 67 struct DispatcherInTransit { |
43 DispatcherInTransit(); | 68 DispatcherInTransit(); |
44 ~DispatcherInTransit(); | 69 ~DispatcherInTransit(); |
45 | 70 |
46 scoped_refptr<Dispatcher> dispatcher; | 71 scoped_refptr<Dispatcher> dispatcher; |
47 MojoHandle local_handle; | 72 MojoHandle local_handle; |
48 }; | 73 }; |
49 | 74 |
50 enum class Type { | 75 enum class Type { |
51 UNKNOWN = 0, | 76 UNKNOWN = 0, |
52 MESSAGE_PIPE, | 77 MESSAGE_PIPE, |
53 DATA_PIPE_PRODUCER, | 78 DATA_PIPE_PRODUCER, |
54 DATA_PIPE_CONSUMER, | 79 DATA_PIPE_CONSUMER, |
55 SHARED_BUFFER, | 80 SHARED_BUFFER, |
56 WAIT_SET, | 81 WAIT_SET, |
57 | 82 |
58 // "Private" types (not exposed via the public interface): | 83 // "Private" types (not exposed via the public interface): |
59 PLATFORM_HANDLE = -1, | 84 PLATFORM_HANDLE = -1, |
60 }; | 85 }; |
61 | 86 |
62 // All Dispatchers must minimally implement these methods. | 87 // All Dispatchers must minimally implement these methods. |
63 | 88 |
64 virtual Type GetType() const = 0; | 89 virtual Type GetType() const = 0; |
65 virtual MojoResult Close() = 0; | 90 virtual MojoResult Close(RequestContext* request_context) = 0; |
91 | |
92 ///////////// Watch API //////////////////// | |
93 | |
94 // NOTE: The Dispatcher implementation MUST NOT hold any locks at any point | |
95 // during which it chooses to run |callback|. | |
96 virtual MojoResult Watch(MojoHandleSignals signals, | |
97 const WatchCallback& callback, | |
98 uintptr_t context, | |
99 RequestContext* request_context); | |
100 | |
101 virtual MojoResult CancelWatch(uintptr_t context); | |
66 | 102 |
67 ///////////// Message pipe API ///////////// | 103 ///////////// Message pipe API ///////////// |
68 | 104 |
69 virtual MojoResult WriteMessage(const void* bytes, | 105 virtual MojoResult WriteMessage(const void* bytes, |
70 uint32_t num_bytes, | 106 uint32_t num_bytes, |
71 const DispatcherInTransit* dispatchers, | 107 const DispatcherInTransit* dispatchers, |
72 uint32_t num_dispatchers, | 108 uint32_t num_dispatchers, |
73 MojoWriteMessageFlags flags); | 109 MojoWriteMessageFlags flags, |
110 RequestContext* request_context); | |
74 | 111 |
75 virtual MojoResult ReadMessage(void* bytes, | 112 virtual MojoResult ReadMessage(void* bytes, |
76 uint32_t* num_bytes, | 113 uint32_t* num_bytes, |
77 MojoHandle* handles, | 114 MojoHandle* handles, |
78 uint32_t* num_handles, | 115 uint32_t* num_handles, |
79 MojoReadMessageFlags flags); | 116 MojoReadMessageFlags flags, |
117 RequestContext* request_context); | |
80 | 118 |
81 ///////////// Shared buffer API ///////////// | 119 ///////////// Shared buffer API ///////////// |
82 | 120 |
83 // |options| may be null. |new_dispatcher| must not be null, but | 121 // |options| may be null. |new_dispatcher| must not be null, but |
84 // |*new_dispatcher| should be null (and will contain the dispatcher for the | 122 // |*new_dispatcher| should be null (and will contain the dispatcher for the |
85 // new handle on success). | 123 // new handle on success). |
86 virtual MojoResult DuplicateBufferHandle( | 124 virtual MojoResult DuplicateBufferHandle( |
87 const MojoDuplicateBufferHandleOptions* options, | 125 const MojoDuplicateBufferHandleOptions* options, |
88 scoped_refptr<Dispatcher>* new_dispatcher); | 126 scoped_refptr<Dispatcher>* new_dispatcher); |
89 | 127 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 // CompleteTransitAndClose() is called. In other words, if CancelTransit() is | 238 // CompleteTransitAndClose() is called. In other words, if CancelTransit() is |
201 // called, the implementation should retain its PlatformHandles in working | 239 // called, the implementation should retain its PlatformHandles in working |
202 // condition. | 240 // condition. |
203 virtual bool EndSerialize(void* destination, | 241 virtual bool EndSerialize(void* destination, |
204 ports::PortName* ports, | 242 ports::PortName* ports, |
205 PlatformHandle* handles); | 243 PlatformHandle* handles); |
206 | 244 |
207 // Does whatever is necessary to begin transit of the dispatcher. This | 245 // Does whatever is necessary to begin transit of the dispatcher. This |
208 // should return |true| if transit is OK, or false if the underlying resource | 246 // should return |true| if transit is OK, or false if the underlying resource |
209 // is deemed busy by the implementation. | 247 // is deemed busy by the implementation. |
210 virtual bool BeginTransit(); | 248 virtual bool BeginTransit(RequestContext* request_context); |
211 | 249 |
212 // Does whatever is necessary to complete transit of the dispatcher, including | 250 // Does whatever is necessary to complete transit of the dispatcher, including |
213 // closure. This is only called upon successfully transmitting an outgoing | 251 // closure. This is only called upon successfully transmitting an outgoing |
214 // message containing this serialized dispatcher. | 252 // message containing this serialized dispatcher. |
215 virtual void CompleteTransitAndClose(); | 253 virtual void CompleteTransitAndClose(RequestContext* request_context); |
216 | 254 |
217 // Does whatever is necessary to cancel transit of the dispatcher. The | 255 // Does whatever is necessary to cancel transit of the dispatcher. The |
218 // dispatcher should remain in a working state and resume normal operation. | 256 // dispatcher should remain in a working state and resume normal operation. |
219 virtual void CancelTransit(); | 257 virtual void CancelTransit(RequestContext* request_context); |
220 | 258 |
221 // Deserializes a specific dispatcher type from an incoming message. | 259 // Deserializes a specific dispatcher type from an incoming message. |
222 static scoped_refptr<Dispatcher> Deserialize( | 260 static scoped_refptr<Dispatcher> Deserialize( |
223 Type type, | 261 Type type, |
224 const void* bytes, | 262 const void* bytes, |
225 size_t num_bytes, | 263 size_t num_bytes, |
226 const ports::PortName* ports, | 264 const ports::PortName* ports, |
227 size_t num_ports, | 265 size_t num_ports, |
228 PlatformHandle* platform_handles, | 266 PlatformHandle* platform_handles, |
229 size_t num_platform_handles); | 267 size_t num_platform_handles); |
(...skipping 10 matching lines...) Expand all Loading... | |
240 // So logging macros and |DCHECK_EQ()|, etc. work. | 278 // So logging macros and |DCHECK_EQ()|, etc. work. |
241 MOJO_SYSTEM_IMPL_EXPORT inline std::ostream& operator<<(std::ostream& out, | 279 MOJO_SYSTEM_IMPL_EXPORT inline std::ostream& operator<<(std::ostream& out, |
242 Dispatcher::Type type) { | 280 Dispatcher::Type type) { |
243 return out << static_cast<int>(type); | 281 return out << static_cast<int>(type); |
244 } | 282 } |
245 | 283 |
246 } // namespace edk | 284 } // namespace edk |
247 } // namespace mojo | 285 } // namespace mojo |
248 | 286 |
249 #endif // MOJO_EDK_SYSTEM_DISPATCHER_H_ | 287 #endif // MOJO_EDK_SYSTEM_DISPATCHER_H_ |
OLD | NEW |