Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: remoting/host/security_key/remote_security_key_ipc_client.cc

Issue 2162083003: Renaming Gnubby and RemoteSecurityKey files/classes/members (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing a GYP build error Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/remote_security_key_ipc_client.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/callback_helpers.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "ipc/ipc_channel.h"
14 #include "ipc/ipc_listener.h"
15 #include "ipc/ipc_message.h"
16 #include "ipc/ipc_message_macros.h"
17 #include "remoting/host/chromoting_messages.h"
18 #include "remoting/host/ipc_constants.h"
19 #include "remoting/host/security_key/remote_security_key_ipc_constants.h"
20
21 namespace remoting {
22
23 RemoteSecurityKeyIpcClient::RemoteSecurityKeyIpcClient()
24 : initial_ipc_channel_name_(remoting::GetRemoteSecurityKeyIpcChannelName()),
25 weak_factory_(this) {}
26
27 RemoteSecurityKeyIpcClient::~RemoteSecurityKeyIpcClient() {}
28
29 bool RemoteSecurityKeyIpcClient::WaitForSecurityKeyIpcServerChannel() {
30 DCHECK(thread_checker_.CalledOnValidThread());
31
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
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.
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 }
53
54 void RemoteSecurityKeyIpcClient::EstablishIpcConnection(
55 const base::Closure& connection_ready_callback,
56 const base::Closure& connection_error_callback) {
57 DCHECK(thread_checker_.CalledOnValidThread());
58 DCHECK(!connection_ready_callback.is_null());
59 DCHECK(!connection_error_callback.is_null());
60 DCHECK(!ipc_channel_);
61
62 connection_ready_callback_ = connection_ready_callback;
63 connection_error_callback_ = connection_error_callback;
64
65 ConnectToIpcChannel(initial_ipc_channel_name_);
66 }
67
68 bool RemoteSecurityKeyIpcClient::SendSecurityKeyRequest(
69 const std::string& request_payload,
70 const ResponseCallback& response_callback) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 DCHECK(!request_payload.empty());
73 DCHECK(!response_callback.is_null());
74
75 if (!ipc_channel_) {
76 LOG(ERROR) << "Request made before IPC connection was established.";
77 return false;
78 }
79
80 if (!response_callback_.is_null()) {
81 LOG(ERROR)
82 << "Request made while waiting for a response to a previous request.";
83 return false;
84 }
85
86 response_callback_ = response_callback;
87 return ipc_channel_->Send(
88 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload));
89 }
90
91 void RemoteSecurityKeyIpcClient::CloseIpcConnection() {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 ipc_channel_.reset();
94 }
95
96 void RemoteSecurityKeyIpcClient::SetInitialIpcChannelNameForTest(
97 const std::string& initial_ipc_channel_name) {
98 initial_ipc_channel_name_ = initial_ipc_channel_name;
99 }
100
101 void RemoteSecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest(
102 uint32_t expected_session_id) {
103 expected_ipc_server_session_id_ = expected_session_id;
104 }
105
106 bool RemoteSecurityKeyIpcClient::OnMessageReceived(
107 const IPC::Message& message) {
108 DCHECK(thread_checker_.CalledOnValidThread());
109
110 bool handled = true;
111 IPC_BEGIN_MESSAGE_MAP(RemoteSecurityKeyIpcClient, message)
112 IPC_MESSAGE_HANDLER(
113 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails,
114 OnConnectionDetails)
115 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response,
116 OnSecurityKeyResponse)
117 IPC_MESSAGE_UNHANDLED(handled = false)
118 IPC_END_MESSAGE_MAP()
119
120 CHECK(handled) << "Received unexpected IPC type: " << message.type();
121 return handled;
122 }
123
124 void RemoteSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) {
125 DCHECK(thread_checker_.CalledOnValidThread());
126
127 #if defined(OS_WIN)
128 DWORD peer_session_id;
129 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) {
130 uint32_t last_error = GetLastError();
131 LOG(ERROR) << "ProcessIdToSessionId failed with error code: " << last_error;
132 base::ResetAndReturn(&connection_error_callback_).Run();
133 return;
134 }
135
136 if (peer_session_id != expected_ipc_server_session_id_) {
137 LOG(ERROR)
138 << "Cannot establish connection with IPC server running in session: "
139 << peer_session_id;
140 base::ResetAndReturn(&connection_error_callback_).Run();
141 return;
142 }
143 #endif // defined(OS_WIN)
144
145 // If we have received the connection details already (i.e.
146 // |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
148 // to receive the ConnectionDetails message before proceeding.
149 if (!ipc_channel_name_.empty()) {
150 base::ResetAndReturn(&connection_ready_callback_).Run();
151 }
152 }
153
154 void RemoteSecurityKeyIpcClient::OnChannelError() {
155 DCHECK(thread_checker_.CalledOnValidThread());
156
157 if (!connection_error_callback_.is_null()) {
158 base::ResetAndReturn(&connection_error_callback_).Run();
159 }
160 }
161
162 void RemoteSecurityKeyIpcClient::OnConnectionDetails(
163 const std::string& channel_name) {
164 DCHECK(thread_checker_.CalledOnValidThread());
165 ipc_channel_name_ = channel_name;
166
167 // 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
169 // 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
171 // existing IPC message handler, thus we post the tasks so they will be
172 // handled after this method completes.
173 base::ThreadTaskRunnerHandle::Get()->PostTask(
174 FROM_HERE, base::Bind(&RemoteSecurityKeyIpcClient::ConnectToIpcChannel,
175 weak_factory_.GetWeakPtr(),
176 base::ConstRef(ipc_channel_name_)));
177 }
178
179 void RemoteSecurityKeyIpcClient::OnSecurityKeyResponse(
180 const std::string& response_data) {
181 DCHECK(thread_checker_.CalledOnValidThread());
182 DCHECK(!connection_error_callback_.is_null());
183
184 if (!response_data.empty()) {
185 base::ResetAndReturn(&response_callback_).Run(response_data);
186 } else {
187 LOG(ERROR) << "Invalid response received";
188 base::ResetAndReturn(&connection_error_callback_).Run();
189 }
190 }
191
192 void RemoteSecurityKeyIpcClient::ConnectToIpcChannel(
193 const std::string& channel_name) {
194 DCHECK(thread_checker_.CalledOnValidThread());
195
196 // Verify that any existing IPC connection has been closed.
197 CloseIpcConnection();
198
199 // 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.
201 const base::TimeDelta kTotalWaitTime =
202 base::TimeDelta::FromMilliseconds(1000);
203 const base::TimeDelta kPerIterationWaitTime =
204 base::TimeDelta::FromMilliseconds(25);
205 const int kLoopIterations = kTotalWaitTime / kPerIterationWaitTime;
206 IPC::ChannelHandle channel_handle(channel_name);
207 for (int i = 0; i < kLoopIterations; i++) {
208 ipc_channel_ = IPC::Channel::CreateNamedClient(channel_handle, this);
209 if (ipc_channel_->Connect()) {
210 return;
211 }
212
213 ipc_channel_.reset();
214 base::PlatformThread::Sleep(kPerIterationWaitTime);
215 }
216
217 if (!connection_error_callback_.is_null()) {
218 base::ResetAndReturn(&connection_error_callback_).Run();
219 }
220 }
221
222 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698