| 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/fake_remote_security_key_ipc_client.h" | 5 #include "remoting/host/security_key/fake_security_key_ipc_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
| 16 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
| 17 #include "remoting/host/chromoting_messages.h" | 17 #include "remoting/host/chromoting_messages.h" |
| 18 | 18 |
| 19 namespace remoting { | 19 namespace remoting { |
| 20 | 20 |
| 21 FakeRemoteSecurityKeyIpcClient::FakeRemoteSecurityKeyIpcClient( | 21 FakeSecurityKeyIpcClient::FakeSecurityKeyIpcClient( |
| 22 const base::Closure& channel_event_callback) | 22 const base::Closure& channel_event_callback) |
| 23 : channel_event_callback_(channel_event_callback), weak_factory_(this) { | 23 : channel_event_callback_(channel_event_callback), weak_factory_(this) { |
| 24 DCHECK(!channel_event_callback_.is_null()); | 24 DCHECK(!channel_event_callback_.is_null()); |
| 25 } | 25 } |
| 26 | 26 |
| 27 FakeRemoteSecurityKeyIpcClient::~FakeRemoteSecurityKeyIpcClient() {} | 27 FakeSecurityKeyIpcClient::~FakeSecurityKeyIpcClient() {} |
| 28 | 28 |
| 29 base::WeakPtr<FakeRemoteSecurityKeyIpcClient> | 29 base::WeakPtr<FakeSecurityKeyIpcClient> FakeSecurityKeyIpcClient::AsWeakPtr() { |
| 30 FakeRemoteSecurityKeyIpcClient::AsWeakPtr() { | |
| 31 return weak_factory_.GetWeakPtr(); | 30 return weak_factory_.GetWeakPtr(); |
| 32 } | 31 } |
| 33 | 32 |
| 34 bool FakeRemoteSecurityKeyIpcClient::WaitForSecurityKeyIpcServerChannel() { | 33 bool FakeSecurityKeyIpcClient::WaitForSecurityKeyIpcServerChannel() { |
| 35 return wait_for_ipc_channel_return_value_; | 34 return wait_for_ipc_channel_return_value_; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void FakeRemoteSecurityKeyIpcClient::EstablishIpcConnection( | 37 void FakeSecurityKeyIpcClient::EstablishIpcConnection( |
| 39 const base::Closure& connection_ready_callback, | 38 const base::Closure& connection_ready_callback, |
| 40 const base::Closure& connection_error_callback) { | 39 const base::Closure& connection_error_callback) { |
| 41 if (establish_ipc_connection_should_succeed_) { | 40 if (establish_ipc_connection_should_succeed_) { |
| 42 connection_ready_callback.Run(); | 41 connection_ready_callback.Run(); |
| 43 } else { | 42 } else { |
| 44 connection_error_callback.Run(); | 43 connection_error_callback.Run(); |
| 45 } | 44 } |
| 46 } | 45 } |
| 47 | 46 |
| 48 bool FakeRemoteSecurityKeyIpcClient::SendSecurityKeyRequest( | 47 bool FakeSecurityKeyIpcClient::SendSecurityKeyRequest( |
| 49 const std::string& request_payload, | 48 const std::string& request_payload, |
| 50 const ResponseCallback& response_callback) { | 49 const ResponseCallback& response_callback) { |
| 51 if (send_security_request_should_succeed_) { | 50 if (send_security_request_should_succeed_) { |
| 52 base::ThreadTaskRunnerHandle::Get()->PostTask( | 51 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 53 FROM_HERE, | 52 FROM_HERE, |
| 54 base::Bind(response_callback, security_key_response_payload_)); | 53 base::Bind(response_callback, security_key_response_payload_)); |
| 55 } | 54 } |
| 56 | 55 |
| 57 return send_security_request_should_succeed_; | 56 return send_security_request_should_succeed_; |
| 58 } | 57 } |
| 59 | 58 |
| 60 void FakeRemoteSecurityKeyIpcClient::CloseIpcConnection() { | 59 void FakeSecurityKeyIpcClient::CloseIpcConnection() { |
| 61 client_channel_.reset(); | 60 client_channel_.reset(); |
| 62 channel_event_callback_.Run(); | 61 channel_event_callback_.Run(); |
| 63 } | 62 } |
| 64 | 63 |
| 65 bool FakeRemoteSecurityKeyIpcClient::ConnectViaIpc( | 64 bool FakeSecurityKeyIpcClient::ConnectViaIpc(const std::string& channel_name) { |
| 66 const std::string& channel_name) { | |
| 67 // The retry loop is needed as the IPC Servers we connect to are reset (torn | 65 // The retry loop is needed as the IPC Servers we connect to are reset (torn |
| 68 // down and recreated) in some tests and we should be resilient in that case. | 66 // down and recreated) in some tests and we should be resilient in that case. |
| 69 IPC::ChannelHandle channel_handle(channel_name); | 67 IPC::ChannelHandle channel_handle(channel_name); |
| 70 for (int i = 0; i < 5; i++) { | 68 for (int i = 0; i < 5; i++) { |
| 71 client_channel_ = IPC::Channel::CreateNamedClient(channel_handle, this); | 69 client_channel_ = IPC::Channel::CreateNamedClient(channel_handle, this); |
| 72 if (client_channel_->Connect()) { | 70 if (client_channel_->Connect()) { |
| 73 return true; | 71 return true; |
| 74 } | 72 } |
| 75 | 73 |
| 76 base::RunLoop run_loop; | 74 base::RunLoop run_loop; |
| 77 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 78 FROM_HERE, run_loop.QuitClosure(), | 76 FROM_HERE, run_loop.QuitClosure(), |
| 79 base::TimeDelta::FromMilliseconds(100)); | 77 base::TimeDelta::FromMilliseconds(100)); |
| 80 run_loop.Run(); | 78 run_loop.Run(); |
| 81 } | 79 } |
| 82 | 80 |
| 83 return false; | 81 return false; |
| 84 } | 82 } |
| 85 | 83 |
| 86 void FakeRemoteSecurityKeyIpcClient::SendSecurityKeyRequestViaIpc( | 84 void FakeSecurityKeyIpcClient::SendSecurityKeyRequestViaIpc( |
| 87 const std::string& request_payload) { | 85 const std::string& request_payload) { |
| 88 client_channel_->Send( | 86 client_channel_->Send( |
| 89 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); | 87 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); |
| 90 } | 88 } |
| 91 | 89 |
| 92 bool FakeRemoteSecurityKeyIpcClient::OnMessageReceived( | 90 bool FakeSecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) { |
| 93 const IPC::Message& message) { | |
| 94 bool handled = true; | 91 bool handled = true; |
| 95 IPC_BEGIN_MESSAGE_MAP(FakeRemoteSecurityKeyIpcClient, message) | 92 IPC_BEGIN_MESSAGE_MAP(FakeSecurityKeyIpcClient, message) |
| 96 IPC_MESSAGE_HANDLER( | 93 IPC_MESSAGE_HANDLER( |
| 97 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails, | 94 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails, |
| 98 OnConnectionDetails) | 95 OnConnectionDetails) |
| 99 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, | 96 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, |
| 100 OnSecurityKeyResponse) | 97 OnSecurityKeyResponse) |
| 101 IPC_MESSAGE_UNHANDLED(handled = false) | 98 IPC_MESSAGE_UNHANDLED(handled = false) |
| 102 IPC_END_MESSAGE_MAP() | 99 IPC_END_MESSAGE_MAP() |
| 103 | 100 |
| 104 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | 101 CHECK(handled) << "Received unexpected IPC type: " << message.type(); |
| 105 return handled; | 102 return handled; |
| 106 } | 103 } |
| 107 | 104 |
| 108 void FakeRemoteSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { | 105 void FakeSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { |
| 109 ipc_channel_connected_ = true; | 106 ipc_channel_connected_ = true; |
| 110 channel_event_callback_.Run(); | 107 channel_event_callback_.Run(); |
| 111 } | 108 } |
| 112 | 109 |
| 113 void FakeRemoteSecurityKeyIpcClient::OnChannelError() { | 110 void FakeSecurityKeyIpcClient::OnChannelError() { |
| 114 ipc_channel_connected_ = false; | 111 ipc_channel_connected_ = false; |
| 115 channel_event_callback_.Run(); | 112 channel_event_callback_.Run(); |
| 116 } | 113 } |
| 117 | 114 |
| 118 void FakeRemoteSecurityKeyIpcClient::OnConnectionDetails( | 115 void FakeSecurityKeyIpcClient::OnConnectionDetails( |
| 119 const std::string& channel_name) { | 116 const std::string& channel_name) { |
| 120 last_message_received_ = channel_name; | 117 last_message_received_ = channel_name; |
| 121 channel_event_callback_.Run(); | 118 channel_event_callback_.Run(); |
| 122 } | 119 } |
| 123 | 120 |
| 124 void FakeRemoteSecurityKeyIpcClient::OnSecurityKeyResponse( | 121 void FakeSecurityKeyIpcClient::OnSecurityKeyResponse( |
| 125 const std::string& request_data) { | 122 const std::string& request_data) { |
| 126 last_message_received_ = request_data; | 123 last_message_received_ = request_data; |
| 127 channel_event_callback_.Run(); | 124 channel_event_callback_.Run(); |
| 128 } | 125 } |
| 129 | 126 |
| 130 } // namespace remoting | 127 } // namespace remoting |
| OLD | NEW |