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"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
16 void ShutdownChannelHelper(const ChannelInfo
& channel_info
) {
17 if (base::MessageLoopProxy::current() ==
18 channel_info
.channel_thread_task_runner
) {
19 channel_info
.channel
->Shutdown();
21 channel_info
.channel
->WillShutdownSoon();
22 channel_info
.channel_thread_task_runner
->PostTask(
23 FROM_HERE
, base::Bind(&Channel::Shutdown
, channel_info
.channel
));
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);
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());