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