OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/edk/system/channel_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/message_loop/message_loop_proxy.h" | |
10 | |
11 namespace mojo { | |
12 namespace system { | |
13 | |
14 namespace { | |
15 | |
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)); | |
24 } | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 ChannelManager::ChannelManager() { | |
30 } | |
31 | |
32 ChannelManager::~ChannelManager() { | |
33 // No need to take the lock. | |
34 for (const auto& map_elem : channel_infos_) | |
35 ShutdownChannelHelper(map_elem.second); | |
36 } | |
37 | |
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()); | |
42 | |
43 { | |
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); | |
48 } | |
49 channel->SetChannelManager(this); | |
50 | |
51 return channel_id; | |
52 } | |
53 | |
54 void ChannelManager::WillShutdownChannel(ChannelId channel_id) { | |
55 GetChannelInfo(channel_id).channel->WillShutdownSoon(); | |
56 } | |
57 | |
58 void ChannelManager::ShutdownChannel(ChannelId channel_id) { | |
59 ChannelInfo channel_info; | |
60 { | |
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); | |
66 } | |
67 ShutdownChannelHelper(channel_info); | |
68 } | |
69 | |
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; | |
75 } | |
76 | |
77 } // namespace system | |
78 } // namespace mojo | |
OLD | NEW |