| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/broker_host.h" | 5 #include "mojo/edk/system/broker_host.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "mojo/edk/embedder/named_platform_channel_pair.h" | 13 #include "mojo/edk/embedder/named_platform_channel_pair.h" |
| 14 #include "mojo/edk/embedder/named_platform_handle.h" | 14 #include "mojo/edk/embedder/named_platform_handle.h" |
| 15 #include "mojo/edk/embedder/platform_handle_vector.h" | 15 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 16 #include "mojo/edk/embedder/platform_shared_buffer.h" | 16 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 17 #include "mojo/edk/system/broker_messages.h" | 17 #include "mojo/edk/system/broker_messages.h" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 namespace edk { | 20 namespace edk { |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 // To prevent abuse, limit the maximum size of shared memory buffers. | |
| 25 // TODO(rockot): Re-consider this limit, or do something smarter. | |
| 26 const uint32_t kMaxSharedBufferSize = 16 * 1024 * 1024; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 BrokerHost::BrokerHost(base::ProcessHandle client_process, | 22 BrokerHost::BrokerHost(base::ProcessHandle client_process, |
| 31 ScopedPlatformHandle platform_handle) | 23 ScopedPlatformHandle platform_handle) |
| 32 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 33 : client_process_(client_process) | 25 : client_process_(client_process) |
| 34 #endif | 26 #endif |
| 35 { | 27 { |
| 36 CHECK(platform_handle.is_valid()); | 28 CHECK(platform_handle.is_valid()); |
| 37 | 29 |
| 38 base::MessageLoop::current()->AddDestructionObserver(this); | 30 base::MessageLoop::current()->AddDestructionObserver(this); |
| 39 | 31 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 BrokerMessageType::INIT, 0, sizeof(*name_data) * pipe_name.length(), | 92 BrokerMessageType::INIT, 0, sizeof(*name_data) * pipe_name.length(), |
| 101 &data, reinterpret_cast<void**>(&name_data)); | 93 &data, reinterpret_cast<void**>(&name_data)); |
| 102 data->pipe_name_length = static_cast<uint32_t>(pipe_name.length()); | 94 data->pipe_name_length = static_cast<uint32_t>(pipe_name.length()); |
| 103 std::copy(pipe_name.begin(), pipe_name.end(), name_data); | 95 std::copy(pipe_name.begin(), pipe_name.end(), name_data); |
| 104 channel_->Write(std::move(message)); | 96 channel_->Write(std::move(message)); |
| 105 } | 97 } |
| 106 | 98 |
| 107 #endif // defined(OS_WIN) | 99 #endif // defined(OS_WIN) |
| 108 | 100 |
| 109 void BrokerHost::OnBufferRequest(uint32_t num_bytes) { | 101 void BrokerHost::OnBufferRequest(uint32_t num_bytes) { |
| 110 scoped_refptr<PlatformSharedBuffer> buffer; | |
| 111 scoped_refptr<PlatformSharedBuffer> read_only_buffer; | 102 scoped_refptr<PlatformSharedBuffer> read_only_buffer; |
| 112 if (num_bytes <= kMaxSharedBufferSize) { | 103 scoped_refptr<PlatformSharedBuffer> buffer = |
| 113 buffer = PlatformSharedBuffer::Create(num_bytes); | 104 PlatformSharedBuffer::Create(num_bytes); |
| 114 if (buffer) | 105 if (buffer) |
| 115 read_only_buffer = buffer->CreateReadOnlyDuplicate(); | 106 read_only_buffer = buffer->CreateReadOnlyDuplicate(); |
| 116 if (!read_only_buffer) | 107 if (!read_only_buffer) |
| 117 buffer = nullptr; | 108 buffer = nullptr; |
| 118 } else { | |
| 119 LOG(ERROR) << "Shared buffer request too large: " << num_bytes; | |
| 120 } | |
| 121 | 109 |
| 122 Channel::MessagePtr message = CreateBrokerMessage( | 110 Channel::MessagePtr message = CreateBrokerMessage( |
| 123 BrokerMessageType::BUFFER_RESPONSE, buffer ? 2 : 0, nullptr); | 111 BrokerMessageType::BUFFER_RESPONSE, buffer ? 2 : 0, nullptr); |
| 124 if (buffer) { | 112 if (buffer) { |
| 125 ScopedPlatformHandleVectorPtr handles; | 113 ScopedPlatformHandleVectorPtr handles; |
| 126 handles.reset(new PlatformHandleVector(2)); | 114 handles.reset(new PlatformHandleVector(2)); |
| 127 handles->at(0) = buffer->PassPlatformHandle().release(); | 115 handles->at(0) = buffer->PassPlatformHandle().release(); |
| 128 handles->at(1) = read_only_buffer->PassPlatformHandle().release(); | 116 handles->at(1) = read_only_buffer->PassPlatformHandle().release(); |
| 129 PrepareHandlesForClient(handles.get()); | 117 PrepareHandlesForClient(handles.get()); |
| 130 message->SetHandles(std::move(handles)); | 118 message->SetHandles(std::move(handles)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 156 break; | 144 break; |
| 157 } | 145 } |
| 158 } | 146 } |
| 159 | 147 |
| 160 void BrokerHost::OnChannelError() { delete this; } | 148 void BrokerHost::OnChannelError() { delete this; } |
| 161 | 149 |
| 162 void BrokerHost::WillDestroyCurrentMessageLoop() { delete this; } | 150 void BrokerHost::WillDestroyCurrentMessageLoop() { delete this; } |
| 163 | 151 |
| 164 } // namespace edk | 152 } // namespace edk |
| 165 } // namespace mojo | 153 } // namespace mojo |
| OLD | NEW |