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 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "mojo/edk/embedder/platform_support.h" | 10 #include "mojo/edk/embedder/platform_support.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 out_options->flags = reader.options().flags; | 54 out_options->flags = reader.options().flags; |
55 | 55 |
56 // Checks for fields beyond |flags|: | 56 // Checks for fields beyond |flags|: |
57 | 57 |
58 // (Nothing here yet.) | 58 // (Nothing here yet.) |
59 | 59 |
60 return MOJO_RESULT_OK; | 60 return MOJO_RESULT_OK; |
61 } | 61 } |
62 | 62 |
63 // static | 63 // static |
64 MojoResult SharedBufferDispatcher::Create( | 64 RefPtr<SharedBufferDispatcher> SharedBufferDispatcher::Create( |
65 embedder::PlatformSupport* platform_support, | 65 embedder::PlatformSupport* platform_support, |
66 const MojoCreateSharedBufferOptions& /*validated_options*/, | 66 const MojoCreateSharedBufferOptions& /*validated_options*/, |
67 uint64_t num_bytes, | 67 uint64_t num_bytes, |
68 scoped_refptr<SharedBufferDispatcher>* result) { | 68 MojoResult* result) { |
69 if (!num_bytes) | 69 if (!num_bytes) { |
70 return MOJO_RESULT_INVALID_ARGUMENT; | 70 *result = MOJO_RESULT_INVALID_ARGUMENT; |
71 if (num_bytes > GetConfiguration().max_shared_memory_num_bytes) | 71 return nullptr; |
72 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 72 } |
| 73 if (num_bytes > GetConfiguration().max_shared_memory_num_bytes) { |
| 74 *result = MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 75 return nullptr; |
| 76 } |
73 | 77 |
74 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 78 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
75 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); | 79 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); |
76 if (!shared_buffer) | 80 if (!shared_buffer) { |
77 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 81 *result = MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 82 return nullptr; |
| 83 } |
78 | 84 |
79 *result = CreateInternal(shared_buffer.Pass()); | 85 *result = MOJO_RESULT_OK; |
80 return MOJO_RESULT_OK; | 86 return CreateInternal(shared_buffer.Pass()); |
81 } | 87 } |
82 | 88 |
83 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 89 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
84 return Type::SHARED_BUFFER; | 90 return Type::SHARED_BUFFER; |
85 } | 91 } |
86 | 92 |
87 // static | 93 // static |
88 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( | 94 RefPtr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( |
89 Channel* channel, | 95 Channel* channel, |
90 const void* source, | 96 const void* source, |
91 size_t size, | 97 size_t size, |
92 embedder::PlatformHandleVector* platform_handles) { | 98 embedder::PlatformHandleVector* platform_handles) { |
93 DCHECK(channel); | 99 DCHECK(channel); |
94 | 100 |
95 if (size != sizeof(SerializedSharedBufferDispatcher)) { | 101 if (size != sizeof(SerializedSharedBufferDispatcher)) { |
96 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; | 102 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; |
97 return nullptr; | 103 return nullptr; |
98 } | 104 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 180 |
175 return MOJO_RESULT_OK; | 181 return MOJO_RESULT_OK; |
176 } | 182 } |
177 | 183 |
178 void SharedBufferDispatcher::CloseImplNoLock() { | 184 void SharedBufferDispatcher::CloseImplNoLock() { |
179 mutex().AssertHeld(); | 185 mutex().AssertHeld(); |
180 DCHECK(shared_buffer_); | 186 DCHECK(shared_buffer_); |
181 shared_buffer_ = nullptr; | 187 shared_buffer_ = nullptr; |
182 } | 188 } |
183 | 189 |
184 scoped_refptr<Dispatcher> | 190 RefPtr<Dispatcher> |
185 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | 191 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
186 mutex().AssertHeld(); | 192 mutex().AssertHeld(); |
187 DCHECK(shared_buffer_); | 193 DCHECK(shared_buffer_); |
188 return CreateInternal(shared_buffer_.Pass()); | 194 return CreateInternal(shared_buffer_.Pass()); |
189 } | 195 } |
190 | 196 |
191 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( | 197 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( |
192 UserPointer<const MojoDuplicateBufferHandleOptions> options, | 198 UserPointer<const MojoDuplicateBufferHandleOptions> options, |
193 scoped_refptr<Dispatcher>* new_dispatcher) { | 199 RefPtr<Dispatcher>* new_dispatcher) { |
194 mutex().AssertHeld(); | 200 mutex().AssertHeld(); |
195 | 201 |
196 MojoDuplicateBufferHandleOptions validated_options; | 202 MojoDuplicateBufferHandleOptions validated_options; |
197 MojoResult result = ValidateDuplicateOptions(options, &validated_options); | 203 MojoResult result = ValidateDuplicateOptions(options, &validated_options); |
198 if (result != MOJO_RESULT_OK) | 204 if (result != MOJO_RESULT_OK) |
199 return result; | 205 return result; |
200 | 206 |
201 // Note: Since this is "duplicate", we keep our ref to |shared_buffer_|. | 207 // Note: Since this is "duplicate", we keep our ref to |shared_buffer_|. |
202 *new_dispatcher = CreateInternal(shared_buffer_); | 208 *new_dispatcher = CreateInternal(shared_buffer_); |
203 return MOJO_RESULT_OK; | 209 return MOJO_RESULT_OK; |
(...skipping 22 matching lines...) Expand all Loading... |
226 if (!*mapping) | 232 if (!*mapping) |
227 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 233 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
228 | 234 |
229 return MOJO_RESULT_OK; | 235 return MOJO_RESULT_OK; |
230 } | 236 } |
231 | 237 |
232 void SharedBufferDispatcher::StartSerializeImplNoLock( | 238 void SharedBufferDispatcher::StartSerializeImplNoLock( |
233 Channel* /*channel*/, | 239 Channel* /*channel*/, |
234 size_t* max_size, | 240 size_t* max_size, |
235 size_t* max_platform_handles) { | 241 size_t* max_platform_handles) { |
236 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. | 242 AssertHasOneRef(); // Only one ref => no need to take the lock. |
237 *max_size = sizeof(SerializedSharedBufferDispatcher); | 243 *max_size = sizeof(SerializedSharedBufferDispatcher); |
238 *max_platform_handles = 1; | 244 *max_platform_handles = 1; |
239 } | 245 } |
240 | 246 |
241 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( | 247 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( |
242 Channel* /*channel*/, | 248 Channel* /*channel*/, |
243 void* destination, | 249 void* destination, |
244 size_t* actual_size, | 250 size_t* actual_size, |
245 embedder::PlatformHandleVector* platform_handles) { | 251 embedder::PlatformHandleVector* platform_handles) { |
246 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. | 252 AssertHasOneRef(); // Only one ref => no need to take the lock. |
247 DCHECK(shared_buffer_); | 253 DCHECK(shared_buffer_); |
248 | 254 |
249 SerializedSharedBufferDispatcher* serialization = | 255 SerializedSharedBufferDispatcher* serialization = |
250 static_cast<SerializedSharedBufferDispatcher*>(destination); | 256 static_cast<SerializedSharedBufferDispatcher*>(destination); |
251 // If there's only one reference to |shared_buffer_|, then it's ours (and no | 257 // If there's only one reference to |shared_buffer_|, then it's ours (and no |
252 // one else can make any more references to it), so we can just take its | 258 // one else can make any more references to it), so we can just take its |
253 // handle. | 259 // handle. |
254 embedder::ScopedPlatformHandle platform_handle( | 260 embedder::ScopedPlatformHandle platform_handle( |
255 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() | 261 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() |
256 : shared_buffer_->DuplicatePlatformHandle()); | 262 : shared_buffer_->DuplicatePlatformHandle()); |
257 if (!platform_handle.is_valid()) { | 263 if (!platform_handle.is_valid()) { |
258 shared_buffer_ = nullptr; | 264 shared_buffer_ = nullptr; |
259 return false; | 265 return false; |
260 } | 266 } |
261 | 267 |
262 serialization->num_bytes = shared_buffer_->GetNumBytes(); | 268 serialization->num_bytes = shared_buffer_->GetNumBytes(); |
263 serialization->platform_handle_index = platform_handles->size(); | 269 serialization->platform_handle_index = platform_handles->size(); |
264 platform_handles->push_back(platform_handle.release()); | 270 platform_handles->push_back(platform_handle.release()); |
265 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 271 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
266 | 272 |
267 shared_buffer_ = nullptr; | 273 shared_buffer_ = nullptr; |
268 | 274 |
269 return true; | 275 return true; |
270 } | 276 } |
271 | 277 |
272 } // namespace system | 278 } // namespace system |
273 } // namespace mojo | 279 } // namespace mojo |
OLD | NEW |