• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CORE_H_
6 #define MOJO_EDK_SYSTEM_CORE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory_handle.h"
16 #include "base/synchronization/lock.h"
17 #include "base/task_runner.h"
18 #include "mojo/edk/embedder/scoped_platform_handle.h"
19 #include "mojo/edk/system/dispatcher.h"
20 #include "mojo/edk/system/handle_signals_state.h"
21 #include "mojo/edk/system/handle_table.h"
22 #include "mojo/edk/system/mapping_table.h"
23 #include "mojo/edk/system/node_controller.h"
24 #include "mojo/edk/system/system_impl_export.h"
25 #include "mojo/public/c/system/buffer.h"
26 #include "mojo/public/c/system/data_pipe.h"
27 #include "mojo/public/c/system/message_pipe.h"
28 #include "mojo/public/c/system/platform_handle.h"
29 #include "mojo/public/c/system/types.h"
30 #include "mojo/public/cpp/system/message_pipe.h"
31 
32 namespace base {
33 class PortProvider;
34 }
35 
36 namespace mojo {
37 namespace edk {
38 
39 // |Core| is an object that implements the Mojo system calls. All public methods
40 // are thread-safe.
41 class MOJO_SYSTEM_IMPL_EXPORT Core {
42  public:
43   Core();
44   virtual ~Core();
45 
46   // Called exactly once, shortly after construction, and before any other
47   // methods are called on this object.
48   void SetIOTaskRunner(scoped_refptr<base::TaskRunner> io_task_runner);
49 
50   // Retrieves the NodeController for the current process.
51   NodeController* GetNodeController();
52 
53   scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle);
54 
55   void SetDefaultProcessErrorCallback(const ProcessErrorCallback& callback);
56 
57   // Called in the parent process any time a new child is launched.
58   void AddChild(base::ProcessHandle process_handle,
59                 ConnectionParams connection_params,
60                 const std::string& child_token,
61                 const ProcessErrorCallback& process_error_callback);
62 
63   // Called in the parent process when a child process fails to launch.
64   void ChildLaunchFailed(const std::string& child_token);
65 
66   // Called to connect to a peer process. This should be called only if there
67   // is no common ancestor for the processes involved within this mojo system.
68   // Both processes must call this function, each passing one end of a platform
69   // channel. This returns one end of a message pipe to each process.
70   ScopedMessagePipeHandle ConnectToPeerProcess(ScopedPlatformHandle pipe_handle,
71                                                const std::string& peer_token);
72   void ClosePeerConnection(const std::string& peer_token);
73 
74   // Called in a child process exactly once during early initialization.
75   void InitChild(ConnectionParams connection_params);
76 
77   // Creates a message pipe endpoint associated with |token|, which a child
78   // holding the token can later locate and connect to.
79   ScopedMessagePipeHandle CreateParentMessagePipe(
80       const std::string& token, const std::string& child_token);
81 
82   // Creates a message pipe endpoint and connects it to a pipe the parent has
83   // associated with |token|.
84   ScopedMessagePipeHandle CreateChildMessagePipe(const std::string& token);
85 
86   // Sets the mach port provider for this process.
87   void SetMachPortProvider(base::PortProvider* port_provider);
88 
89   MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher);
90 
91   // Adds new dispatchers for non-message-pipe handles received in a message.
92   // |dispatchers| and |handles| should be the same size.
93   bool AddDispatchersFromTransit(
94       const std::vector<Dispatcher::DispatcherInTransit>& dispatchers,
95       MojoHandle* handles);
96 
97   // See "mojo/edk/embedder/embedder.h" for more information on these functions.
98   MojoResult CreatePlatformHandleWrapper(ScopedPlatformHandle platform_handle,
99                                          MojoHandle* wrapper_handle);
100 
101   MojoResult PassWrappedPlatformHandle(MojoHandle wrapper_handle,
102                                        ScopedPlatformHandle* platform_handle);
103 
104   MojoResult CreateSharedBufferWrapper(
105       base::SharedMemoryHandle shared_memory_handle,
106       size_t num_bytes,
107       bool read_only,
108       MojoHandle* mojo_wrapper_handle);
109 
110   MojoResult PassSharedMemoryHandle(
111       MojoHandle mojo_handle,
112       base::SharedMemoryHandle* shared_memory_handle,
113       size_t* num_bytes,
114       bool* read_only);
115 
116   // Requests that the EDK tear itself down. |callback| will be called once
117   // the shutdown process is complete. Note that |callback| is always called
118   // asynchronously on the calling thread if said thread is running a message
119   // loop, and the calling thread must continue running a MessageLoop at least
120   // until the callback is called. If there is no running loop, the |callback|
121   // may be called from any thread. Beware!
122   void RequestShutdown(const base::Closure& callback);
123 
124   MojoResult SetProperty(MojoPropertyType type, const void* value);
125 
126   // ---------------------------------------------------------------------------
127 
128   // The following methods are essentially implementations of the Mojo Core
129   // functions of the Mojo API, with the C interface translated to C++ by
130   // "mojo/edk/embedder/entrypoints.cc". The best way to understand the contract
131   // of these methods is to look at the header files defining the corresponding
132   // API functions, referenced below.
133 
134   // These methods correspond to the API functions defined in
135   // "mojo/public/c/system/functions.h":
136   MojoTimeTicks GetTimeTicksNow();
137   MojoResult Close(MojoHandle handle);
138   MojoResult Wait(MojoHandle handle,
139                   MojoHandleSignals signals,
140                   MojoDeadline deadline,
141                   MojoHandleSignalsState* signals_state);
142   MojoResult WaitMany(const MojoHandle* handles,
143                       const MojoHandleSignals* signals,
144                       uint32_t num_handles,
145                       MojoDeadline deadline,
146                       uint32_t* result_index,
147                       MojoHandleSignalsState* signals_states);
148   MojoResult Watch(MojoHandle handle,
149                    MojoHandleSignals signals,
150                    MojoWatchCallback callback,
151                    uintptr_t context);
152   MojoResult CancelWatch(MojoHandle handle, uintptr_t context);
153   MojoResult AllocMessage(uint32_t num_bytes,
154                           const MojoHandle* handles,
155                           uint32_t num_handles,
156                           MojoAllocMessageFlags flags,
157                           MojoMessageHandle* message);
158   MojoResult FreeMessage(MojoMessageHandle message);
159   MojoResult GetMessageBuffer(MojoMessageHandle message, void** buffer);
160   MojoResult GetProperty(MojoPropertyType type, void* value);
161 
162   // These methods correspond to the API functions defined in
163   // "mojo/public/c/system/wait_set.h":
164   MojoResult CreateWaitSet(MojoHandle* wait_set_handle);
165   MojoResult AddHandle(MojoHandle wait_set_handle,
166                        MojoHandle handle,
167                        MojoHandleSignals signals);
168   MojoResult RemoveHandle(MojoHandle wait_set_handle,
169                           MojoHandle handle);
170   MojoResult GetReadyHandles(MojoHandle wait_set_handle,
171                              uint32_t* count,
172                              MojoHandle* handles,
173                              MojoResult* results,
174                              MojoHandleSignalsState* signals_states);
175 
176   // These methods correspond to the API functions defined in
177   // "mojo/public/c/system/message_pipe.h":
178   MojoResult CreateMessagePipe(
179       const MojoCreateMessagePipeOptions* options,
180       MojoHandle* message_pipe_handle0,
181       MojoHandle* message_pipe_handle1);
182   MojoResult WriteMessage(MojoHandle message_pipe_handle,
183                           const void* bytes,
184                           uint32_t num_bytes,
185                           const MojoHandle* handles,
186                           uint32_t num_handles,
187                           MojoWriteMessageFlags flags);
188   MojoResult WriteMessageNew(MojoHandle message_pipe_handle,
189                              MojoMessageHandle message,
190                              MojoWriteMessageFlags flags);
191   MojoResult ReadMessage(MojoHandle message_pipe_handle,
192                          void* bytes,
193                          uint32_t* num_bytes,
194                          MojoHandle* handles,
195                          uint32_t* num_handles,
196                          MojoReadMessageFlags flags);
197   MojoResult ReadMessageNew(MojoHandle message_pipe_handle,
198                             MojoMessageHandle* message,
199                             uint32_t* num_bytes,
200                             MojoHandle* handles,
201                             uint32_t* num_handles,
202                             MojoReadMessageFlags flags);
203   MojoResult FuseMessagePipes(MojoHandle handle0, MojoHandle handle1);
204   MojoResult NotifyBadMessage(MojoMessageHandle message,
205                               const char* error,
206                               size_t error_num_bytes);
207 
208   // These methods correspond to the API functions defined in
209   // "mojo/public/c/system/data_pipe.h":
210   MojoResult CreateDataPipe(
211       const MojoCreateDataPipeOptions* options,
212       MojoHandle* data_pipe_producer_handle,
213       MojoHandle* data_pipe_consumer_handle);
214   MojoResult WriteData(MojoHandle data_pipe_producer_handle,
215                        const void* elements,
216                        uint32_t* num_bytes,
217                        MojoWriteDataFlags flags);
218   MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle,
219                             void** buffer,
220                             uint32_t* buffer_num_bytes,
221                             MojoWriteDataFlags flags);
222   MojoResult EndWriteData(MojoHandle data_pipe_producer_handle,
223                           uint32_t num_bytes_written);
224   MojoResult ReadData(MojoHandle data_pipe_consumer_handle,
225                       void* elements,
226                       uint32_t* num_bytes,
227                       MojoReadDataFlags flags);
228   MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle,
229                            const void** buffer,
230                            uint32_t* buffer_num_bytes,
231                            MojoReadDataFlags flags);
232   MojoResult EndReadData(MojoHandle data_pipe_consumer_handle,
233                          uint32_t num_bytes_read);
234 
235   // These methods correspond to the API functions defined in
236   // "mojo/public/c/system/buffer.h":
237   MojoResult CreateSharedBuffer(
238       const MojoCreateSharedBufferOptions* options,
239       uint64_t num_bytes,
240       MojoHandle* shared_buffer_handle);
241   MojoResult DuplicateBufferHandle(
242       MojoHandle buffer_handle,
243       const MojoDuplicateBufferHandleOptions* options,
244       MojoHandle* new_buffer_handle);
245   MojoResult MapBuffer(MojoHandle buffer_handle,
246                        uint64_t offset,
247                        uint64_t num_bytes,
248                        void** buffer,
249                        MojoMapBufferFlags flags);
250   MojoResult UnmapBuffer(void* buffer);
251 
252   // These methods correspond to the API functions defined in
253   // "mojo/public/c/system/platform_handle.h".
254   MojoResult WrapPlatformHandle(const MojoPlatformHandle* platform_handle,
255                                 MojoHandle* mojo_handle);
256   MojoResult UnwrapPlatformHandle(MojoHandle mojo_handle,
257                                   MojoPlatformHandle* platform_handle);
258   MojoResult WrapPlatformSharedBufferHandle(
259       const MojoPlatformHandle* platform_handle,
260       size_t size,
261       MojoPlatformSharedBufferHandleFlags flags,
262       MojoHandle* mojo_handle);
263   MojoResult UnwrapPlatformSharedBufferHandle(
264       MojoHandle mojo_handle,
265       MojoPlatformHandle* platform_handle,
266       size_t* size,
267       MojoPlatformSharedBufferHandleFlags* flags);
268 
269   void GetActiveHandlesForTest(std::vector<MojoHandle>* handles);
270 
271  private:
272   MojoResult WaitManyInternal(const MojoHandle* handles,
273                               const MojoHandleSignals* signals,
274                               uint32_t num_handles,
275                               MojoDeadline deadline,
276                               uint32_t* result_index,
277                               HandleSignalsState* signals_states);
278 
279   // Used to pass ownership of our NodeController over to the IO thread in the
280   // event that we're torn down before said thread.
281   static void PassNodeControllerToIOThread(
282       std::unique_ptr<NodeController> node_controller);
283 
284   // Guards node_controller_.
285   //
286   // TODO(rockot): Consider removing this. It's only needed because we
287   // initialize node_controller_ lazily and that may happen on any thread.
288   // Otherwise it's effectively const and shouldn't need to be guarded.
289   //
290   // We can get rid of lazy initialization if we defer Mojo initialization far
291   // enough that zygotes don't do it. The zygote can't create a NodeController.
292   base::Lock node_controller_lock_;
293 
294   // This is lazily initialized on first access. Always use GetNodeController()
295   // to access it.
296   std::unique_ptr<NodeController> node_controller_;
297 
298   // The default callback to invoke, if any, when a process error is reported
299   // but cannot be associated with a specific process.
300   ProcessErrorCallback default_process_error_callback_;
301 
302   base::Lock handles_lock_;
303   HandleTable handles_;
304 
305   base::Lock mapping_table_lock_;  // Protects |mapping_table_|.
306   MappingTable mapping_table_;
307 
308   base::Lock property_lock_;
309   // Properties that can be read using the MojoGetProperty() API.
310   bool property_sync_call_allowed_ = true;
311 
312   DISALLOW_COPY_AND_ASSIGN(Core);
313 };
314 
315 }  // namespace edk
316 }  // namespace mojo
317 
318 #endif  // MOJO_EDK_SYSTEM_CORE_H_
319