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