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

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

Issue 2170563002: Revert of Renaming Gnubby and RemoteSecurityKey files/classes/members (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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/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/security_key_ipc_constants.h"
20
21 namespace remoting {
22
23 SecurityKeyIpcClient::SecurityKeyIpcClient()
24 : initial_ipc_channel_name_(remoting::GetSecurityKeyIpcChannelName()),
25 weak_factory_(this) {}
26
27 SecurityKeyIpcClient::~SecurityKeyIpcClient() {}
28
29 bool SecurityKeyIpcClient::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 SecurityKeyIpcClient::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 SecurityKeyIpcClient::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 SecurityKeyIpcClient::CloseIpcConnection() {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 ipc_channel_.reset();
94 }
95
96 void SecurityKeyIpcClient::SetInitialIpcChannelNameForTest(
97 const std::string& initial_ipc_channel_name) {
98 initial_ipc_channel_name_ = initial_ipc_channel_name;
99 }
100
101 void SecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest(
102 uint32_t expected_session_id) {
103 expected_ipc_server_session_id_ = expected_session_id;
104 }
105
106 bool SecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) {
107 DCHECK(thread_checker_.CalledOnValidThread());
108
109 bool handled = true;
110 IPC_BEGIN_MESSAGE_MAP(SecurityKeyIpcClient, message)
111 IPC_MESSAGE_HANDLER(
112 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails,
113 OnConnectionDetails)
114 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response,
115 OnSecurityKeyResponse)
116 IPC_MESSAGE_UNHANDLED(handled = false)
117 IPC_END_MESSAGE_MAP()
118
119 CHECK(handled) << "Received unexpected IPC type: " << message.type();
120 return handled;
121 }
122
123 void SecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) {
124 DCHECK(thread_checker_.CalledOnValidThread());
125
126 #if defined(OS_WIN)
127 DWORD peer_session_id;
128 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) {
129 uint32_t last_error = GetLastError();
130 LOG(ERROR) << "ProcessIdToSessionId failed with error code: " << last_error;
131 base::ResetAndReturn(&connection_error_callback_).Run();
132 return;
133 }
134
135 if (peer_session_id != expected_ipc_server_session_id_) {
136 LOG(ERROR)
137 << "Cannot establish connection with IPC server running in session: "
138 << peer_session_id;
139 base::ResetAndReturn(&connection_error_callback_).Run();
140 return;
141 }
142 #endif // defined(OS_WIN)
143
144 // If we have received the connection details already (i.e.
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 }
152
153 void SecurityKeyIpcClient::OnChannelError() {
154 DCHECK(thread_checker_.CalledOnValidThread());
155
156 if (!connection_error_callback_.is_null()) {
157 base::ResetAndReturn(&connection_error_callback_).Run();
158 }
159 }
160
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(
179 const std::string& response_data) {
180 DCHECK(thread_checker_.CalledOnValidThread());
181 DCHECK(!connection_error_callback_.is_null());
182
183 if (!response_data.empty()) {
184 base::ResetAndReturn(&response_callback_).Run(response_data);
185 } else {
186 LOG(ERROR) << "Invalid response received";
187 base::ResetAndReturn(&connection_error_callback_).Run();
188 }
189 }
190
191 void SecurityKeyIpcClient::ConnectToIpcChannel(
192 const std::string& channel_name) {
193 DCHECK(thread_checker_.CalledOnValidThread());
194
195 // Verify that any existing IPC connection has been closed.
196 CloseIpcConnection();
197
198 // The retry loop is needed as the IPC Servers we connect to are reset (torn
199 // down and recreated) and we should be resilient in that case.
200 const base::TimeDelta kTotalWaitTime =
201 base::TimeDelta::FromMilliseconds(1000);
202 const base::TimeDelta kPerIterationWaitTime =
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
212 ipc_channel_.reset();
213 base::PlatformThread::Sleep(kPerIterationWaitTime);
214 }
215
216 if (!connection_error_callback_.is_null()) {
217 base::ResetAndReturn(&connection_error_callback_).Run();
218 }
219 }
220
221 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/security_key/security_key_ipc_client.h ('k') | remoting/host/security_key/security_key_ipc_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698