| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/security_key/remote_security_key_message_handler.h" | 5 #include "remoting/host/security_key/security_key_message_handler.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "remoting/host/security_key/remote_security_key_ipc_client.h" | 14 #include "remoting/host/security_key/security_key_ipc_client.h" |
| 15 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 15 #include "remoting/host/security_key/security_key_ipc_constants.h" |
| 16 #include "remoting/host/security_key/remote_security_key_message_reader_impl.h" | 16 #include "remoting/host/security_key/security_key_message_reader_impl.h" |
| 17 #include "remoting/host/security_key/remote_security_key_message_writer_impl.h" | 17 #include "remoting/host/security_key/security_key_message_writer_impl.h" |
| 18 | 18 |
| 19 namespace remoting { | 19 namespace remoting { |
| 20 | 20 |
| 21 RemoteSecurityKeyMessageHandler::RemoteSecurityKeyMessageHandler() {} | 21 SecurityKeyMessageHandler::SecurityKeyMessageHandler() {} |
| 22 | 22 |
| 23 RemoteSecurityKeyMessageHandler::~RemoteSecurityKeyMessageHandler() {} | 23 SecurityKeyMessageHandler::~SecurityKeyMessageHandler() {} |
| 24 | 24 |
| 25 void RemoteSecurityKeyMessageHandler::Start( | 25 void SecurityKeyMessageHandler::Start( |
| 26 base::File message_read_stream, | 26 base::File message_read_stream, |
| 27 base::File message_write_stream, | 27 base::File message_write_stream, |
| 28 std::unique_ptr<RemoteSecurityKeyIpcClient> ipc_client, | 28 std::unique_ptr<SecurityKeyIpcClient> ipc_client, |
| 29 const base::Closure& error_callback) { | 29 const base::Closure& error_callback) { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 DCHECK(message_read_stream.IsValid()); | 31 DCHECK(message_read_stream.IsValid()); |
| 32 DCHECK(message_write_stream.IsValid()); | 32 DCHECK(message_write_stream.IsValid()); |
| 33 DCHECK(ipc_client); | 33 DCHECK(ipc_client); |
| 34 DCHECK(!error_callback.is_null()); | 34 DCHECK(!error_callback.is_null()); |
| 35 DCHECK(error_callback_.is_null()); | 35 DCHECK(error_callback_.is_null()); |
| 36 | 36 |
| 37 if (!reader_) { | 37 if (!reader_) { |
| 38 reader_.reset( | 38 reader_.reset( |
| 39 new RemoteSecurityKeyMessageReaderImpl(std::move(message_read_stream))); | 39 new SecurityKeyMessageReaderImpl(std::move(message_read_stream))); |
| 40 } | 40 } |
| 41 | 41 |
| 42 if (!writer_) { | 42 if (!writer_) { |
| 43 writer_.reset(new RemoteSecurityKeyMessageWriterImpl( | 43 writer_.reset( |
| 44 std::move(message_write_stream))); | 44 new SecurityKeyMessageWriterImpl(std::move(message_write_stream))); |
| 45 } | 45 } |
| 46 | 46 |
| 47 ipc_client_ = std::move(ipc_client); | 47 ipc_client_ = std::move(ipc_client); |
| 48 error_callback_ = error_callback; | 48 error_callback_ = error_callback; |
| 49 | 49 |
| 50 reader_->Start( | 50 reader_->Start( |
| 51 base::Bind( | 51 base::Bind(&SecurityKeyMessageHandler::ProcessSecurityKeyMessage, |
| 52 &RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage, | 52 base::Unretained(this)), |
| 53 base::Unretained(this)), | 53 base::Bind(&SecurityKeyMessageHandler::OnError, base::Unretained(this))); |
| 54 base::Bind(&RemoteSecurityKeyMessageHandler::OnError, | |
| 55 base::Unretained(this))); | |
| 56 } | 54 } |
| 57 | 55 |
| 58 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageReaderForTest( | 56 void SecurityKeyMessageHandler::SetSecurityKeyMessageReaderForTest( |
| 59 std::unique_ptr<RemoteSecurityKeyMessageReader> reader) { | 57 std::unique_ptr<SecurityKeyMessageReader> reader) { |
| 60 DCHECK(!reader_); | 58 DCHECK(!reader_); |
| 61 reader_ = std::move(reader); | 59 reader_ = std::move(reader); |
| 62 } | 60 } |
| 63 | 61 |
| 64 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageWriterForTest( | 62 void SecurityKeyMessageHandler::SetSecurityKeyMessageWriterForTest( |
| 65 std::unique_ptr<RemoteSecurityKeyMessageWriter> writer) { | 63 std::unique_ptr<SecurityKeyMessageWriter> writer) { |
| 66 DCHECK(!writer_); | 64 DCHECK(!writer_); |
| 67 writer_ = std::move(writer); | 65 writer_ = std::move(writer); |
| 68 } | 66 } |
| 69 | 67 |
| 70 void RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage( | 68 void SecurityKeyMessageHandler::ProcessSecurityKeyMessage( |
| 71 std::unique_ptr<SecurityKeyMessage> message) { | 69 std::unique_ptr<SecurityKeyMessage> message) { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 | 71 |
| 74 RemoteSecurityKeyMessageType message_type = message->type(); | 72 SecurityKeyMessageType message_type = message->type(); |
| 75 if (message_type == RemoteSecurityKeyMessageType::CONNECT) { | 73 if (message_type == SecurityKeyMessageType::CONNECT) { |
| 76 HandleConnectRequest(message->payload()); | 74 HandleConnectRequest(message->payload()); |
| 77 } else if (message_type == RemoteSecurityKeyMessageType::REQUEST) { | 75 } else if (message_type == SecurityKeyMessageType::REQUEST) { |
| 78 HandleSecurityKeyRequest(message->payload()); | 76 HandleSecurityKeyRequest(message->payload()); |
| 79 } else { | 77 } else { |
| 80 LOG(ERROR) << "Unknown message type: " | 78 LOG(ERROR) << "Unknown message type: " |
| 81 << static_cast<uint8_t>(message_type); | 79 << static_cast<uint8_t>(message_type); |
| 82 SendMessage(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND); | 80 SendMessage(SecurityKeyMessageType::UNKNOWN_COMMAND); |
| 83 } | 81 } |
| 84 } | 82 } |
| 85 | 83 |
| 86 void RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange( | 84 void SecurityKeyMessageHandler::HandleIpcConnectionChange( |
| 87 bool connection_established) { | 85 bool connection_established) { |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 if (connection_established) { | 87 if (connection_established) { |
| 90 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, | 88 SendMessageWithPayload(SecurityKeyMessageType::CONNECT_RESPONSE, |
| 91 std::string(1, kConnectResponseActiveSession)); | 89 std::string(1, kConnectResponseActiveSession)); |
| 92 } else { | 90 } else { |
| 93 SendMessageWithPayload( | 91 SendMessageWithPayload( |
| 94 RemoteSecurityKeyMessageType::CONNECT_ERROR, | 92 SecurityKeyMessageType::CONNECT_ERROR, |
| 95 "Unknown error occurred while establishing connection."); | 93 "Unknown error occurred while establishing connection."); |
| 96 } | 94 } |
| 97 } | 95 } |
| 98 | 96 |
| 99 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse( | 97 void SecurityKeyMessageHandler::HandleSecurityKeyResponse( |
| 100 const std::string& response_data) { | 98 const std::string& response_data) { |
| 101 if (response_data.compare(kRemoteSecurityKeyConnectionError) == 0) { | 99 if (response_data.compare(kSecurityKeyConnectionError) == 0) { |
| 102 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | 100 SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR, |
| 103 "An error occurred during the request."); | 101 "An error occurred during the request."); |
| 104 return; | 102 return; |
| 105 } | 103 } |
| 106 | 104 |
| 107 if (response_data.empty()) { | 105 if (response_data.empty()) { |
| 108 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | 106 SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR, |
| 109 "Invalid client response received."); | 107 "Invalid client response received."); |
| 110 return; | 108 return; |
| 111 } | 109 } |
| 112 | 110 |
| 113 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_RESPONSE, | 111 SendMessageWithPayload(SecurityKeyMessageType::REQUEST_RESPONSE, |
| 114 response_data); | 112 response_data); |
| 115 } | 113 } |
| 116 | 114 |
| 117 void RemoteSecurityKeyMessageHandler::HandleConnectRequest( | 115 void SecurityKeyMessageHandler::HandleConnectRequest( |
| 118 const std::string& message_payload) { | 116 const std::string& message_payload) { |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 if (!message_payload.empty()) { | 118 if (!message_payload.empty()) { |
| 121 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_ERROR, | 119 SendMessageWithPayload(SecurityKeyMessageType::CONNECT_ERROR, |
| 122 "Unexpected payload data received."); | 120 "Unexpected payload data received."); |
| 123 return; | 121 return; |
| 124 } | 122 } |
| 125 | 123 |
| 126 if (ipc_client_->WaitForSecurityKeyIpcServerChannel()) { | 124 if (ipc_client_->WaitForSecurityKeyIpcServerChannel()) { |
| 127 // If we find an IPC server, then attempt to establish a connection. | 125 // If we find an IPC server, then attempt to establish a connection. |
| 128 ipc_client_->EstablishIpcConnection( | 126 ipc_client_->EstablishIpcConnection( |
| 129 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange, | 127 base::Bind(&SecurityKeyMessageHandler::HandleIpcConnectionChange, |
| 130 base::Unretained(this), true), | 128 base::Unretained(this), true), |
| 131 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange, | 129 base::Bind(&SecurityKeyMessageHandler::HandleIpcConnectionChange, |
| 132 base::Unretained(this), false)); | 130 base::Unretained(this), false)); |
| 133 } else { | 131 } else { |
| 134 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, | 132 SendMessageWithPayload(SecurityKeyMessageType::CONNECT_RESPONSE, |
| 135 std::string(1, kConnectResponseNoSession)); | 133 std::string(1, kConnectResponseNoSession)); |
| 136 } | 134 } |
| 137 } | 135 } |
| 138 | 136 |
| 139 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyRequest( | 137 void SecurityKeyMessageHandler::HandleSecurityKeyRequest( |
| 140 const std::string& message_payload) { | 138 const std::string& message_payload) { |
| 141 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
| 142 if (message_payload.empty()) { | 140 if (message_payload.empty()) { |
| 143 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | 141 SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR, |
| 144 "Request sent without request data."); | 142 "Request sent without request data."); |
| 145 return; | 143 return; |
| 146 } | 144 } |
| 147 | 145 |
| 148 if (!ipc_client_->SendSecurityKeyRequest( | 146 if (!ipc_client_->SendSecurityKeyRequest( |
| 149 message_payload, | 147 message_payload, |
| 150 base::Bind( | 148 base::Bind(&SecurityKeyMessageHandler::HandleSecurityKeyResponse, |
| 151 &RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse, | 149 base::Unretained(this)))) { |
| 152 base::Unretained(this)))) { | 150 SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR, |
| 153 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 154 "Failed to send request data."); | 151 "Failed to send request data."); |
| 155 } | 152 } |
| 156 } | 153 } |
| 157 | 154 |
| 158 void RemoteSecurityKeyMessageHandler::SendMessage( | 155 void SecurityKeyMessageHandler::SendMessage( |
| 159 RemoteSecurityKeyMessageType message_type) { | 156 SecurityKeyMessageType message_type) { |
| 160 if (!writer_->WriteMessage(message_type)) { | 157 if (!writer_->WriteMessage(message_type)) { |
| 161 OnError(); | 158 OnError(); |
| 162 } | 159 } |
| 163 } | 160 } |
| 164 | 161 |
| 165 void RemoteSecurityKeyMessageHandler::SendMessageWithPayload( | 162 void SecurityKeyMessageHandler::SendMessageWithPayload( |
| 166 RemoteSecurityKeyMessageType message_type, | 163 SecurityKeyMessageType message_type, |
| 167 const std::string& message_payload) { | 164 const std::string& message_payload) { |
| 168 if (!writer_->WriteMessageWithPayload(message_type, message_payload)) { | 165 if (!writer_->WriteMessageWithPayload(message_type, message_payload)) { |
| 169 OnError(); | 166 OnError(); |
| 170 } | 167 } |
| 171 } | 168 } |
| 172 | 169 |
| 173 void RemoteSecurityKeyMessageHandler::OnError() { | 170 void SecurityKeyMessageHandler::OnError() { |
| 174 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 175 ipc_client_.reset(); | 172 ipc_client_.reset(); |
| 176 writer_.reset(); | 173 writer_.reset(); |
| 177 reader_.reset(); | 174 reader_.reset(); |
| 178 | 175 |
| 179 if (!error_callback_.is_null()) { | 176 if (!error_callback_.is_null()) { |
| 180 base::ResetAndReturn(&error_callback_).Run(); | 177 base::ResetAndReturn(&error_callback_).Run(); |
| 181 } | 178 } |
| 182 } | 179 } |
| 183 | 180 |
| 184 } // namespace remoting | 181 } // namespace remoting |
| OLD | NEW |