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_CORE_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CORE_H_ |
6 #define MOJO_EDK_SYSTEM_CORE_H_ | 6 #define MOJO_EDK_SYSTEM_CORE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/task_runner.h" |
| 16 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 17 #include "mojo/edk/system/dispatcher.h" |
| 18 #include "mojo/edk/system/handle_signals_state.h" |
14 #include "mojo/edk/system/handle_table.h" | 19 #include "mojo/edk/system/handle_table.h" |
15 #include "mojo/edk/system/mapping_table.h" | 20 #include "mojo/edk/system/mapping_table.h" |
| 21 #include "mojo/edk/system/node_controller.h" |
16 #include "mojo/edk/system/system_impl_export.h" | 22 #include "mojo/edk/system/system_impl_export.h" |
17 #include "mojo/public/c/system/buffer.h" | 23 #include "mojo/public/c/system/buffer.h" |
18 #include "mojo/public/c/system/data_pipe.h" | 24 #include "mojo/public/c/system/data_pipe.h" |
19 #include "mojo/public/c/system/message_pipe.h" | 25 #include "mojo/public/c/system/message_pipe.h" |
20 #include "mojo/public/c/system/types.h" | 26 #include "mojo/public/c/system/types.h" |
21 #include "mojo/public/cpp/system/macros.h" | 27 #include "mojo/public/cpp/system/message_pipe.h" |
22 | 28 |
23 namespace mojo { | 29 namespace mojo { |
24 | |
25 namespace edk { | 30 namespace edk { |
26 | 31 |
27 class Dispatcher; | |
28 class PlatformSupport; | |
29 struct HandleSignalsState; | |
30 | |
31 // |Core| is an object that implements the Mojo system calls. All public methods | 32 // |Core| is an object that implements the Mojo system calls. All public methods |
32 // are thread-safe. | 33 // are thread-safe. |
33 class MOJO_SYSTEM_IMPL_EXPORT Core { | 34 class MOJO_SYSTEM_IMPL_EXPORT Core { |
34 public: | 35 public: |
35 // --------------------------------------------------------------------------- | 36 explicit Core(); |
36 | |
37 // These methods are only to be used by via the embedder API (and internally): | |
38 | |
39 // |*platform_support| must outlive this object. | |
40 explicit Core(PlatformSupport* platform_support); | |
41 virtual ~Core(); | 37 virtual ~Core(); |
42 | 38 |
43 // Adds |dispatcher| to the handle table, returning the handle for it. Returns | 39 // Called exactly once, shortly after construction, and before any other |
44 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full. | 40 // methods are called on this object. |
45 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); | 41 void SetIOTaskRunner(scoped_refptr<base::TaskRunner> io_task_runner); |
46 | 42 |
47 // Looks up the dispatcher for the given handle. Returns null if the handle is | 43 // Retrieves the NodeController for the current process. |
48 // invalid. | 44 NodeController* GetNodeController(); |
| 45 |
49 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); | 46 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); |
50 | 47 |
| 48 // Called in the parent process any time a new child is launched. |
| 49 void AddChild(base::ProcessHandle process_handle, |
| 50 ScopedPlatformHandle platform_handle); |
| 51 |
| 52 // Called in a child process exactly once during early initialization. |
| 53 void InitChild(ScopedPlatformHandle platform_handle); |
| 54 |
| 55 // These each create message pipe endpoints connected to an endpoint in a |
| 56 // remote embedder. |platform_handle| is used as a channel to negotiate the |
| 57 // connection. This is only here to facilitate legacy embedder code. See |
| 58 // mojo::edk::CreateMessagePipe in mojo/edk/embedder/embedder.h. |
| 59 void CreateParentMessagePipe( |
| 60 ScopedPlatformHandle platform_handle, |
| 61 const base::Callback<void(ScopedMessagePipeHandle)>& callback); |
| 62 void CreateChildMessagePipe( |
| 63 ScopedPlatformHandle platform_handle, |
| 64 const base::Callback<void(ScopedMessagePipeHandle)>& callback); |
| 65 |
| 66 // Creates a message pipe endpoint associated with |token|, which a child |
| 67 // holding the token can later locate and connect to. |
| 68 void CreateParentMessagePipe( |
| 69 const std::string& token, |
| 70 const base::Callback<void(ScopedMessagePipeHandle)>& callback); |
| 71 |
| 72 // Creates a message pipe endpoint associated with |token|, which will be |
| 73 // passed to the parent in order to find an associated remote port and connect |
| 74 // to it. |
| 75 void CreateChildMessagePipe( |
| 76 const std::string& token, |
| 77 const base::Callback<void(ScopedMessagePipeHandle)>& callback); |
| 78 |
| 79 MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher); |
| 80 |
| 81 // Adds new dispatchers for non-message-pipe handles received in a message. |
| 82 // |dispatchers| and |handles| should be the same size. |
| 83 bool AddDispatchersFromTransit( |
| 84 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers, |
| 85 MojoHandle* handles); |
| 86 |
| 87 MojoResult CreatePlatformHandleWrapper(ScopedPlatformHandle platform_handle, |
| 88 MojoHandle* wrapper_handle); |
| 89 |
| 90 MojoResult PassWrappedPlatformHandle(MojoHandle wrapper_handle, |
| 91 ScopedPlatformHandle* platform_handle); |
| 92 |
| 93 // Requests that the EDK tear itself down. |callback| will be called once |
| 94 // the shutdown process is complete. Note that |callback| is always called |
| 95 // asynchronously on the calling thread if said thread is running a message |
| 96 // loop, and the calling thread must continue running a MessageLoop at least |
| 97 // until the callback is called. If there is no running loop, the |callback| |
| 98 // may be called from any thread. Beware! |
| 99 void RequestShutdown(const base::Closure& callback); |
| 100 |
51 // Watches on the given handle for the given signals, calling |callback| when | 101 // Watches on the given handle for the given signals, calling |callback| when |
52 // a signal is satisfied or when all signals become unsatisfiable. |callback| | 102 // a signal is satisfied or when all signals become unsatisfiable. |callback| |
53 // must satisfy stringent requirements -- see |Awakable::Awake()| in | 103 // must satisfy stringent requirements -- see |Awakable::Awake()| in |
54 // awakable.h. In particular, it must not call any Mojo system functions. | 104 // awakable.h. In particular, it must not call any Mojo system functions. |
55 MojoResult AsyncWait(MojoHandle handle, | 105 MojoResult AsyncWait(MojoHandle handle, |
56 MojoHandleSignals signals, | 106 MojoHandleSignals signals, |
57 const base::Callback<void(MojoResult)>& callback); | 107 const base::Callback<void(MojoResult)>& callback); |
58 | 108 |
59 PlatformSupport* platform_support() const { | |
60 return platform_support_; | |
61 } | |
62 | |
63 // --------------------------------------------------------------------------- | 109 // --------------------------------------------------------------------------- |
64 | 110 |
65 // The following methods are essentially implementations of the Mojo Core | 111 // The following methods are essentially implementations of the Mojo Core |
66 // functions of the Mojo API, with the C interface translated to C++ by | 112 // functions of the Mojo API, with the C interface translated to C++ by |
67 // "mojo/edk/embedder/entrypoints.cc". The best way to understand the contract | 113 // "mojo/edk/embedder/entrypoints.cc". The best way to understand the contract |
68 // of these methods is to look at the header files defining the corresponding | 114 // of these methods is to look at the header files defining the corresponding |
69 // API functions, referenced below. | 115 // API functions, referenced below. |
70 | 116 |
71 // These methods correspond to the API functions defined in | 117 // These methods correspond to the API functions defined in |
72 // "mojo/public/c/system/functions.h": | 118 // "mojo/public/c/system/functions.h": |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 MojoHandle buffer_handle, | 199 MojoHandle buffer_handle, |
154 const MojoDuplicateBufferHandleOptions* options, | 200 const MojoDuplicateBufferHandleOptions* options, |
155 MojoHandle* new_buffer_handle); | 201 MojoHandle* new_buffer_handle); |
156 MojoResult MapBuffer(MojoHandle buffer_handle, | 202 MojoResult MapBuffer(MojoHandle buffer_handle, |
157 uint64_t offset, | 203 uint64_t offset, |
158 uint64_t num_bytes, | 204 uint64_t num_bytes, |
159 void** buffer, | 205 void** buffer, |
160 MojoMapBufferFlags flags); | 206 MojoMapBufferFlags flags); |
161 MojoResult UnmapBuffer(void* buffer); | 207 MojoResult UnmapBuffer(void* buffer); |
162 | 208 |
| 209 void GetActiveHandlesForTest(std::vector<MojoHandle>* handles); |
| 210 |
163 private: | 211 private: |
164 friend bool internal::ShutdownCheckNoLeaks(Core*); | |
165 | |
166 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic | |
167 // validation of arguments. |*result_index| is only set if the result (whether | |
168 // success or failure) applies to a specific handle, so its value should be | |
169 // preinitialized to |static_cast<uint32_t>(-1)|. | |
170 MojoResult WaitManyInternal(const MojoHandle* handles, | 212 MojoResult WaitManyInternal(const MojoHandle* handles, |
171 const MojoHandleSignals* signals, | 213 const MojoHandleSignals* signals, |
172 uint32_t num_handles, | 214 uint32_t num_handles, |
173 MojoDeadline deadline, | 215 MojoDeadline deadline, |
174 uint32_t* result_index, | 216 uint32_t *result_index, |
175 HandleSignalsState* signals_states); | 217 HandleSignalsState* signals_states); |
176 | 218 |
177 PlatformSupport* const platform_support_; | 219 // Used to pass ownership of our NodeController over to the IO thread in the |
| 220 // event that we're torn down before said thread. |
| 221 static void PassNodeControllerToIOThread( |
| 222 scoped_ptr<NodeController> node_controller); |
178 | 223 |
179 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we | 224 // This is lazily initialized on first access. Always use GetNodeController() |
180 // had them). | 225 // to access it. |
181 base::Lock handle_table_lock_; // Protects |handle_table_|. | 226 scoped_ptr<NodeController> node_controller_; |
182 HandleTable handle_table_; | 227 |
| 228 base::Lock handles_lock_; |
| 229 HandleTable handles_; |
183 | 230 |
184 base::Lock mapping_table_lock_; // Protects |mapping_table_|. | 231 base::Lock mapping_table_lock_; // Protects |mapping_table_|. |
185 MappingTable mapping_table_; | 232 MappingTable mapping_table_; |
186 | 233 |
187 MOJO_DISALLOW_COPY_AND_ASSIGN(Core); | 234 DISALLOW_COPY_AND_ASSIGN(Core); |
188 }; | 235 }; |
189 | 236 |
190 } // namespace edk | 237 } // namespace edk |
191 } // namespace mojo | 238 } // namespace mojo |
192 | 239 |
193 #endif // MOJO_EDK_SYSTEM_CORE_H_ | 240 #endif // MOJO_EDK_SYSTEM_CORE_H_ |
OLD | NEW |