Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Viet-Trung Luu | 604aea9 | 2015-10-22 12:50:28 -0700 | [diff] [blame] | 5 | #include "base/logging.h" |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 6 | #include "mojo/edk/embedder/embedder_internal.h" |
| 7 | #include "mojo/edk/system/core.h" |
| 8 | #include "mojo/edk/system/dispatcher.h" |
Viet-Trung Luu | c1ffb9f | 2016-05-05 09:14:11 -0700 | [diff] [blame] | 9 | #include "mojo/edk/system/handle.h" |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 10 | #include "mojo/edk/util/ref_ptr.h" |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 11 | #include "mojo/public/c/system/buffer.h" |
| 12 | #include "mojo/public/c/system/data_pipe.h" |
Viet-Trung Luu | a2e2b6d | 2016-03-11 12:39:43 -0800 | [diff] [blame] | 13 | #include "mojo/public/c/system/handle.h" |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 14 | #include "mojo/public/c/system/message_pipe.h" |
Viet-Trung Luu | a2e2b6d | 2016-03-11 12:39:43 -0800 | [diff] [blame] | 15 | #include "mojo/public/c/system/result.h" |
| 16 | #include "mojo/public/c/system/time.h" |
| 17 | #include "mojo/public/c/system/wait.h" |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 18 | #include "mojo/public/platform/native/system_impl_private.h" |
| 19 | |
| 20 | using mojo::embedder::internal::g_core; |
| 21 | using mojo::system::Core; |
| 22 | using mojo::system::Dispatcher; |
Viet-Trung Luu | 453f346 | 2016-05-09 16:11:49 -0700 | [diff] [blame] | 23 | using mojo::system::Handle; |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 24 | using mojo::system::MakeUserPointer; |
Viet-Trung Luu | 6a1f1a8 | 2015-10-30 15:18:01 -0700 | [diff] [blame] | 25 | using mojo::util::RefPtr; |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 26 | |
| 27 | // Definitions of the system functions, but with an explicit parameter for the |
| 28 | // core object rather than using the default singleton. Also includes functions |
| 29 | // for manipulating core objects. |
| 30 | extern "C" { |
| 31 | |
| 32 | MojoSystemImpl MojoSystemImplGetDefaultImpl() { |
| 33 | return static_cast<MojoSystemImpl>(g_core); |
| 34 | } |
| 35 | |
| 36 | MojoSystemImpl MojoSystemImplCreateImpl() { |
| 37 | Core* created_core = new Core(g_core->platform_support()); |
| 38 | return static_cast<MojoSystemImpl>(created_core); |
| 39 | } |
| 40 | |
| 41 | MojoResult MojoSystemImplTransferHandle(MojoSystemImpl from_system, |
| 42 | MojoHandle handle, |
| 43 | MojoSystemImpl to_system, |
| 44 | MojoHandle* result_handle) { |
| 45 | Core* from_core = static_cast<Core*>(from_system); |
| 46 | if (from_core == nullptr) |
| 47 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 48 | |
| 49 | if (handle == MOJO_HANDLE_INVALID) |
| 50 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 51 | |
| 52 | Core* to_core = static_cast<Core*>(to_system); |
| 53 | if (to_core == nullptr) |
| 54 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 55 | |
| 56 | if (result_handle == nullptr) |
| 57 | return MOJO_RESULT_INVALID_ARGUMENT; |
| 58 | |
Viet-Trung Luu | 453f346 | 2016-05-09 16:11:49 -0700 | [diff] [blame] | 59 | Handle h; |
| 60 | MojoResult result = from_core->GetAndRemoveHandle(handle, &h); |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 61 | if (result != MOJO_RESULT_OK) |
| 62 | return result; |
| 63 | |
Viet-Trung Luu | 453f346 | 2016-05-09 16:11:49 -0700 | [diff] [blame] | 64 | MojoHandle created_handle = |
| 65 | to_core->AddHandle(Handle(h.dispatcher.Clone(), h.rights)); |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 66 | if (created_handle == MOJO_HANDLE_INVALID) { |
| 67 | // The handle has been lost, unfortunately. There's no guarentee we can put |
| 68 | // it back where it came from, or get the original ID back. Holding locks |
| 69 | // for multiple cores risks deadlock, so that isn't a solution. This case |
| 70 | // should not happen for reasonable uses of this API, however. |
Viet-Trung Luu | 453f346 | 2016-05-09 16:11:49 -0700 | [diff] [blame] | 71 | // TODO(vtl): This behaviour is pretty crappy. This can be fixed by marking |
| 72 | // the original handle as busy and only removing it on success, though |
| 73 | // that'd require some work. |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "Could not transfer handle"; |
Viet-Trung Luu | 453f346 | 2016-05-09 16:11:49 -0700 | [diff] [blame] | 75 | h.dispatcher->Close(); |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 76 | return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 77 | } |
| 78 | |
| 79 | MakeUserPointer(result_handle).Put(created_handle); |
| 80 | return MOJO_RESULT_OK; |
| 81 | } |
| 82 | |
| 83 | MojoTimeTicks MojoSystemImplGetTimeTicksNow(MojoSystemImpl system) { |
| 84 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 85 | DCHECK(core); |
| 86 | return core->GetTimeTicksNow(); |
| 87 | } |
| 88 | |
| 89 | MojoResult MojoSystemImplClose(MojoSystemImpl system, MojoHandle handle) { |
| 90 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 91 | DCHECK(core); |
| 92 | return core->Close(handle); |
| 93 | } |
| 94 | |
Viet-Trung Luu | e5ace23 | 2016-05-19 14:54:48 -0700 | [diff] [blame] | 95 | MojoResult MojoSystemImplGetRights(MojoSystemImpl system, |
| 96 | MojoHandle handle, |
| 97 | MojoHandleRights* rights) { |
| 98 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 99 | DCHECK(core); |
| 100 | return core->GetRights(handle, MakeUserPointer(rights)); |
| 101 | } |
| 102 | |
Viet-Trung Luu | ef5cd66 | 2016-05-23 12:14:01 -0700 | [diff] [blame^] | 103 | MojoResult MojoSystemImplDuplicateHandleWithReducedRights( |
| 104 | MojoSystemImpl system, |
| 105 | MojoHandle handle, |
| 106 | MojoHandleRights rights_to_remove, |
| 107 | MojoHandle* new_handle) { |
| 108 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 109 | DCHECK(core); |
| 110 | return core->DuplicateHandleWithReducedRights(handle, rights_to_remove, |
| 111 | MakeUserPointer(new_handle)); |
| 112 | } |
| 113 | |
| 114 | MojoResult MojoSystemImplDuplicateHandle(MojoSystemImpl system, |
| 115 | MojoHandle handle, |
| 116 | MojoHandle* new_handle) { |
| 117 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 118 | DCHECK(core); |
| 119 | return core->DuplicateHandleWithReducedRights(handle, MOJO_HANDLE_RIGHT_NONE, |
| 120 | MakeUserPointer(new_handle)); |
| 121 | } |
| 122 | |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 123 | MojoResult MojoSystemImplWait(MojoSystemImpl system, |
| 124 | MojoHandle handle, |
| 125 | MojoHandleSignals signals, |
| 126 | MojoDeadline deadline, |
| 127 | MojoHandleSignalsState* signals_state) { |
| 128 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 129 | DCHECK(core); |
| 130 | return core->Wait(handle, signals, deadline, MakeUserPointer(signals_state)); |
| 131 | } |
| 132 | |
| 133 | MojoResult MojoSystemImplWaitMany(MojoSystemImpl system, |
| 134 | const MojoHandle* handles, |
| 135 | const MojoHandleSignals* signals, |
| 136 | uint32_t num_handles, |
| 137 | MojoDeadline deadline, |
| 138 | uint32_t* result_index, |
| 139 | MojoHandleSignalsState* signals_states) { |
| 140 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 141 | DCHECK(core); |
| 142 | return core->WaitMany(MakeUserPointer(handles), MakeUserPointer(signals), |
| 143 | num_handles, deadline, MakeUserPointer(result_index), |
| 144 | MakeUserPointer(signals_states)); |
| 145 | } |
| 146 | |
| 147 | MojoResult MojoSystemImplCreateMessagePipe( |
| 148 | MojoSystemImpl system, |
| 149 | const MojoCreateMessagePipeOptions* options, |
| 150 | MojoHandle* message_pipe_handle0, |
| 151 | MojoHandle* message_pipe_handle1) { |
| 152 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 153 | DCHECK(core); |
| 154 | return core->CreateMessagePipe(MakeUserPointer(options), |
| 155 | MakeUserPointer(message_pipe_handle0), |
| 156 | MakeUserPointer(message_pipe_handle1)); |
| 157 | } |
| 158 | |
| 159 | MojoResult MojoSystemImplWriteMessage(MojoSystemImpl system, |
| 160 | MojoHandle message_pipe_handle, |
| 161 | const void* bytes, |
| 162 | uint32_t num_bytes, |
| 163 | const MojoHandle* handles, |
| 164 | uint32_t num_handles, |
| 165 | MojoWriteMessageFlags flags) { |
| 166 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 167 | DCHECK(core); |
| 168 | return core->WriteMessage(message_pipe_handle, MakeUserPointer(bytes), |
| 169 | num_bytes, MakeUserPointer(handles), num_handles, |
| 170 | flags); |
| 171 | } |
| 172 | |
| 173 | MojoResult MojoSystemImplReadMessage(MojoSystemImpl system, |
| 174 | MojoHandle message_pipe_handle, |
| 175 | void* bytes, |
| 176 | uint32_t* num_bytes, |
| 177 | MojoHandle* handles, |
| 178 | uint32_t* num_handles, |
| 179 | MojoReadMessageFlags flags) { |
| 180 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 181 | DCHECK(core); |
| 182 | return core->ReadMessage(message_pipe_handle, MakeUserPointer(bytes), |
| 183 | MakeUserPointer(num_bytes), MakeUserPointer(handles), |
| 184 | MakeUserPointer(num_handles), flags); |
| 185 | } |
| 186 | |
| 187 | MojoResult MojoSystemImplCreateDataPipe( |
| 188 | MojoSystemImpl system, |
| 189 | const MojoCreateDataPipeOptions* options, |
| 190 | MojoHandle* data_pipe_producer_handle, |
| 191 | MojoHandle* data_pipe_consumer_handle) { |
| 192 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 193 | DCHECK(core); |
| 194 | return core->CreateDataPipe(MakeUserPointer(options), |
| 195 | MakeUserPointer(data_pipe_producer_handle), |
| 196 | MakeUserPointer(data_pipe_consumer_handle)); |
| 197 | } |
| 198 | |
Viet-Trung Luu | d1825d2 | 2016-04-13 17:13:45 -0700 | [diff] [blame] | 199 | MojoResult MojoSystemImplSetDataPipeProducerOptions( |
| 200 | MojoSystemImpl system, |
| 201 | MojoHandle data_pipe_producer_handle, |
| 202 | const struct MojoDataPipeProducerOptions* options) { |
| 203 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 204 | DCHECK(core); |
| 205 | return core->SetDataPipeProducerOptions(data_pipe_producer_handle, |
| 206 | MakeUserPointer(options)); |
| 207 | } |
| 208 | |
| 209 | MojoResult MojoSystemImplGetDataPipeProducerOptions( |
| 210 | MojoSystemImpl system, |
| 211 | MojoHandle data_pipe_producer_handle, |
| 212 | struct MojoDataPipeProducerOptions* options, |
| 213 | uint32_t options_num_bytes) { |
| 214 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 215 | DCHECK(core); |
| 216 | return core->GetDataPipeProducerOptions( |
| 217 | data_pipe_producer_handle, MakeUserPointer(options), options_num_bytes); |
| 218 | } |
| 219 | |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 220 | MojoResult MojoSystemImplWriteData(MojoSystemImpl system, |
| 221 | MojoHandle data_pipe_producer_handle, |
| 222 | const void* elements, |
| 223 | uint32_t* num_elements, |
| 224 | MojoWriteDataFlags flags) { |
| 225 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 226 | DCHECK(core); |
| 227 | return core->WriteData(data_pipe_producer_handle, MakeUserPointer(elements), |
| 228 | MakeUserPointer(num_elements), flags); |
| 229 | } |
| 230 | |
| 231 | MojoResult MojoSystemImplBeginWriteData(MojoSystemImpl system, |
| 232 | MojoHandle data_pipe_producer_handle, |
| 233 | void** buffer, |
| 234 | uint32_t* buffer_num_elements, |
| 235 | MojoWriteDataFlags flags) { |
| 236 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 237 | DCHECK(core); |
| 238 | return core->BeginWriteData(data_pipe_producer_handle, |
| 239 | MakeUserPointer(buffer), |
| 240 | MakeUserPointer(buffer_num_elements), flags); |
| 241 | } |
| 242 | |
| 243 | MojoResult MojoSystemImplEndWriteData(MojoSystemImpl system, |
| 244 | MojoHandle data_pipe_producer_handle, |
| 245 | uint32_t num_elements_written) { |
| 246 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 247 | DCHECK(core); |
| 248 | return core->EndWriteData(data_pipe_producer_handle, num_elements_written); |
| 249 | } |
| 250 | |
Viet-Trung Luu | 58ec2b7 | 2016-04-05 13:43:25 -0700 | [diff] [blame] | 251 | MojoResult MojoSystemImplSetDataPipeConsumerOptions( |
| 252 | MojoSystemImpl system, |
| 253 | MojoHandle data_pipe_consumer_handle, |
| 254 | const struct MojoDataPipeConsumerOptions* options) { |
| 255 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 256 | DCHECK(core); |
| 257 | return core->SetDataPipeConsumerOptions(data_pipe_consumer_handle, |
| 258 | MakeUserPointer(options)); |
| 259 | } |
| 260 | |
| 261 | MojoResult MojoSystemImplGetDataPipeConsumerOptions( |
| 262 | MojoSystemImpl system, |
| 263 | MojoHandle data_pipe_consumer_handle, |
| 264 | struct MojoDataPipeConsumerOptions* options, |
| 265 | uint32_t options_num_bytes) { |
| 266 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 267 | DCHECK(core); |
| 268 | return core->GetDataPipeConsumerOptions( |
| 269 | data_pipe_consumer_handle, MakeUserPointer(options), options_num_bytes); |
| 270 | } |
| 271 | |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 272 | MojoResult MojoSystemImplReadData(MojoSystemImpl system, |
| 273 | MojoHandle data_pipe_consumer_handle, |
| 274 | void* elements, |
| 275 | uint32_t* num_elements, |
| 276 | MojoReadDataFlags flags) { |
| 277 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 278 | DCHECK(core); |
| 279 | return core->ReadData(data_pipe_consumer_handle, MakeUserPointer(elements), |
| 280 | MakeUserPointer(num_elements), flags); |
| 281 | } |
| 282 | |
| 283 | MojoResult MojoSystemImplBeginReadData(MojoSystemImpl system, |
| 284 | MojoHandle data_pipe_consumer_handle, |
| 285 | const void** buffer, |
| 286 | uint32_t* buffer_num_elements, |
| 287 | MojoReadDataFlags flags) { |
| 288 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 289 | DCHECK(core); |
| 290 | return core->BeginReadData(data_pipe_consumer_handle, MakeUserPointer(buffer), |
| 291 | MakeUserPointer(buffer_num_elements), flags); |
| 292 | } |
| 293 | |
| 294 | MojoResult MojoSystemImplEndReadData(MojoSystemImpl system, |
| 295 | MojoHandle data_pipe_consumer_handle, |
| 296 | uint32_t num_elements_read) { |
| 297 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 298 | DCHECK(core); |
| 299 | return core->EndReadData(data_pipe_consumer_handle, num_elements_read); |
| 300 | } |
| 301 | |
| 302 | MojoResult MojoSystemImplCreateSharedBuffer( |
| 303 | MojoSystemImpl system, |
| 304 | const MojoCreateSharedBufferOptions* options, |
| 305 | uint64_t num_bytes, |
| 306 | MojoHandle* shared_buffer_handle) { |
| 307 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 308 | DCHECK(core); |
| 309 | return core->CreateSharedBuffer(MakeUserPointer(options), num_bytes, |
| 310 | MakeUserPointer(shared_buffer_handle)); |
| 311 | } |
| 312 | |
| 313 | MojoResult MojoSystemImplDuplicateBufferHandle( |
| 314 | MojoSystemImpl system, |
| 315 | MojoHandle buffer_handle, |
| 316 | const MojoDuplicateBufferHandleOptions* options, |
| 317 | MojoHandle* new_buffer_handle) { |
| 318 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 319 | DCHECK(core); |
| 320 | return core->DuplicateBufferHandle(buffer_handle, MakeUserPointer(options), |
| 321 | MakeUserPointer(new_buffer_handle)); |
| 322 | } |
| 323 | |
Viet-Trung Luu | 9beb4f7 | 2016-03-09 15:58:11 -0800 | [diff] [blame] | 324 | MojoResult MojoSystemImplGetBufferInformation(MojoSystemImpl system, |
| 325 | MojoHandle buffer_handle, |
| 326 | MojoBufferInformation* info, |
| 327 | uint32_t info_num_bytes) { |
| 328 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 329 | DCHECK(core); |
| 330 | return core->GetBufferInformation(buffer_handle, MakeUserPointer(info), |
| 331 | info_num_bytes); |
| 332 | } |
| 333 | |
Nick Bray | da897d6 | 2015-04-08 16:28:55 -0700 | [diff] [blame] | 334 | MojoResult MojoSystemImplMapBuffer(MojoSystemImpl system, |
| 335 | MojoHandle buffer_handle, |
| 336 | uint64_t offset, |
| 337 | uint64_t num_bytes, |
| 338 | void** buffer, |
| 339 | MojoMapBufferFlags flags) { |
| 340 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 341 | DCHECK(core); |
| 342 | return core->MapBuffer(buffer_handle, offset, num_bytes, |
| 343 | MakeUserPointer(buffer), flags); |
| 344 | } |
| 345 | |
| 346 | MojoResult MojoSystemImplUnmapBuffer(MojoSystemImpl system, void* buffer) { |
| 347 | mojo::system::Core* core = static_cast<mojo::system::Core*>(system); |
| 348 | DCHECK(core); |
| 349 | return core->UnmapBuffer(MakeUserPointer(buffer)); |
| 350 | } |
| 351 | |
| 352 | } // extern "C" |