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