| 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/channel_manager.h" | 5 #include "mojo/edk/system/channel_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "mojo/edk/platform/platform_handle.h" | 9 #include "mojo/edk/platform/platform_handle.h" |
| 10 #include "mojo/edk/system/channel.h" | 10 #include "mojo/edk/system/channel.h" |
| 11 #include "mojo/edk/system/channel_endpoint.h" | 11 #include "mojo/edk/system/channel_endpoint.h" |
| 12 #include "mojo/edk/system/message_pipe_dispatcher.h" | 12 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 13 | 13 |
| 14 using mojo::platform::PlatformHandle; | 14 using mojo::platform::PlatformHandle; |
| 15 using mojo::platform::PlatformHandleWatcher; |
| 15 using mojo::platform::ScopedPlatformHandle; | 16 using mojo::platform::ScopedPlatformHandle; |
| 16 using mojo::platform::TaskRunner; | 17 using mojo::platform::TaskRunner; |
| 17 using mojo::util::MakeRefCounted; | 18 using mojo::util::MakeRefCounted; |
| 18 using mojo::util::MutexLocker; | 19 using mojo::util::MutexLocker; |
| 19 using mojo::util::RefPtr; | 20 using mojo::util::RefPtr; |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 namespace system { | 23 namespace system { |
| 23 | 24 |
| 24 ChannelManager::ChannelManager(embedder::PlatformSupport* platform_support, | 25 ChannelManager::ChannelManager(embedder::PlatformSupport* platform_support, |
| 25 RefPtr<TaskRunner>&& io_thread_task_runner, | 26 RefPtr<TaskRunner>&& io_task_runner, |
| 27 PlatformHandleWatcher* io_watcher, |
| 26 ConnectionManager* connection_manager) | 28 ConnectionManager* connection_manager) |
| 27 : platform_support_(platform_support), | 29 : platform_support_(platform_support), |
| 28 io_thread_task_runner_(std::move(io_thread_task_runner)), | 30 io_task_runner_(std::move(io_task_runner)), |
| 31 io_watcher_(io_watcher), |
| 29 connection_manager_(connection_manager) { | 32 connection_manager_(connection_manager) { |
| 30 DCHECK(platform_support_); | 33 DCHECK(platform_support_); |
| 31 DCHECK(io_thread_task_runner_); | 34 DCHECK(io_task_runner_); |
| 35 DCHECK(io_watcher_); |
| 32 // (|connection_manager_| may be null.) | 36 // (|connection_manager_| may be null.) |
| 33 } | 37 } |
| 34 | 38 |
| 35 ChannelManager::~ChannelManager() { | 39 ChannelManager::~ChannelManager() { |
| 36 // |Shutdown()| must be called before destruction and have been completed. | 40 // |Shutdown()| must be called before destruction and have been completed. |
| 37 // TODO(vtl): This doesn't verify the above condition very strictly at all | 41 // TODO(vtl): This doesn't verify the above condition very strictly at all |
| 38 // (e.g., we may never have had any channels, or we may have manually shut all | 42 // (e.g., we may never have had any channels, or we may have manually shut all |
| 39 // the channels down). | 43 // the channels down). |
| 40 DCHECK(channels_.empty()); | 44 DCHECK(channels_.empty()); |
| 41 } | 45 } |
| 42 | 46 |
| 43 void ChannelManager::ShutdownOnIOThread() { | 47 void ChannelManager::ShutdownOnIOThread() { |
| 44 // Taking this lock really shouldn't be necessary, but we do it for | 48 // Taking this lock really shouldn't be necessary, but we do it for |
| 45 // consistency. | 49 // consistency. |
| 46 ChannelIdToChannelMap channels; | 50 ChannelIdToChannelMap channels; |
| 47 { | 51 { |
| 48 MutexLocker locker(&mutex_); | 52 MutexLocker locker(&mutex_); |
| 49 channels.swap(channels_); | 53 channels.swap(channels_); |
| 50 } | 54 } |
| 51 | 55 |
| 52 for (auto& channel : channels) | 56 for (auto& channel : channels) |
| 53 channel.second->Shutdown(); | 57 channel.second->Shutdown(); |
| 54 } | 58 } |
| 55 | 59 |
| 56 void ChannelManager::Shutdown( | 60 void ChannelManager::Shutdown( |
| 57 std::function<void()>&& callback, | 61 std::function<void()>&& callback, |
| 58 RefPtr<TaskRunner>&& callback_thread_task_runner) { | 62 RefPtr<TaskRunner>&& callback_thread_task_runner) { |
| 59 // TODO(vtl): With C++14 lambda captures, we'll be able to move |callback| and | 63 // TODO(vtl): With C++14 lambda captures, we'll be able to move |callback| and |
| 60 // |callback_thread_task_runner| instead of copying them. | 64 // |callback_thread_task_runner| instead of copying them. |
| 61 io_thread_task_runner_->PostTask( | 65 io_task_runner_->PostTask( |
| 62 [this, callback, callback_thread_task_runner]() mutable { | 66 [this, callback, callback_thread_task_runner]() mutable { |
| 63 ShutdownOnIOThread(); | 67 ShutdownOnIOThread(); |
| 64 if (callback_thread_task_runner) | 68 if (callback_thread_task_runner) |
| 65 callback_thread_task_runner->PostTask(std::move(callback)); | 69 callback_thread_task_runner->PostTask(std::move(callback)); |
| 66 else | 70 else |
| 67 callback(); | 71 callback(); |
| 68 }); | 72 }); |
| 69 } | 73 } |
| 70 | 74 |
| 71 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannelOnIOThread( | 75 RefPtr<MessagePipeDispatcher> ChannelManager::CreateChannelOnIOThread( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 93 RefPtr<TaskRunner>&& callback_thread_task_runner) { | 97 RefPtr<TaskRunner>&& callback_thread_task_runner) { |
| 94 DCHECK(callback); | 98 DCHECK(callback); |
| 95 // (|callback_thread_task_runner| may be null.) | 99 // (|callback_thread_task_runner| may be null.) |
| 96 | 100 |
| 97 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; | 101 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint; |
| 98 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( | 102 auto dispatcher = MessagePipeDispatcher::CreateRemoteMessagePipe( |
| 99 &bootstrap_channel_endpoint); | 103 &bootstrap_channel_endpoint); |
| 100 // TODO(vtl): We have to copy or "unscope" various things due to C++11 lambda | 104 // TODO(vtl): We have to copy or "unscope" various things due to C++11 lambda |
| 101 // capture limitations. | 105 // capture limitations. |
| 102 PlatformHandle raw_platform_handle = platform_handle.release(); | 106 PlatformHandle raw_platform_handle = platform_handle.release(); |
| 103 io_thread_task_runner_->PostTask([this, channel_id, raw_platform_handle, | 107 io_task_runner_->PostTask([this, channel_id, raw_platform_handle, |
| 104 bootstrap_channel_endpoint, callback, | 108 bootstrap_channel_endpoint, callback, |
| 105 callback_thread_task_runner]() mutable { | 109 callback_thread_task_runner]() mutable { |
| 106 CreateChannelOnIOThreadHelper(channel_id, | 110 CreateChannelOnIOThreadHelper(channel_id, |
| 107 ScopedPlatformHandle(raw_platform_handle), | 111 ScopedPlatformHandle(raw_platform_handle), |
| 108 std::move(bootstrap_channel_endpoint)); | 112 std::move(bootstrap_channel_endpoint)); |
| 109 if (callback_thread_task_runner) | 113 if (callback_thread_task_runner) |
| 110 callback_thread_task_runner->PostTask(std::move(callback)); | 114 callback_thread_task_runner->PostTask(std::move(callback)); |
| 111 else | 115 else |
| 112 callback(); | 116 callback(); |
| 113 }); | 117 }); |
| 114 return dispatcher; | 118 return dispatcher; |
| 115 } | 119 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 145 { | 149 { |
| 146 MutexLocker locker(&mutex_); | 150 MutexLocker locker(&mutex_); |
| 147 auto it = channels_.find(channel_id); | 151 auto it = channels_.find(channel_id); |
| 148 DCHECK(it != channels_.end()); | 152 DCHECK(it != channels_.end()); |
| 149 channel.swap(it->second); | 153 channel.swap(it->second); |
| 150 channels_.erase(it); | 154 channels_.erase(it); |
| 151 } | 155 } |
| 152 channel->WillShutdownSoon(); | 156 channel->WillShutdownSoon(); |
| 153 // TODO(vtl): With C++14 lambda captures, we'll be able to move stuff instead | 157 // TODO(vtl): With C++14 lambda captures, we'll be able to move stuff instead |
| 154 // of copying. | 158 // of copying. |
| 155 io_thread_task_runner_->PostTask( | 159 io_task_runner_->PostTask( |
| 156 [channel, callback, callback_thread_task_runner]() mutable { | 160 [channel, callback, callback_thread_task_runner]() mutable { |
| 157 channel->Shutdown(); | 161 channel->Shutdown(); |
| 158 if (callback_thread_task_runner) | 162 if (callback_thread_task_runner) |
| 159 callback_thread_task_runner->PostTask(std::move(callback)); | 163 callback_thread_task_runner->PostTask(std::move(callback)); |
| 160 else | 164 else |
| 161 callback(); | 165 callback(); |
| 162 }); | 166 }); |
| 163 } | 167 } |
| 164 | 168 |
| 165 RefPtr<Channel> ChannelManager::CreateChannelOnIOThreadHelper( | 169 RefPtr<Channel> ChannelManager::CreateChannelOnIOThreadHelper( |
| 166 ChannelId channel_id, | 170 ChannelId channel_id, |
| 167 ScopedPlatformHandle platform_handle, | 171 ScopedPlatformHandle platform_handle, |
| 168 RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint) { | 172 RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint) { |
| 169 DCHECK_NE(channel_id, kInvalidChannelId); | 173 DCHECK_NE(channel_id, kInvalidChannelId); |
| 170 DCHECK(platform_handle.is_valid()); | 174 DCHECK(platform_handle.is_valid()); |
| 171 | 175 |
| 172 // Create and initialize a |Channel|. | 176 // Create and initialize a |Channel|. |
| 173 auto channel = MakeRefCounted<Channel>(platform_support_); | 177 auto channel = MakeRefCounted<Channel>(platform_support_); |
| 174 channel->Init(RawChannel::Create(platform_handle.Pass())); | 178 channel->Init(io_task_runner_.Clone(), io_watcher_, |
| 179 RawChannel::Create(platform_handle.Pass())); |
| 175 if (bootstrap_channel_endpoint) | 180 if (bootstrap_channel_endpoint) |
| 176 channel->SetBootstrapEndpoint(std::move(bootstrap_channel_endpoint)); | 181 channel->SetBootstrapEndpoint(std::move(bootstrap_channel_endpoint)); |
| 177 | 182 |
| 178 { | 183 { |
| 179 MutexLocker locker(&mutex_); | 184 MutexLocker locker(&mutex_); |
| 180 CHECK(channels_.find(channel_id) == channels_.end()); | 185 CHECK(channels_.find(channel_id) == channels_.end()); |
| 181 channels_[channel_id] = channel; | 186 channels_[channel_id] = channel; |
| 182 } | 187 } |
| 183 channel->SetChannelManager(this); | 188 channel->SetChannelManager(this); |
| 184 return channel; | 189 return channel; |
| 185 } | 190 } |
| 186 | 191 |
| 187 } // namespace system | 192 } // namespace system |
| 188 } // namespace mojo | 193 } // namespace mojo |
| OLD | NEW |