| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_EMBEDDER_EMBEDDER_H_ | 5 #ifndef MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
| 6 #define MOJO_EDK_EMBEDDER_EMBEDDER_H_ | 6 #define MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
| 7 | 7 |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | |
| 11 | 10 |
| 12 #include "mojo/edk/embedder/channel_info_forward.h" | |
| 13 #include "mojo/edk/embedder/process_type.h" | |
| 14 #include "mojo/edk/embedder/slave_info.h" | |
| 15 #include "mojo/edk/platform/scoped_platform_handle.h" | 11 #include "mojo/edk/platform/scoped_platform_handle.h" |
| 16 #include "mojo/edk/platform/task_runner.h" | 12 #include "mojo/public/c/system/types.h" |
| 17 #include "mojo/public/cpp/system/message_pipe.h" | |
| 18 | 13 |
| 19 namespace mojo { | 14 namespace mojo { |
| 20 | 15 |
| 21 namespace platform { | |
| 22 class PlatformHandleWatcher; | |
| 23 } | |
| 24 | |
| 25 namespace embedder { | 16 namespace embedder { |
| 26 | 17 |
| 27 struct Configuration; | 18 struct Configuration; |
| 28 class PlatformSupport; | 19 class PlatformSupport; |
| 29 class ProcessDelegate; | |
| 30 | 20 |
| 31 // Basic configuration/initialization ------------------------------------------ | 21 // Basic configuration/initialization ------------------------------------------ |
| 32 | 22 |
| 33 // |Init()| sets up the basic Mojo system environment, making the |Mojo...()| | 23 // |Init()| sets up the basic Mojo system environment, making the |Mojo...()| |
| 34 // functions available and functional. This is never shut down (except in tests | 24 // functions available and functional. This is never shut down (except in tests |
| 35 // -- see test_embedder.h). | 25 // -- see test_embedder.h). |
| 36 | 26 |
| 37 // Returns the global configuration. In general, you should not need to change | 27 // Returns the global configuration. In general, you should not need to change |
| 38 // the configuration, but if you do you must do it before calling |Init()|. | 28 // the configuration, but if you do you must do it before calling |Init()|. |
| 39 Configuration* GetConfiguration(); | 29 Configuration* GetConfiguration(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 63 platform::ScopedPlatformHandle platform_handle, | 53 platform::ScopedPlatformHandle platform_handle, |
| 64 MojoHandle* platform_handle_wrapper_handle); | 54 MojoHandle* platform_handle_wrapper_handle); |
| 65 | 55 |
| 66 // Retrieves the |PlatformHandle| that was wrapped into a |MojoHandle| (using | 56 // Retrieves the |PlatformHandle| that was wrapped into a |MojoHandle| (using |
| 67 // |CreatePlatformHandleWrapper()| above). Note that the |MojoHandle| must still | 57 // |CreatePlatformHandleWrapper()| above). Note that the |MojoHandle| must still |
| 68 // be closed separately. | 58 // be closed separately. |
| 69 MojoResult PassWrappedPlatformHandle( | 59 MojoResult PassWrappedPlatformHandle( |
| 70 MojoHandle platform_handle_wrapper_handle, | 60 MojoHandle platform_handle_wrapper_handle, |
| 71 platform::ScopedPlatformHandle* platform_handle); | 61 platform::ScopedPlatformHandle* platform_handle); |
| 72 | 62 |
| 73 // Initialialization/shutdown for interprocess communication (IPC) ------------- | |
| 74 | |
| 75 // |InitIPCSupport()| sets up the subsystem for interprocess communication, | |
| 76 // making the IPC functions (in the following section) available and functional. | |
| 77 // (This may only be done after |Init()|.) | |
| 78 // | |
| 79 // This subsystem may be shut down, using |ShutdownIPCSupportOnIOThread()| or | |
| 80 // |ShutdownIPCSupport()|. None of the IPC functions may be called while or | |
| 81 // after either of these is called. | |
| 82 | |
| 83 // Initializes a process of the given type; to be called after |Init()|. | |
| 84 // - |process_delegate| must be a process delegate of the appropriate type | |
| 85 // corresponding to |process_type|; its methods will be called on | |
| 86 // |delegate_thread_task_runner|. | |
| 87 // - |delegate_thread_task_runner|, |process_delegate|, |io_task_runner|, | |
| 88 // and |io_watcher| should live at least until |ShutdownIPCSupport()|'s | |
| 89 // callback has been run or |ShutdownIPCSupportOnIOThread()| has completed. | |
| 90 // - For slave processes (i.e., |process_type| is |ProcessType::SLAVE|), | |
| 91 // |platform_handle| should be connected to the handle passed to | |
| 92 // |ConnectToSlave()| (in the master process). For other processes, | |
| 93 // |platform_handle| is ignored (and should not be valid). | |
| 94 void InitIPCSupport( | |
| 95 ProcessType process_type, | |
| 96 util::RefPtr<platform::TaskRunner>&& delegate_thread_task_runner, | |
| 97 ProcessDelegate* process_delegate, | |
| 98 util::RefPtr<platform::TaskRunner>&& io_task_runner, | |
| 99 platform::PlatformHandleWatcher* io_watcher, | |
| 100 platform::ScopedPlatformHandle platform_handle); | |
| 101 | |
| 102 // Shuts down the subsystem initialized by |InitIPCSupport()|. This must be | |
| 103 // called on the I/O thread (given to |InitIPCSupport()|). This completes | |
| 104 // synchronously and does not result in a call to the process delegate's | |
| 105 // |OnShutdownComplete()|. | |
| 106 void ShutdownIPCSupportOnIOThread(); | |
| 107 | |
| 108 // Like |ShutdownIPCSupportOnIOThread()|, but may be called from any thread, | |
| 109 // signaling shutdown completion via the process delegate's | |
| 110 // |OnShutdownComplete()|. | |
| 111 void ShutdownIPCSupport(); | |
| 112 | |
| 113 // Interprocess communication (IPC) functions ---------------------------------- | |
| 114 | |
| 115 // Called in the master process to connect to a slave process to the IPC system. | |
| 116 // (This should only be called in a process initialized (using | |
| 117 // |InitIPCSupport()|) with process type |ProcessType::MASTER|.) | |
| 118 // | |
| 119 // This should typically be called *before* the slave process is even created. | |
| 120 // It requires an OS "pipe" to be established between the master and slave | |
| 121 // processes, with |platform_handle| being a handle to the end that remains on | |
| 122 // the master. | |
| 123 // | |
| 124 // This will establish a channel and an initial message pipe (to which it | |
| 125 // returns a handle), an ID string (returned in |*platform_connection_id|) that | |
| 126 // must be passed to the slave (e.g., on the command line), and a | |
| 127 // |ChannelInfo*| (in |*channel_info|) which should eventually be given to | |
| 128 // |DestroyChannel()|/|DestroyChannelOnIOThread()|, but only after | |
| 129 // |did_connect_to_slave_callback| has been run. | |
| 130 // | |
| 131 // |did_connect_to_slave_callback| will be run either using | |
| 132 // |did_connect_to_slave_runner| (if non-null) or on the I/O thread, once the | |
| 133 // |ChannelInfo*| is valid. | |
| 134 // | |
| 135 // TODO(vtl): The API is a little crazy with respect to the |ChannelInfo*|. | |
| 136 ScopedMessagePipeHandle ConnectToSlave( | |
| 137 SlaveInfo slave_info, | |
| 138 platform::ScopedPlatformHandle platform_handle, | |
| 139 std::function<void()>&& did_connect_to_slave_callback, | |
| 140 util::RefPtr<platform::TaskRunner>&& did_connect_to_slave_runner, | |
| 141 std::string* platform_connection_id, | |
| 142 ChannelInfo** channel_info); | |
| 143 | |
| 144 // Called in a slave process to connect it to the IPC system. (This should only | |
| 145 // be called in a process initialized (using |InitIPCSupport()|) with process | |
| 146 // type |ProcessType::SLAVE|.) This should be called exactly once in each slave | |
| 147 // process. | |
| 148 // | |
| 149 // See |ConnectToSlave()| for details. (Note that if this fails in any way, | |
| 150 // e.g., if |platform_connection_id| is invalid, this will CHECK-fail and | |
| 151 // terminate the process.) |channel_info|, |did_connect_to_master_callback|, and | |
| 152 // |did_connect_to_master_runner| are analagous to in |ConnectToSlave()|. | |
| 153 // | |
| 154 // TODO(vtl): The API is a little crazy with respect to the |ChannelInfo*|. | |
| 155 ScopedMessagePipeHandle ConnectToMaster( | |
| 156 const std::string& platform_connection_id, | |
| 157 std::function<void()>&& did_connect_to_master_callback, | |
| 158 util::RefPtr<platform::TaskRunner>&& did_connect_to_master_runner, | |
| 159 ChannelInfo** channel_info); | |
| 160 | |
| 161 // A "channel" is a connection on top of an OS "pipe", on top of which Mojo | |
| 162 // message pipes (etc.) can be multiplexed. It must "live" on some I/O thread. | |
| 163 // | |
| 164 // There are two channel creation APIs: |CreateChannelOnIOThread()| creates a | |
| 165 // channel synchronously and must be called from the I/O thread, while | |
| 166 // |CreateChannel()| is asynchronous and may be called from any thread. | |
| 167 // |DestroyChannel()| is used to destroy the channel in either case and may be | |
| 168 // called from any thread, but completes synchronously when called from the I/O | |
| 169 // thread. | |
| 170 // | |
| 171 // Both creation functions have a |platform_handle| argument, which should be an | |
| 172 // OS-dependent handle to one side of a suitable bidirectional OS "pipe" (e.g., | |
| 173 // a file descriptor to a Unix domain socket); this "pipe" should be connected | |
| 174 // and ready for operation (e.g., to be written to or read from). | |
| 175 // | |
| 176 // Both (synchronously) return a handle to the bootstrap message pipe on the | |
| 177 // channel that was (or is to be) created, or |MOJO_HANDLE_INVALID| on error | |
| 178 // (but note that this will happen only if, e.g., the handle table is full). | |
| 179 // This message pipe may be used immediately, but since channel operation | |
| 180 // actually begins asynchronously, other errors may still occur (e.g., if the | |
| 181 // other end of the "pipe" is closed) and be reported in the usual way to the | |
| 182 // returned handle. | |
| 183 // | |
| 184 // (E.g., a message written immediately to the returned handle will be queued | |
| 185 // and the handle immediately closed, before the channel begins operation. In | |
| 186 // this case, the channel should connect as usual, send the queued message, and | |
| 187 // report that the handle was closed to the other side. The message sent may | |
| 188 // have other handles, so there may still be message pipes "on" this channel.) | |
| 189 // | |
| 190 // Both also produce a |ChannelInfo*| (a pointer to an opaque object) -- the | |
| 191 // first synchronously and second asynchronously. | |
| 192 // | |
| 193 // The destruction functions are similarly synchronous and asynchronous, | |
| 194 // respectively, and take the |ChannelInfo*| produced by the creation functions. | |
| 195 | |
| 196 // Creates a channel; must only be called from the I/O thread. |platform_handle| | |
| 197 // should be a handle to a connected OS "pipe". Eventually (even on failure), | |
| 198 // the "out" value |*channel_info| should be passed to |DestoryChannel()| to | |
| 199 // tear down the channel. Returns a handle to the bootstrap message pipe. | |
| 200 ScopedMessagePipeHandle CreateChannelOnIOThread( | |
| 201 platform::ScopedPlatformHandle platform_handle, | |
| 202 ChannelInfo** channel_info); | |
| 203 | |
| 204 // Creates a channel asynchronously; may be called from any thread. | |
| 205 // |platform_handle| should be a handle to a connected OS "pipe". | |
| 206 // |did_create_channel_callback| should be the callback to call with the | |
| 207 // |ChannelInfo*|, which should eventually be passed to |DestroyChannel()| to | |
| 208 // tear down the channel; the callback will be called using | |
| 209 // |did_create_channel_runner| if that is non-null, or otherwise it will be | |
| 210 // posted to the I/O thread. Returns a handle to the bootstrap message pipe. | |
| 211 // | |
| 212 // Note: This should only be used to establish a channel with a process of type | |
| 213 // |ProcessType::NONE|. This function may be removed in the future. | |
| 214 ScopedMessagePipeHandle CreateChannel( | |
| 215 platform::ScopedPlatformHandle platform_handle, | |
| 216 std::function<void(ChannelInfo*)>&& did_create_channel_callback, | |
| 217 util::RefPtr<platform::TaskRunner>&& did_create_channel_runner); | |
| 218 | |
| 219 // Destroys a channel that was created using |ConnectToMaster()|, | |
| 220 // |ConnectToSlave()|, |CreateChannel()|, or |CreateChannelOnIOThread()|; must | |
| 221 // be called from the channel's I'O thread. Completes synchronously (and posts | |
| 222 // no tasks). | |
| 223 void DestroyChannelOnIOThread(ChannelInfo* channel_info); | |
| 224 | |
| 225 // Like |DestroyChannelOnIOThread()|, but asynchronous and may be called from | |
| 226 // any thread. The callback will be called using |did_destroy_channel_runner| | |
| 227 // if that is non-null, or otherwise it will be called on the "channel thread". | |
| 228 // The "channel thread" must remain alive and continue to process tasks until | |
| 229 // the callback has been executed. | |
| 230 void DestroyChannel( | |
| 231 ChannelInfo* channel_info, | |
| 232 std::function<void()>&& did_destroy_channel_callback, | |
| 233 util::RefPtr<platform::TaskRunner>&& did_destroy_channel_runner); | |
| 234 | |
| 235 // Inform the channel that it will soon be destroyed (doing so is optional). | |
| 236 // This may be called from any thread, but the caller must ensure that this is | |
| 237 // called before |DestroyChannel()|. | |
| 238 void WillDestroyChannelSoon(ChannelInfo* channel_info); | |
| 239 | |
| 240 } // namespace embedder | 63 } // namespace embedder |
| 241 } // namespace mojo | 64 } // namespace mojo |
| 242 | 65 |
| 243 #endif // MOJO_EDK_EMBEDDER_EMBEDDER_H_ | 66 #endif // MOJO_EDK_EMBEDDER_EMBEDDER_H_ |
| OLD | NEW |