OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include <windows.h> |
| 6 |
| 7 #include <limits> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "mojo/edk/embedder/platform_handle.h" |
| 12 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 13 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 14 #include "mojo/edk/system/broker.h" |
| 15 #include "mojo/edk/system/broker_messages.h" |
| 16 #include "mojo/edk/system/channel.h" |
| 17 |
| 18 namespace mojo { |
| 19 namespace edk { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // 256 bytes should be enough for anyone! |
| 24 const size_t kMaxBrokerMessageSize = 256; |
| 25 |
| 26 bool WaitForBrokerMessage(PlatformHandle platform_handle, |
| 27 BrokerMessageType expected_type, |
| 28 size_t expected_num_handles, |
| 29 ScopedPlatformHandle* out_handles) { |
| 30 char buffer[kMaxBrokerMessageSize]; |
| 31 DWORD bytes_read = 0; |
| 32 BOOL result = ::ReadFile(platform_handle.handle, buffer, |
| 33 kMaxBrokerMessageSize, &bytes_read, nullptr); |
| 34 if (!result) { |
| 35 PLOG(ERROR) << "Error reading broker pipe"; |
| 36 return false; |
| 37 } |
| 38 |
| 39 Channel::MessagePtr message = |
| 40 Channel::Message::Deserialize(buffer, static_cast<size_t>(bytes_read)); |
| 41 if (!message || message->payload_size() < sizeof(BrokerMessageHeader)) { |
| 42 LOG(ERROR) << "Invalid broker message"; |
| 43 return false; |
| 44 } |
| 45 |
| 46 if (message->num_handles() != expected_num_handles) { |
| 47 LOG(ERROR) << "Received unexpected number of handles in broker message"; |
| 48 return false; |
| 49 } |
| 50 |
| 51 const BrokerMessageHeader* header = |
| 52 reinterpret_cast<const BrokerMessageHeader*>(message->payload()); |
| 53 if (header->type != expected_type) { |
| 54 LOG(ERROR) << "Unknown broker message type"; |
| 55 return false; |
| 56 } |
| 57 |
| 58 ScopedPlatformHandleVectorPtr handles = message->TakeHandles(); |
| 59 DCHECK(handles); |
| 60 DCHECK_EQ(handles->size(), expected_num_handles); |
| 61 DCHECK(out_handles); |
| 62 |
| 63 for (size_t i = 0; i < expected_num_handles; ++i) |
| 64 out_handles[i] = ScopedPlatformHandle(handles->at(i)); |
| 65 handles->clear(); |
| 66 return true; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 Broker::Broker(ScopedPlatformHandle handle) : sync_channel_(std::move(handle)) { |
| 72 CHECK(sync_channel_.is_valid()); |
| 73 bool result = WaitForBrokerMessage( |
| 74 sync_channel_.get(), BrokerMessageType::INIT, 1, &parent_channel_); |
| 75 DCHECK(result); |
| 76 } |
| 77 |
| 78 Broker::~Broker() {} |
| 79 |
| 80 ScopedPlatformHandle Broker::GetParentPlatformHandle() { |
| 81 return std::move(parent_channel_); |
| 82 } |
| 83 |
| 84 scoped_refptr<PlatformSharedBuffer> Broker::GetSharedBuffer(size_t num_bytes) { |
| 85 base::AutoLock lock(lock_); |
| 86 BufferRequestData* buffer_request; |
| 87 Channel::MessagePtr out_message = CreateBrokerMessage( |
| 88 BrokerMessageType::BUFFER_REQUEST, 0, &buffer_request); |
| 89 buffer_request->size = base::checked_cast<uint32_t>(num_bytes); |
| 90 DWORD bytes_written = 0; |
| 91 BOOL result = ::WriteFile(sync_channel_.get().handle, out_message->data(), |
| 92 static_cast<DWORD>(out_message->data_num_bytes()), |
| 93 &bytes_written, nullptr); |
| 94 if (!result || |
| 95 static_cast<size_t>(bytes_written) != out_message->data_num_bytes()) { |
| 96 LOG(ERROR) << "Error sending sync broker message"; |
| 97 return nullptr; |
| 98 } |
| 99 |
| 100 ScopedPlatformHandle handles[2]; |
| 101 if (WaitForBrokerMessage(sync_channel_.get(), |
| 102 BrokerMessageType::BUFFER_RESPONSE, 2, |
| 103 &handles[0])) { |
| 104 return PlatformSharedBuffer::CreateFromPlatformHandlePair( |
| 105 num_bytes, std::move(handles[0]), std::move(handles[1])); |
| 106 } |
| 107 |
| 108 return nullptr; |
| 109 } |
| 110 |
| 111 } // namespace edk |
| 112 } // namespace mojo |
OLD | NEW |