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