More explicit thread checking in SafeBrowsingDatabase.
[chromium-blink-merge.git] / mojo / edk / system / channel_manager.cc
blob4e58f89abe0ce64b9df3dc2883b18195f350f1d0
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
3 // found in the LICENSE file.
5 #include "mojo/edk/system/channel_manager.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
11 namespace mojo {
12 namespace system {
14 namespace {
16 void ShutdownChannelHelper(const ChannelInfo& channel_info) {
17 if (base::MessageLoopProxy::current() ==
18 channel_info.channel_thread_task_runner) {
19 channel_info.channel->Shutdown();
20 } else {
21 channel_info.channel->WillShutdownSoon();
22 channel_info.channel_thread_task_runner->PostTask(
23 FROM_HERE, base::Bind(&Channel::Shutdown, channel_info.channel));
27 } // namespace
29 ChannelManager::ChannelManager() {
32 ChannelManager::~ChannelManager() {
33 // No need to take the lock.
34 for (const auto& map_elem : channel_infos_)
35 ShutdownChannelHelper(map_elem.second);
38 ChannelId ChannelManager::AddChannel(
39 scoped_refptr<Channel> channel,
40 scoped_refptr<base::TaskRunner> channel_thread_task_runner) {
41 ChannelId channel_id = GetChannelId(channel.get());
44 base::AutoLock locker(lock_);
45 DCHECK(channel_infos_.find(channel_id) == channel_infos_.end());
46 channel_infos_[channel_id] =
47 ChannelInfo(channel, channel_thread_task_runner);
49 channel->SetChannelManager(this);
51 return channel_id;
54 void ChannelManager::WillShutdownChannel(ChannelId channel_id) {
55 GetChannelInfo(channel_id).channel->WillShutdownSoon();
58 void ChannelManager::ShutdownChannel(ChannelId channel_id) {
59 ChannelInfo channel_info;
61 base::AutoLock locker(lock_);
62 auto it = channel_infos_.find(channel_id);
63 DCHECK(it != channel_infos_.end());
64 channel_info.Swap(&it->second);
65 channel_infos_.erase(it);
67 ShutdownChannelHelper(channel_info);
70 ChannelInfo ChannelManager::GetChannelInfo(ChannelId channel_id) {
71 base::AutoLock locker(lock_);
72 auto it = channel_infos_.find(channel_id);
73 DCHECK(it != channel_infos_.end());
74 return it->second;
77 } // namespace system
78 } // namespace mojo