| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "mojo/edk/system/endpoint_relayer.h" | 12 #include "mojo/edk/system/endpoint_relayer.h" |
| 14 #include "mojo/edk/system/transport_data.h" | 13 #include "mojo/edk/system/transport_data.h" |
| 14 #include "mojo/edk/util/string_printf.h" |
| 15 | 15 |
| 16 using mojo::platform::ScopedPlatformHandle; | 16 using mojo::platform::ScopedPlatformHandle; |
| 17 using mojo::util::MakeRefCounted; | 17 using mojo::util::MakeRefCounted; |
| 18 using mojo::util::MutexLocker; | 18 using mojo::util::MutexLocker; |
| 19 using mojo::util::RefPtr; | 19 using mojo::util::RefPtr; |
| 20 using mojo::util::StringPrintf; |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 namespace system { | 23 namespace system { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 struct SerializedEndpoint { | 27 struct SerializedEndpoint { |
| 27 // This is the endpoint ID on the receiving side, and should be a "remote ID". | 28 // This is the endpoint ID on the receiving side, and should be a "remote ID". |
| 28 // (The receiving side should already have had an endpoint attached and been | 29 // (The receiving side should already have had an endpoint attached and been |
| 29 // run via the |Channel|s. This endpoint will have both IDs assigned, so this | 30 // run via the |Channel|s. This endpoint will have both IDs assigned, so this |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // this function is still running. | 156 // this function is still running. |
| 156 RefPtr<Channel> self(this); | 157 RefPtr<Channel> self(this); |
| 157 | 158 |
| 158 if (!DetachEndpointInternal(endpoint, local_id, remote_id)) | 159 if (!DetachEndpointInternal(endpoint, local_id, remote_id)) |
| 159 return; | 160 return; |
| 160 | 161 |
| 161 // Note: Send the remove message outside the lock. | 162 // Note: Send the remove message outside the lock. |
| 162 if (!SendControlMessage(MessageInTransit::Subtype::CHANNEL_REMOVE_ENDPOINT, | 163 if (!SendControlMessage(MessageInTransit::Subtype::CHANNEL_REMOVE_ENDPOINT, |
| 163 local_id, remote_id, 0, nullptr)) { | 164 local_id, remote_id, 0, nullptr)) { |
| 164 HandleLocalError( | 165 HandleLocalError( |
| 165 base::StringPrintf("Failed to send message to remove remote endpoint " | 166 StringPrintf("Failed to send message to remove remote endpoint (local " |
| 166 "(local ID %u, remote ID %u)", | 167 "ID %u, remote ID %u)", |
| 167 static_cast<unsigned>(local_id.value()), | 168 static_cast<unsigned>(local_id.value()), |
| 168 static_cast<unsigned>(remote_id.value())) | 169 static_cast<unsigned>(remote_id.value())) |
| 169 .c_str()); | 170 .c_str()); |
| 170 } | 171 } |
| 171 } | 172 } |
| 172 | 173 |
| 173 size_t Channel::GetSerializedEndpointSize() const { | 174 size_t Channel::GetSerializedEndpointSize() const { |
| 174 return sizeof(SerializedEndpoint); | 175 return sizeof(SerializedEndpoint); |
| 175 } | 176 } |
| 176 | 177 |
| 177 void Channel::SerializeEndpointWithClosedPeer( | 178 void Channel::SerializeEndpointWithClosedPeer( |
| 178 void* destination, | 179 void* destination, |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 | 303 |
| 303 switch (message_view.type()) { | 304 switch (message_view.type()) { |
| 304 case MessageInTransit::Type::ENDPOINT_CLIENT: | 305 case MessageInTransit::Type::ENDPOINT_CLIENT: |
| 305 case MessageInTransit::Type::ENDPOINT: | 306 case MessageInTransit::Type::ENDPOINT: |
| 306 OnReadMessageForEndpoint(message_view, std::move(platform_handles)); | 307 OnReadMessageForEndpoint(message_view, std::move(platform_handles)); |
| 307 break; | 308 break; |
| 308 case MessageInTransit::Type::CHANNEL: | 309 case MessageInTransit::Type::CHANNEL: |
| 309 OnReadMessageForChannel(message_view, std::move(platform_handles)); | 310 OnReadMessageForChannel(message_view, std::move(platform_handles)); |
| 310 break; | 311 break; |
| 311 default: | 312 default: |
| 312 HandleRemoteError( | 313 HandleRemoteError(StringPrintf("Received message of invalid type %u", |
| 313 base::StringPrintf("Received message of invalid type %u", | 314 static_cast<unsigned>(message_view.type())) |
| 314 static_cast<unsigned>(message_view.type())) | 315 .c_str()); |
| 315 .c_str()); | |
| 316 break; | 316 break; |
| 317 } | 317 } |
| 318 } | 318 } |
| 319 | 319 |
| 320 void Channel::OnError(Error error) { | 320 void Channel::OnError(Error error) { |
| 321 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 321 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 322 DCHECK(thread_checker_.IsCreationThreadCurrent()); | 322 DCHECK(thread_checker_.IsCreationThreadCurrent()); |
| 323 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 323 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 324 | 324 |
| 325 switch (error) { | 325 switch (error) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 "= " << local_id | 383 "= " << local_id |
| 384 << ", remote ID = " << message_view.source_id() << ")"; | 384 << ", remote ID = " << message_view.source_id() << ")"; |
| 385 return; | 385 return; |
| 386 } | 386 } |
| 387 | 387 |
| 388 endpoint = it->second; | 388 endpoint = it->second; |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 if (!endpoint) { | 391 if (!endpoint) { |
| 392 HandleRemoteError( | 392 HandleRemoteError( |
| 393 base::StringPrintf( | 393 StringPrintf( |
| 394 "Received a message for nonexistent local destination ID %u", | 394 "Received a message for nonexistent local destination ID %u", |
| 395 static_cast<unsigned>(local_id.value())).c_str()); | 395 static_cast<unsigned>(local_id.value())) |
| 396 .c_str()); |
| 396 // This is strongly indicative of some problem. However, it's not a fatal | 397 // This is strongly indicative of some problem. However, it's not a fatal |
| 397 // error, since it may indicate a buggy (or hostile) remote process. Don't | 398 // error, since it may indicate a buggy (or hostile) remote process. Don't |
| 398 // die even for Debug builds, since handling this properly needs to be | 399 // die even for Debug builds, since handling this properly needs to be |
| 399 // tested (TODO(vtl)). | 400 // tested (TODO(vtl)). |
| 400 DLOG(ERROR) << "This should not happen under normal operation."; | 401 DLOG(ERROR) << "This should not happen under normal operation."; |
| 401 return; | 402 return; |
| 402 } | 403 } |
| 403 | 404 |
| 404 std::unique_ptr<MessageInTransit> message(new MessageInTransit(message_view)); | 405 std::unique_ptr<MessageInTransit> message(new MessageInTransit(message_view)); |
| 405 if (message_view.transport_data_buffer_size() > 0) { | 406 if (message_view.transport_data_buffer_size() > 0) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 endpoint = std::move(it->second); | 538 endpoint = std::move(it->second); |
| 538 local_id_to_endpoint_map_.erase(it); | 539 local_id_to_endpoint_map_.erase(it); |
| 539 // Detach and send the remove ack message outside the lock. | 540 // Detach and send the remove ack message outside the lock. |
| 540 } | 541 } |
| 541 | 542 |
| 542 endpoint->DetachFromChannel(); | 543 endpoint->DetachFromChannel(); |
| 543 | 544 |
| 544 if (!SendControlMessage( | 545 if (!SendControlMessage( |
| 545 MessageInTransit::Subtype::CHANNEL_REMOVE_ENDPOINT_ACK, local_id, | 546 MessageInTransit::Subtype::CHANNEL_REMOVE_ENDPOINT_ACK, local_id, |
| 546 remote_id, 0, nullptr)) { | 547 remote_id, 0, nullptr)) { |
| 547 HandleLocalError(base::StringPrintf( | 548 HandleLocalError( |
| 548 "Failed to send message to ack remove remote endpoint " | 549 StringPrintf("Failed to send message to ack remove remote endpoint " |
| 549 "(local ID %u, remote ID %u)", | 550 "(local ID %u, remote ID %u)", |
| 550 static_cast<unsigned>(local_id.value()), | 551 static_cast<unsigned>(local_id.value()), |
| 551 static_cast<unsigned>(remote_id.value())).c_str()); | 552 static_cast<unsigned>(remote_id.value())) |
| 553 .c_str()); |
| 552 } | 554 } |
| 553 | 555 |
| 554 return true; | 556 return true; |
| 555 } | 557 } |
| 556 | 558 |
| 557 bool Channel::OnRemoveEndpointAck(ChannelEndpointId local_id) { | 559 bool Channel::OnRemoveEndpointAck(ChannelEndpointId local_id) { |
| 558 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 560 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 559 DCHECK(thread_checker_.IsCreationThreadCurrent()); | 561 DCHECK(thread_checker_.IsCreationThreadCurrent()); |
| 560 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 562 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 561 | 563 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 | 614 |
| 613 // TODO(vtl): We also need to check for collisions of remote IDs here. | 615 // TODO(vtl): We also need to check for collisions of remote IDs here. |
| 614 remote_id = remote_id_generator_.GetNext(); | 616 remote_id = remote_id_generator_.GetNext(); |
| 615 | 617 |
| 616 local_id_to_endpoint_map_[local_id] = endpoint; | 618 local_id_to_endpoint_map_[local_id] = endpoint; |
| 617 } | 619 } |
| 618 | 620 |
| 619 if (!SendControlMessage( | 621 if (!SendControlMessage( |
| 620 MessageInTransit::Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT, local_id, | 622 MessageInTransit::Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT, local_id, |
| 621 remote_id, 0, nullptr)) { | 623 remote_id, 0, nullptr)) { |
| 622 HandleLocalError(base::StringPrintf( | 624 HandleLocalError( |
| 623 "Failed to send message to run remote endpoint (local " | 625 StringPrintf("Failed to send message to run remote endpoint (local " |
| 624 "ID %u, remote ID %u)", | 626 "ID %u, remote ID %u)", |
| 625 static_cast<unsigned>(local_id.value()), | 627 static_cast<unsigned>(local_id.value()), |
| 626 static_cast<unsigned>(remote_id.value())).c_str()); | 628 static_cast<unsigned>(remote_id.value())) |
| 629 .c_str()); |
| 627 // TODO(vtl): Should we continue on to |AttachAndRun()|? | 630 // TODO(vtl): Should we continue on to |AttachAndRun()|? |
| 628 } | 631 } |
| 629 | 632 |
| 630 endpoint->AttachAndRun(this, local_id, remote_id); | 633 endpoint->AttachAndRun(this, local_id, remote_id); |
| 631 return remote_id; | 634 return remote_id; |
| 632 } | 635 } |
| 633 | 636 |
| 634 bool Channel::SendControlMessage(MessageInTransit::Subtype subtype, | 637 bool Channel::SendControlMessage(MessageInTransit::Subtype subtype, |
| 635 ChannelEndpointId local_id, | 638 ChannelEndpointId local_id, |
| 636 ChannelEndpointId remote_id, | 639 ChannelEndpointId remote_id, |
| 637 uint32_t num_bytes, | 640 uint32_t num_bytes, |
| 638 const void* bytes) { | 641 const void* bytes) { |
| 639 DVLOG(2) << "Sending channel control message: subtype " << subtype | 642 DVLOG(2) << "Sending channel control message: subtype " << subtype |
| 640 << ", local ID " << local_id << ", remote ID " << remote_id; | 643 << ", local ID " << local_id << ", remote ID " << remote_id; |
| 641 std::unique_ptr<MessageInTransit> message(new MessageInTransit( | 644 std::unique_ptr<MessageInTransit> message(new MessageInTransit( |
| 642 MessageInTransit::Type::CHANNEL, subtype, num_bytes, bytes)); | 645 MessageInTransit::Type::CHANNEL, subtype, num_bytes, bytes)); |
| 643 message->set_source_id(local_id); | 646 message->set_source_id(local_id); |
| 644 message->set_destination_id(remote_id); | 647 message->set_destination_id(remote_id); |
| 645 return WriteMessage(std::move(message)); | 648 return WriteMessage(std::move(message)); |
| 646 } | 649 } |
| 647 | 650 |
| 648 } // namespace system | 651 } // namespace system |
| 649 } // namespace mojo | 652 } // namespace mojo |
| OLD | NEW |