OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.h" | 5 #include "mojo/edk/system/channel.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "mojo/edk/embedder/platform_handle_vector.h" | 14 #include "mojo/edk/embedder/platform_handle_vector.h" |
15 #include "mojo/edk/system/transport_data.h" | 15 #include "mojo/edk/system/transport_data.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace system { | 18 namespace system { |
19 | 19 |
20 Channel::Channel(embedder::PlatformSupport* platform_support) | 20 Channel::Channel(embedder::PlatformSupport* platform_support) |
21 : platform_support_(platform_support), | 21 : platform_support_(platform_support), |
22 is_running_(false), | 22 is_running_(false), |
23 is_shutting_down_(false), | 23 is_shutting_down_(false) { |
24 channel_manager_(nullptr) { | |
25 } | 24 } |
26 | 25 |
27 bool Channel::Init(scoped_ptr<RawChannel> raw_channel) { | 26 bool Channel::Init(scoped_ptr<RawChannel> raw_channel) { |
28 DCHECK(creation_thread_checker_.CalledOnValidThread()); | 27 DCHECK(creation_thread_checker_.CalledOnValidThread()); |
29 DCHECK(raw_channel); | 28 DCHECK(raw_channel); |
30 | 29 |
31 // No need to take |lock_|, since this must be called before this object | 30 // No need to take |lock_|, since this must be called before this object |
32 // becomes thread-safe. | 31 // becomes thread-safe. |
33 DCHECK(!is_running_); | 32 DCHECK(!is_running_); |
34 raw_channel_ = raw_channel.Pass(); | 33 raw_channel_ = raw_channel.Pass(); |
35 | 34 |
36 if (!raw_channel_->Init(this)) { | 35 if (!raw_channel_->Init(this)) { |
37 raw_channel_.reset(); | 36 raw_channel_.reset(); |
38 return false; | 37 return false; |
39 } | 38 } |
40 | 39 |
41 is_running_ = true; | 40 is_running_ = true; |
42 return true; | 41 return true; |
43 } | 42 } |
44 | 43 |
45 void Channel::SetChannelManager(ChannelManager* channel_manager) { | |
46 DCHECK(channel_manager); | |
47 | |
48 base::AutoLock locker(lock_); | |
49 DCHECK(!is_shutting_down_); | |
50 DCHECK(!channel_manager_); | |
51 channel_manager_ = channel_manager; | |
52 } | |
53 | |
54 void Channel::Shutdown() { | 44 void Channel::Shutdown() { |
55 DCHECK(creation_thread_checker_.CalledOnValidThread()); | 45 DCHECK(creation_thread_checker_.CalledOnValidThread()); |
56 | 46 |
57 IdToEndpointMap to_destroy; | 47 IdToEndpointMap to_destroy; |
58 { | 48 { |
59 base::AutoLock locker(lock_); | 49 base::AutoLock locker(lock_); |
60 if (!is_running_) | 50 if (!is_running_) |
61 return; | 51 return; |
62 | 52 |
63 // Note: Don't reset |raw_channel_|, in case we're being called from within | 53 // Note: Don't reset |raw_channel_|, in case we're being called from within |
64 // |OnReadMessage()| or |OnError()|. | 54 // |OnReadMessage()| or |OnError()|. |
65 raw_channel_->Shutdown(); | 55 raw_channel_->Shutdown(); |
66 is_running_ = false; | 56 is_running_ = false; |
67 | 57 |
68 // We need to deal with it outside the lock. | 58 // We need to deal with it outside the lock. |
69 std::swap(to_destroy, local_id_to_endpoint_map_); | 59 std::swap(to_destroy, local_id_to_endpoint_map_); |
70 } | 60 } |
71 | 61 |
72 size_t num_live = 0; | 62 size_t num_live = 0; |
73 size_t num_zombies = 0; | 63 size_t num_zombies = 0; |
74 for (IdToEndpointMap::iterator it = to_destroy.begin(); | 64 for (IdToEndpointMap::iterator it = to_destroy.begin(); |
75 it != to_destroy.end(); ++it) { | 65 it != to_destroy.end(); |
| 66 ++it) { |
76 if (it->second.get()) { | 67 if (it->second.get()) { |
77 num_live++; | 68 num_live++; |
78 it->second->OnDisconnect(); | 69 it->second->OnDisconnect(); |
79 it->second->DetachFromChannel(); | 70 it->second->DetachFromChannel(); |
80 } else { | 71 } else { |
81 num_zombies++; | 72 num_zombies++; |
82 } | 73 } |
83 } | 74 } |
84 DVLOG_IF(2, num_live || num_zombies) << "Shut down Channel with " << num_live | 75 DVLOG_IF(2, num_live || num_zombies) << "Shut down Channel with " << num_live |
85 << " live endpoints and " << num_zombies | 76 << " live endpoints and " << num_zombies |
86 << " zombies"; | 77 << " zombies"; |
87 } | 78 } |
88 | 79 |
89 void Channel::WillShutdownSoon() { | 80 void Channel::WillShutdownSoon() { |
90 base::AutoLock locker(lock_); | 81 base::AutoLock locker(lock_); |
91 is_shutting_down_ = true; | 82 is_shutting_down_ = true; |
92 channel_manager_ = nullptr; | |
93 } | 83 } |
94 | 84 |
95 // Note: |endpoint| being a |scoped_refptr| makes this function safe, since it | 85 // Note: |endpoint| being a |scoped_refptr| makes this function safe, since it |
96 // keeps the endpoint alive even after the lock is released. Otherwise, there's | 86 // keeps the endpoint alive even after the lock is released. Otherwise, there's |
97 // the temptation to simply pass the result of |new ChannelEndpoint(...)| | 87 // the temptation to simply pass the result of |new ChannelEndpoint(...)| |
98 // directly to this function, which wouldn't be sufficient for safety. | 88 // directly to this function, which wouldn't be sufficient for safety. |
99 ChannelEndpointId Channel::AttachAndRunEndpoint( | 89 ChannelEndpointId Channel::AttachAndRunEndpoint( |
100 scoped_refptr<ChannelEndpoint> endpoint, | 90 scoped_refptr<ChannelEndpoint> endpoint, |
101 bool is_bootstrap) { | 91 bool is_bootstrap) { |
102 DCHECK(endpoint.get()); | 92 DCHECK(endpoint.get()); |
(...skipping 20 matching lines...) Expand all Loading... |
123 | 113 |
124 // TODO(vtl): We also need to check for collisions of remote IDs here. | 114 // TODO(vtl): We also need to check for collisions of remote IDs here. |
125 remote_id = remote_id_generator_.GetNext(); | 115 remote_id = remote_id_generator_.GetNext(); |
126 } | 116 } |
127 | 117 |
128 local_id_to_endpoint_map_[local_id] = endpoint; | 118 local_id_to_endpoint_map_[local_id] = endpoint; |
129 } | 119 } |
130 | 120 |
131 if (!is_bootstrap) { | 121 if (!is_bootstrap) { |
132 if (!SendControlMessage( | 122 if (!SendControlMessage( |
133 MessageInTransit::kSubtypeChannelAttachAndRunEndpoint, local_id, | 123 MessageInTransit::kSubtypeChannelAttachAndRunEndpoint, |
| 124 local_id, |
134 remote_id)) { | 125 remote_id)) { |
135 HandleLocalError(base::StringPrintf( | 126 HandleLocalError(base::StringPrintf( |
136 "Failed to send message to run remote message pipe endpoint (local " | 127 "Failed to send message to run remote message pipe endpoint (local " |
137 "ID %u, remote ID %u)", | 128 "ID %u, remote ID %u)", |
138 static_cast<unsigned>(local_id.value()), | 129 static_cast<unsigned>(local_id.value()), |
139 static_cast<unsigned>(remote_id.value()))); | 130 static_cast<unsigned>(remote_id.value()))); |
140 // TODO(vtl): Should we continue on to |AttachAndRun()|? | 131 // TODO(vtl): Should we continue on to |AttachAndRun()|? |
141 } | 132 } |
142 } | 133 } |
143 | 134 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 if (it == local_id_to_endpoint_map_.end() || it->second.get() != endpoint) | 178 if (it == local_id_to_endpoint_map_.end() || it->second.get() != endpoint) |
188 return; | 179 return; |
189 | 180 |
190 DCHECK(it->second.get()); | 181 DCHECK(it->second.get()); |
191 it->second = nullptr; | 182 it->second = nullptr; |
192 | 183 |
193 // Send a remove message outside the lock. | 184 // Send a remove message outside the lock. |
194 } | 185 } |
195 | 186 |
196 if (!SendControlMessage( | 187 if (!SendControlMessage( |
197 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpoint, local_id, | 188 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpoint, |
| 189 local_id, |
198 remote_id)) { | 190 remote_id)) { |
199 HandleLocalError(base::StringPrintf( | 191 HandleLocalError(base::StringPrintf( |
200 "Failed to send message to remove remote message pipe endpoint (local " | 192 "Failed to send message to remove remote message pipe endpoint (local " |
201 "ID %u, remote ID %u)", | 193 "ID %u, remote ID %u)", |
202 static_cast<unsigned>(local_id.value()), | 194 static_cast<unsigned>(local_id.value()), |
203 static_cast<unsigned>(remote_id.value()))); | 195 static_cast<unsigned>(remote_id.value()))); |
204 } | 196 } |
205 } | 197 } |
206 | 198 |
207 scoped_refptr<MessagePipe> Channel::PassIncomingMessagePipe( | 199 scoped_refptr<MessagePipe> Channel::PassIncomingMessagePipe( |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 452 |
461 endpoint = it->second; | 453 endpoint = it->second; |
462 local_id_to_endpoint_map_.erase(it); | 454 local_id_to_endpoint_map_.erase(it); |
463 // Detach and send the remove ack message outside the lock. | 455 // Detach and send the remove ack message outside the lock. |
464 } | 456 } |
465 | 457 |
466 endpoint->DetachFromChannel(); | 458 endpoint->DetachFromChannel(); |
467 | 459 |
468 if (!SendControlMessage( | 460 if (!SendControlMessage( |
469 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpointAck, | 461 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpointAck, |
470 local_id, remote_id)) { | 462 local_id, |
| 463 remote_id)) { |
471 HandleLocalError(base::StringPrintf( | 464 HandleLocalError(base::StringPrintf( |
472 "Failed to send message to remove remote message pipe endpoint ack " | 465 "Failed to send message to remove remote message pipe endpoint ack " |
473 "(local ID %u, remote ID %u)", | 466 "(local ID %u, remote ID %u)", |
474 static_cast<unsigned>(local_id.value()), | 467 static_cast<unsigned>(local_id.value()), |
475 static_cast<unsigned>(remote_id.value()))); | 468 static_cast<unsigned>(remote_id.value()))); |
476 } | 469 } |
477 | 470 |
478 endpoint->OnDisconnect(); | 471 endpoint->OnDisconnect(); |
479 return true; | 472 return true; |
480 } | 473 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 // TODO(vtl): Is this how we really want to handle this? | 514 // TODO(vtl): Is this how we really want to handle this? |
522 // Sometimes we'll want to propagate the error back to the message pipe | 515 // Sometimes we'll want to propagate the error back to the message pipe |
523 // (endpoint), and notify it that the remote is (effectively) closed. | 516 // (endpoint), and notify it that the remote is (effectively) closed. |
524 // Sometimes we'll want to kill the channel (and notify all the endpoints that | 517 // Sometimes we'll want to kill the channel (and notify all the endpoints that |
525 // their remotes are dead. | 518 // their remotes are dead. |
526 LOG(WARNING) << error_message; | 519 LOG(WARNING) << error_message; |
527 } | 520 } |
528 | 521 |
529 } // namespace system | 522 } // namespace system |
530 } // namespace mojo | 523 } // namespace mojo |
OLD | NEW |