| 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/system/core.h" | 5 #include "mojo/system/core.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // isn't done, in the in-process case, calls on the old handle may complete | 224 // isn't done, in the in-process case, calls on the old handle may complete |
| 225 // after the the message has been received and a new handle created (and | 225 // after the the message has been received and a new handle created (and |
| 226 // possibly even after calls have been made on the new handle). | 226 // possibly even after calls have been made on the new handle). |
| 227 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, | 227 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, |
| 228 UserPointer<const void> bytes, | 228 UserPointer<const void> bytes, |
| 229 uint32_t num_bytes, | 229 uint32_t num_bytes, |
| 230 UserPointer<const MojoHandle> handles, | 230 UserPointer<const MojoHandle> handles, |
| 231 uint32_t num_handles, | 231 uint32_t num_handles, |
| 232 MojoWriteMessageFlags flags) { | 232 MojoWriteMessageFlags flags) { |
| 233 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); | 233 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); |
| 234 if (!dispatcher) | 234 if (!dispatcher.get()) |
| 235 return MOJO_RESULT_INVALID_ARGUMENT; | 235 return MOJO_RESULT_INVALID_ARGUMENT; |
| 236 | 236 |
| 237 // Easy case: not sending any handles. | 237 // Easy case: not sending any handles. |
| 238 if (num_handles == 0) | 238 if (num_handles == 0) |
| 239 return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags); | 239 return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags); |
| 240 | 240 |
| 241 // We have to handle |handles| here, since we have to mark them busy in the | 241 // We have to handle |handles| here, since we have to mark them busy in the |
| 242 // global handle table. We can't delegate this to the dispatcher, since the | 242 // global handle table. We can't delegate this to the dispatcher, since the |
| 243 // handle table lock must be acquired before the dispatcher lock. | 243 // handle table lock must be acquired before the dispatcher lock. |
| 244 // | 244 // |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 return rv; | 293 return rv; |
| 294 } | 294 } |
| 295 | 295 |
| 296 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, | 296 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, |
| 297 UserPointer<void> bytes, | 297 UserPointer<void> bytes, |
| 298 UserPointer<uint32_t> num_bytes, | 298 UserPointer<uint32_t> num_bytes, |
| 299 UserPointer<MojoHandle> handles, | 299 UserPointer<MojoHandle> handles, |
| 300 UserPointer<uint32_t> num_handles, | 300 UserPointer<uint32_t> num_handles, |
| 301 MojoReadMessageFlags flags) { | 301 MojoReadMessageFlags flags) { |
| 302 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); | 302 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); |
| 303 if (!dispatcher) | 303 if (!dispatcher.get()) |
| 304 return MOJO_RESULT_INVALID_ARGUMENT; | 304 return MOJO_RESULT_INVALID_ARGUMENT; |
| 305 | 305 |
| 306 uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get(); | 306 uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get(); |
| 307 | 307 |
| 308 MojoResult rv; | 308 MojoResult rv; |
| 309 if (num_handles_value == 0) { | 309 if (num_handles_value == 0) { |
| 310 // Easy case: won't receive any handles. | 310 // Easy case: won't receive any handles. |
| 311 rv = dispatcher->ReadMessage( | 311 rv = dispatcher->ReadMessage( |
| 312 bytes, num_bytes, NULL, &num_handles_value, flags); | 312 bytes, num_bytes, NULL, &num_handles_value, flags); |
| 313 } else { | 313 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 327 success = handle_table_.AddDispatcherVector( | 327 success = handle_table_.AddDispatcherVector( |
| 328 dispatchers, handles_writer.GetPointer()); | 328 dispatchers, handles_writer.GetPointer()); |
| 329 } | 329 } |
| 330 if (success) { | 330 if (success) { |
| 331 handles_writer.Commit(); | 331 handles_writer.Commit(); |
| 332 } else { | 332 } else { |
| 333 LOG(ERROR) << "Received message with " << dispatchers.size() | 333 LOG(ERROR) << "Received message with " << dispatchers.size() |
| 334 << " handles, but handle table full"; | 334 << " handles, but handle table full"; |
| 335 // Close dispatchers (outside the lock). | 335 // Close dispatchers (outside the lock). |
| 336 for (size_t i = 0; i < dispatchers.size(); i++) { | 336 for (size_t i = 0; i < dispatchers.size(); i++) { |
| 337 if (dispatchers[i]) | 337 if (dispatchers[i].get()) |
| 338 dispatchers[i]->Close(); | 338 dispatchers[i]->Close(); |
| 339 } | 339 } |
| 340 if (rv == MOJO_RESULT_OK) | 340 if (rv == MOJO_RESULT_OK) |
| 341 rv = MOJO_RESULT_RESOURCE_EXHAUSTED; | 341 rv = MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 342 } | 342 } |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 if (!num_handles.IsNull()) | 346 if (!num_handles.IsNull()) |
| 347 num_handles.Put(num_handles_value); | 347 num_handles.Put(num_handles_value); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 data_pipe_consumer_handle.Put(handle_pair.second); | 386 data_pipe_consumer_handle.Put(handle_pair.second); |
| 387 return MOJO_RESULT_OK; | 387 return MOJO_RESULT_OK; |
| 388 } | 388 } |
| 389 | 389 |
| 390 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, | 390 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, |
| 391 UserPointer<const void> elements, | 391 UserPointer<const void> elements, |
| 392 UserPointer<uint32_t> num_bytes, | 392 UserPointer<uint32_t> num_bytes, |
| 393 MojoWriteDataFlags flags) { | 393 MojoWriteDataFlags flags) { |
| 394 scoped_refptr<Dispatcher> dispatcher( | 394 scoped_refptr<Dispatcher> dispatcher( |
| 395 GetDispatcher(data_pipe_producer_handle)); | 395 GetDispatcher(data_pipe_producer_handle)); |
| 396 if (!dispatcher) | 396 if (!dispatcher.get()) |
| 397 return MOJO_RESULT_INVALID_ARGUMENT; | 397 return MOJO_RESULT_INVALID_ARGUMENT; |
| 398 | 398 |
| 399 return dispatcher->WriteData(elements, num_bytes, flags); | 399 return dispatcher->WriteData(elements, num_bytes, flags); |
| 400 } | 400 } |
| 401 | 401 |
| 402 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, | 402 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, |
| 403 UserPointer<void*> buffer, | 403 UserPointer<void*> buffer, |
| 404 UserPointer<uint32_t> buffer_num_bytes, | 404 UserPointer<uint32_t> buffer_num_bytes, |
| 405 MojoWriteDataFlags flags) { | 405 MojoWriteDataFlags flags) { |
| 406 scoped_refptr<Dispatcher> dispatcher( | 406 scoped_refptr<Dispatcher> dispatcher( |
| 407 GetDispatcher(data_pipe_producer_handle)); | 407 GetDispatcher(data_pipe_producer_handle)); |
| 408 if (!dispatcher) | 408 if (!dispatcher.get()) |
| 409 return MOJO_RESULT_INVALID_ARGUMENT; | 409 return MOJO_RESULT_INVALID_ARGUMENT; |
| 410 | 410 |
| 411 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); | 411 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); |
| 412 } | 412 } |
| 413 | 413 |
| 414 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, | 414 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, |
| 415 uint32_t num_bytes_written) { | 415 uint32_t num_bytes_written) { |
| 416 scoped_refptr<Dispatcher> dispatcher( | 416 scoped_refptr<Dispatcher> dispatcher( |
| 417 GetDispatcher(data_pipe_producer_handle)); | 417 GetDispatcher(data_pipe_producer_handle)); |
| 418 if (!dispatcher) | 418 if (!dispatcher.get()) |
| 419 return MOJO_RESULT_INVALID_ARGUMENT; | 419 return MOJO_RESULT_INVALID_ARGUMENT; |
| 420 | 420 |
| 421 return dispatcher->EndWriteData(num_bytes_written); | 421 return dispatcher->EndWriteData(num_bytes_written); |
| 422 } | 422 } |
| 423 | 423 |
| 424 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, | 424 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, |
| 425 UserPointer<void> elements, | 425 UserPointer<void> elements, |
| 426 UserPointer<uint32_t> num_bytes, | 426 UserPointer<uint32_t> num_bytes, |
| 427 MojoReadDataFlags flags) { | 427 MojoReadDataFlags flags) { |
| 428 scoped_refptr<Dispatcher> dispatcher( | 428 scoped_refptr<Dispatcher> dispatcher( |
| 429 GetDispatcher(data_pipe_consumer_handle)); | 429 GetDispatcher(data_pipe_consumer_handle)); |
| 430 if (!dispatcher) | 430 if (!dispatcher.get()) |
| 431 return MOJO_RESULT_INVALID_ARGUMENT; | 431 return MOJO_RESULT_INVALID_ARGUMENT; |
| 432 | 432 |
| 433 return dispatcher->ReadData(elements, num_bytes, flags); | 433 return dispatcher->ReadData(elements, num_bytes, flags); |
| 434 } | 434 } |
| 435 | 435 |
| 436 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, | 436 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, |
| 437 UserPointer<const void*> buffer, | 437 UserPointer<const void*> buffer, |
| 438 UserPointer<uint32_t> buffer_num_bytes, | 438 UserPointer<uint32_t> buffer_num_bytes, |
| 439 MojoReadDataFlags flags) { | 439 MojoReadDataFlags flags) { |
| 440 scoped_refptr<Dispatcher> dispatcher( | 440 scoped_refptr<Dispatcher> dispatcher( |
| 441 GetDispatcher(data_pipe_consumer_handle)); | 441 GetDispatcher(data_pipe_consumer_handle)); |
| 442 if (!dispatcher) | 442 if (!dispatcher.get()) |
| 443 return MOJO_RESULT_INVALID_ARGUMENT; | 443 return MOJO_RESULT_INVALID_ARGUMENT; |
| 444 | 444 |
| 445 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); | 445 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); |
| 446 } | 446 } |
| 447 | 447 |
| 448 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, | 448 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, |
| 449 uint32_t num_bytes_read) { | 449 uint32_t num_bytes_read) { |
| 450 scoped_refptr<Dispatcher> dispatcher( | 450 scoped_refptr<Dispatcher> dispatcher( |
| 451 GetDispatcher(data_pipe_consumer_handle)); | 451 GetDispatcher(data_pipe_consumer_handle)); |
| 452 if (!dispatcher) | 452 if (!dispatcher.get()) |
| 453 return MOJO_RESULT_INVALID_ARGUMENT; | 453 return MOJO_RESULT_INVALID_ARGUMENT; |
| 454 | 454 |
| 455 return dispatcher->EndReadData(num_bytes_read); | 455 return dispatcher->EndReadData(num_bytes_read); |
| 456 } | 456 } |
| 457 | 457 |
| 458 MojoResult Core::CreateSharedBuffer( | 458 MojoResult Core::CreateSharedBuffer( |
| 459 UserPointer<const MojoCreateSharedBufferOptions> options, | 459 UserPointer<const MojoCreateSharedBufferOptions> options, |
| 460 uint64_t num_bytes, | 460 uint64_t num_bytes, |
| 461 UserPointer<MojoHandle> shared_buffer_handle) { | 461 UserPointer<MojoHandle> shared_buffer_handle) { |
| 462 MojoCreateSharedBufferOptions validated_options = {}; | 462 MojoCreateSharedBufferOptions validated_options = {}; |
| 463 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( | 463 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( |
| 464 options, &validated_options); | 464 options, &validated_options); |
| 465 if (result != MOJO_RESULT_OK) | 465 if (result != MOJO_RESULT_OK) |
| 466 return result; | 466 return result; |
| 467 | 467 |
| 468 scoped_refptr<SharedBufferDispatcher> dispatcher; | 468 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 469 result = SharedBufferDispatcher::Create( | 469 result = SharedBufferDispatcher::Create( |
| 470 platform_support(), validated_options, num_bytes, &dispatcher); | 470 platform_support(), validated_options, num_bytes, &dispatcher); |
| 471 if (result != MOJO_RESULT_OK) { | 471 if (result != MOJO_RESULT_OK) { |
| 472 DCHECK(!dispatcher); | 472 DCHECK(!dispatcher.get()); |
| 473 return result; | 473 return result; |
| 474 } | 474 } |
| 475 | 475 |
| 476 MojoHandle h = AddDispatcher(dispatcher); | 476 MojoHandle h = AddDispatcher(dispatcher); |
| 477 if (h == MOJO_HANDLE_INVALID) { | 477 if (h == MOJO_HANDLE_INVALID) { |
| 478 LOG(ERROR) << "Handle table full"; | 478 LOG(ERROR) << "Handle table full"; |
| 479 dispatcher->Close(); | 479 dispatcher->Close(); |
| 480 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 480 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 481 } | 481 } |
| 482 | 482 |
| 483 shared_buffer_handle.Put(h); | 483 shared_buffer_handle.Put(h); |
| 484 return MOJO_RESULT_OK; | 484 return MOJO_RESULT_OK; |
| 485 } | 485 } |
| 486 | 486 |
| 487 MojoResult Core::DuplicateBufferHandle( | 487 MojoResult Core::DuplicateBufferHandle( |
| 488 MojoHandle buffer_handle, | 488 MojoHandle buffer_handle, |
| 489 UserPointer<const MojoDuplicateBufferHandleOptions> options, | 489 UserPointer<const MojoDuplicateBufferHandleOptions> options, |
| 490 UserPointer<MojoHandle> new_buffer_handle) { | 490 UserPointer<MojoHandle> new_buffer_handle) { |
| 491 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 491 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 492 if (!dispatcher) | 492 if (!dispatcher.get()) |
| 493 return MOJO_RESULT_INVALID_ARGUMENT; | 493 return MOJO_RESULT_INVALID_ARGUMENT; |
| 494 | 494 |
| 495 // Don't verify |options| here; that's the dispatcher's job. | 495 // Don't verify |options| here; that's the dispatcher's job. |
| 496 scoped_refptr<Dispatcher> new_dispatcher; | 496 scoped_refptr<Dispatcher> new_dispatcher; |
| 497 MojoResult result = | 497 MojoResult result = |
| 498 dispatcher->DuplicateBufferHandle(options, &new_dispatcher); | 498 dispatcher->DuplicateBufferHandle(options, &new_dispatcher); |
| 499 if (result != MOJO_RESULT_OK) | 499 if (result != MOJO_RESULT_OK) |
| 500 return result; | 500 return result; |
| 501 | 501 |
| 502 MojoHandle new_handle = AddDispatcher(new_dispatcher); | 502 MojoHandle new_handle = AddDispatcher(new_dispatcher); |
| 503 if (new_handle == MOJO_HANDLE_INVALID) { | 503 if (new_handle == MOJO_HANDLE_INVALID) { |
| 504 LOG(ERROR) << "Handle table full"; | 504 LOG(ERROR) << "Handle table full"; |
| 505 dispatcher->Close(); | 505 dispatcher->Close(); |
| 506 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 506 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 507 } | 507 } |
| 508 | 508 |
| 509 new_buffer_handle.Put(new_handle); | 509 new_buffer_handle.Put(new_handle); |
| 510 return MOJO_RESULT_OK; | 510 return MOJO_RESULT_OK; |
| 511 } | 511 } |
| 512 | 512 |
| 513 MojoResult Core::MapBuffer(MojoHandle buffer_handle, | 513 MojoResult Core::MapBuffer(MojoHandle buffer_handle, |
| 514 uint64_t offset, | 514 uint64_t offset, |
| 515 uint64_t num_bytes, | 515 uint64_t num_bytes, |
| 516 UserPointer<void*> buffer, | 516 UserPointer<void*> buffer, |
| 517 MojoMapBufferFlags flags) { | 517 MojoMapBufferFlags flags) { |
| 518 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 518 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 519 if (!dispatcher) | 519 if (!dispatcher.get()) |
| 520 return MOJO_RESULT_INVALID_ARGUMENT; | 520 return MOJO_RESULT_INVALID_ARGUMENT; |
| 521 | 521 |
| 522 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 522 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; |
| 523 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); | 523 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); |
| 524 if (result != MOJO_RESULT_OK) | 524 if (result != MOJO_RESULT_OK) |
| 525 return result; | 525 return result; |
| 526 | 526 |
| 527 DCHECK(mapping); | 527 DCHECK(mapping); |
| 528 void* address = mapping->GetBase(); | 528 void* address = mapping->GetBase(); |
| 529 { | 529 { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 552 MojoDeadline deadline, | 552 MojoDeadline deadline, |
| 553 uint32_t* result_index, | 553 uint32_t* result_index, |
| 554 HandleSignalsState* signals_states) { | 554 HandleSignalsState* signals_states) { |
| 555 DCHECK_GT(num_handles, 0u); | 555 DCHECK_GT(num_handles, 0u); |
| 556 DCHECK_EQ(*result_index, static_cast<uint32_t>(-1)); | 556 DCHECK_EQ(*result_index, static_cast<uint32_t>(-1)); |
| 557 | 557 |
| 558 DispatcherVector dispatchers; | 558 DispatcherVector dispatchers; |
| 559 dispatchers.reserve(num_handles); | 559 dispatchers.reserve(num_handles); |
| 560 for (uint32_t i = 0; i < num_handles; i++) { | 560 for (uint32_t i = 0; i < num_handles; i++) { |
| 561 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); | 561 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); |
| 562 if (!dispatcher) { | 562 if (!dispatcher.get()) { |
| 563 *result_index = i; | 563 *result_index = i; |
| 564 return MOJO_RESULT_INVALID_ARGUMENT; | 564 return MOJO_RESULT_INVALID_ARGUMENT; |
| 565 } | 565 } |
| 566 dispatchers.push_back(dispatcher); | 566 dispatchers.push_back(dispatcher); |
| 567 } | 567 } |
| 568 | 568 |
| 569 // TODO(vtl): Should make the waiter live (permanently) in TLS. | 569 // TODO(vtl): Should make the waiter live (permanently) in TLS. |
| 570 Waiter waiter; | 570 Waiter waiter; |
| 571 waiter.Init(); | 571 waiter.Init(); |
| 572 | 572 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 597 if (signals_states) { | 597 if (signals_states) { |
| 598 for (; i < num_handles; i++) | 598 for (; i < num_handles; i++) |
| 599 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); | 599 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); |
| 600 } | 600 } |
| 601 | 601 |
| 602 return rv; | 602 return rv; |
| 603 } | 603 } |
| 604 | 604 |
| 605 } // namespace system | 605 } // namespace system |
| 606 } // namespace mojo | 606 } // namespace mojo |
| OLD | NEW |