| 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 <stddef.h> | 7 #include "base/synchronization/lock.h" |
| 8 | 8 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 9 #include <algorithm> | |
| 10 #include <limits> | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 | 9 |
| 15 namespace mojo { | 10 namespace mojo { |
| 16 namespace edk { | 11 namespace edk { |
| 17 | 12 |
| 18 namespace { | 13 // static |
| 19 | 14 scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Create( |
| 20 const uint32_t kInvalidPlatformHandleIndex = static_cast<uint32_t>(-1); | 15 ScopedPlatformHandle platform_handle) { |
| 21 | 16 return new PlatformHandleDispatcher(std::move(platform_handle)); |
| 22 struct MOJO_ALIGNAS(8) SerializedPlatformHandleDispatcher { | 17 } |
| 23 MOJO_ALIGNAS(4) uint32_t | |
| 24 platform_handle_index; // (Or |kInvalidPlatformHandleIndex|.) | |
| 25 }; | |
| 26 | |
| 27 } // namespace | |
| 28 | 18 |
| 29 ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() { | 19 ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() { |
| 30 base::AutoLock locker(lock()); | |
| 31 return std::move(platform_handle_); | 20 return std::move(platform_handle_); |
| 32 } | 21 } |
| 33 | 22 |
| 34 Dispatcher::Type PlatformHandleDispatcher::GetType() const { | 23 Dispatcher::Type PlatformHandleDispatcher::GetType() const { |
| 35 return Type::PLATFORM_HANDLE; | 24 return Type::PLATFORM_HANDLE; |
| 36 } | 25 } |
| 37 | 26 |
| 27 MojoResult PlatformHandleDispatcher::Close() { |
| 28 base::AutoLock lock(lock_); |
| 29 if (is_closed_ || in_transit_) |
| 30 return MOJO_RESULT_INVALID_ARGUMENT; |
| 31 is_closed_ = true; |
| 32 platform_handle_.reset(); |
| 33 return MOJO_RESULT_OK; |
| 34 } |
| 35 |
| 36 void PlatformHandleDispatcher::StartSerialize(uint32_t* num_bytes, |
| 37 uint32_t* num_ports, |
| 38 uint32_t* num_handles) { |
| 39 *num_bytes = 0; |
| 40 *num_ports = 0; |
| 41 *num_handles = 1; |
| 42 } |
| 43 |
| 44 bool PlatformHandleDispatcher::EndSerialize(void* destination, |
| 45 ports::PortName* ports, |
| 46 PlatformHandle* handles) { |
| 47 base::AutoLock lock(lock_); |
| 48 if (is_closed_) |
| 49 return false; |
| 50 handles[0] = platform_handle_.get(); |
| 51 return true; |
| 52 } |
| 53 |
| 54 bool PlatformHandleDispatcher::BeginTransit() { |
| 55 base::AutoLock lock(lock_); |
| 56 if (in_transit_) |
| 57 return false; |
| 58 in_transit_ = !is_closed_; |
| 59 return in_transit_; |
| 60 } |
| 61 |
| 62 void PlatformHandleDispatcher::CompleteTransitAndClose() { |
| 63 base::AutoLock lock(lock_); |
| 64 |
| 65 in_transit_ = false; |
| 66 is_closed_ = true; |
| 67 |
| 68 // The system has taken ownership of our handle. |
| 69 ignore_result(platform_handle_.release()); |
| 70 } |
| 71 |
| 72 void PlatformHandleDispatcher::CancelTransit() { |
| 73 base::AutoLock lock(lock_); |
| 74 in_transit_ = false; |
| 75 } |
| 76 |
| 38 // static | 77 // static |
| 39 scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize( | 78 scoped_refptr<PlatformHandleDispatcher> PlatformHandleDispatcher::Deserialize( |
| 40 const void* source, | 79 const void* bytes, |
| 41 size_t size, | 80 size_t num_bytes, |
| 42 PlatformHandleVector* platform_handles) { | 81 const ports::PortName* ports, |
| 43 if (size != sizeof(SerializedPlatformHandleDispatcher)) { | 82 size_t num_ports, |
| 44 LOG(ERROR) << "Invalid serialized platform handle dispatcher (bad size)"; | 83 PlatformHandle* handles, |
| 84 size_t num_handles) { |
| 85 if (num_bytes || num_ports || num_handles != 1) |
| 45 return nullptr; | 86 return nullptr; |
| 46 } | |
| 47 | 87 |
| 48 const SerializedPlatformHandleDispatcher* serialization = | 88 PlatformHandle handle; |
| 49 static_cast<const SerializedPlatformHandleDispatcher*>(source); | 89 std::swap(handle, handles[0]); |
| 50 size_t platform_handle_index = serialization->platform_handle_index; | |
| 51 | 90 |
| 52 // Starts off invalid, which is what we want. | 91 return PlatformHandleDispatcher::Create(ScopedPlatformHandle(handle)); |
| 53 PlatformHandle platform_handle; | |
| 54 | |
| 55 if (platform_handle_index != kInvalidPlatformHandleIndex) { | |
| 56 if (!platform_handles || | |
| 57 platform_handle_index >= platform_handles->size()) { | |
| 58 LOG(ERROR) | |
| 59 << "Invalid serialized platform handle dispatcher (missing handles)"; | |
| 60 return nullptr; | |
| 61 } | |
| 62 | |
| 63 // We take ownership of the handle, so we have to invalidate the one in | |
| 64 // |platform_handles|. | |
| 65 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | |
| 66 } | |
| 67 | |
| 68 return Create(ScopedPlatformHandle(platform_handle)); | |
| 69 } | 92 } |
| 70 | 93 |
| 71 PlatformHandleDispatcher::PlatformHandleDispatcher( | 94 PlatformHandleDispatcher::PlatformHandleDispatcher( |
| 72 ScopedPlatformHandle platform_handle) | 95 ScopedPlatformHandle platform_handle) |
| 73 : platform_handle_(std::move(platform_handle)) {} | 96 : platform_handle_(std::move(platform_handle)) {} |
| 74 | 97 |
| 75 PlatformHandleDispatcher::~PlatformHandleDispatcher() { | 98 PlatformHandleDispatcher::~PlatformHandleDispatcher() { |
| 76 } | 99 DCHECK(is_closed_ && !in_transit_); |
| 77 | 100 DCHECK(!platform_handle_.is_valid()); |
| 78 void PlatformHandleDispatcher::CloseImplNoLock() { | |
| 79 lock().AssertAcquired(); | |
| 80 platform_handle_.reset(); | |
| 81 } | |
| 82 | |
| 83 scoped_refptr<Dispatcher> | |
| 84 PlatformHandleDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() { | |
| 85 lock().AssertAcquired(); | |
| 86 return Create(std::move(platform_handle_)); | |
| 87 } | |
| 88 | |
| 89 void PlatformHandleDispatcher::StartSerializeImplNoLock( | |
| 90 size_t* max_size, | |
| 91 size_t* max_platform_handles) { | |
| 92 *max_size = sizeof(SerializedPlatformHandleDispatcher); | |
| 93 *max_platform_handles = 1; | |
| 94 } | |
| 95 | |
| 96 bool PlatformHandleDispatcher::EndSerializeAndCloseImplNoLock( | |
| 97 void* destination, | |
| 98 size_t* actual_size, | |
| 99 PlatformHandleVector* platform_handles) { | |
| 100 SerializedPlatformHandleDispatcher* serialization = | |
| 101 static_cast<SerializedPlatformHandleDispatcher*>(destination); | |
| 102 if (platform_handle_.is_valid()) { | |
| 103 DCHECK(platform_handles->size() < std::numeric_limits<uint32_t>::max()); | |
| 104 serialization->platform_handle_index = | |
| 105 static_cast<uint32_t>(platform_handles->size()); | |
| 106 platform_handles->push_back(platform_handle_.release()); | |
| 107 } else { | |
| 108 serialization->platform_handle_index = kInvalidPlatformHandleIndex; | |
| 109 } | |
| 110 | |
| 111 *actual_size = sizeof(SerializedPlatformHandleDispatcher); | |
| 112 return true; | |
| 113 } | 101 } |
| 114 | 102 |
| 115 } // namespace edk | 103 } // namespace edk |
| 116 } // namespace mojo | 104 } // namespace mojo |
| OLD | NEW |