| 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/system/shared_buffer_dispatcher.h" | 5 #include "mojo/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 "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // static | 63 // static |
| 64 MojoResult SharedBufferDispatcher::Create( | 64 MojoResult SharedBufferDispatcher::Create( |
| 65 const MojoCreateSharedBufferOptions& /*validated_options*/, | 65 const MojoCreateSharedBufferOptions& /*validated_options*/, |
| 66 uint64_t num_bytes, | 66 uint64_t num_bytes, |
| 67 scoped_refptr<SharedBufferDispatcher>* result) { | 67 scoped_refptr<SharedBufferDispatcher>* result) { |
| 68 if (!num_bytes) | 68 if (!num_bytes) |
| 69 return MOJO_RESULT_INVALID_ARGUMENT; | 69 return MOJO_RESULT_INVALID_ARGUMENT; |
| 70 if (num_bytes > kMaxSharedMemoryNumBytes) | 70 if (num_bytes > kMaxSharedMemoryNumBytes) |
| 71 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 71 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 72 | 72 |
| 73 scoped_refptr<RawSharedBuffer> shared_buffer( | 73 // TODO(vtl): Call out to "platform support" for this. |
| 74 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
| 74 RawSharedBuffer::Create(static_cast<size_t>(num_bytes))); | 75 RawSharedBuffer::Create(static_cast<size_t>(num_bytes))); |
| 75 if (!shared_buffer) | 76 if (!shared_buffer) |
| 76 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 77 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 77 | 78 |
| 78 *result = new SharedBufferDispatcher(shared_buffer); | 79 *result = new SharedBufferDispatcher(shared_buffer); |
| 79 return MOJO_RESULT_OK; | 80 return MOJO_RESULT_OK; |
| 80 } | 81 } |
| 81 | 82 |
| 82 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 83 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
| 83 return kTypeSharedBuffer; | 84 return kTypeSharedBuffer; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 112 } | 113 } |
| 113 | 114 |
| 114 // Starts off invalid, which is what we want. | 115 // Starts off invalid, which is what we want. |
| 115 embedder::PlatformHandle platform_handle; | 116 embedder::PlatformHandle platform_handle; |
| 116 // We take ownership of the handle, so we have to invalidate the one in | 117 // We take ownership of the handle, so we have to invalidate the one in |
| 117 // |platform_handles|. | 118 // |platform_handles|. |
| 118 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 119 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
| 119 | 120 |
| 120 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be | 121 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
| 121 // closed even if creation fails. | 122 // closed even if creation fails. |
| 122 scoped_refptr<RawSharedBuffer> shared_buffer( | 123 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
| 123 RawSharedBuffer::CreateFromPlatformHandle( | 124 RawSharedBuffer::CreateFromPlatformHandle( |
| 124 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); | 125 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); |
| 125 if (!shared_buffer) { | 126 if (!shared_buffer) { |
| 126 LOG(ERROR) | 127 LOG(ERROR) |
| 127 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; | 128 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
| 128 return scoped_refptr<SharedBufferDispatcher>(); | 129 return scoped_refptr<SharedBufferDispatcher>(); |
| 129 } | 130 } |
| 130 | 131 |
| 131 return scoped_refptr<SharedBufferDispatcher>( | 132 return scoped_refptr<SharedBufferDispatcher>( |
| 132 new SharedBufferDispatcher(shared_buffer)); | 133 new SharedBufferDispatcher(shared_buffer)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 SharedBufferDispatcher::SharedBufferDispatcher( | 136 SharedBufferDispatcher::SharedBufferDispatcher( |
| 136 scoped_refptr<RawSharedBuffer> shared_buffer) | 137 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer) |
| 137 : shared_buffer_(shared_buffer) { | 138 : shared_buffer_(shared_buffer) { |
| 138 DCHECK(shared_buffer_); | 139 DCHECK(shared_buffer_); |
| 139 } | 140 } |
| 140 | 141 |
| 141 SharedBufferDispatcher::~SharedBufferDispatcher() { | 142 SharedBufferDispatcher::~SharedBufferDispatcher() { |
| 142 } | 143 } |
| 143 | 144 |
| 144 // static | 145 // static |
| 145 MojoResult SharedBufferDispatcher::ValidateDuplicateOptions( | 146 MojoResult SharedBufferDispatcher::ValidateDuplicateOptions( |
| 146 UserPointer<const MojoDuplicateBufferHandleOptions> in_options, | 147 UserPointer<const MojoDuplicateBufferHandleOptions> in_options, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 176 void SharedBufferDispatcher::CloseImplNoLock() { | 177 void SharedBufferDispatcher::CloseImplNoLock() { |
| 177 lock().AssertAcquired(); | 178 lock().AssertAcquired(); |
| 178 DCHECK(shared_buffer_); | 179 DCHECK(shared_buffer_); |
| 179 shared_buffer_ = NULL; | 180 shared_buffer_ = NULL; |
| 180 } | 181 } |
| 181 | 182 |
| 182 scoped_refptr<Dispatcher> | 183 scoped_refptr<Dispatcher> |
| 183 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | 184 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
| 184 lock().AssertAcquired(); | 185 lock().AssertAcquired(); |
| 185 DCHECK(shared_buffer_); | 186 DCHECK(shared_buffer_); |
| 186 scoped_refptr<RawSharedBuffer> shared_buffer; | 187 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer; |
| 187 shared_buffer.swap(shared_buffer_); | 188 shared_buffer.swap(shared_buffer_); |
| 188 return scoped_refptr<Dispatcher>(new SharedBufferDispatcher(shared_buffer)); | 189 return scoped_refptr<Dispatcher>(new SharedBufferDispatcher(shared_buffer)); |
| 189 } | 190 } |
| 190 | 191 |
| 191 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( | 192 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( |
| 192 UserPointer<const MojoDuplicateBufferHandleOptions> options, | 193 UserPointer<const MojoDuplicateBufferHandleOptions> options, |
| 193 scoped_refptr<Dispatcher>* new_dispatcher) { | 194 scoped_refptr<Dispatcher>* new_dispatcher) { |
| 194 lock().AssertAcquired(); | 195 lock().AssertAcquired(); |
| 195 | 196 |
| 196 MojoDuplicateBufferHandleOptions validated_options; | 197 MojoDuplicateBufferHandleOptions validated_options; |
| 197 MojoResult result = ValidateDuplicateOptions(options, &validated_options); | 198 MojoResult result = ValidateDuplicateOptions(options, &validated_options); |
| 198 if (result != MOJO_RESULT_OK) | 199 if (result != MOJO_RESULT_OK) |
| 199 return result; | 200 return result; |
| 200 | 201 |
| 201 *new_dispatcher = new SharedBufferDispatcher(shared_buffer_); | 202 *new_dispatcher = new SharedBufferDispatcher(shared_buffer_); |
| 202 return MOJO_RESULT_OK; | 203 return MOJO_RESULT_OK; |
| 203 } | 204 } |
| 204 | 205 |
| 205 MojoResult SharedBufferDispatcher::MapBufferImplNoLock( | 206 MojoResult SharedBufferDispatcher::MapBufferImplNoLock( |
| 206 uint64_t offset, | 207 uint64_t offset, |
| 207 uint64_t num_bytes, | 208 uint64_t num_bytes, |
| 208 MojoMapBufferFlags flags, | 209 MojoMapBufferFlags flags, |
| 209 scoped_ptr<RawSharedBufferMapping>* mapping) { | 210 scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) { |
| 210 lock().AssertAcquired(); | 211 lock().AssertAcquired(); |
| 211 DCHECK(shared_buffer_); | 212 DCHECK(shared_buffer_); |
| 212 | 213 |
| 213 if (offset > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) | 214 if (offset > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) |
| 214 return MOJO_RESULT_INVALID_ARGUMENT; | 215 return MOJO_RESULT_INVALID_ARGUMENT; |
| 215 if (num_bytes > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) | 216 if (num_bytes > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) |
| 216 return MOJO_RESULT_INVALID_ARGUMENT; | 217 return MOJO_RESULT_INVALID_ARGUMENT; |
| 217 | 218 |
| 218 if (!shared_buffer_->IsValidMap(static_cast<size_t>(offset), | 219 if (!shared_buffer_->IsValidMap(static_cast<size_t>(offset), |
| 219 static_cast<size_t>(num_bytes))) | 220 static_cast<size_t>(num_bytes))) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // one else can make any more references to it), so we can just take its | 252 // one else can make any more references to it), so we can just take its |
| 252 // handle. | 253 // handle. |
| 253 embedder::ScopedPlatformHandle platform_handle( | 254 embedder::ScopedPlatformHandle platform_handle( |
| 254 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() | 255 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() |
| 255 : shared_buffer_->DuplicatePlatformHandle()); | 256 : shared_buffer_->DuplicatePlatformHandle()); |
| 256 if (!platform_handle.is_valid()) { | 257 if (!platform_handle.is_valid()) { |
| 257 shared_buffer_ = NULL; | 258 shared_buffer_ = NULL; |
| 258 return false; | 259 return false; |
| 259 } | 260 } |
| 260 | 261 |
| 261 serialization->num_bytes = shared_buffer_->num_bytes(); | 262 serialization->num_bytes = shared_buffer_->GetNumBytes(); |
| 262 serialization->platform_handle_index = platform_handles->size(); | 263 serialization->platform_handle_index = platform_handles->size(); |
| 263 platform_handles->push_back(platform_handle.release()); | 264 platform_handles->push_back(platform_handle.release()); |
| 264 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 265 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
| 265 | 266 |
| 266 shared_buffer_ = NULL; | 267 shared_buffer_ = NULL; |
| 267 | 268 |
| 268 return true; | 269 return true; |
| 269 } | 270 } |
| 270 | 271 |
| 271 } // namespace system | 272 } // namespace system |
| 272 } // namespace mojo | 273 } // namespace mojo |
| OLD | NEW |