| 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/embedder/platform_support.h" | 11 #include "mojo/embedder/platform_support.h" |
| 12 #include "mojo/embedder/simple_platform_shared_buffer.h" // TODO(vtl): Remove. | |
| 13 #include "mojo/public/c/system/macros.h" | 12 #include "mojo/public/c/system/macros.h" |
| 13 #include "mojo/system/channel.h" |
| 14 #include "mojo/system/constants.h" | 14 #include "mojo/system/constants.h" |
| 15 #include "mojo/system/memory.h" | 15 #include "mojo/system/memory.h" |
| 16 #include "mojo/system/options_validation.h" | 16 #include "mojo/system/options_validation.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace system { | 19 namespace system { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 struct SerializedSharedBufferDispatcher { | 23 struct SerializedSharedBufferDispatcher { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 84 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
| 85 return kTypeSharedBuffer; | 85 return kTypeSharedBuffer; |
| 86 } | 86 } |
| 87 | 87 |
| 88 // static | 88 // static |
| 89 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( | 89 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( |
| 90 Channel* channel, | 90 Channel* channel, |
| 91 const void* source, | 91 const void* source, |
| 92 size_t size, | 92 size_t size, |
| 93 embedder::PlatformHandleVector* platform_handles) { | 93 embedder::PlatformHandleVector* platform_handles) { |
| 94 DCHECK(channel); |
| 95 |
| 94 if (size != sizeof(SerializedSharedBufferDispatcher)) { | 96 if (size != sizeof(SerializedSharedBufferDispatcher)) { |
| 95 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; | 97 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; |
| 96 return scoped_refptr<SharedBufferDispatcher>(); | 98 return scoped_refptr<SharedBufferDispatcher>(); |
| 97 } | 99 } |
| 98 | 100 |
| 99 const SerializedSharedBufferDispatcher* serialization = | 101 const SerializedSharedBufferDispatcher* serialization = |
| 100 static_cast<const SerializedSharedBufferDispatcher*>(source); | 102 static_cast<const SerializedSharedBufferDispatcher*>(source); |
| 101 size_t num_bytes = serialization->num_bytes; | 103 size_t num_bytes = serialization->num_bytes; |
| 102 size_t platform_handle_index = serialization->platform_handle_index; | 104 size_t platform_handle_index = serialization->platform_handle_index; |
| 103 | 105 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 } | 116 } |
| 115 | 117 |
| 116 // Starts off invalid, which is what we want. | 118 // Starts off invalid, which is what we want. |
| 117 embedder::PlatformHandle platform_handle; | 119 embedder::PlatformHandle platform_handle; |
| 118 // 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 |
| 119 // |platform_handles|. | 121 // |platform_handles|. |
| 120 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 122 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
| 121 | 123 |
| 122 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be | 124 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
| 123 // closed even if creation fails. | 125 // closed even if creation fails. |
| 124 // TODO(vtl): This is obviously wrong -- but we need to have a | |
| 125 // |PlatformSupport| plumbed through (probably via the |Channel|), and use its | |
| 126 // |CreateSharedBufferFromHandle()|. | |
| 127 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 126 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
| 128 embedder::SimplePlatformSharedBuffer::CreateFromPlatformHandle( | 127 channel->platform_support()->CreateSharedBufferFromHandle( |
| 129 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); | 128 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); |
| 130 if (!shared_buffer) { | 129 if (!shared_buffer) { |
| 131 LOG(ERROR) | 130 LOG(ERROR) |
| 132 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; | 131 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
| 133 return scoped_refptr<SharedBufferDispatcher>(); | 132 return scoped_refptr<SharedBufferDispatcher>(); |
| 134 } | 133 } |
| 135 | 134 |
| 136 return scoped_refptr<SharedBufferDispatcher>( | 135 return scoped_refptr<SharedBufferDispatcher>( |
| 137 new SharedBufferDispatcher(shared_buffer)); | 136 new SharedBufferDispatcher(shared_buffer)); |
| 138 } | 137 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 platform_handles->push_back(platform_handle.release()); | 267 platform_handles->push_back(platform_handle.release()); |
| 269 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 268 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
| 270 | 269 |
| 271 shared_buffer_ = NULL; | 270 shared_buffer_ = NULL; |
| 272 | 271 |
| 273 return true; | 272 return true; |
| 274 } | 273 } |
| 275 | 274 |
| 276 } // namespace system | 275 } // namespace system |
| 277 } // namespace mojo | 276 } // namespace mojo |
| OLD | NEW |