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 | |
10 namespace mojo { | |
11 namespace system { | |
12 | |
13 namespace { | |
14 | |
15 void ShutdownChannelHelper(const ChannelInfo& channel_info) { | |
16 channel_info.channel->WillShutdownSoon(); | |
17 channel_info.channel_thread_task_runner->PostTask( | |
18 FROM_HERE, base::Bind(&Channel::Shutdown, channel_info.channel)); | |
19 } | |
20 | |
21 } // namespace | |
22 | |
23 ChannelManager::ChannelManager() { | |
24 } | |
25 | |
26 ChannelManager::~ChannelManager() { | |
27 // No need to take the lock. | |
28 for (const auto& map_elem : channel_infos_) | |
29 ShutdownChannelHelper(map_elem.second); | |
30 } | |
31 | |
32 ChannelId ChannelManager::AddChannel( | |
33 scoped_refptr<Channel> channel, | |
34 scoped_refptr<base::TaskRunner> channel_thread_task_runner) { | |
35 ChannelId channel_id = GetChannelId(channel.get()); | |
36 | |
37 { | |
38 base::AutoLock locker(lock_); | |
39 DCHECK(channel_infos_.find(channel_id) == channel_infos_.end()); | |
40 channel_infos_[channel_id] = | |
41 ChannelInfo(channel, channel_thread_task_runner); | |
42 } | |
43 channel->SetChannelManager(this); | |
44 | |
45 return channel_id; | |
46 } | |
47 | |
48 void ChannelManager::ShutdownChannel(ChannelId channel_id) { | |
49 ChannelInfo channel_info; | |
50 { | |
51 base::AutoLock locker(lock_); | |
52 | |
53 auto it = channel_infos_.find(channel_id); | |
54 DCHECK(it != channel_infos_.end()); | |
55 channel_info.Swap(&it->second); | |
56 channel_infos_.erase(it); | |
57 } | |
58 ShutdownChannelHelper(channel_info); | |
59 } | |
60 | |
61 } // namespace system | |
62 } // namespace mojo | |
OLD | NEW |