Chromium Code Reviews| 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/remote_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/remote_security_key_ipc_client.h" | |
| 15 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | |
| 16 #include "remoting/host/security_key/remote_security_key_message_reader_impl.h" | |
| 17 #include "remoting/host/security_key/remote_security_key_message_writer_impl.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 base::File DuplicatePlatformFile(base::File file) { | |
|
Sergey Ulanov
2016/04/05 18:57:40
Do you really need this function? There is base::F
joedow
2016/04/05 21:29:30
Done.
| |
| 22 base::PlatformFile result; | |
| 23 #if defined(OS_WIN) | |
| 24 if (!DuplicateHandle(GetCurrentProcess(), file.TakePlatformFile(), | |
| 25 GetCurrentProcess(), &result, 0, FALSE, | |
| 26 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { | |
| 27 PLOG(ERROR) << "Failed to duplicate handle " << file.GetPlatformFile(); | |
| 28 return base::File(); | |
| 29 } | |
| 30 return base::File(result); | |
| 31 #elif defined(OS_POSIX) | |
| 32 result = dup(file.GetPlatformFile()); | |
| 33 return base::File(result); | |
| 34 #else | |
| 35 #error Not implemented. | |
| 36 #endif | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 namespace remoting { | |
| 42 | |
| 43 RemoteSecurityKeyMessageHandler::RemoteSecurityKeyMessageHandler() {} | |
| 44 | |
| 45 RemoteSecurityKeyMessageHandler::~RemoteSecurityKeyMessageHandler() {} | |
| 46 | |
| 47 void RemoteSecurityKeyMessageHandler::Start( | |
| 48 base::File message_read_stream, | |
| 49 base::File message_write_stream, | |
| 50 scoped_ptr<RemoteSecurityKeyIpcClient> ipc_client, | |
| 51 const base::Closure& shutdown_closure) { | |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 53 DCHECK(message_read_stream.IsValid()); | |
| 54 DCHECK(message_write_stream.IsValid()); | |
| 55 DCHECK(ipc_client); | |
| 56 DCHECK(!shutdown_closure.is_null()); | |
| 57 DCHECK(shutdown_closure_.is_null()); | |
| 58 | |
| 59 if (!reader_) { | |
| 60 reader_.reset(new RemoteSecurityKeyMessageReaderImpl( | |
| 61 DuplicatePlatformFile(std::move(message_read_stream)))); | |
| 62 } | |
| 63 | |
| 64 if (!writer_) { | |
| 65 writer_.reset(new RemoteSecurityKeyMessageWriterImpl( | |
| 66 DuplicatePlatformFile(std::move(message_write_stream)))); | |
| 67 } | |
| 68 | |
| 69 ipc_client_ = std::move(ipc_client); | |
| 70 shutdown_closure_ = shutdown_closure; | |
| 71 | |
| 72 reader_->Start( | |
| 73 base::Bind( | |
| 74 &RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage, | |
| 75 base::Unretained(this)), | |
| 76 base::Bind(&RemoteSecurityKeyMessageHandler::Shutdown, | |
| 77 base::Unretained(this))); | |
| 78 } | |
| 79 | |
| 80 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageReaderForTest( | |
| 81 scoped_ptr<RemoteSecurityKeyMessageReader> reader) { | |
| 82 DCHECK(!reader_); | |
| 83 reader_ = std::move(reader); | |
| 84 } | |
| 85 | |
| 86 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageWriterForTest( | |
| 87 scoped_ptr<RemoteSecurityKeyMessageWriter> writer) { | |
| 88 DCHECK(!writer_); | |
| 89 writer_ = std::move(writer); | |
| 90 } | |
| 91 | |
| 92 void RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage( | |
| 93 scoped_ptr<SecurityKeyMessage> message) { | |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 95 | |
| 96 RemoteSecurityKeyMessageType message_type = message->type(); | |
| 97 if (message_type == RemoteSecurityKeyMessageType::CONNECT) { | |
| 98 HandleConnectRequest(message->payload()); | |
| 99 } else if (message_type == RemoteSecurityKeyMessageType::REQUEST) { | |
| 100 HandleSecurityKeyRequest(message->payload()); | |
| 101 } else { | |
| 102 LOG(ERROR) << "Unknown message type: " | |
| 103 << static_cast<uint8_t>(message_type); | |
| 104 SendMessage(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange( | |
| 109 bool connection_established) { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 if (connection_established) { | |
| 112 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, "1"); | |
| 113 } else { | |
| 114 SendMessageWithPayload( | |
| 115 RemoteSecurityKeyMessageType::CONNECT_ERROR, | |
| 116 "Unknown error occurred while establishing connection."); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse( | |
| 121 const std::string& response_data) { | |
| 122 if (response_data.compare(kRemoteSecurityKeyConnectionError) == 0) { | |
| 123 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 124 "An error occurred during the request."); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 if (response_data.empty()) { | |
| 129 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 130 "Invalid client response received."); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_RESPONSE, | |
| 135 response_data); | |
| 136 } | |
| 137 | |
| 138 void RemoteSecurityKeyMessageHandler::HandleConnectRequest( | |
| 139 const std::string& message_payload) { | |
| 140 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 141 if (!message_payload.empty()) { | |
| 142 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_ERROR, | |
| 143 "Unexpected payload data received."); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 if (ipc_client_->WaitForSecurityKeyIpcServerChannel()) { | |
| 148 // If we find an IPC server, then attempt to establish a connection. | |
| 149 ipc_client_->EstablishIpcConnection( | |
| 150 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange, | |
| 151 base::Unretained(this), true), | |
| 152 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange, | |
| 153 base::Unretained(this), false)); | |
| 154 } else { | |
| 155 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, "0"); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyRequest( | |
| 160 const std::string& message_payload) { | |
| 161 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 162 if (message_payload.empty()) { | |
| 163 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 164 "Request sent without request data."); | |
| 165 return; | |
| 166 } | |
| 167 | |
| 168 if (!ipc_client_->SendSecurityKeyRequest( | |
| 169 message_payload, | |
| 170 base::Bind( | |
| 171 &RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse, | |
| 172 base::Unretained(this)))) { | |
| 173 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 174 "Failed to send request data."); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 void RemoteSecurityKeyMessageHandler::SendMessage( | |
| 179 RemoteSecurityKeyMessageType message_type) { | |
| 180 if (!writer_->WriteMessage(message_type)) { | |
| 181 Shutdown(); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void RemoteSecurityKeyMessageHandler::SendMessageWithPayload( | |
| 186 RemoteSecurityKeyMessageType message_type, | |
| 187 const std::string& message_payload) { | |
| 188 if (!writer_->WriteMessageWithPayload(message_type, message_payload)) { | |
| 189 Shutdown(); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 void RemoteSecurityKeyMessageHandler::Shutdown() { | |
|
Sergey Ulanov
2016/04/05 18:57:40
Maybe call this OnError() to make it clear it's us
joedow
2016/04/05 21:29:30
Done.
| |
| 194 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 195 ipc_client_.reset(); | |
| 196 writer_.reset(); | |
| 197 reader_.reset(); | |
| 198 | |
| 199 if (!shutdown_closure_.is_null()) { | |
| 200 base::ResetAndReturn(&shutdown_closure_).Run(); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 } // namespace remoting | |
| OLD | NEW |