| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/parent_token_serializer_state_win.h" | 5 #include "mojo/edk/system/broker_state.h" |
| 6 | 6 |
| 7 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
| 8 #include "mojo/edk/embedder/embedder_internal.h" | 8 #include "mojo/edk/embedder/embedder_internal.h" |
| 9 #include "mojo/edk/embedder/platform_channel_pair.h" | 9 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 namespace edk { | 12 namespace edk { |
| 13 | 13 |
| 14 ParentTokenSerializerState* ParentTokenSerializerState::GetInstance() { | 14 BrokerState* BrokerState::GetInstance() { |
| 15 return base::Singleton< | 15 return base::Singleton< |
| 16 ParentTokenSerializerState, | 16 BrokerState, base::LeakySingletonTraits<BrokerState>>::get(); |
| 17 base::LeakySingletonTraits<ParentTokenSerializerState>>::get(); | |
| 18 } | 17 } |
| 19 | 18 |
| 20 void ParentTokenSerializerState::CreatePlatformChannelPair( | 19 #if defined(OS_WIN) |
| 20 void BrokerState::CreatePlatformChannelPair( |
| 21 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { | 21 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { |
| 22 PlatformChannelPair channel_pair; | 22 PlatformChannelPair channel_pair; |
| 23 *server = channel_pair.PassServerHandle(); | 23 *server = channel_pair.PassServerHandle(); |
| 24 *client = channel_pair.PassClientHandle(); | 24 *client = channel_pair.PassClientHandle(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void ParentTokenSerializerState::HandleToToken( | 27 void BrokerState::HandleToToken( |
| 28 const PlatformHandle* platform_handles, | 28 const PlatformHandle* platform_handles, |
| 29 size_t count, | 29 size_t count, |
| 30 uint64_t* tokens) { | 30 uint64_t* tokens) { |
| 31 base::AutoLock auto_locker(lock_); | 31 base::AutoLock auto_locker(lock_); |
| 32 for (size_t i = 0; i < count; ++i) { | 32 for (size_t i = 0; i < count; ++i) { |
| 33 if (platform_handles[i].is_valid()) { | 33 if (platform_handles[i].is_valid()) { |
| 34 uint64_t token; | 34 uint64_t token; |
| 35 do { | 35 do { |
| 36 token = base::RandUint64(); | 36 token = base::RandUint64(); |
| 37 } while (!token || token_map_.find(token) != token_map_.end()); | 37 } while (!token || token_map_.find(token) != token_map_.end()); |
| 38 tokens[i] = token; | 38 tokens[i] = token; |
| 39 token_map_[tokens[i]] = platform_handles[i].handle; | 39 token_map_[tokens[i]] = platform_handles[i].handle; |
| 40 } else { | 40 } else { |
| 41 DLOG(WARNING) << "ParentTokenSerializerState got invalid handle."; | 41 DLOG(WARNING) << "BrokerState got invalid handle."; |
| 42 tokens[i] = 0; | 42 tokens[i] = 0; |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 void ParentTokenSerializerState::TokenToHandle( | 47 void BrokerState::TokenToHandle(const uint64_t* tokens, |
| 48 const uint64_t* tokens, | 48 size_t count, |
| 49 size_t count, | 49 PlatformHandle* handles) { |
| 50 PlatformHandle* handles) { | |
| 51 base::AutoLock auto_locker(lock_); | 50 base::AutoLock auto_locker(lock_); |
| 52 for (size_t i = 0; i < count; ++i) { | 51 for (size_t i = 0; i < count; ++i) { |
| 53 auto it = token_map_.find(tokens[i]); | 52 auto it = token_map_.find(tokens[i]); |
| 54 if (it == token_map_.end()) { | 53 if (it == token_map_.end()) { |
| 55 DLOG(WARNING) << "TokenToHandle didn't find token."; | 54 DLOG(WARNING) << "TokenToHandle didn't find token."; |
| 56 } else { | 55 } else { |
| 57 handles[i].handle = it->second; | 56 handles[i].handle = it->second; |
| 58 token_map_.erase(it); | 57 token_map_.erase(it); |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 } | 60 } |
| 61 #endif |
| 62 | 62 |
| 63 ParentTokenSerializerState::ParentTokenSerializerState() | 63 BrokerState::BrokerState() : broker_thread_("Mojo Broker Thread") { |
| 64 : token_serialize_thread_("Token Serializer Watcher") { | |
| 65 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 64 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
| 66 token_serialize_thread_.StartWithOptions(options); | 65 broker_thread_.StartWithOptions(options); |
| 67 DCHECK(!internal::g_token_serializer); | 66 DCHECK(!internal::g_broker); |
| 68 internal::g_token_serializer = this; | 67 internal::g_broker = this; |
| 69 } | 68 } |
| 70 | 69 |
| 71 ParentTokenSerializerState::~ParentTokenSerializerState() { | 70 BrokerState::~BrokerState() { |
| 72 } | 71 } |
| 73 | 72 |
| 74 } // namespace edk | 73 } // namespace edk |
| 75 } // namespace mojo | 74 } // namespace mojo |
| OLD | NEW |