| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "mojo/edk/system/message_in_transit.h" | 16 #include "mojo/edk/system/message_in_transit.h" |
| 17 #include "mojo/edk/system/transport_data.h" | 17 #include "mojo/edk/system/transport_data.h" |
| 18 | 18 |
| 19 using mojo::embedder::ScopedPlatformHandle; |
| 19 using mojo::util::MutexLocker; | 20 using mojo::util::MutexLocker; |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 namespace system { | 23 namespace system { |
| 23 | 24 |
| 24 const size_t kReadSize = 4096; | 25 const size_t kReadSize = 4096; |
| 25 | 26 |
| 26 // RawChannel::ReadBuffer ------------------------------------------------------ | 27 // RawChannel::ReadBuffer ------------------------------------------------------ |
| 27 | 28 |
| 28 RawChannel::ReadBuffer::ReadBuffer() : buffer_(kReadSize), num_valid_bytes_(0) { | 29 RawChannel::ReadBuffer::ReadBuffer() : buffer_(kReadSize), num_valid_bytes_(0) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 51 | 52 |
| 52 bool RawChannel::WriteBuffer::HavePlatformHandlesToSend() const { | 53 bool RawChannel::WriteBuffer::HavePlatformHandlesToSend() const { |
| 53 if (message_queue_.IsEmpty()) | 54 if (message_queue_.IsEmpty()) |
| 54 return false; | 55 return false; |
| 55 | 56 |
| 56 const TransportData* transport_data = | 57 const TransportData* transport_data = |
| 57 message_queue_.PeekMessage()->transport_data(); | 58 message_queue_.PeekMessage()->transport_data(); |
| 58 if (!transport_data) | 59 if (!transport_data) |
| 59 return false; | 60 return false; |
| 60 | 61 |
| 61 const embedder::PlatformHandleVector* all_platform_handles = | 62 const std::vector<ScopedPlatformHandle>* all_platform_handles = |
| 62 transport_data->platform_handles(); | 63 transport_data->platform_handles(); |
| 63 if (!all_platform_handles) { | 64 if (!all_platform_handles) { |
| 64 DCHECK_EQ(platform_handles_offset_, 0u); | 65 DCHECK_EQ(platform_handles_offset_, 0u); |
| 65 return false; | 66 return false; |
| 66 } | 67 } |
| 67 if (platform_handles_offset_ >= all_platform_handles->size()) { | 68 if (platform_handles_offset_ >= all_platform_handles->size()) { |
| 68 DCHECK_EQ(platform_handles_offset_, all_platform_handles->size()); | 69 DCHECK_EQ(platform_handles_offset_, all_platform_handles->size()); |
| 69 return false; | 70 return false; |
| 70 } | 71 } |
| 71 | 72 |
| 72 return true; | 73 return true; |
| 73 } | 74 } |
| 74 | 75 |
| 75 void RawChannel::WriteBuffer::GetPlatformHandlesToSend( | 76 void RawChannel::WriteBuffer::GetPlatformHandlesToSend( |
| 76 size_t* num_platform_handles, | 77 size_t* num_platform_handles, |
| 77 embedder::PlatformHandle** platform_handles, | 78 embedder::PlatformHandle** platform_handles, |
| 78 void** serialization_data) { | 79 void** serialization_data) { |
| 79 DCHECK(HavePlatformHandlesToSend()); | 80 DCHECK(HavePlatformHandlesToSend()); |
| 80 | 81 |
| 81 MessageInTransit* message = message_queue_.PeekMessage(); | 82 MessageInTransit* message = message_queue_.PeekMessage(); |
| 82 TransportData* transport_data = message->transport_data(); | 83 TransportData* transport_data = message->transport_data(); |
| 83 embedder::PlatformHandleVector* all_platform_handles = | 84 std::vector<ScopedPlatformHandle>* all_platform_handles = |
| 84 transport_data->platform_handles(); | 85 transport_data->platform_handles(); |
| 85 *num_platform_handles = | 86 *num_platform_handles = |
| 86 all_platform_handles->size() - platform_handles_offset_; | 87 all_platform_handles->size() - platform_handles_offset_; |
| 87 *platform_handles = &(*all_platform_handles)[platform_handles_offset_]; | 88 *platform_handles = reinterpret_cast<embedder::PlatformHandle*>( |
| 89 &(*all_platform_handles)[platform_handles_offset_]); |
| 88 | 90 |
| 89 if (serialized_platform_handle_size_ > 0) { | 91 if (serialized_platform_handle_size_ > 0) { |
| 90 size_t serialization_data_offset = | 92 size_t serialization_data_offset = |
| 91 transport_data->platform_handle_table_offset(); | 93 transport_data->platform_handle_table_offset(); |
| 92 serialization_data_offset += | 94 serialization_data_offset += |
| 93 platform_handles_offset_ * serialized_platform_handle_size_; | 95 platform_handles_offset_ * serialized_platform_handle_size_; |
| 94 *serialization_data = static_cast<char*>(transport_data->buffer()) + | 96 *serialization_data = static_cast<char*>(transport_data->buffer()) + |
| 95 serialization_data_offset; | 97 serialization_data_offset; |
| 96 } else { | 98 } else { |
| 97 *serialization_data = nullptr; | 99 *serialization_data = nullptr; |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 CallOnError(Delegate::ERROR_READ_BAD_MESSAGE); | 321 CallOnError(Delegate::ERROR_READ_BAD_MESSAGE); |
| 320 return; // |this| may have been destroyed in |CallOnError()|. | 322 return; // |this| may have been destroyed in |CallOnError()|. |
| 321 } | 323 } |
| 322 | 324 |
| 323 if (message_view.type() == MessageInTransit::Type::RAW_CHANNEL) { | 325 if (message_view.type() == MessageInTransit::Type::RAW_CHANNEL) { |
| 324 if (!OnReadMessageForRawChannel(message_view)) { | 326 if (!OnReadMessageForRawChannel(message_view)) { |
| 325 CallOnError(Delegate::ERROR_READ_BAD_MESSAGE); | 327 CallOnError(Delegate::ERROR_READ_BAD_MESSAGE); |
| 326 return; // |this| may have been destroyed in |CallOnError()|. | 328 return; // |this| may have been destroyed in |CallOnError()|. |
| 327 } | 329 } |
| 328 } else { | 330 } else { |
| 329 embedder::ScopedPlatformHandleVectorPtr platform_handles; | 331 std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles; |
| 330 if (message_view.transport_data_buffer()) { | 332 if (message_view.transport_data_buffer()) { |
| 331 size_t num_platform_handles; | 333 size_t num_platform_handles; |
| 332 const void* platform_handle_table; | 334 const void* platform_handle_table; |
| 333 TransportData::GetPlatformHandleTable( | 335 TransportData::GetPlatformHandleTable( |
| 334 message_view.transport_data_buffer(), &num_platform_handles, | 336 message_view.transport_data_buffer(), &num_platform_handles, |
| 335 &platform_handle_table); | 337 &platform_handle_table); |
| 336 | 338 |
| 337 if (num_platform_handles > 0) { | 339 if (num_platform_handles > 0) { |
| 338 platform_handles = GetReadPlatformHandles(num_platform_handles, | 340 platform_handles = GetReadPlatformHandles(num_platform_handles, |
| 339 platform_handle_table); | 341 platform_handle_table); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 | 508 |
| 507 write_stopped_ = true; | 509 write_stopped_ = true; |
| 508 write_buffer_->message_queue_.Clear(); | 510 write_buffer_->message_queue_.Clear(); |
| 509 write_buffer_->platform_handles_offset_ = 0; | 511 write_buffer_->platform_handles_offset_ = 0; |
| 510 write_buffer_->data_offset_ = 0; | 512 write_buffer_->data_offset_ = 0; |
| 511 return false; | 513 return false; |
| 512 } | 514 } |
| 513 | 515 |
| 514 } // namespace system | 516 } // namespace system |
| 515 } // namespace mojo | 517 } // namespace mojo |
| OLD | NEW |