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/shared_buffer_dispatcher.h" | 5 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "mojo/edk/embedder/platform_support.h" | 11 #include "mojo/edk/embedder/platform_support.h" |
12 #include "mojo/edk/system/channel.h" | 12 #include "mojo/edk/system/channel.h" |
13 #include "mojo/edk/system/configuration.h" | 13 #include "mojo/edk/system/configuration.h" |
14 #include "mojo/edk/system/memory.h" | 14 #include "mojo/edk/system/memory.h" |
15 #include "mojo/edk/system/options_validation.h" | 15 #include "mojo/edk/system/options_validation.h" |
16 #include "mojo/public/c/system/macros.h" | 16 #include "mojo/public/c/system/macros.h" |
17 | 17 |
| 18 using mojo::embedder::ScopedPlatformHandle; |
18 using mojo::util::RefPtr; | 19 using mojo::util::RefPtr; |
19 | 20 |
20 namespace mojo { | 21 namespace mojo { |
21 namespace system { | 22 namespace system { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 struct MOJO_ALIGNAS(8) SerializedSharedBufferDispatcher { | 26 struct MOJO_ALIGNAS(8) SerializedSharedBufferDispatcher { |
26 uint64_t num_bytes; | 27 uint64_t num_bytes; |
27 uint32_t platform_handle_index; | 28 uint32_t platform_handle_index; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 92 |
92 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 93 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
93 return Type::SHARED_BUFFER; | 94 return Type::SHARED_BUFFER; |
94 } | 95 } |
95 | 96 |
96 // static | 97 // static |
97 RefPtr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( | 98 RefPtr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( |
98 Channel* channel, | 99 Channel* channel, |
99 const void* source, | 100 const void* source, |
100 size_t size, | 101 size_t size, |
101 embedder::PlatformHandleVector* platform_handles) { | 102 std::vector<ScopedPlatformHandle>* platform_handles) { |
102 DCHECK(channel); | 103 DCHECK(channel); |
103 | 104 |
104 if (size != sizeof(SerializedSharedBufferDispatcher)) { | 105 if (size != sizeof(SerializedSharedBufferDispatcher)) { |
105 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; | 106 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; |
106 return nullptr; | 107 return nullptr; |
107 } | 108 } |
108 | 109 |
109 const SerializedSharedBufferDispatcher* serialization = | 110 const SerializedSharedBufferDispatcher* serialization = |
110 static_cast<const SerializedSharedBufferDispatcher*>(source); | 111 static_cast<const SerializedSharedBufferDispatcher*>(source); |
111 | 112 |
(...skipping 21 matching lines...) Expand all Loading... |
133 return nullptr; | 134 return nullptr; |
134 } | 135 } |
135 | 136 |
136 if (!platform_handles || platform_handle_index >= platform_handles->size()) { | 137 if (!platform_handles || platform_handle_index >= platform_handles->size()) { |
137 LOG(ERROR) | 138 LOG(ERROR) |
138 << "Invalid serialized shared buffer dispatcher (missing handles)"; | 139 << "Invalid serialized shared buffer dispatcher (missing handles)"; |
139 return nullptr; | 140 return nullptr; |
140 } | 141 } |
141 | 142 |
142 // Starts off invalid, which is what we want. | 143 // Starts off invalid, which is what we want. |
143 embedder::PlatformHandle platform_handle; | 144 ScopedPlatformHandle platform_handle; |
144 // We take ownership of the handle, so we have to invalidate the one in | 145 // We take ownership of the handle, so we have to invalidate the one in |
145 // |platform_handles|. | 146 // |platform_handles|. |
146 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 147 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
147 | 148 |
148 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be | 149 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
149 // closed even if creation fails. | 150 // closed even if creation fails. |
150 auto shared_buffer = | 151 auto shared_buffer = |
151 channel->platform_support()->CreateSharedBufferFromHandle( | 152 channel->platform_support()->CreateSharedBufferFromHandle( |
152 num_bytes, embedder::ScopedPlatformHandle(platform_handle)); | 153 num_bytes, std::move(platform_handle)); |
153 if (!shared_buffer) { | 154 if (!shared_buffer) { |
154 LOG(ERROR) | 155 LOG(ERROR) |
155 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; | 156 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
156 return nullptr; | 157 return nullptr; |
157 } | 158 } |
158 | 159 |
159 return CreateInternal(std::move(shared_buffer)); | 160 return CreateInternal(std::move(shared_buffer)); |
160 } | 161 } |
161 | 162 |
162 SharedBufferDispatcher::SharedBufferDispatcher( | 163 SharedBufferDispatcher::SharedBufferDispatcher( |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 size_t* max_platform_handles) { | 261 size_t* max_platform_handles) { |
261 AssertHasOneRef(); // Only one ref => no need to take the lock. | 262 AssertHasOneRef(); // Only one ref => no need to take the lock. |
262 *max_size = sizeof(SerializedSharedBufferDispatcher); | 263 *max_size = sizeof(SerializedSharedBufferDispatcher); |
263 *max_platform_handles = 1; | 264 *max_platform_handles = 1; |
264 } | 265 } |
265 | 266 |
266 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( | 267 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( |
267 Channel* /*channel*/, | 268 Channel* /*channel*/, |
268 void* destination, | 269 void* destination, |
269 size_t* actual_size, | 270 size_t* actual_size, |
270 embedder::PlatformHandleVector* platform_handles) { | 271 std::vector<ScopedPlatformHandle>* platform_handles) { |
271 AssertHasOneRef(); // Only one ref => no need to take the lock. | 272 AssertHasOneRef(); // Only one ref => no need to take the lock. |
272 DCHECK(shared_buffer_); | 273 DCHECK(shared_buffer_); |
273 | 274 |
274 SerializedSharedBufferDispatcher* serialization = | 275 SerializedSharedBufferDispatcher* serialization = |
275 static_cast<SerializedSharedBufferDispatcher*>(destination); | 276 static_cast<SerializedSharedBufferDispatcher*>(destination); |
276 // If there's only one reference to |shared_buffer_|, then it's ours (and no | 277 // If there's only one reference to |shared_buffer_|, then it's ours (and no |
277 // one else can make any more references to it), so we can just take its | 278 // one else can make any more references to it), so we can just take its |
278 // handle. | 279 // handle. |
279 embedder::ScopedPlatformHandle platform_handle( | 280 ScopedPlatformHandle platform_handle( |
280 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() | 281 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() |
281 : shared_buffer_->DuplicatePlatformHandle()); | 282 : shared_buffer_->DuplicatePlatformHandle()); |
282 if (!platform_handle.is_valid()) { | 283 if (!platform_handle.is_valid()) { |
283 shared_buffer_ = nullptr; | 284 shared_buffer_ = nullptr; |
284 return false; | 285 return false; |
285 } | 286 } |
286 | 287 |
287 serialization->num_bytes = shared_buffer_->GetNumBytes(); | 288 serialization->num_bytes = shared_buffer_->GetNumBytes(); |
288 serialization->platform_handle_index = platform_handles->size(); | 289 serialization->platform_handle_index = platform_handles->size(); |
289 platform_handles->push_back(platform_handle.release()); | 290 platform_handles->push_back(std::move(platform_handle)); |
290 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 291 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
291 | 292 |
292 shared_buffer_ = nullptr; | 293 shared_buffer_ = nullptr; |
293 | 294 |
294 return true; | 295 return true; |
295 } | 296 } |
296 | 297 |
297 } // namespace system | 298 } // namespace system |
298 } // namespace mojo | 299 } // namespace mojo |
OLD | NEW |