Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(747)

Side by Side Diff: mojo/edk/system/channel_manager.cc

Issue 728613002: Make the embedder API use the ChannelManager. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: update comment Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/channel_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
9 10
10 namespace mojo { 11 namespace mojo {
11 namespace system { 12 namespace system {
12 13
13 namespace { 14 namespace {
14 15
15 void ShutdownChannelHelper(const ChannelInfo& channel_info) { 16 void ShutdownChannelHelper(const ChannelInfo& channel_info) {
16 channel_info.channel->WillShutdownSoon(); 17 if (base::MessageLoopProxy::current() ==
17 channel_info.channel_thread_task_runner->PostTask( 18 channel_info.channel_thread_task_runner) {
18 FROM_HERE, base::Bind(&Channel::Shutdown, channel_info.channel)); 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 }
19 } 25 }
20 26
21 } // namespace 27 } // namespace
22 28
23 ChannelManager::ChannelManager() { 29 ChannelManager::ChannelManager() {
24 } 30 }
25 31
26 ChannelManager::~ChannelManager() { 32 ChannelManager::~ChannelManager() {
27 // No need to take the lock. 33 // No need to take the lock.
28 for (const auto& map_elem : channel_infos_) 34 for (const auto& map_elem : channel_infos_)
29 ShutdownChannelHelper(map_elem.second); 35 ShutdownChannelHelper(map_elem.second);
30 } 36 }
31 37
32 ChannelId ChannelManager::AddChannel( 38 ChannelId ChannelManager::AddChannel(
33 scoped_refptr<Channel> channel, 39 scoped_refptr<Channel> channel,
34 scoped_refptr<base::TaskRunner> channel_thread_task_runner) { 40 scoped_refptr<base::TaskRunner> channel_thread_task_runner) {
35 ChannelId channel_id = GetChannelId(channel.get()); 41 ChannelId channel_id = GetChannelId(channel.get());
36 42
37 { 43 {
38 base::AutoLock locker(lock_); 44 base::AutoLock locker(lock_);
39 DCHECK(channel_infos_.find(channel_id) == channel_infos_.end()); 45 DCHECK(channel_infos_.find(channel_id) == channel_infos_.end());
40 channel_infos_[channel_id] = 46 channel_infos_[channel_id] =
41 ChannelInfo(channel, channel_thread_task_runner); 47 ChannelInfo(channel, channel_thread_task_runner);
42 } 48 }
43 channel->SetChannelManager(this); 49 channel->SetChannelManager(this);
44 50
45 return channel_id; 51 return channel_id;
46 } 52 }
47 53
54 void ChannelManager::WillShutdownChannel(ChannelId channel_id) {
55 GetChannelInfo(channel_id).channel->WillShutdownSoon();
56 }
57
48 void ChannelManager::ShutdownChannel(ChannelId channel_id) { 58 void ChannelManager::ShutdownChannel(ChannelId channel_id) {
49 ChannelInfo channel_info; 59 ShutdownChannelHelper(GetChannelInfo(channel_id));
50 { 60 }
51 base::AutoLock locker(lock_);
52 61
53 auto it = channel_infos_.find(channel_id); 62 ChannelInfo ChannelManager::GetChannelInfo(ChannelId channel_id) {
54 DCHECK(it != channel_infos_.end()); 63 ChannelInfo rv;
55 channel_info.Swap(&it->second); 64 base::AutoLock locker(lock_);
56 channel_infos_.erase(it); 65 auto it = channel_infos_.find(channel_id);
57 } 66 DCHECK(it != channel_infos_.end());
58 ShutdownChannelHelper(channel_info); 67 rv.Swap(&it->second);
68 channel_infos_.erase(it);
69 return rv;
59 } 70 }
60 71
61 } // namespace system 72 } // namespace system
62 } // namespace mojo 73 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/channel_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698