Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 5 | #ifndef MOJO_CORE_CORE_H_ |
| 6 | #define MOJO_CORE_CORE_H_ |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 7 | |
dcheng | 40c732a8 | 2016-04-22 00:57:38 | [diff] [blame] | 8 | #include <memory> |
lhchavez | 723b70d | 2016-12-22 20:42:49 | [diff] [blame] | 9 | #include <string> |
Hyowon Kim | 624a7aa7 | 2023-12-05 07:56:28 | [diff] [blame] | 10 | #include <string_view> |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 11 | #include <vector> |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 12 | |
Avi Drissman | d70f89a | 2023-01-11 23:52:55 | [diff] [blame] | 13 | #include "base/functional/callback.h" |
Lei Zhang | b856768 | 2023-01-06 17:54:04 | [diff] [blame] | 14 | #include "base/memory/scoped_refptr.h" |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.h" |
Ken Rockot | 0e45f80 | 2018-06-27 23:56:01 | [diff] [blame] | 17 | #include "build/build_config.h" |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 18 | #include "mojo/core/dispatcher.h" |
| 19 | #include "mojo/core/handle_signals_state.h" |
| 20 | #include "mojo/core/handle_table.h" |
| 21 | #include "mojo/core/node_controller.h" |
| 22 | #include "mojo/core/system_impl_export.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 23 | #include "mojo/public/c/system/buffer.h" |
| 24 | #include "mojo/public/c/system/data_pipe.h" |
Ken Rockot | f86cdb8b | 2018-05-19 23:35:01 | [diff] [blame] | 25 | #include "mojo/public/c/system/invitation.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 26 | #include "mojo/public/c/system/message_pipe.h" |
rockot | 28a287e | 2016-05-24 02:51:37 | [diff] [blame] | 27 | #include "mojo/public/c/system/platform_handle.h" |
Ken Rockot | 0db0bcf | 2018-07-12 22:01:12 | [diff] [blame] | 28 | #include "mojo/public/c/system/quota.h" |
Ken Rockot | 13e55a3 | 2018-03-09 00:11:19 | [diff] [blame] | 29 | #include "mojo/public/c/system/trap.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 30 | #include "mojo/public/c/system/types.h" |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 31 | |
| 32 | namespace mojo { |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 33 | namespace core { |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 34 | |
Ken Rockot | 062ff1a5 | 2018-04-23 20:42:38 | [diff] [blame] | 35 | class PlatformSharedMemoryMapping; |
| 36 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 37 | // |Core| is an object that implements the Mojo system calls. All public methods |
| 38 | // are thread-safe. |
| 39 | class MOJO_SYSTEM_IMPL_EXPORT Core { |
| 40 | public: |
lhchavez | 723b70d | 2016-12-22 20:42:49 | [diff] [blame] | 41 | Core(); |
Peter Boström | a817628 | 2021-09-23 22:33:56 | [diff] [blame] | 42 | |
| 43 | Core(const Core&) = delete; |
| 44 | Core& operator=(const Core&) = delete; |
| 45 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 46 | virtual ~Core(); |
| 47 | |
Ken Rockot | 898d796d5 | 2018-03-23 19:03:52 | [diff] [blame] | 48 | static Core* Get(); |
| 49 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 50 | // Called exactly once, shortly after construction, and before any other |
| 51 | // methods are called on this object. |
Gabriel Charette | e926fc1 | 2019-12-16 19:00:02 | [diff] [blame] | 52 | void SetIOTaskRunner( |
| 53 | scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 54 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 55 | // Retrieves the NodeController for the current process. |
| 56 | NodeController* GetNodeController(); |
| 57 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 58 | scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); |
Ken Rockot | 062ff1a5 | 2018-04-23 20:42:38 | [diff] [blame] | 59 | scoped_refptr<Dispatcher> GetAndRemoveDispatcher(MojoHandle handle); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 60 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 61 | // Creates a message pipe endpoint with an unbound peer port returned in |
| 62 | // |*peer|. Useful for setting up cross-process bootstrap message pipes. The |
| 63 | // returned message pipe handle is usable immediately by the caller. |
| 64 | // |
| 65 | // The value returned in |*peer| may be passed along with a broker client |
| 66 | // invitation. See SendBrokerClientInvitation() below. |
Ken Rockot | d86d1e4 | 2018-03-15 22:47:43 | [diff] [blame] | 67 | MojoHandle CreatePartialMessagePipe(ports::PortRef* peer); |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 68 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 69 | // Like above but exchanges an existing ports::PortRef for a message pipe |
| 70 | // handle which wraps it. |
Ken Rockot | d86d1e4 | 2018-03-15 22:47:43 | [diff] [blame] | 71 | MojoHandle CreatePartialMessagePipe(const ports::PortRef& port); |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 72 | |
| 73 | // Sends a broker client invitation to |target_process| over the connection |
| 74 | // medium in |connection_params|. The other end of the connection medium in |
| 75 | // |connection_params| can be used within the target process to call |
| 76 | // AcceptBrokerClientInvitation() and complete the process's admission into |
| 77 | // this process graph. |
| 78 | // |
| 79 | // |attached_ports| is a list of named port references to be attached to the |
| 80 | // invitation. An attached port can be claimed (as a message pipe handle) by |
| 81 | // the invitee. |
| 82 | void SendBrokerClientInvitation( |
Robert Sesek | 0e7f165a | 2020-11-20 21:31:45 | [diff] [blame] | 83 | base::Process target_process, |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 84 | ConnectionParams connection_params, |
| 85 | const std::vector<std::pair<std::string, ports::PortRef>>& attached_ports, |
| 86 | const ProcessErrorCallback& process_error_callback); |
| 87 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 88 | // Extracts a named message pipe endpoint from the broker client invitation |
| 89 | // accepted by this process. Must only be called after |
| 90 | // AcceptBrokerClientInvitation. |
Ken Rockot | d86d1e4 | 2018-03-15 22:47:43 | [diff] [blame] | 91 | MojoHandle ExtractMessagePipeFromInvitation(const std::string& name); |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 92 | |
sammc | b0a39f8c | 2016-08-10 06:29:54 | [diff] [blame] | 93 | // Called to connect to a peer process. This should be called only if there |
| 94 | // is no common ancestor for the processes involved within this mojo system. |
| 95 | // Both processes must call this function, each passing one end of a platform |
Ken Rockot | 47f4e89 | 2017-05-18 20:30:21 | [diff] [blame] | 96 | // channel. |port| is a port to be merged with the remote peer's port, which |
| 97 | // it will provide via the same API. |
| 98 | // |
Ken Rockot | 277b84c | 2018-06-08 09:39:48 | [diff] [blame] | 99 | // |connection_name| if non-empty guarantees that no other isolated |
| 100 | // connections exist in the calling process using the same name. This is |
| 101 | // useful for invitation endpoints that use a named server accepting multiple |
| 102 | // connections. |
| 103 | void ConnectIsolated(ConnectionParams connection_params, |
| 104 | const ports::PortRef& port, |
Hyowon Kim | 85512d9e | 2023-11-30 01:36:59 | [diff] [blame] | 105 | std::string_view connection_name); |
sammc | b0a39f8c | 2016-08-10 06:29:54 | [diff] [blame] | 106 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 107 | MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher); |
| 108 | |
| 109 | // Adds new dispatchers for non-message-pipe handles received in a message. |
| 110 | // |dispatchers| and |handles| should be the same size. |
| 111 | bool AddDispatchersFromTransit( |
| 112 | const std::vector<Dispatcher::DispatcherInTransit>& dispatchers, |
| 113 | MojoHandle* handles); |
| 114 | |
Ken Rockot | 15b2b0b | 2017-06-12 20:36:05 | [diff] [blame] | 115 | // Marks a set of handles as busy and acquires references to each of their |
| 116 | // dispatchers. The caller MUST eventually call ReleaseDispatchersForTransit() |
Ken Rockot | 766e766 | 2017-08-10 05:37:15 | [diff] [blame] | 117 | // on the resulting |*dispatchers|. Note that |*dispatchers| contents are |
| 118 | // extended, not replaced, by this call. |
Ken Rockot | 15b2b0b | 2017-06-12 20:36:05 | [diff] [blame] | 119 | MojoResult AcquireDispatchersForTransit( |
| 120 | const MojoHandle* handles, |
| 121 | size_t num_handles, |
| 122 | std::vector<Dispatcher::DispatcherInTransit>* dispatchers); |
| 123 | |
| 124 | // Releases dispatchers previously acquired by |
| 125 | // |AcquireDispatchersForTransit()|. |in_transit| should be |true| if the |
| 126 | // caller has fully serialized every dispatcher in |dispatchers|, in which |
| 127 | // case this will close and remove their handles from the handle table. |
| 128 | // |
| 129 | // If |in_transit| is false, this simply unmarks the dispatchers as busy, |
| 130 | // making them available for general use once again. |
| 131 | void ReleaseDispatchersForTransit( |
| 132 | const std::vector<Dispatcher::DispatcherInTransit>& dispatchers, |
| 133 | bool in_transit); |
| 134 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 135 | // Requests that the EDK tear itself down. |callback| will be called once |
| 136 | // the shutdown process is complete. Note that |callback| is always called |
| 137 | // asynchronously on the calling thread if said thread is running a message |
| 138 | // loop, and the calling thread must continue running a MessageLoop at least |
| 139 | // until the callback is called. If there is no running loop, the |callback| |
| 140 | // may be called from any thread. Beware! |
Anand K. Mistry | b6558d9 | 2019-11-07 01:16:27 | [diff] [blame] | 141 | void RequestShutdown(base::OnceClosure callback); |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 142 | |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 143 | // --------------------------------------------------------------------------- |
| 144 | |
| 145 | // The following methods are essentially implementations of the Mojo Core |
| 146 | // functions of the Mojo API, with the C interface translated to C++ by |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 147 | // "mojo/core/embedder/entrypoints.cc". The best way to understand the |
| 148 | // contract of these methods is to look at the header files defining the |
| 149 | // corresponding API functions, referenced below. |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 150 | |
| 151 | // These methods correspond to the API functions defined in |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 152 | // "mojo/public/c/system/functions.h": |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 153 | MojoTimeTicks GetTimeTicksNow(); |
| 154 | MojoResult Close(MojoHandle handle); |
rockot | c794954 | 2017-03-16 09:03:47 | [diff] [blame] | 155 | MojoResult QueryHandleSignalsState(MojoHandle handle, |
| 156 | MojoHandleSignalsState* signals_state); |
Ken Rockot | 13e55a3 | 2018-03-09 00:11:19 | [diff] [blame] | 157 | MojoResult CreateTrap(MojoTrapEventHandler handler, |
| 158 | const MojoCreateTrapOptions* options, |
| 159 | MojoHandle* trap_handle); |
| 160 | MojoResult AddTrigger(MojoHandle trap_handle, |
| 161 | MojoHandle handle, |
| 162 | MojoHandleSignals signals, |
| 163 | MojoTriggerCondition condition, |
| 164 | uintptr_t context, |
| 165 | const MojoAddTriggerOptions* options); |
| 166 | MojoResult RemoveTrigger(MojoHandle trap_handle, |
| 167 | uintptr_t context, |
| 168 | const MojoRemoveTriggerOptions* options); |
| 169 | MojoResult ArmTrap(MojoHandle trap_handle, |
| 170 | const MojoArmTrapOptions* options, |
Ken Rockot | 7831a95 | 2018-07-12 01:11:30 | [diff] [blame] | 171 | uint32_t* num_blocking_events, |
| 172 | MojoTrapEvent* blocking_events); |
Ken Rockot | f3208fc | 2018-03-07 20:35:08 | [diff] [blame] | 173 | MojoResult CreateMessage(const MojoCreateMessageOptions* options, |
| 174 | MojoMessageHandle* message_handle); |
Ken Rockot | a2af2b23 | 2017-06-16 05:54:31 | [diff] [blame] | 175 | MojoResult DestroyMessage(MojoMessageHandle message_handle); |
Ken Rockot | f3208fc | 2018-03-07 20:35:08 | [diff] [blame] | 176 | MojoResult SerializeMessage(MojoMessageHandle message_handle, |
| 177 | const MojoSerializeMessageOptions* options); |
Andrea Orru | 96e9434 | 2024-03-16 01:45:33 | [diff] [blame] | 178 | MojoResult ReserveMessageCapacity(MojoMessageHandle message_handle, |
| 179 | uint32_t payload_buffer_size, |
| 180 | uint32_t* buffer_size); |
Ken Rockot | 8e49dd7 | 2018-03-07 18:06:40 | [diff] [blame] | 181 | MojoResult AppendMessageData(MojoMessageHandle message_handle, |
| 182 | uint32_t additional_payload_size, |
| 183 | const MojoHandle* handles, |
| 184 | uint32_t num_handles, |
| 185 | const MojoAppendMessageDataOptions* options, |
| 186 | void** buffer, |
| 187 | uint32_t* buffer_size); |
| 188 | MojoResult GetMessageData(MojoMessageHandle message_handle, |
| 189 | const MojoGetMessageDataOptions* options, |
| 190 | void** buffer, |
| 191 | uint32_t* num_bytes, |
| 192 | MojoHandle* handles, |
| 193 | uint32_t* num_handles); |
Ken Rockot | f3208fc | 2018-03-07 20:35:08 | [diff] [blame] | 194 | MojoResult SetMessageContext(MojoMessageHandle message_handle, |
| 195 | uintptr_t context, |
| 196 | MojoMessageContextSerializer serializer, |
| 197 | MojoMessageContextDestructor destructor, |
| 198 | const MojoSetMessageContextOptions* options); |
Ken Rockot | 65a6f7e | 2017-06-30 21:59:03 | [diff] [blame] | 199 | MojoResult GetMessageContext(MojoMessageHandle message_handle, |
Ken Rockot | f3208fc | 2018-03-07 20:35:08 | [diff] [blame] | 200 | const MojoGetMessageContextOptions* options, |
| 201 | uintptr_t* context); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 202 | |
| 203 | // These methods correspond to the API functions defined in |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 204 | // "mojo/public/c/system/message_pipe.h": |
Ken Rockot | 61a3aab | 2017-06-20 16:19:57 | [diff] [blame] | 205 | MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, |
| 206 | MojoHandle* message_pipe_handle0, |
| 207 | MojoHandle* message_pipe_handle1); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 208 | MojoResult WriteMessage(MojoHandle message_pipe_handle, |
Ken Rockot | 349df72 | 2017-06-16 05:48:49 | [diff] [blame] | 209 | MojoMessageHandle message_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 210 | const MojoWriteMessageOptions* options); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 211 | MojoResult ReadMessage(MojoHandle message_pipe_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 212 | const MojoReadMessageOptions* options, |
| 213 | MojoMessageHandle* message_handle); |
| 214 | MojoResult FuseMessagePipes(MojoHandle handle0, |
| 215 | MojoHandle handle1, |
| 216 | const MojoFuseMessagePipesOptions* options); |
Ken Rockot | 5d98372 | 2017-05-31 04:46:57 | [diff] [blame] | 217 | MojoResult NotifyBadMessage(MojoMessageHandle message_handle, |
rockot | b46bf91 | 2016-06-11 23:18:16 | [diff] [blame] | 218 | const char* error, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 219 | size_t error_num_bytes, |
| 220 | const MojoNotifyBadMessageOptions* options); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 221 | |
| 222 | // These methods correspond to the API functions defined in |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 223 | // "mojo/public/c/system/data_pipe.h": |
Ken Rockot | 61a3aab | 2017-06-20 16:19:57 | [diff] [blame] | 224 | MojoResult CreateDataPipe(const MojoCreateDataPipeOptions* options, |
| 225 | MojoHandle* data_pipe_producer_handle, |
| 226 | MojoHandle* data_pipe_consumer_handle); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 227 | MojoResult WriteData(MojoHandle data_pipe_producer_handle, |
| 228 | const void* elements, |
| 229 | uint32_t* num_bytes, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 230 | const MojoWriteDataOptions* options); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 231 | MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 232 | const MojoBeginWriteDataOptions* options, |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 233 | void** buffer, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 234 | uint32_t* buffer_num_bytes); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 235 | MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 236 | uint32_t num_bytes_written, |
| 237 | const MojoEndWriteDataOptions* options); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 238 | MojoResult ReadData(MojoHandle data_pipe_consumer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 239 | const MojoReadDataOptions* options, |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 240 | void* elements, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 241 | uint32_t* num_bytes); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 242 | MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 243 | const MojoBeginReadDataOptions* options, |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 244 | const void** buffer, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 245 | uint32_t* buffer_num_bytes); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 246 | MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 247 | uint32_t num_bytes_read, |
| 248 | const MojoEndReadDataOptions* options); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 249 | |
| 250 | // These methods correspond to the API functions defined in |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 251 | // "mojo/public/c/system/buffer.h": |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 252 | MojoResult CreateSharedBuffer(uint64_t num_bytes, |
| 253 | const MojoCreateSharedBufferOptions* options, |
Ken Rockot | 61a3aab | 2017-06-20 16:19:57 | [diff] [blame] | 254 | MojoHandle* shared_buffer_handle); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 255 | MojoResult DuplicateBufferHandle( |
| 256 | MojoHandle buffer_handle, |
| 257 | const MojoDuplicateBufferHandleOptions* options, |
| 258 | MojoHandle* new_buffer_handle); |
| 259 | MojoResult MapBuffer(MojoHandle buffer_handle, |
| 260 | uint64_t offset, |
| 261 | uint64_t num_bytes, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 262 | const MojoMapBufferOptions* options, |
| 263 | void** buffer); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 264 | MojoResult UnmapBuffer(void* buffer); |
Lei Zhang | 7b81888 | 2018-03-24 01:38:40 | [diff] [blame] | 265 | MojoResult GetBufferInfo(MojoHandle buffer_handle, |
Ken Rockot | c12080c | 2018-05-03 18:10:21 | [diff] [blame] | 266 | const MojoGetBufferInfoOptions* options, |
Lei Zhang | 7b81888 | 2018-03-24 01:38:40 | [diff] [blame] | 267 | MojoSharedBufferInfo* info); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 268 | |
rockot | 28a287e | 2016-05-24 02:51:37 | [diff] [blame] | 269 | // These methods correspond to the API functions defined in |
| 270 | // "mojo/public/c/system/platform_handle.h". |
Ken Rockot | a22cf31 | 2018-06-21 19:56:54 | [diff] [blame] | 271 | MojoResult WrapPlatformHandle(const MojoPlatformHandle* platform_handle, |
| 272 | const MojoWrapPlatformHandleOptions* options, |
| 273 | MojoHandle* mojo_handle); |
| 274 | MojoResult UnwrapPlatformHandle( |
rockot | 28a287e | 2016-05-24 02:51:37 | [diff] [blame] | 275 | MojoHandle mojo_handle, |
Ken Rockot | 88be045 | 2018-05-16 17:31:04 | [diff] [blame] | 276 | const MojoUnwrapPlatformHandleOptions* options, |
| 277 | MojoPlatformHandle* platform_handle); |
| 278 | MojoResult WrapPlatformSharedMemoryRegion( |
| 279 | const MojoPlatformHandle* platform_handles, |
| 280 | uint32_t num_platform_handles, |
| 281 | uint64_t size, |
| 282 | const MojoSharedBufferGuid* guid, |
| 283 | MojoPlatformSharedMemoryRegionAccessMode access_mode, |
| 284 | const MojoWrapPlatformSharedMemoryRegionOptions* options, |
| 285 | MojoHandle* mojo_handle); |
| 286 | MojoResult UnwrapPlatformSharedMemoryRegion( |
| 287 | MojoHandle mojo_handle, |
| 288 | const MojoUnwrapPlatformSharedMemoryRegionOptions* options, |
| 289 | MojoPlatformHandle* platform_handles, |
| 290 | uint32_t* num_platform_handles, |
| 291 | uint64_t* size, |
Ken Rockot | 61a3aab | 2017-06-20 16:19:57 | [diff] [blame] | 292 | MojoSharedBufferGuid* guid, |
Ken Rockot | 88be045 | 2018-05-16 17:31:04 | [diff] [blame] | 293 | MojoPlatformSharedMemoryRegionAccessMode* access_mode); |
rockot | 28a287e | 2016-05-24 02:51:37 | [diff] [blame] | 294 | |
Ken Rockot | f86cdb8b | 2018-05-19 23:35:01 | [diff] [blame] | 295 | // Invitation API. |
| 296 | MojoResult CreateInvitation(const MojoCreateInvitationOptions* options, |
| 297 | MojoHandle* invitation_handle); |
| 298 | MojoResult AttachMessagePipeToInvitation( |
| 299 | MojoHandle invitation_handle, |
Ken Rockot | 189566cd | 2018-05-24 21:04:17 | [diff] [blame] | 300 | const void* name, |
| 301 | uint32_t name_num_bytes, |
Ken Rockot | f86cdb8b | 2018-05-19 23:35:01 | [diff] [blame] | 302 | const MojoAttachMessagePipeToInvitationOptions* options, |
| 303 | MojoHandle* message_pipe_handle); |
| 304 | MojoResult ExtractMessagePipeFromInvitation( |
| 305 | MojoHandle invitation_handle, |
Ken Rockot | 189566cd | 2018-05-24 21:04:17 | [diff] [blame] | 306 | const void* name, |
| 307 | uint32_t name_num_bytes, |
Ken Rockot | f86cdb8b | 2018-05-19 23:35:01 | [diff] [blame] | 308 | const MojoExtractMessagePipeFromInvitationOptions* options, |
| 309 | MojoHandle* message_pipe_handle); |
| 310 | MojoResult SendInvitation( |
| 311 | MojoHandle invitation_handle, |
| 312 | const MojoPlatformProcessHandle* process_handle, |
| 313 | const MojoInvitationTransportEndpoint* transport_endpoint, |
| 314 | MojoProcessErrorHandler error_handler, |
| 315 | uintptr_t error_handler_context, |
| 316 | const MojoSendInvitationOptions* options); |
| 317 | MojoResult AcceptInvitation( |
| 318 | const MojoInvitationTransportEndpoint* transport_endpoint, |
| 319 | const MojoAcceptInvitationOptions* options, |
| 320 | MojoHandle* invitation_handle); |
| 321 | |
Ken Rockot | 0db0bcf | 2018-07-12 22:01:12 | [diff] [blame] | 322 | // Quota API. |
| 323 | MojoResult SetQuota(MojoHandle handle, |
| 324 | MojoQuotaType type, |
| 325 | uint64_t limit, |
| 326 | const MojoSetQuotaOptions* options); |
| 327 | MojoResult QueryQuota(MojoHandle handle, |
| 328 | MojoQuotaType type, |
| 329 | const MojoQueryQuotaOptions* options, |
| 330 | uint64_t* limit, |
| 331 | uint64_t* usage); |
| 332 | |
Ken Rockot | f6a4207 | 2020-06-03 23:57:24 | [diff] [blame] | 333 | MojoResult SetDefaultProcessErrorHandler( |
| 334 | MojoDefaultProcessErrorHandler handler, |
| 335 | const MojoSetDefaultProcessErrorHandlerOptions* options); |
| 336 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 337 | void GetActiveHandlesForTest(std::vector<MojoHandle>* handles); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 338 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 339 | private: |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 340 | // Used to pass ownership of our NodeController over to the IO thread in the |
| 341 | // event that we're torn down before said thread. |
| 342 | static void PassNodeControllerToIOThread( |
dcheng | 40c732a8 | 2016-04-22 00:57:38 | [diff] [blame] | 343 | std::unique_ptr<NodeController> node_controller); |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 344 | |
rockot | 935f263 | 2016-02-20 03:40:59 | [diff] [blame] | 345 | // Guards node_controller_. |
| 346 | // |
| 347 | // TODO(rockot): Consider removing this. It's only needed because we |
| 348 | // initialize node_controller_ lazily and that may happen on any thread. |
| 349 | // Otherwise it's effectively const and shouldn't need to be guarded. |
| 350 | // |
| 351 | // We can get rid of lazy initialization if we defer Mojo initialization far |
| 352 | // enough that zygotes don't do it. The zygote can't create a NodeController. |
| 353 | base::Lock node_controller_lock_; |
| 354 | |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 355 | // This is lazily initialized on first access. Always use GetNodeController() |
| 356 | // to access it. |
dcheng | 40c732a8 | 2016-04-22 00:57:38 | [diff] [blame] | 357 | std::unique_ptr<NodeController> node_controller_; |
rockot | ce69a04 | 2016-01-26 19:23:21 | [diff] [blame] | 358 | |
rockot | 160577e9 | 2016-08-03 20:24:44 | [diff] [blame] | 359 | // The default callback to invoke, if any, when a process error is reported |
| 360 | // but cannot be associated with a specific process. |
| 361 | ProcessErrorCallback default_process_error_callback_; |
| 362 | |
erikchen | 337e9da | 2017-06-09 17:49:45 | [diff] [blame] | 363 | std::unique_ptr<HandleTable> handles_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 364 | |
| 365 | base::Lock mapping_table_lock_; // Protects |mapping_table_|. |
Ken Rockot | 062ff1a5 | 2018-04-23 20:42:38 | [diff] [blame] | 366 | |
| 367 | using MappingTable = |
| 368 | std::unordered_map<void*, std::unique_ptr<PlatformSharedMemoryMapping>>; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 369 | MappingTable mapping_table_; |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 370 | }; |
| 371 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 372 | } // namespace core |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 373 | } // namespace mojo |
| 374 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 375 | #endif // MOJO_CORE_CORE_H_ |