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 "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 const MojoCreateSharedBufferOptions& /*validated_options*/, | 67 const MojoCreateSharedBufferOptions& /*validated_options*/, |
68 uint64_t num_bytes, | 68 uint64_t num_bytes, |
69 scoped_refptr<SharedBufferDispatcher>* result) { | 69 scoped_refptr<SharedBufferDispatcher>* result) { |
70 if (!num_bytes) | 70 if (!num_bytes) |
71 return MOJO_RESULT_INVALID_ARGUMENT; | 71 return MOJO_RESULT_INVALID_ARGUMENT; |
72 if (num_bytes > GetConfiguration().max_shared_memory_num_bytes) | 72 if (num_bytes > GetConfiguration().max_shared_memory_num_bytes) |
73 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 73 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
74 | 74 |
75 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 75 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
76 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); | 76 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); |
77 if (!shared_buffer.get()) | 77 if (!shared_buffer) |
78 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 78 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
79 | 79 |
80 *result = new SharedBufferDispatcher(shared_buffer); | 80 *result = new SharedBufferDispatcher(shared_buffer); |
81 return MOJO_RESULT_OK; | 81 return MOJO_RESULT_OK; |
82 } | 82 } |
83 | 83 |
84 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 84 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
85 return kTypeSharedBuffer; | 85 return kTypeSharedBuffer; |
86 } | 86 } |
87 | 87 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 embedder::PlatformHandle platform_handle; | 119 embedder::PlatformHandle platform_handle; |
120 // We take ownership of the handle, so we have to invalidate the one in | 120 // We take ownership of the handle, so we have to invalidate the one in |
121 // |platform_handles|. | 121 // |platform_handles|. |
122 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 122 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
123 | 123 |
124 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be | 124 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
125 // closed even if creation fails. | 125 // closed even if creation fails. |
126 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 126 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
127 channel->platform_support()->CreateSharedBufferFromHandle( | 127 channel->platform_support()->CreateSharedBufferFromHandle( |
128 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); | 128 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); |
129 if (!shared_buffer.get()) { | 129 if (!shared_buffer) { |
130 LOG(ERROR) | 130 LOG(ERROR) |
131 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; | 131 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
132 return nullptr; | 132 return nullptr; |
133 } | 133 } |
134 | 134 |
135 return scoped_refptr<SharedBufferDispatcher>( | 135 return scoped_refptr<SharedBufferDispatcher>( |
136 new SharedBufferDispatcher(shared_buffer)); | 136 new SharedBufferDispatcher(shared_buffer)); |
137 } | 137 } |
138 | 138 |
139 SharedBufferDispatcher::SharedBufferDispatcher( | 139 SharedBufferDispatcher::SharedBufferDispatcher( |
140 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer) | 140 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer) |
141 : shared_buffer_(shared_buffer) { | 141 : shared_buffer_(shared_buffer) { |
142 DCHECK(shared_buffer_.get()); | 142 DCHECK(shared_buffer_); |
143 } | 143 } |
144 | 144 |
145 SharedBufferDispatcher::~SharedBufferDispatcher() { | 145 SharedBufferDispatcher::~SharedBufferDispatcher() { |
146 } | 146 } |
147 | 147 |
148 // static | 148 // static |
149 MojoResult SharedBufferDispatcher::ValidateDuplicateOptions( | 149 MojoResult SharedBufferDispatcher::ValidateDuplicateOptions( |
150 UserPointer<const MojoDuplicateBufferHandleOptions> in_options, | 150 UserPointer<const MojoDuplicateBufferHandleOptions> in_options, |
151 MojoDuplicateBufferHandleOptions* out_options) { | 151 MojoDuplicateBufferHandleOptions* out_options) { |
152 const MojoDuplicateBufferHandleOptionsFlags kKnownFlags = | 152 const MojoDuplicateBufferHandleOptionsFlags kKnownFlags = |
(...skipping 19 matching lines...) Expand all Loading... |
172 | 172 |
173 // Checks for fields beyond |flags|: | 173 // Checks for fields beyond |flags|: |
174 | 174 |
175 // (Nothing here yet.) | 175 // (Nothing here yet.) |
176 | 176 |
177 return MOJO_RESULT_OK; | 177 return MOJO_RESULT_OK; |
178 } | 178 } |
179 | 179 |
180 void SharedBufferDispatcher::CloseImplNoLock() { | 180 void SharedBufferDispatcher::CloseImplNoLock() { |
181 lock().AssertAcquired(); | 181 lock().AssertAcquired(); |
182 DCHECK(shared_buffer_.get()); | 182 DCHECK(shared_buffer_); |
183 shared_buffer_ = nullptr; | 183 shared_buffer_ = nullptr; |
184 } | 184 } |
185 | 185 |
186 scoped_refptr<Dispatcher> | 186 scoped_refptr<Dispatcher> |
187 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | 187 SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
188 lock().AssertAcquired(); | 188 lock().AssertAcquired(); |
189 DCHECK(shared_buffer_.get()); | 189 DCHECK(shared_buffer_); |
190 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer; | 190 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer; |
191 shared_buffer.swap(shared_buffer_); | 191 shared_buffer.swap(shared_buffer_); |
192 return scoped_refptr<Dispatcher>(new SharedBufferDispatcher(shared_buffer)); | 192 return scoped_refptr<Dispatcher>(new SharedBufferDispatcher(shared_buffer)); |
193 } | 193 } |
194 | 194 |
195 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( | 195 MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock( |
196 UserPointer<const MojoDuplicateBufferHandleOptions> options, | 196 UserPointer<const MojoDuplicateBufferHandleOptions> options, |
197 scoped_refptr<Dispatcher>* new_dispatcher) { | 197 scoped_refptr<Dispatcher>* new_dispatcher) { |
198 lock().AssertAcquired(); | 198 lock().AssertAcquired(); |
199 | 199 |
200 MojoDuplicateBufferHandleOptions validated_options; | 200 MojoDuplicateBufferHandleOptions validated_options; |
201 MojoResult result = ValidateDuplicateOptions(options, &validated_options); | 201 MojoResult result = ValidateDuplicateOptions(options, &validated_options); |
202 if (result != MOJO_RESULT_OK) | 202 if (result != MOJO_RESULT_OK) |
203 return result; | 203 return result; |
204 | 204 |
205 *new_dispatcher = new SharedBufferDispatcher(shared_buffer_); | 205 *new_dispatcher = new SharedBufferDispatcher(shared_buffer_); |
206 return MOJO_RESULT_OK; | 206 return MOJO_RESULT_OK; |
207 } | 207 } |
208 | 208 |
209 MojoResult SharedBufferDispatcher::MapBufferImplNoLock( | 209 MojoResult SharedBufferDispatcher::MapBufferImplNoLock( |
210 uint64_t offset, | 210 uint64_t offset, |
211 uint64_t num_bytes, | 211 uint64_t num_bytes, |
212 MojoMapBufferFlags flags, | 212 MojoMapBufferFlags flags, |
213 scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) { | 213 scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) { |
214 lock().AssertAcquired(); | 214 lock().AssertAcquired(); |
215 DCHECK(shared_buffer_.get()); | 215 DCHECK(shared_buffer_); |
216 | 216 |
217 if (offset > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) | 217 if (offset > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) |
218 return MOJO_RESULT_INVALID_ARGUMENT; | 218 return MOJO_RESULT_INVALID_ARGUMENT; |
219 if (num_bytes > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) | 219 if (num_bytes > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) |
220 return MOJO_RESULT_INVALID_ARGUMENT; | 220 return MOJO_RESULT_INVALID_ARGUMENT; |
221 | 221 |
222 if (!shared_buffer_->IsValidMap(static_cast<size_t>(offset), | 222 if (!shared_buffer_->IsValidMap(static_cast<size_t>(offset), |
223 static_cast<size_t>(num_bytes))) | 223 static_cast<size_t>(num_bytes))) |
224 return MOJO_RESULT_INVALID_ARGUMENT; | 224 return MOJO_RESULT_INVALID_ARGUMENT; |
225 | 225 |
(...skipping 14 matching lines...) Expand all Loading... |
240 *max_size = sizeof(SerializedSharedBufferDispatcher); | 240 *max_size = sizeof(SerializedSharedBufferDispatcher); |
241 *max_platform_handles = 1; | 241 *max_platform_handles = 1; |
242 } | 242 } |
243 | 243 |
244 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( | 244 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( |
245 Channel* /*channel*/, | 245 Channel* /*channel*/, |
246 void* destination, | 246 void* destination, |
247 size_t* actual_size, | 247 size_t* actual_size, |
248 embedder::PlatformHandleVector* platform_handles) { | 248 embedder::PlatformHandleVector* platform_handles) { |
249 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. | 249 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. |
250 DCHECK(shared_buffer_.get()); | 250 DCHECK(shared_buffer_); |
251 | 251 |
252 SerializedSharedBufferDispatcher* serialization = | 252 SerializedSharedBufferDispatcher* serialization = |
253 static_cast<SerializedSharedBufferDispatcher*>(destination); | 253 static_cast<SerializedSharedBufferDispatcher*>(destination); |
254 // If there's only one reference to |shared_buffer_|, then it's ours (and no | 254 // If there's only one reference to |shared_buffer_|, then it's ours (and no |
255 // one else can make any more references to it), so we can just take its | 255 // one else can make any more references to it), so we can just take its |
256 // handle. | 256 // handle. |
257 embedder::ScopedPlatformHandle platform_handle( | 257 embedder::ScopedPlatformHandle platform_handle( |
258 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() | 258 shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle() |
259 : shared_buffer_->DuplicatePlatformHandle()); | 259 : shared_buffer_->DuplicatePlatformHandle()); |
260 if (!platform_handle.is_valid()) { | 260 if (!platform_handle.is_valid()) { |
261 shared_buffer_ = nullptr; | 261 shared_buffer_ = nullptr; |
262 return false; | 262 return false; |
263 } | 263 } |
264 | 264 |
265 serialization->num_bytes = shared_buffer_->GetNumBytes(); | 265 serialization->num_bytes = shared_buffer_->GetNumBytes(); |
266 serialization->platform_handle_index = platform_handles->size(); | 266 serialization->platform_handle_index = platform_handles->size(); |
267 platform_handles->push_back(platform_handle.release()); | 267 platform_handles->push_back(platform_handle.release()); |
268 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 268 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
269 | 269 |
270 shared_buffer_ = nullptr; | 270 shared_buffer_ = nullptr; |
271 | 271 |
272 return true; | 272 return true; |
273 } | 273 } |
274 | 274 |
275 } // namespace system | 275 } // namespace system |
276 } // namespace mojo | 276 } // namespace mojo |
OLD | NEW |