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