stream_message_pipe_adapter.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 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.
  4. #include "remoting/protocol/stream_message_pipe_adapter.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "net/base/net_errors.h"
  9. #include "net/traffic_annotation/network_traffic_annotation.h"
  10. #include "remoting/base/buffered_socket_writer.h"
  11. #include "remoting/base/compound_buffer.h"
  12. #include "remoting/protocol/message_serialization.h"
  13. #include "remoting/protocol/p2p_stream_socket.h"
  14. #include "remoting/protocol/stream_channel_factory.h"
  15. namespace remoting {
  16. namespace protocol {
  17. StreamMessagePipeAdapter::StreamMessagePipeAdapter(
  18. std::unique_ptr<P2PStreamSocket> socket,
  19. ErrorCallback error_callback)
  20. : socket_(std::move(socket)), error_callback_(std::move(error_callback)) {
  21. DCHECK(socket_);
  22. DCHECK(error_callback_);
  23. }
  24. StreamMessagePipeAdapter::~StreamMessagePipeAdapter() = default;
  25. void StreamMessagePipeAdapter::Start(EventHandler* event_handler) {
  26. DCHECK(event_handler);
  27. event_handler_ = event_handler;
  28. writer_ = std::make_unique<BufferedSocketWriter>();
  29. writer_->Start(base::BindRepeating(&P2PStreamSocket::Write,
  30. base::Unretained(socket_.get())),
  31. base::BindOnce(&StreamMessagePipeAdapter::CloseOnError,
  32. base::Unretained(this)));
  33. reader_ = std::make_unique<MessageReader>();
  34. reader_->StartReading(socket_.get(),
  35. base::BindRepeating(&EventHandler::OnMessageReceived,
  36. base::Unretained(event_handler_)),
  37. base::BindOnce(&StreamMessagePipeAdapter::CloseOnError,
  38. base::Unretained(this)));
  39. event_handler_->OnMessagePipeOpen();
  40. }
  41. void StreamMessagePipeAdapter::Send(google::protobuf::MessageLite* message,
  42. base::OnceClosure done) {
  43. net::NetworkTrafficAnnotationTag traffic_annotation =
  44. net::DefineNetworkTrafficAnnotation("stream_message_pipe_adapter", R"(
  45. semantics {
  46. sender: "Chrome Remote Desktop"
  47. description: "Chrome Remote Desktop P2P channel."
  48. trigger: "Initiating a Chrome Remote Desktop connection."
  49. data:
  50. "Chrome Remote Desktop session data, including video and input "
  51. "events."
  52. destination: OTHER
  53. destination_other:
  54. "The Chrome Remote Desktop client/host that user is connecting to."
  55. }
  56. policy {
  57. cookies_allowed: NO
  58. setting:
  59. "This request cannot be stopped in settings, but will not be sent "
  60. "if user does not use Chrome Remote Desktop."
  61. policy_exception_justification:
  62. "Not implemented. 'RemoteAccessHostClientDomainList' and "
  63. "'RemoteAccessHostDomainList' policies can limit the domains to "
  64. "which a connection can be made, but they cannot be used to block "
  65. "the request to all domains. Please refer to help desk for other "
  66. "approaches to manage this feature."
  67. })");
  68. if (writer_)
  69. writer_->Write(SerializeAndFrameMessage(*message), std::move(done),
  70. traffic_annotation);
  71. }
  72. void StreamMessagePipeAdapter::CloseOnError(int error) {
  73. // Stop reading and writing on error.
  74. writer_.reset();
  75. reader_.reset();
  76. if (error == 0) {
  77. event_handler_->OnMessagePipeClosed();
  78. } else if (error_callback_) {
  79. std::move(error_callback_).Run(error);
  80. }
  81. }
  82. StreamMessageChannelFactoryAdapter::StreamMessageChannelFactoryAdapter(
  83. StreamChannelFactory* stream_channel_factory,
  84. const ErrorCallback& error_callback)
  85. : stream_channel_factory_(stream_channel_factory),
  86. error_callback_(error_callback) {}
  87. StreamMessageChannelFactoryAdapter::~StreamMessageChannelFactoryAdapter() =
  88. default;
  89. void StreamMessageChannelFactoryAdapter::CreateChannel(
  90. const std::string& name,
  91. ChannelCreatedCallback callback) {
  92. stream_channel_factory_->CreateChannel(
  93. name,
  94. base::BindOnce(&StreamMessageChannelFactoryAdapter::OnChannelCreated,
  95. base::Unretained(this), std::move(callback)));
  96. }
  97. void StreamMessageChannelFactoryAdapter::CancelChannelCreation(
  98. const std::string& name) {
  99. stream_channel_factory_->CancelChannelCreation(name);
  100. }
  101. void StreamMessageChannelFactoryAdapter::OnChannelCreated(
  102. ChannelCreatedCallback callback,
  103. std::unique_ptr<P2PStreamSocket> socket) {
  104. if (!socket) {
  105. error_callback_.Run(net::ERR_FAILED);
  106. return;
  107. }
  108. std::move(callback).Run(std::make_unique<StreamMessagePipeAdapter>(
  109. std::move(socket), error_callback_));
  110. }
  111. } // namespace protocol
  112. } // namespace remoting