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" |
11 #include "mojo/system/constants.h" | 11 #include "mojo/system/constants.h" |
12 #include "mojo/system/memory.h" | 12 #include "mojo/system/memory.h" |
13 #include "mojo/system/raw_shared_buffer.h" | 13 #include "mojo/system/raw_shared_buffer.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace system { | 16 namespace system { |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 struct SerializedSharedBufferDispatcher { |
| 21 size_t num_bytes; |
| 22 size_t platform_handle_index; |
| 23 }; |
| 24 |
| 25 } // namespace |
| 26 |
18 // static | 27 // static |
19 MojoResult SharedBufferDispatcher::ValidateOptions( | 28 MojoResult SharedBufferDispatcher::ValidateOptions( |
20 const MojoCreateSharedBufferOptions* in_options, | 29 const MojoCreateSharedBufferOptions* in_options, |
21 MojoCreateSharedBufferOptions* out_options) { | 30 MojoCreateSharedBufferOptions* out_options) { |
22 static const MojoCreateSharedBufferOptions kDefaultOptions = { | 31 static const MojoCreateSharedBufferOptions kDefaultOptions = { |
23 static_cast<uint32_t>(sizeof(MojoCreateSharedBufferOptions)), | 32 static_cast<uint32_t>(sizeof(MojoCreateSharedBufferOptions)), |
24 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE | 33 MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE |
25 }; | 34 }; |
26 if (!in_options) { | 35 if (!in_options) { |
27 *out_options = kDefaultOptions; | 36 *out_options = kDefaultOptions; |
(...skipping 26 matching lines...) Expand all Loading... |
54 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 63 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
55 | 64 |
56 *result = new SharedBufferDispatcher(shared_buffer); | 65 *result = new SharedBufferDispatcher(shared_buffer); |
57 return MOJO_RESULT_OK; | 66 return MOJO_RESULT_OK; |
58 } | 67 } |
59 | 68 |
60 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 69 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
61 return kTypeSharedBuffer; | 70 return kTypeSharedBuffer; |
62 } | 71 } |
63 | 72 |
| 73 // static |
| 74 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( |
| 75 Channel* channel, |
| 76 const void* source, |
| 77 size_t size, |
| 78 embedder::PlatformHandleVector* platform_handles) { |
| 79 if (size != sizeof(SerializedSharedBufferDispatcher)) { |
| 80 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; |
| 81 return scoped_refptr<SharedBufferDispatcher>(); |
| 82 } |
| 83 |
| 84 const SerializedSharedBufferDispatcher* serialization = |
| 85 static_cast<const SerializedSharedBufferDispatcher*>(source); |
| 86 size_t num_bytes = serialization->num_bytes; |
| 87 size_t platform_handle_index = serialization->platform_handle_index; |
| 88 |
| 89 if (!num_bytes) { |
| 90 LOG(ERROR) |
| 91 << "Invalid serialized shared buffer dispatcher (invalid num_bytes)"; |
| 92 return scoped_refptr<SharedBufferDispatcher>(); |
| 93 } |
| 94 |
| 95 if (!platform_handles || platform_handle_index >= platform_handles->size()) { |
| 96 LOG(ERROR) |
| 97 << "Invalid serialized shared buffer dispatcher (missing handles)"; |
| 98 return scoped_refptr<SharedBufferDispatcher>(); |
| 99 } |
| 100 |
| 101 // Starts off invalid, which is what we want. |
| 102 embedder::PlatformHandle platform_handle; |
| 103 // We take ownership of the handle, so we have to invalidate the one in |
| 104 // |platform_handles|. |
| 105 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
| 106 |
| 107 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
| 108 // closed even if creation fails. |
| 109 scoped_refptr<RawSharedBuffer> shared_buffer( |
| 110 RawSharedBuffer::CreateFromPlatformHandle(num_bytes, |
| 111 embedder::ScopedPlatformHandle(platform_handle))); |
| 112 if (!shared_buffer) { |
| 113 LOG(ERROR) |
| 114 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
| 115 return scoped_refptr<SharedBufferDispatcher>(); |
| 116 } |
| 117 |
| 118 return scoped_refptr<SharedBufferDispatcher>(new SharedBufferDispatcher( |
| 119 shared_buffer)); |
| 120 } |
| 121 |
64 SharedBufferDispatcher::SharedBufferDispatcher( | 122 SharedBufferDispatcher::SharedBufferDispatcher( |
65 scoped_refptr<RawSharedBuffer> shared_buffer) | 123 scoped_refptr<RawSharedBuffer> shared_buffer) |
66 : shared_buffer_(shared_buffer) { | 124 : shared_buffer_(shared_buffer) { |
67 DCHECK(shared_buffer_); | 125 DCHECK(shared_buffer_); |
68 } | 126 } |
69 | 127 |
70 SharedBufferDispatcher::~SharedBufferDispatcher() { | 128 SharedBufferDispatcher::~SharedBufferDispatcher() { |
71 } | 129 } |
72 | 130 |
73 void SharedBufferDispatcher::CloseImplNoLock() { | 131 void SharedBufferDispatcher::CloseImplNoLock() { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 183 |
126 DCHECK(mapping); | 184 DCHECK(mapping); |
127 *mapping = shared_buffer_->MapNoCheck(static_cast<size_t>(offset), | 185 *mapping = shared_buffer_->MapNoCheck(static_cast<size_t>(offset), |
128 static_cast<size_t>(num_bytes)); | 186 static_cast<size_t>(num_bytes)); |
129 if (!*mapping) | 187 if (!*mapping) |
130 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 188 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
131 | 189 |
132 return MOJO_RESULT_OK; | 190 return MOJO_RESULT_OK; |
133 } | 191 } |
134 | 192 |
| 193 void SharedBufferDispatcher::StartSerializeImplNoLock( |
| 194 Channel* /*channel*/, |
| 195 size_t* max_size, |
| 196 size_t* max_platform_handles) { |
| 197 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. |
| 198 *max_size = sizeof(SerializedSharedBufferDispatcher); |
| 199 *max_platform_handles = 1; |
| 200 } |
| 201 |
| 202 bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock( |
| 203 Channel* /*channel*/, |
| 204 void* destination, |
| 205 size_t* actual_size, |
| 206 embedder::PlatformHandleVector* platform_handles) { |
| 207 DCHECK(HasOneRef()); // Only one ref => no need to take the lock. |
| 208 DCHECK(shared_buffer_); |
| 209 |
| 210 SerializedSharedBufferDispatcher* serialization = |
| 211 static_cast<SerializedSharedBufferDispatcher*>(destination); |
| 212 // If there's only one reference to |shared_buffer_|, then it's ours (and no |
| 213 // one else can make any more references to it), so we can just take its |
| 214 // handle. |
| 215 embedder::ScopedPlatformHandle platform_handle( |
| 216 shared_buffer_->HasOneRef() ? |
| 217 shared_buffer_->PassPlatformHandle() : |
| 218 shared_buffer_->DuplicatePlatformHandle()); |
| 219 if (!platform_handle.is_valid()) { |
| 220 shared_buffer_ = NULL; |
| 221 return false; |
| 222 } |
| 223 |
| 224 serialization->num_bytes = shared_buffer_->num_bytes(); |
| 225 serialization->platform_handle_index = platform_handles->size(); |
| 226 platform_handles->push_back(platform_handle.release()); |
| 227 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
| 228 |
| 229 shared_buffer_ = NULL; |
| 230 |
| 231 return true; |
| 232 } |
| 233 |
135 MojoWaitFlags SharedBufferDispatcher::SatisfiedFlagsNoLock() const { | 234 MojoWaitFlags SharedBufferDispatcher::SatisfiedFlagsNoLock() const { |
136 // TODO(vtl): Add transferrable flag. | 235 // TODO(vtl): Add transferrable flag. |
137 return MOJO_WAIT_FLAG_NONE; | 236 return MOJO_WAIT_FLAG_NONE; |
138 } | 237 } |
139 | 238 |
140 MojoWaitFlags SharedBufferDispatcher::SatisfiableFlagsNoLock() const { | 239 MojoWaitFlags SharedBufferDispatcher::SatisfiableFlagsNoLock() const { |
141 // TODO(vtl): Add transferrable flag. | 240 // TODO(vtl): Add transferrable flag. |
142 return MOJO_WAIT_FLAG_NONE; | 241 return MOJO_WAIT_FLAG_NONE; |
143 } | 242 } |
144 | 243 |
145 } // namespace system | 244 } // namespace system |
146 } // namespace mojo | 245 } // namespace mojo |
OLD | NEW |