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