| 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 #include "mojo/edk/system/core.h" | 5 #include "mojo/edk/system/core.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "mojo/edk/system/async_waiter.h" | 24 #include "mojo/edk/system/async_waiter.h" |
| 25 #include "mojo/edk/system/channel.h" | 25 #include "mojo/edk/system/channel.h" |
| 26 #include "mojo/edk/system/configuration.h" | 26 #include "mojo/edk/system/configuration.h" |
| 27 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" | 27 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
| 28 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" | 28 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" |
| 29 #include "mojo/edk/system/handle_signals_state.h" | 29 #include "mojo/edk/system/handle_signals_state.h" |
| 30 #include "mojo/edk/system/message_pipe_dispatcher.h" | 30 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 31 #include "mojo/edk/system/platform_handle_dispatcher.h" | 31 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 32 #include "mojo/edk/system/ports/node.h" | 32 #include "mojo/edk/system/ports/node.h" |
| 33 #include "mojo/edk/system/remote_message_pipe_bootstrap.h" | 33 #include "mojo/edk/system/remote_message_pipe_bootstrap.h" |
| 34 #include "mojo/edk/system/request_context.h" |
| 34 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 35 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
| 35 #include "mojo/edk/system/wait_set_dispatcher.h" | 36 #include "mojo/edk/system/wait_set_dispatcher.h" |
| 36 #include "mojo/edk/system/waiter.h" | 37 #include "mojo/edk/system/waiter.h" |
| 37 | 38 |
| 38 namespace mojo { | 39 namespace mojo { |
| 39 namespace edk { | 40 namespace edk { |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 42 | 43 |
| 43 // This is an unnecessarily large limit that is relatively easy to enforce. | 44 // This is an unnecessarily large limit that is relatively easy to enforce. |
| 44 const uint32_t kMaxHandlesPerMessage = 1024 * 1024; | 45 const uint32_t kMaxHandlesPerMessage = 1024 * 1024; |
| 45 | 46 |
| 46 // TODO: Maybe we could negotiate a debugging pipe ID for cross-process pipes | 47 // TODO: Maybe we could negotiate a debugging pipe ID for cross-process pipes |
| 47 // too; for now we just use a constant. This only affects bootstrap pipes. | 48 // too; for now we just use a constant. This only affects bootstrap pipes. |
| 48 const uint64_t kUnknownPipeIdForDebug = 0x7f7f7f7f7f7f7f7fUL; | 49 const uint64_t kUnknownPipeIdForDebug = 0x7f7f7f7f7f7f7f7fUL; |
| 49 | 50 |
| 51 void CallWatchCallback(MojoWatchCallback callback, |
| 52 uintptr_t context, |
| 53 MojoResult result, |
| 54 const HandleSignalsState& signals_state) { |
| 55 callback(context, result, |
| 56 static_cast<MojoHandleSignalsState>(signals_state)); |
| 57 } |
| 58 |
| 50 } // namespace | 59 } // namespace |
| 51 | 60 |
| 52 Core::Core() {} | 61 Core::Core() {} |
| 53 | 62 |
| 54 Core::~Core() { | 63 Core::~Core() { |
| 55 if (node_controller_ && node_controller_->io_task_runner()) { | 64 if (node_controller_ && node_controller_->io_task_runner()) { |
| 56 // If this races with IO thread shutdown the callback will be dropped and | 65 // If this races with IO thread shutdown the callback will be dropped and |
| 57 // the NodeController will be shutdown on this thread anyway, which is also | 66 // the NodeController will be shutdown on this thread anyway, which is also |
| 58 // just fine. | 67 // just fine. |
| 59 scoped_refptr<base::TaskRunner> io_task_runner = | 68 scoped_refptr<base::TaskRunner> io_task_runner = |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 if (rv == MOJO_RESULT_OK) | 270 if (rv == MOJO_RESULT_OK) |
| 262 ignore_result(waiter.release()); | 271 ignore_result(waiter.release()); |
| 263 return rv; | 272 return rv; |
| 264 } | 273 } |
| 265 | 274 |
| 266 MojoTimeTicks Core::GetTimeTicksNow() { | 275 MojoTimeTicks Core::GetTimeTicksNow() { |
| 267 return base::TimeTicks::Now().ToInternalValue(); | 276 return base::TimeTicks::Now().ToInternalValue(); |
| 268 } | 277 } |
| 269 | 278 |
| 270 MojoResult Core::Close(MojoHandle handle) { | 279 MojoResult Core::Close(MojoHandle handle) { |
| 280 RequestContext request_context; |
| 271 scoped_refptr<Dispatcher> dispatcher; | 281 scoped_refptr<Dispatcher> dispatcher; |
| 272 { | 282 { |
| 273 base::AutoLock lock(handles_lock_); | 283 base::AutoLock lock(handles_lock_); |
| 274 MojoResult rv = handles_.GetAndRemoveDispatcher(handle, &dispatcher); | 284 MojoResult rv = handles_.GetAndRemoveDispatcher(handle, &dispatcher); |
| 275 if (rv != MOJO_RESULT_OK) | 285 if (rv != MOJO_RESULT_OK) |
| 276 return rv; | 286 return rv; |
| 277 } | 287 } |
| 278 dispatcher->Close(); | 288 dispatcher->Close(); |
| 279 return MOJO_RESULT_OK; | 289 return MOJO_RESULT_OK; |
| 280 } | 290 } |
| 281 | 291 |
| 282 MojoResult Core::Wait(MojoHandle handle, | 292 MojoResult Core::Wait(MojoHandle handle, |
| 283 MojoHandleSignals signals, | 293 MojoHandleSignals signals, |
| 284 MojoDeadline deadline, | 294 MojoDeadline deadline, |
| 285 MojoHandleSignalsState* signals_state) { | 295 MojoHandleSignalsState* signals_state) { |
| 296 RequestContext request_context; |
| 286 uint32_t unused = static_cast<uint32_t>(-1); | 297 uint32_t unused = static_cast<uint32_t>(-1); |
| 287 HandleSignalsState hss; | 298 HandleSignalsState hss; |
| 288 MojoResult rv = WaitManyInternal(&handle, &signals, 1, deadline, &unused, | 299 MojoResult rv = WaitManyInternal(&handle, &signals, 1, deadline, &unused, |
| 289 signals_state ? &hss : nullptr); | 300 signals_state ? &hss : nullptr); |
| 290 if (rv != MOJO_RESULT_INVALID_ARGUMENT && signals_state) | 301 if (rv != MOJO_RESULT_INVALID_ARGUMENT && signals_state) |
| 291 *signals_state = hss; | 302 *signals_state = hss; |
| 292 return rv; | 303 return rv; |
| 293 } | 304 } |
| 294 | 305 |
| 295 MojoResult Core::WaitMany(const MojoHandle* handles, | 306 MojoResult Core::WaitMany(const MojoHandle* handles, |
| 296 const MojoHandleSignals* signals, | 307 const MojoHandleSignals* signals, |
| 297 uint32_t num_handles, | 308 uint32_t num_handles, |
| 298 MojoDeadline deadline, | 309 MojoDeadline deadline, |
| 299 uint32_t* result_index, | 310 uint32_t* result_index, |
| 300 MojoHandleSignalsState* signals_state) { | 311 MojoHandleSignalsState* signals_state) { |
| 312 RequestContext request_context; |
| 301 if (num_handles < 1) | 313 if (num_handles < 1) |
| 302 return MOJO_RESULT_INVALID_ARGUMENT; | 314 return MOJO_RESULT_INVALID_ARGUMENT; |
| 303 if (num_handles > GetConfiguration().max_wait_many_num_handles) | 315 if (num_handles > GetConfiguration().max_wait_many_num_handles) |
| 304 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 316 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 305 | 317 |
| 306 uint32_t index = static_cast<uint32_t>(-1); | 318 uint32_t index = static_cast<uint32_t>(-1); |
| 307 MojoResult rv; | 319 MojoResult rv; |
| 308 if (!signals_state) { | 320 if (!signals_state) { |
| 309 rv = WaitManyInternal(handles, signals, num_handles, deadline, &index, | 321 rv = WaitManyInternal(handles, signals, num_handles, deadline, &index, |
| 310 nullptr); | 322 nullptr); |
| 311 } else { | 323 } else { |
| 312 // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a | 324 // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a |
| 313 // subclass of |MojoHandleSignalsState| that doesn't add any data members. | 325 // subclass of |MojoHandleSignalsState| that doesn't add any data members. |
| 314 rv = WaitManyInternal(handles, signals, num_handles, deadline, &index, | 326 rv = WaitManyInternal(handles, signals, num_handles, deadline, &index, |
| 315 reinterpret_cast<HandleSignalsState*>(signals_state)); | 327 reinterpret_cast<HandleSignalsState*>(signals_state)); |
| 316 } | 328 } |
| 317 if (index != static_cast<uint32_t>(-1) && result_index) | 329 if (index != static_cast<uint32_t>(-1) && result_index) |
| 318 *result_index = index; | 330 *result_index = index; |
| 319 return rv; | 331 return rv; |
| 320 } | 332 } |
| 321 | 333 |
| 334 MojoResult Core::Watch(MojoHandle handle, |
| 335 MojoHandleSignals signals, |
| 336 MojoWatchCallback callback, |
| 337 uintptr_t context) { |
| 338 RequestContext request_context; |
| 339 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handle); |
| 340 if (!dispatcher) |
| 341 return MOJO_RESULT_INVALID_ARGUMENT; |
| 342 return dispatcher->Watch( |
| 343 signals, base::Bind(&CallWatchCallback, callback, context), context); |
| 344 } |
| 345 |
| 346 MojoResult Core::CancelWatch(MojoHandle handle, uintptr_t context) { |
| 347 RequestContext request_context; |
| 348 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handle); |
| 349 if (!dispatcher) |
| 350 return MOJO_RESULT_INVALID_ARGUMENT; |
| 351 return dispatcher->CancelWatch(context); |
| 352 } |
| 353 |
| 322 MojoResult Core::CreateWaitSet(MojoHandle* wait_set_handle) { | 354 MojoResult Core::CreateWaitSet(MojoHandle* wait_set_handle) { |
| 355 RequestContext request_context; |
| 323 if (!wait_set_handle) | 356 if (!wait_set_handle) |
| 324 return MOJO_RESULT_INVALID_ARGUMENT; | 357 return MOJO_RESULT_INVALID_ARGUMENT; |
| 325 | 358 |
| 326 scoped_refptr<WaitSetDispatcher> dispatcher = new WaitSetDispatcher(); | 359 scoped_refptr<WaitSetDispatcher> dispatcher = new WaitSetDispatcher(); |
| 327 MojoHandle h = AddDispatcher(dispatcher); | 360 MojoHandle h = AddDispatcher(dispatcher); |
| 328 if (h == MOJO_HANDLE_INVALID) { | 361 if (h == MOJO_HANDLE_INVALID) { |
| 329 LOG(ERROR) << "Handle table full"; | 362 LOG(ERROR) << "Handle table full"; |
| 330 dispatcher->Close(); | 363 dispatcher->Close(); |
| 331 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 364 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 332 } | 365 } |
| 333 | 366 |
| 334 *wait_set_handle = h; | 367 *wait_set_handle = h; |
| 335 return MOJO_RESULT_OK; | 368 return MOJO_RESULT_OK; |
| 336 } | 369 } |
| 337 | 370 |
| 338 MojoResult Core::AddHandle(MojoHandle wait_set_handle, | 371 MojoResult Core::AddHandle(MojoHandle wait_set_handle, |
| 339 MojoHandle handle, | 372 MojoHandle handle, |
| 340 MojoHandleSignals signals) { | 373 MojoHandleSignals signals) { |
| 374 RequestContext request_context; |
| 341 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); | 375 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); |
| 342 if (!wait_set_dispatcher) | 376 if (!wait_set_dispatcher) |
| 343 return MOJO_RESULT_INVALID_ARGUMENT; | 377 return MOJO_RESULT_INVALID_ARGUMENT; |
| 344 | 378 |
| 345 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(handle)); | 379 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(handle)); |
| 346 if (!dispatcher) | 380 if (!dispatcher) |
| 347 return MOJO_RESULT_INVALID_ARGUMENT; | 381 return MOJO_RESULT_INVALID_ARGUMENT; |
| 348 | 382 |
| 349 return wait_set_dispatcher->AddWaitingDispatcher(dispatcher, signals, handle); | 383 return wait_set_dispatcher->AddWaitingDispatcher(dispatcher, signals, handle); |
| 350 } | 384 } |
| 351 | 385 |
| 352 MojoResult Core::RemoveHandle(MojoHandle wait_set_handle, | 386 MojoResult Core::RemoveHandle(MojoHandle wait_set_handle, |
| 353 MojoHandle handle) { | 387 MojoHandle handle) { |
| 388 RequestContext request_context; |
| 354 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); | 389 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); |
| 355 if (!wait_set_dispatcher) | 390 if (!wait_set_dispatcher) |
| 356 return MOJO_RESULT_INVALID_ARGUMENT; | 391 return MOJO_RESULT_INVALID_ARGUMENT; |
| 357 | 392 |
| 358 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(handle)); | 393 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(handle)); |
| 359 if (!dispatcher) | 394 if (!dispatcher) |
| 360 return MOJO_RESULT_INVALID_ARGUMENT; | 395 return MOJO_RESULT_INVALID_ARGUMENT; |
| 361 | 396 |
| 362 return wait_set_dispatcher->RemoveWaitingDispatcher(dispatcher); | 397 return wait_set_dispatcher->RemoveWaitingDispatcher(dispatcher); |
| 363 } | 398 } |
| 364 | 399 |
| 365 MojoResult Core::GetReadyHandles(MojoHandle wait_set_handle, | 400 MojoResult Core::GetReadyHandles(MojoHandle wait_set_handle, |
| 366 uint32_t* count, | 401 uint32_t* count, |
| 367 MojoHandle* handles, | 402 MojoHandle* handles, |
| 368 MojoResult* results, | 403 MojoResult* results, |
| 369 MojoHandleSignalsState* signals_states) { | 404 MojoHandleSignalsState* signals_states) { |
| 405 RequestContext request_context; |
| 370 if (!handles || !count || !(*count) || !results) | 406 if (!handles || !count || !(*count) || !results) |
| 371 return MOJO_RESULT_INVALID_ARGUMENT; | 407 return MOJO_RESULT_INVALID_ARGUMENT; |
| 372 | 408 |
| 373 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); | 409 scoped_refptr<Dispatcher> wait_set_dispatcher(GetDispatcher(wait_set_handle)); |
| 374 if (!wait_set_dispatcher) | 410 if (!wait_set_dispatcher) |
| 375 return MOJO_RESULT_INVALID_ARGUMENT; | 411 return MOJO_RESULT_INVALID_ARGUMENT; |
| 376 | 412 |
| 377 DispatcherVector awoken_dispatchers; | 413 DispatcherVector awoken_dispatchers; |
| 378 base::StackVector<uintptr_t, 16> contexts; | 414 base::StackVector<uintptr_t, 16> contexts; |
| 379 contexts->assign(*count, MOJO_HANDLE_INVALID); | 415 contexts->assign(*count, MOJO_HANDLE_INVALID); |
| 380 | 416 |
| 381 MojoResult result = wait_set_dispatcher->GetReadyDispatchers( | 417 MojoResult result = wait_set_dispatcher->GetReadyDispatchers( |
| 382 count, &awoken_dispatchers, results, contexts->data()); | 418 count, &awoken_dispatchers, results, contexts->data()); |
| 383 | 419 |
| 384 if (result == MOJO_RESULT_OK) { | 420 if (result == MOJO_RESULT_OK) { |
| 385 for (size_t i = 0; i < *count; i++) { | 421 for (size_t i = 0; i < *count; i++) { |
| 386 handles[i] = static_cast<MojoHandle>(contexts[i]); | 422 handles[i] = static_cast<MojoHandle>(contexts[i]); |
| 387 if (signals_states) | 423 if (signals_states) |
| 388 signals_states[i] = awoken_dispatchers[i]->GetHandleSignalsState(); | 424 signals_states[i] = awoken_dispatchers[i]->GetHandleSignalsState(); |
| 389 } | 425 } |
| 390 } | 426 } |
| 391 | 427 |
| 392 return result; | 428 return result; |
| 393 } | 429 } |
| 394 | 430 |
| 395 MojoResult Core::CreateMessagePipe( | 431 MojoResult Core::CreateMessagePipe( |
| 396 const MojoCreateMessagePipeOptions* options, | 432 const MojoCreateMessagePipeOptions* options, |
| 397 MojoHandle* message_pipe_handle0, | 433 MojoHandle* message_pipe_handle0, |
| 398 MojoHandle* message_pipe_handle1) { | 434 MojoHandle* message_pipe_handle1) { |
| 435 RequestContext request_context; |
| 399 ports::PortRef port0, port1; | 436 ports::PortRef port0, port1; |
| 400 GetNodeController()->node()->CreatePortPair(&port0, &port1); | 437 GetNodeController()->node()->CreatePortPair(&port0, &port1); |
| 401 | 438 |
| 402 CHECK(message_pipe_handle0); | 439 CHECK(message_pipe_handle0); |
| 403 CHECK(message_pipe_handle1); | 440 CHECK(message_pipe_handle1); |
| 404 | 441 |
| 405 uint64_t pipe_id = base::RandUint64(); | 442 uint64_t pipe_id = base::RandUint64(); |
| 406 | 443 |
| 407 *message_pipe_handle0 = AddDispatcher( | 444 *message_pipe_handle0 = AddDispatcher( |
| 408 new MessagePipeDispatcher(GetNodeController(), port0, pipe_id, 0)); | 445 new MessagePipeDispatcher(GetNodeController(), port0, pipe_id, 0)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 420 | 457 |
| 421 return MOJO_RESULT_OK; | 458 return MOJO_RESULT_OK; |
| 422 } | 459 } |
| 423 | 460 |
| 424 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, | 461 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, |
| 425 const void* bytes, | 462 const void* bytes, |
| 426 uint32_t num_bytes, | 463 uint32_t num_bytes, |
| 427 const MojoHandle* handles, | 464 const MojoHandle* handles, |
| 428 uint32_t num_handles, | 465 uint32_t num_handles, |
| 429 MojoWriteMessageFlags flags) { | 466 MojoWriteMessageFlags flags) { |
| 467 RequestContext request_context; |
| 430 auto dispatcher = GetDispatcher(message_pipe_handle); | 468 auto dispatcher = GetDispatcher(message_pipe_handle); |
| 431 if (!dispatcher) | 469 if (!dispatcher) |
| 432 return MOJO_RESULT_INVALID_ARGUMENT; | 470 return MOJO_RESULT_INVALID_ARGUMENT; |
| 433 | 471 |
| 434 if (num_handles == 0) // Fast path: no handles. | 472 if (num_handles == 0) // Fast path: no handles. |
| 435 return dispatcher->WriteMessage(bytes, num_bytes, nullptr, 0, flags); | 473 return dispatcher->WriteMessage(bytes, num_bytes, nullptr, 0, flags); |
| 436 | 474 |
| 437 CHECK(handles); | 475 CHECK(handles); |
| 438 | 476 |
| 439 if (num_handles > kMaxHandlesPerMessage) | 477 if (num_handles > kMaxHandlesPerMessage) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 469 | 507 |
| 470 return rv; | 508 return rv; |
| 471 } | 509 } |
| 472 | 510 |
| 473 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, | 511 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, |
| 474 void* bytes, | 512 void* bytes, |
| 475 uint32_t* num_bytes, | 513 uint32_t* num_bytes, |
| 476 MojoHandle* handles, | 514 MojoHandle* handles, |
| 477 uint32_t* num_handles, | 515 uint32_t* num_handles, |
| 478 MojoReadMessageFlags flags) { | 516 MojoReadMessageFlags flags) { |
| 517 RequestContext request_context; |
| 479 CHECK((!num_handles || !*num_handles || handles) && | 518 CHECK((!num_handles || !*num_handles || handles) && |
| 480 (!num_bytes || !*num_bytes || bytes)); | 519 (!num_bytes || !*num_bytes || bytes)); |
| 481 auto dispatcher = GetDispatcher(message_pipe_handle); | 520 auto dispatcher = GetDispatcher(message_pipe_handle); |
| 482 if (!dispatcher) | 521 if (!dispatcher) |
| 483 return MOJO_RESULT_INVALID_ARGUMENT; | 522 return MOJO_RESULT_INVALID_ARGUMENT; |
| 484 return dispatcher->ReadMessage(bytes, num_bytes, handles, num_handles, flags); | 523 return dispatcher->ReadMessage(bytes, num_bytes, handles, num_handles, flags); |
| 485 } | 524 } |
| 486 | 525 |
| 487 MojoResult Core::CreateDataPipe( | 526 MojoResult Core::CreateDataPipe( |
| 488 const MojoCreateDataPipeOptions* options, | 527 const MojoCreateDataPipeOptions* options, |
| 489 MojoHandle* data_pipe_producer_handle, | 528 MojoHandle* data_pipe_producer_handle, |
| 490 MojoHandle* data_pipe_consumer_handle) { | 529 MojoHandle* data_pipe_consumer_handle) { |
| 530 RequestContext request_context; |
| 491 if (options && options->struct_size != sizeof(MojoCreateDataPipeOptions)) | 531 if (options && options->struct_size != sizeof(MojoCreateDataPipeOptions)) |
| 492 return MOJO_RESULT_INVALID_ARGUMENT; | 532 return MOJO_RESULT_INVALID_ARGUMENT; |
| 493 | 533 |
| 494 MojoCreateDataPipeOptions create_options; | 534 MojoCreateDataPipeOptions create_options; |
| 495 create_options.struct_size = sizeof(MojoCreateDataPipeOptions); | 535 create_options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 496 create_options.flags = options ? options->flags : 0; | 536 create_options.flags = options ? options->flags : 0; |
| 497 create_options.element_num_bytes = options ? options->element_num_bytes : 1; | 537 create_options.element_num_bytes = options ? options->element_num_bytes : 1; |
| 498 // TODO: Use Configuration to get default data pipe capacity. | 538 // TODO: Use Configuration to get default data pipe capacity. |
| 499 create_options.capacity_num_bytes = | 539 create_options.capacity_num_bytes = |
| 500 options && options->capacity_num_bytes ? options->capacity_num_bytes | 540 options && options->capacity_num_bytes ? options->capacity_num_bytes |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 575 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 536 } | 576 } |
| 537 | 577 |
| 538 return MOJO_RESULT_OK; | 578 return MOJO_RESULT_OK; |
| 539 } | 579 } |
| 540 | 580 |
| 541 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, | 581 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, |
| 542 const void* elements, | 582 const void* elements, |
| 543 uint32_t* num_bytes, | 583 uint32_t* num_bytes, |
| 544 MojoWriteDataFlags flags) { | 584 MojoWriteDataFlags flags) { |
| 585 RequestContext request_context; |
| 545 scoped_refptr<Dispatcher> dispatcher( | 586 scoped_refptr<Dispatcher> dispatcher( |
| 546 GetDispatcher(data_pipe_producer_handle)); | 587 GetDispatcher(data_pipe_producer_handle)); |
| 547 if (!dispatcher) | 588 if (!dispatcher) |
| 548 return MOJO_RESULT_INVALID_ARGUMENT; | 589 return MOJO_RESULT_INVALID_ARGUMENT; |
| 549 | 590 |
| 550 return dispatcher->WriteData(elements, num_bytes, flags); | 591 return dispatcher->WriteData(elements, num_bytes, flags); |
| 551 } | 592 } |
| 552 | 593 |
| 553 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, | 594 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, |
| 554 void** buffer, | 595 void** buffer, |
| 555 uint32_t* buffer_num_bytes, | 596 uint32_t* buffer_num_bytes, |
| 556 MojoWriteDataFlags flags) { | 597 MojoWriteDataFlags flags) { |
| 598 RequestContext request_context; |
| 557 scoped_refptr<Dispatcher> dispatcher( | 599 scoped_refptr<Dispatcher> dispatcher( |
| 558 GetDispatcher(data_pipe_producer_handle)); | 600 GetDispatcher(data_pipe_producer_handle)); |
| 559 if (!dispatcher) | 601 if (!dispatcher) |
| 560 return MOJO_RESULT_INVALID_ARGUMENT; | 602 return MOJO_RESULT_INVALID_ARGUMENT; |
| 561 | 603 |
| 562 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); | 604 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); |
| 563 } | 605 } |
| 564 | 606 |
| 565 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, | 607 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, |
| 566 uint32_t num_bytes_written) { | 608 uint32_t num_bytes_written) { |
| 609 RequestContext request_context; |
| 567 scoped_refptr<Dispatcher> dispatcher( | 610 scoped_refptr<Dispatcher> dispatcher( |
| 568 GetDispatcher(data_pipe_producer_handle)); | 611 GetDispatcher(data_pipe_producer_handle)); |
| 569 if (!dispatcher) | 612 if (!dispatcher) |
| 570 return MOJO_RESULT_INVALID_ARGUMENT; | 613 return MOJO_RESULT_INVALID_ARGUMENT; |
| 571 | 614 |
| 572 return dispatcher->EndWriteData(num_bytes_written); | 615 return dispatcher->EndWriteData(num_bytes_written); |
| 573 } | 616 } |
| 574 | 617 |
| 575 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, | 618 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, |
| 576 void* elements, | 619 void* elements, |
| 577 uint32_t* num_bytes, | 620 uint32_t* num_bytes, |
| 578 MojoReadDataFlags flags) { | 621 MojoReadDataFlags flags) { |
| 622 RequestContext request_context; |
| 579 scoped_refptr<Dispatcher> dispatcher( | 623 scoped_refptr<Dispatcher> dispatcher( |
| 580 GetDispatcher(data_pipe_consumer_handle)); | 624 GetDispatcher(data_pipe_consumer_handle)); |
| 581 if (!dispatcher) | 625 if (!dispatcher) |
| 582 return MOJO_RESULT_INVALID_ARGUMENT; | 626 return MOJO_RESULT_INVALID_ARGUMENT; |
| 583 | 627 |
| 584 return dispatcher->ReadData(elements, num_bytes, flags); | 628 return dispatcher->ReadData(elements, num_bytes, flags); |
| 585 } | 629 } |
| 586 | 630 |
| 587 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, | 631 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, |
| 588 const void** buffer, | 632 const void** buffer, |
| 589 uint32_t* buffer_num_bytes, | 633 uint32_t* buffer_num_bytes, |
| 590 MojoReadDataFlags flags) { | 634 MojoReadDataFlags flags) { |
| 635 RequestContext request_context; |
| 591 scoped_refptr<Dispatcher> dispatcher( | 636 scoped_refptr<Dispatcher> dispatcher( |
| 592 GetDispatcher(data_pipe_consumer_handle)); | 637 GetDispatcher(data_pipe_consumer_handle)); |
| 593 if (!dispatcher) | 638 if (!dispatcher) |
| 594 return MOJO_RESULT_INVALID_ARGUMENT; | 639 return MOJO_RESULT_INVALID_ARGUMENT; |
| 595 | 640 |
| 596 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); | 641 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); |
| 597 } | 642 } |
| 598 | 643 |
| 599 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, | 644 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, |
| 600 uint32_t num_bytes_read) { | 645 uint32_t num_bytes_read) { |
| 646 RequestContext request_context; |
| 601 scoped_refptr<Dispatcher> dispatcher( | 647 scoped_refptr<Dispatcher> dispatcher( |
| 602 GetDispatcher(data_pipe_consumer_handle)); | 648 GetDispatcher(data_pipe_consumer_handle)); |
| 603 if (!dispatcher) | 649 if (!dispatcher) |
| 604 return MOJO_RESULT_INVALID_ARGUMENT; | 650 return MOJO_RESULT_INVALID_ARGUMENT; |
| 605 | 651 |
| 606 return dispatcher->EndReadData(num_bytes_read); | 652 return dispatcher->EndReadData(num_bytes_read); |
| 607 } | 653 } |
| 608 | 654 |
| 609 MojoResult Core::CreateSharedBuffer( | 655 MojoResult Core::CreateSharedBuffer( |
| 610 const MojoCreateSharedBufferOptions* options, | 656 const MojoCreateSharedBufferOptions* options, |
| 611 uint64_t num_bytes, | 657 uint64_t num_bytes, |
| 612 MojoHandle* shared_buffer_handle) { | 658 MojoHandle* shared_buffer_handle) { |
| 659 RequestContext request_context; |
| 613 MojoCreateSharedBufferOptions validated_options = {}; | 660 MojoCreateSharedBufferOptions validated_options = {}; |
| 614 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( | 661 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( |
| 615 options, &validated_options); | 662 options, &validated_options); |
| 616 if (result != MOJO_RESULT_OK) | 663 if (result != MOJO_RESULT_OK) |
| 617 return result; | 664 return result; |
| 618 | 665 |
| 619 scoped_refptr<SharedBufferDispatcher> dispatcher; | 666 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 620 result = SharedBufferDispatcher::Create( | 667 result = SharedBufferDispatcher::Create( |
| 621 validated_options, GetNodeController(), num_bytes, &dispatcher); | 668 validated_options, GetNodeController(), num_bytes, &dispatcher); |
| 622 if (result != MOJO_RESULT_OK) { | 669 if (result != MOJO_RESULT_OK) { |
| 623 DCHECK(!dispatcher); | 670 DCHECK(!dispatcher); |
| 624 return result; | 671 return result; |
| 625 } | 672 } |
| 626 | 673 |
| 627 *shared_buffer_handle = AddDispatcher(dispatcher); | 674 *shared_buffer_handle = AddDispatcher(dispatcher); |
| 628 if (*shared_buffer_handle == MOJO_HANDLE_INVALID) { | 675 if (*shared_buffer_handle == MOJO_HANDLE_INVALID) { |
| 629 LOG(ERROR) << "Handle table full"; | 676 LOG(ERROR) << "Handle table full"; |
| 630 dispatcher->Close(); | 677 dispatcher->Close(); |
| 631 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 678 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 632 } | 679 } |
| 633 | 680 |
| 634 return MOJO_RESULT_OK; | 681 return MOJO_RESULT_OK; |
| 635 } | 682 } |
| 636 | 683 |
| 637 MojoResult Core::DuplicateBufferHandle( | 684 MojoResult Core::DuplicateBufferHandle( |
| 638 MojoHandle buffer_handle, | 685 MojoHandle buffer_handle, |
| 639 const MojoDuplicateBufferHandleOptions* options, | 686 const MojoDuplicateBufferHandleOptions* options, |
| 640 MojoHandle* new_buffer_handle) { | 687 MojoHandle* new_buffer_handle) { |
| 688 RequestContext request_context; |
| 641 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 689 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 642 if (!dispatcher) | 690 if (!dispatcher) |
| 643 return MOJO_RESULT_INVALID_ARGUMENT; | 691 return MOJO_RESULT_INVALID_ARGUMENT; |
| 644 | 692 |
| 645 // Don't verify |options| here; that's the dispatcher's job. | 693 // Don't verify |options| here; that's the dispatcher's job. |
| 646 scoped_refptr<Dispatcher> new_dispatcher; | 694 scoped_refptr<Dispatcher> new_dispatcher; |
| 647 MojoResult result = | 695 MojoResult result = |
| 648 dispatcher->DuplicateBufferHandle(options, &new_dispatcher); | 696 dispatcher->DuplicateBufferHandle(options, &new_dispatcher); |
| 649 if (result != MOJO_RESULT_OK) | 697 if (result != MOJO_RESULT_OK) |
| 650 return result; | 698 return result; |
| 651 | 699 |
| 652 *new_buffer_handle = AddDispatcher(new_dispatcher); | 700 *new_buffer_handle = AddDispatcher(new_dispatcher); |
| 653 if (*new_buffer_handle == MOJO_HANDLE_INVALID) { | 701 if (*new_buffer_handle == MOJO_HANDLE_INVALID) { |
| 654 LOG(ERROR) << "Handle table full"; | 702 LOG(ERROR) << "Handle table full"; |
| 655 dispatcher->Close(); | 703 dispatcher->Close(); |
| 656 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 704 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 657 } | 705 } |
| 658 | 706 |
| 659 return MOJO_RESULT_OK; | 707 return MOJO_RESULT_OK; |
| 660 } | 708 } |
| 661 | 709 |
| 662 MojoResult Core::MapBuffer(MojoHandle buffer_handle, | 710 MojoResult Core::MapBuffer(MojoHandle buffer_handle, |
| 663 uint64_t offset, | 711 uint64_t offset, |
| 664 uint64_t num_bytes, | 712 uint64_t num_bytes, |
| 665 void** buffer, | 713 void** buffer, |
| 666 MojoMapBufferFlags flags) { | 714 MojoMapBufferFlags flags) { |
| 715 RequestContext request_context; |
| 667 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 716 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 668 if (!dispatcher) | 717 if (!dispatcher) |
| 669 return MOJO_RESULT_INVALID_ARGUMENT; | 718 return MOJO_RESULT_INVALID_ARGUMENT; |
| 670 | 719 |
| 671 scoped_ptr<PlatformSharedBufferMapping> mapping; | 720 scoped_ptr<PlatformSharedBufferMapping> mapping; |
| 672 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); | 721 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); |
| 673 if (result != MOJO_RESULT_OK) | 722 if (result != MOJO_RESULT_OK) |
| 674 return result; | 723 return result; |
| 675 | 724 |
| 676 DCHECK(mapping); | 725 DCHECK(mapping); |
| 677 void* address = mapping->GetBase(); | 726 void* address = mapping->GetBase(); |
| 678 { | 727 { |
| 679 base::AutoLock locker(mapping_table_lock_); | 728 base::AutoLock locker(mapping_table_lock_); |
| 680 result = mapping_table_.AddMapping(std::move(mapping)); | 729 result = mapping_table_.AddMapping(std::move(mapping)); |
| 681 } | 730 } |
| 682 if (result != MOJO_RESULT_OK) | 731 if (result != MOJO_RESULT_OK) |
| 683 return result; | 732 return result; |
| 684 | 733 |
| 685 *buffer = address; | 734 *buffer = address; |
| 686 return MOJO_RESULT_OK; | 735 return MOJO_RESULT_OK; |
| 687 } | 736 } |
| 688 | 737 |
| 689 MojoResult Core::UnmapBuffer(void* buffer) { | 738 MojoResult Core::UnmapBuffer(void* buffer) { |
| 739 RequestContext request_context; |
| 690 base::AutoLock lock(mapping_table_lock_); | 740 base::AutoLock lock(mapping_table_lock_); |
| 691 return mapping_table_.RemoveMapping(buffer); | 741 return mapping_table_.RemoveMapping(buffer); |
| 692 } | 742 } |
| 693 | 743 |
| 694 void Core::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) { | 744 void Core::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) { |
| 695 base::AutoLock lock(handles_lock_); | 745 base::AutoLock lock(handles_lock_); |
| 696 handles_.GetActiveHandlesForTest(handles); | 746 handles_.GetActiveHandlesForTest(handles); |
| 697 } | 747 } |
| 698 | 748 |
| 699 MojoResult Core::WaitManyInternal(const MojoHandle* handles, | 749 MojoResult Core::WaitManyInternal(const MojoHandle* handles, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 scoped_ptr<NodeController> node_controller) { | 819 scoped_ptr<NodeController> node_controller) { |
| 770 // It's OK to leak this reference. At this point we know the IO loop is still | 820 // It's OK to leak this reference. At this point we know the IO loop is still |
| 771 // running, and we know the NodeController will observe its eventual | 821 // running, and we know the NodeController will observe its eventual |
| 772 // destruction. This tells the NodeController to delete itself when that | 822 // destruction. This tells the NodeController to delete itself when that |
| 773 // happens. | 823 // happens. |
| 774 node_controller.release()->DestroyOnIOThreadShutdown(); | 824 node_controller.release()->DestroyOnIOThreadShutdown(); |
| 775 } | 825 } |
| 776 | 826 |
| 777 } // namespace edk | 827 } // namespace edk |
| 778 } // namespace mojo | 828 } // namespace mojo |
| OLD | NEW |