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/remote_security_key_ipc_client.h" | 5 #include "remoting/host/security_key/security_key_ipc_client.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "ipc/ipc_channel.h" | 13 #include "ipc/ipc_channel.h" |
14 #include "ipc/ipc_listener.h" | 14 #include "ipc/ipc_listener.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 #include "remoting/host/ipc_constants.h" | 18 #include "remoting/host/ipc_constants.h" |
19 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 19 #include "remoting/host/security_key/security_key_ipc_constants.h" |
20 | 20 |
21 namespace remoting { | 21 namespace remoting { |
22 | 22 |
23 RemoteSecurityKeyIpcClient::RemoteSecurityKeyIpcClient() | 23 SecurityKeyIpcClient::SecurityKeyIpcClient() |
24 : initial_ipc_channel_name_(remoting::GetRemoteSecurityKeyIpcChannelName()), | 24 : initial_ipc_channel_name_(remoting::GetSecurityKeyIpcChannelName()), |
25 weak_factory_(this) {} | 25 weak_factory_(this) {} |
26 | 26 |
27 RemoteSecurityKeyIpcClient::~RemoteSecurityKeyIpcClient() {} | 27 SecurityKeyIpcClient::~SecurityKeyIpcClient() {} |
28 | 28 |
29 bool RemoteSecurityKeyIpcClient::WaitForSecurityKeyIpcServerChannel() { | 29 bool SecurityKeyIpcClient::WaitForSecurityKeyIpcServerChannel() { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
31 | 31 |
32 // The retry loop is needed as the IPC Servers we connect to are reset (torn | 32 // The retry loop is needed as the IPC Servers we connect to are reset (torn |
33 // down and recreated) and we should be resilient in that case. We need to | 33 // down and recreated) and we should be resilient in that case. We need to |
34 // strike a balance between resilience and speed as we do not want to add | 34 // strike a balance between resilience and speed as we do not want to add |
35 // un-necessary delay to the local scenario when no session is active. | 35 // un-necessary delay to the local scenario when no session is active. |
36 // 500ms was chosen as a reasonable balance between reliability of remote | 36 // 500ms was chosen as a reasonable balance between reliability of remote |
37 // session detection and overhead added to the local security key operation | 37 // session detection and overhead added to the local security key operation |
38 // when no remote session is present. | 38 // when no remote session is present. |
39 const base::TimeDelta kTotalWaitTime = base::TimeDelta::FromMilliseconds(500); | 39 const base::TimeDelta kTotalWaitTime = base::TimeDelta::FromMilliseconds(500); |
40 const base::TimeDelta kPerIterationWaitTime = | 40 const base::TimeDelta kPerIterationWaitTime = |
41 base::TimeDelta::FromMilliseconds(10); | 41 base::TimeDelta::FromMilliseconds(10); |
42 const int kLoopIterations = kTotalWaitTime / kPerIterationWaitTime; | 42 const int kLoopIterations = kTotalWaitTime / kPerIterationWaitTime; |
43 for (int i = 0; i < kLoopIterations; i++) { | 43 for (int i = 0; i < kLoopIterations; i++) { |
44 if (IPC::Channel::IsNamedServerInitialized(initial_ipc_channel_name_)) { | 44 if (IPC::Channel::IsNamedServerInitialized(initial_ipc_channel_name_)) { |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 base::PlatformThread::Sleep(kPerIterationWaitTime); | 48 base::PlatformThread::Sleep(kPerIterationWaitTime); |
49 } | 49 } |
50 | 50 |
51 return false; | 51 return false; |
52 } | 52 } |
53 | 53 |
54 void RemoteSecurityKeyIpcClient::EstablishIpcConnection( | 54 void SecurityKeyIpcClient::EstablishIpcConnection( |
55 const base::Closure& connection_ready_callback, | 55 const base::Closure& connection_ready_callback, |
56 const base::Closure& connection_error_callback) { | 56 const base::Closure& connection_error_callback) { |
57 DCHECK(thread_checker_.CalledOnValidThread()); | 57 DCHECK(thread_checker_.CalledOnValidThread()); |
58 DCHECK(!connection_ready_callback.is_null()); | 58 DCHECK(!connection_ready_callback.is_null()); |
59 DCHECK(!connection_error_callback.is_null()); | 59 DCHECK(!connection_error_callback.is_null()); |
60 DCHECK(!ipc_channel_); | 60 DCHECK(!ipc_channel_); |
61 | 61 |
62 connection_ready_callback_ = connection_ready_callback; | 62 connection_ready_callback_ = connection_ready_callback; |
63 connection_error_callback_ = connection_error_callback; | 63 connection_error_callback_ = connection_error_callback; |
64 | 64 |
65 ConnectToIpcChannel(initial_ipc_channel_name_); | 65 ConnectToIpcChannel(initial_ipc_channel_name_); |
66 } | 66 } |
67 | 67 |
68 bool RemoteSecurityKeyIpcClient::SendSecurityKeyRequest( | 68 bool SecurityKeyIpcClient::SendSecurityKeyRequest( |
69 const std::string& request_payload, | 69 const std::string& request_payload, |
70 const ResponseCallback& response_callback) { | 70 const ResponseCallback& response_callback) { |
71 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
72 DCHECK(!request_payload.empty()); | 72 DCHECK(!request_payload.empty()); |
73 DCHECK(!response_callback.is_null()); | 73 DCHECK(!response_callback.is_null()); |
74 | 74 |
75 if (!ipc_channel_) { | 75 if (!ipc_channel_) { |
76 LOG(ERROR) << "Request made before IPC connection was established."; | 76 LOG(ERROR) << "Request made before IPC connection was established."; |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 if (!response_callback_.is_null()) { | 80 if (!response_callback_.is_null()) { |
81 LOG(ERROR) | 81 LOG(ERROR) |
82 << "Request made while waiting for a response to a previous request."; | 82 << "Request made while waiting for a response to a previous request."; |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 response_callback_ = response_callback; | 86 response_callback_ = response_callback; |
87 return ipc_channel_->Send( | 87 return ipc_channel_->Send( |
88 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); | 88 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); |
89 } | 89 } |
90 | 90 |
91 void RemoteSecurityKeyIpcClient::CloseIpcConnection() { | 91 void SecurityKeyIpcClient::CloseIpcConnection() { |
92 DCHECK(thread_checker_.CalledOnValidThread()); | 92 DCHECK(thread_checker_.CalledOnValidThread()); |
93 ipc_channel_.reset(); | 93 ipc_channel_.reset(); |
94 } | 94 } |
95 | 95 |
96 void RemoteSecurityKeyIpcClient::SetInitialIpcChannelNameForTest( | 96 void SecurityKeyIpcClient::SetInitialIpcChannelNameForTest( |
97 const std::string& initial_ipc_channel_name) { | 97 const std::string& initial_ipc_channel_name) { |
98 initial_ipc_channel_name_ = initial_ipc_channel_name; | 98 initial_ipc_channel_name_ = initial_ipc_channel_name; |
99 } | 99 } |
100 | 100 |
101 void RemoteSecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest( | 101 void SecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest( |
102 uint32_t expected_session_id) { | 102 uint32_t expected_session_id) { |
103 expected_ipc_server_session_id_ = expected_session_id; | 103 expected_ipc_server_session_id_ = expected_session_id; |
104 } | 104 } |
105 | 105 |
106 bool RemoteSecurityKeyIpcClient::OnMessageReceived( | 106 bool SecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) { |
107 const IPC::Message& message) { | |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 107 DCHECK(thread_checker_.CalledOnValidThread()); |
109 | 108 |
110 bool handled = true; | 109 bool handled = true; |
111 IPC_BEGIN_MESSAGE_MAP(RemoteSecurityKeyIpcClient, message) | 110 IPC_BEGIN_MESSAGE_MAP(SecurityKeyIpcClient, message) |
112 IPC_MESSAGE_HANDLER( | 111 IPC_MESSAGE_HANDLER( |
113 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails, | 112 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails, |
114 OnConnectionDetails) | 113 OnConnectionDetails) |
115 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, | 114 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, |
116 OnSecurityKeyResponse) | 115 OnSecurityKeyResponse) |
117 IPC_MESSAGE_UNHANDLED(handled = false) | 116 IPC_MESSAGE_UNHANDLED(handled = false) |
118 IPC_END_MESSAGE_MAP() | 117 IPC_END_MESSAGE_MAP() |
119 | 118 |
120 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | 119 CHECK(handled) << "Received unexpected IPC type: " << message.type(); |
121 return handled; | 120 return handled; |
122 } | 121 } |
123 | 122 |
124 void RemoteSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { | 123 void SecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { |
125 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
126 | 125 |
127 #if defined(OS_WIN) | 126 #if defined(OS_WIN) |
128 DWORD peer_session_id; | 127 DWORD peer_session_id; |
129 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { | 128 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { |
130 uint32_t last_error = GetLastError(); | 129 uint32_t last_error = GetLastError(); |
131 LOG(ERROR) << "ProcessIdToSessionId failed with error code: " << last_error; | 130 LOG(ERROR) << "ProcessIdToSessionId failed with error code: " << last_error; |
132 base::ResetAndReturn(&connection_error_callback_).Run(); | 131 base::ResetAndReturn(&connection_error_callback_).Run(); |
133 return; | 132 return; |
134 } | 133 } |
135 | 134 |
136 if (peer_session_id != expected_ipc_server_session_id_) { | 135 if (peer_session_id != expected_ipc_server_session_id_) { |
137 LOG(ERROR) | 136 LOG(ERROR) |
138 << "Cannot establish connection with IPC server running in session: " | 137 << "Cannot establish connection with IPC server running in session: " |
139 << peer_session_id; | 138 << peer_session_id; |
140 base::ResetAndReturn(&connection_error_callback_).Run(); | 139 base::ResetAndReturn(&connection_error_callback_).Run(); |
141 return; | 140 return; |
142 } | 141 } |
143 #endif // defined(OS_WIN) | 142 #endif // defined(OS_WIN) |
144 | 143 |
145 // If we have received the connection details already (i.e. | 144 // If we have received the connection details already (i.e. |
146 // |ipc_channel_name_| is populated) then we signal that the connection is | 145 // |ipc_channel_name_| is populated) then we signal that the connection is |
147 // ready for use. Otherwise this is the initial connection and we will wait | 146 // ready for use. Otherwise this is the initial connection and we will wait |
148 // to receive the ConnectionDetails message before proceeding. | 147 // to receive the ConnectionDetails message before proceeding. |
149 if (!ipc_channel_name_.empty()) { | 148 if (!ipc_channel_name_.empty()) { |
150 base::ResetAndReturn(&connection_ready_callback_).Run(); | 149 base::ResetAndReturn(&connection_ready_callback_).Run(); |
151 } | 150 } |
152 } | 151 } |
153 | 152 |
154 void RemoteSecurityKeyIpcClient::OnChannelError() { | 153 void SecurityKeyIpcClient::OnChannelError() { |
155 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
156 | 155 |
157 if (!connection_error_callback_.is_null()) { | 156 if (!connection_error_callback_.is_null()) { |
158 base::ResetAndReturn(&connection_error_callback_).Run(); | 157 base::ResetAndReturn(&connection_error_callback_).Run(); |
159 } | 158 } |
160 } | 159 } |
161 | 160 |
162 void RemoteSecurityKeyIpcClient::OnConnectionDetails( | 161 void SecurityKeyIpcClient::OnConnectionDetails( |
163 const std::string& channel_name) { | 162 const std::string& channel_name) { |
164 DCHECK(thread_checker_.CalledOnValidThread()); | 163 DCHECK(thread_checker_.CalledOnValidThread()); |
165 ipc_channel_name_ = channel_name; | 164 ipc_channel_name_ = channel_name; |
166 | 165 |
167 // Now that we have received the name for the IPC channel we will use for our | 166 // Now that we have received the name for the IPC channel we will use for our |
168 // security key request, we want to disconnect from the intial IPC channel | 167 // security key request, we want to disconnect from the intial IPC channel |
169 // and then connect to the new one. | 168 // and then connect to the new one. |
170 // NOTE: We do not want to perform these tasks now as we are in the middle of | 169 // NOTE: We do not want to perform these tasks now as we are in the middle of |
171 // existing IPC message handler, thus we post the tasks so they will be | 170 // existing IPC message handler, thus we post the tasks so they will be |
172 // handled after this method completes. | 171 // handled after this method completes. |
173 base::ThreadTaskRunnerHandle::Get()->PostTask( | 172 base::ThreadTaskRunnerHandle::Get()->PostTask( |
174 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcClient::ConnectToIpcChannel, | 173 FROM_HERE, base::Bind(&SecurityKeyIpcClient::ConnectToIpcChannel, |
175 weak_factory_.GetWeakPtr(), | 174 weak_factory_.GetWeakPtr(), |
176 base::ConstRef(ipc_channel_name_))); | 175 base::ConstRef(ipc_channel_name_))); |
177 } | 176 } |
178 | 177 |
179 void RemoteSecurityKeyIpcClient::OnSecurityKeyResponse( | 178 void SecurityKeyIpcClient::OnSecurityKeyResponse( |
180 const std::string& response_data) { | 179 const std::string& response_data) { |
181 DCHECK(thread_checker_.CalledOnValidThread()); | 180 DCHECK(thread_checker_.CalledOnValidThread()); |
182 DCHECK(!connection_error_callback_.is_null()); | 181 DCHECK(!connection_error_callback_.is_null()); |
183 | 182 |
184 if (!response_data.empty()) { | 183 if (!response_data.empty()) { |
185 base::ResetAndReturn(&response_callback_).Run(response_data); | 184 base::ResetAndReturn(&response_callback_).Run(response_data); |
186 } else { | 185 } else { |
187 LOG(ERROR) << "Invalid response received"; | 186 LOG(ERROR) << "Invalid response received"; |
188 base::ResetAndReturn(&connection_error_callback_).Run(); | 187 base::ResetAndReturn(&connection_error_callback_).Run(); |
189 } | 188 } |
190 } | 189 } |
191 | 190 |
192 void RemoteSecurityKeyIpcClient::ConnectToIpcChannel( | 191 void SecurityKeyIpcClient::ConnectToIpcChannel( |
193 const std::string& channel_name) { | 192 const std::string& channel_name) { |
194 DCHECK(thread_checker_.CalledOnValidThread()); | 193 DCHECK(thread_checker_.CalledOnValidThread()); |
195 | 194 |
196 // Verify that any existing IPC connection has been closed. | 195 // Verify that any existing IPC connection has been closed. |
197 CloseIpcConnection(); | 196 CloseIpcConnection(); |
198 | 197 |
199 // The retry loop is needed as the IPC Servers we connect to are reset (torn | 198 // The retry loop is needed as the IPC Servers we connect to are reset (torn |
200 // down and recreated) and we should be resilient in that case. | 199 // down and recreated) and we should be resilient in that case. |
201 const base::TimeDelta kTotalWaitTime = | 200 const base::TimeDelta kTotalWaitTime = |
202 base::TimeDelta::FromMilliseconds(1000); | 201 base::TimeDelta::FromMilliseconds(1000); |
(...skipping 10 matching lines...) Expand all Loading... |
213 ipc_channel_.reset(); | 212 ipc_channel_.reset(); |
214 base::PlatformThread::Sleep(kPerIterationWaitTime); | 213 base::PlatformThread::Sleep(kPerIterationWaitTime); |
215 } | 214 } |
216 | 215 |
217 if (!connection_error_callback_.is_null()) { | 216 if (!connection_error_callback_.is_null()) { |
218 base::ResetAndReturn(&connection_error_callback_).Run(); | 217 base::ResetAndReturn(&connection_error_callback_).Run(); |
219 } | 218 } |
220 } | 219 } |
221 | 220 |
222 } // namespace remoting | 221 } // namespace remoting |
OLD | NEW |