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