| 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/platform_handle_dispatcher.h" | 5 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 | 11 |
| 12 using mojo::embedder::ScopedPlatformHandle; |
| 11 using mojo::util::MutexLocker; | 13 using mojo::util::MutexLocker; |
| 12 using mojo::util::RefPtr; | 14 using mojo::util::RefPtr; |
| 13 | 15 |
| 14 namespace mojo { | 16 namespace mojo { |
| 15 namespace system { | 17 namespace system { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 const size_t kInvalidPlatformHandleIndex = static_cast<size_t>(-1); | 21 const size_t kInvalidPlatformHandleIndex = static_cast<size_t>(-1); |
| 20 | 22 |
| 21 struct SerializedPlatformHandleDispatcher { | 23 struct SerializedPlatformHandleDispatcher { |
| 22 size_t platform_handle_index; // (Or |kInvalidPlatformHandleIndex|.) | 24 size_t platform_handle_index; // (Or |kInvalidPlatformHandleIndex|.) |
| 23 }; | 25 }; |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 embedder::ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() { | 29 ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() { |
| 28 MutexLocker locker(&mutex()); | 30 MutexLocker locker(&mutex()); |
| 29 return platform_handle_.Pass(); | 31 return platform_handle_.Pass(); |
| 30 } | 32 } |
| 31 | 33 |
| 32 Dispatcher::Type PlatformHandleDispatcher::GetType() const { | 34 Dispatcher::Type PlatformHandleDispatcher::GetType() const { |
| 33 return Type::PLATFORM_HANDLE; | 35 return Type::PLATFORM_HANDLE; |
| 34 } | 36 } |
| 35 | 37 |
| 36 // static | 38 // static |
| 37 RefPtr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize( | 39 RefPtr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize( |
| 38 Channel* channel, | 40 Channel* channel, |
| 39 const void* source, | 41 const void* source, |
| 40 size_t size, | 42 size_t size, |
| 41 embedder::PlatformHandleVector* platform_handles) { | 43 std::vector<ScopedPlatformHandle>* platform_handles) { |
| 42 if (size != sizeof(SerializedPlatformHandleDispatcher)) { | 44 if (size != sizeof(SerializedPlatformHandleDispatcher)) { |
| 43 LOG(ERROR) << "Invalid serialized platform handle dispatcher (bad size)"; | 45 LOG(ERROR) << "Invalid serialized platform handle dispatcher (bad size)"; |
| 44 return nullptr; | 46 return nullptr; |
| 45 } | 47 } |
| 46 | 48 |
| 47 const SerializedPlatformHandleDispatcher* serialization = | 49 const SerializedPlatformHandleDispatcher* serialization = |
| 48 static_cast<const SerializedPlatformHandleDispatcher*>(source); | 50 static_cast<const SerializedPlatformHandleDispatcher*>(source); |
| 49 size_t platform_handle_index = serialization->platform_handle_index; | 51 size_t platform_handle_index = serialization->platform_handle_index; |
| 50 | 52 |
| 51 // Starts off invalid, which is what we want. | 53 // Starts off invalid, which is what we want. |
| 52 embedder::PlatformHandle platform_handle; | 54 ScopedPlatformHandle platform_handle; |
| 53 | 55 |
| 54 if (platform_handle_index != kInvalidPlatformHandleIndex) { | 56 if (platform_handle_index != kInvalidPlatformHandleIndex) { |
| 55 if (!platform_handles || | 57 if (!platform_handles || |
| 56 platform_handle_index >= platform_handles->size()) { | 58 platform_handle_index >= platform_handles->size()) { |
| 57 LOG(ERROR) | 59 LOG(ERROR) |
| 58 << "Invalid serialized platform handle dispatcher (missing handles)"; | 60 << "Invalid serialized platform handle dispatcher (missing handles)"; |
| 59 return nullptr; | 61 return nullptr; |
| 60 } | 62 } |
| 61 | 63 |
| 62 // We take ownership of the handle, so we have to invalidate the one in | 64 // We take ownership of the handle, so we have to invalidate the one in |
| 63 // |platform_handles|. | 65 // |platform_handles|. |
| 64 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 66 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
| 65 } | 67 } |
| 66 | 68 |
| 67 return Create(embedder::ScopedPlatformHandle(platform_handle)); | 69 return Create(std::move(platform_handle)); |
| 68 } | 70 } |
| 69 | 71 |
| 70 PlatformHandleDispatcher::PlatformHandleDispatcher( | 72 PlatformHandleDispatcher::PlatformHandleDispatcher( |
| 71 embedder::ScopedPlatformHandle platform_handle) | 73 ScopedPlatformHandle platform_handle) |
| 72 : platform_handle_(platform_handle.Pass()) { | 74 : platform_handle_(platform_handle.Pass()) {} |
| 73 } | |
| 74 | 75 |
| 75 PlatformHandleDispatcher::~PlatformHandleDispatcher() { | 76 PlatformHandleDispatcher::~PlatformHandleDispatcher() { |
| 76 } | 77 } |
| 77 | 78 |
| 78 void PlatformHandleDispatcher::CloseImplNoLock() { | 79 void PlatformHandleDispatcher::CloseImplNoLock() { |
| 79 mutex().AssertHeld(); | 80 mutex().AssertHeld(); |
| 80 platform_handle_.reset(); | 81 platform_handle_.reset(); |
| 81 } | 82 } |
| 82 | 83 |
| 83 RefPtr<Dispatcher> | 84 RefPtr<Dispatcher> |
| 84 PlatformHandleDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | 85 PlatformHandleDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { |
| 85 mutex().AssertHeld(); | 86 mutex().AssertHeld(); |
| 86 return Create(platform_handle_.Pass()); | 87 return Create(platform_handle_.Pass()); |
| 87 } | 88 } |
| 88 | 89 |
| 89 void PlatformHandleDispatcher::StartSerializeImplNoLock( | 90 void PlatformHandleDispatcher::StartSerializeImplNoLock( |
| 90 Channel* /*channel*/, | 91 Channel* /*channel*/, |
| 91 size_t* max_size, | 92 size_t* max_size, |
| 92 size_t* max_platform_handles) { | 93 size_t* max_platform_handles) { |
| 93 AssertHasOneRef(); // Only one ref => no need to take the lock. | 94 AssertHasOneRef(); // Only one ref => no need to take the lock. |
| 94 *max_size = sizeof(SerializedPlatformHandleDispatcher); | 95 *max_size = sizeof(SerializedPlatformHandleDispatcher); |
| 95 *max_platform_handles = 1; | 96 *max_platform_handles = 1; |
| 96 } | 97 } |
| 97 | 98 |
| 98 bool PlatformHandleDispatcher::EndSerializeAndCloseImplNoLock( | 99 bool PlatformHandleDispatcher::EndSerializeAndCloseImplNoLock( |
| 99 Channel* /*channel*/, | 100 Channel* /*channel*/, |
| 100 void* destination, | 101 void* destination, |
| 101 size_t* actual_size, | 102 size_t* actual_size, |
| 102 embedder::PlatformHandleVector* platform_handles) { | 103 std::vector<ScopedPlatformHandle>* platform_handles) { |
| 103 AssertHasOneRef(); // Only one ref => no need to take the lock. | 104 AssertHasOneRef(); // Only one ref => no need to take the lock. |
| 104 | 105 |
| 105 SerializedPlatformHandleDispatcher* serialization = | 106 SerializedPlatformHandleDispatcher* serialization = |
| 106 static_cast<SerializedPlatformHandleDispatcher*>(destination); | 107 static_cast<SerializedPlatformHandleDispatcher*>(destination); |
| 107 if (platform_handle_.is_valid()) { | 108 if (platform_handle_.is_valid()) { |
| 108 serialization->platform_handle_index = platform_handles->size(); | 109 serialization->platform_handle_index = platform_handles->size(); |
| 109 platform_handles->push_back(platform_handle_.release()); | 110 platform_handles->push_back(std::move(platform_handle_)); |
| 110 } else { | 111 } else { |
| 111 serialization->platform_handle_index = kInvalidPlatformHandleIndex; | 112 serialization->platform_handle_index = kInvalidPlatformHandleIndex; |
| 112 } | 113 } |
| 113 | 114 |
| 114 *actual_size = sizeof(SerializedPlatformHandleDispatcher); | 115 *actual_size = sizeof(SerializedPlatformHandleDispatcher); |
| 115 return true; | 116 return true; |
| 116 } | 117 } |
| 117 | 118 |
| 118 } // namespace system | 119 } // namespace system |
| 119 } // namespace mojo | 120 } // namespace mojo |
| OLD | NEW |