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

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

Issue 2478443002: Use ChannelMojo for remote security key channels. (Closed)
Patch Set: Created 4 years, 1 month 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
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::CheckForSecurityKeyIpcServerChannel() {
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_);
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 } 36 }
50 37 return channel_handle_.is_valid();
51 return false;
52 } 38 }
53 39
54 void SecurityKeyIpcClient::EstablishIpcConnection( 40 void SecurityKeyIpcClient::EstablishIpcConnection(
55 const base::Closure& connection_ready_callback, 41 const base::Closure& connection_ready_callback,
56 const base::Closure& connection_error_callback) { 42 const base::Closure& connection_error_callback) {
57 DCHECK(thread_checker_.CalledOnValidThread()); 43 DCHECK(thread_checker_.CalledOnValidThread());
58 DCHECK(!connection_ready_callback.is_null()); 44 DCHECK(!connection_ready_callback.is_null());
59 DCHECK(!connection_error_callback.is_null()); 45 DCHECK(!connection_error_callback.is_null());
60 DCHECK(!ipc_channel_); 46 DCHECK(!ipc_channel_);
61 47
62 connection_ready_callback_ = connection_ready_callback; 48 connection_ready_callback_ = connection_ready_callback;
63 connection_error_callback_ = connection_error_callback; 49 connection_error_callback_ = connection_error_callback;
64 50
65 ConnectToIpcChannel(initial_ipc_channel_name_); 51 ConnectToIpcChannel();
66 } 52 }
67 53
68 bool SecurityKeyIpcClient::SendSecurityKeyRequest( 54 bool SecurityKeyIpcClient::SendSecurityKeyRequest(
69 const std::string& request_payload, 55 const std::string& request_payload,
70 const ResponseCallback& response_callback) { 56 const ResponseCallback& response_callback) {
71 DCHECK(thread_checker_.CalledOnValidThread()); 57 DCHECK(thread_checker_.CalledOnValidThread());
72 DCHECK(!request_payload.empty()); 58 DCHECK(!request_payload.empty());
73 DCHECK(!response_callback.is_null()); 59 DCHECK(!response_callback.is_null());
74 60
75 if (!ipc_channel_) { 61 if (!ipc_channel_) {
(...skipping 10 matching lines...) Expand all
86 response_callback_ = response_callback; 72 response_callback_ = response_callback;
87 return ipc_channel_->Send( 73 return ipc_channel_->Send(
88 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); 74 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload));
89 } 75 }
90 76
91 void SecurityKeyIpcClient::CloseIpcConnection() { 77 void SecurityKeyIpcClient::CloseIpcConnection() {
92 DCHECK(thread_checker_.CalledOnValidThread()); 78 DCHECK(thread_checker_.CalledOnValidThread());
93 ipc_channel_.reset(); 79 ipc_channel_.reset();
94 } 80 }
95 81
96 void SecurityKeyIpcClient::SetInitialIpcChannelNameForTest( 82 void SecurityKeyIpcClient::SetIpcChannelHandleForTest(
97 const std::string& initial_ipc_channel_name) { 83 const mojo::edk::NamedPlatformHandle& channel_handle) {
98 initial_ipc_channel_name_ = initial_ipc_channel_name; 84 named_channel_handle_ = channel_handle;
99 } 85 }
100 86
101 void SecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest( 87 void SecurityKeyIpcClient::SetExpectedIpcServerSessionIdForTest(
102 uint32_t expected_session_id) { 88 uint32_t expected_session_id) {
103 expected_ipc_server_session_id_ = expected_session_id; 89 expected_ipc_server_session_id_ = expected_session_id;
104 } 90 }
105 91
106 bool SecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) { 92 bool SecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) {
107 DCHECK(thread_checker_.CalledOnValidThread()); 93 DCHECK(thread_checker_.CalledOnValidThread());
108 94
109 bool handled = true; 95 bool handled = true;
110 IPC_BEGIN_MESSAGE_MAP(SecurityKeyIpcClient, message) 96 IPC_BEGIN_MESSAGE_MAP(SecurityKeyIpcClient, message)
111 IPC_MESSAGE_HANDLER(
112 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails,
113 OnConnectionDetails)
114 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, 97 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response,
115 OnSecurityKeyResponse) 98 OnSecurityKeyResponse)
116 IPC_MESSAGE_UNHANDLED(handled = false) 99 IPC_MESSAGE_UNHANDLED(handled = false)
117 IPC_END_MESSAGE_MAP() 100 IPC_END_MESSAGE_MAP()
118 101
119 CHECK(handled) << "Received unexpected IPC type: " << message.type(); 102 CHECK(handled) << "Received unexpected IPC type: " << message.type();
120 return handled; 103 return handled;
121 } 104 }
122 105
123 void SecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { 106 void SecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) {
124 DCHECK(thread_checker_.CalledOnValidThread()); 107 DCHECK(thread_checker_.CalledOnValidThread());
125 108
126 #if defined(OS_WIN) 109 #if defined(OS_WIN)
127 DWORD peer_session_id; 110 DWORD peer_session_id;
128 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { 111 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) {
129 uint32_t last_error = GetLastError(); 112 PLOG(ERROR) << "ProcessIdToSessionId failed";
130 LOG(ERROR) << "ProcessIdToSessionId failed with error code: " << last_error;
131 base::ResetAndReturn(&connection_error_callback_).Run(); 113 base::ResetAndReturn(&connection_error_callback_).Run();
132 return; 114 return;
133 } 115 }
134 116
135 if (peer_session_id != expected_ipc_server_session_id_) { 117 if (peer_session_id != expected_ipc_server_session_id_) {
136 LOG(ERROR) 118 LOG(ERROR)
137 << "Cannot establish connection with IPC server running in session: " 119 << "Cannot establish connection with IPC server running in session: "
138 << peer_session_id; 120 << peer_session_id;
139 base::ResetAndReturn(&connection_error_callback_).Run(); 121 base::ResetAndReturn(&connection_error_callback_).Run();
140 return; 122 return;
141 } 123 }
142 #endif // defined(OS_WIN) 124 #endif // defined(OS_WIN)
143 125
144 // If we have received the connection details already (i.e. 126 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 } 127 }
152 128
153 void SecurityKeyIpcClient::OnChannelError() { 129 void SecurityKeyIpcClient::OnChannelError() {
154 DCHECK(thread_checker_.CalledOnValidThread()); 130 DCHECK(thread_checker_.CalledOnValidThread());
155 131
156 if (!connection_error_callback_.is_null()) { 132 if (!connection_error_callback_.is_null()) {
157 base::ResetAndReturn(&connection_error_callback_).Run(); 133 base::ResetAndReturn(&connection_error_callback_).Run();
158 } 134 }
159 } 135 }
160 136
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( 137 void SecurityKeyIpcClient::OnSecurityKeyResponse(
179 const std::string& response_data) { 138 const std::string& response_data) {
180 DCHECK(thread_checker_.CalledOnValidThread()); 139 DCHECK(thread_checker_.CalledOnValidThread());
181 DCHECK(!connection_error_callback_.is_null()); 140 DCHECK(!connection_error_callback_.is_null());
182 141
183 if (!response_data.empty()) { 142 if (!response_data.empty()) {
184 base::ResetAndReturn(&response_callback_).Run(response_data); 143 base::ResetAndReturn(&response_callback_).Run(response_data);
185 } else { 144 } else {
186 LOG(ERROR) << "Invalid response received"; 145 LOG(ERROR) << "Invalid response received";
187 base::ResetAndReturn(&connection_error_callback_).Run(); 146 base::ResetAndReturn(&connection_error_callback_).Run();
188 } 147 }
189 } 148 }
190 149
191 void SecurityKeyIpcClient::ConnectToIpcChannel( 150 void SecurityKeyIpcClient::ConnectToIpcChannel() {
192 const std::string& channel_name) {
193 DCHECK(thread_checker_.CalledOnValidThread()); 151 DCHECK(thread_checker_.CalledOnValidThread());
194 152
195 // Verify that any existing IPC connection has been closed. 153 // Verify that any existing IPC connection has been closed.
196 CloseIpcConnection(); 154 CloseIpcConnection();
197 155
198 // The retry loop is needed as the IPC Servers we connect to are reset (torn 156 if (!channel_handle_.is_valid() && !CheckForSecurityKeyIpcServerChannel()) {
199 // down and recreated) and we should be resilient in that case. 157 if (!connection_error_callback_.is_null()) {
200 const base::TimeDelta kTotalWaitTime = 158 base::ResetAndReturn(&connection_error_callback_).Run();
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 } 159 }
160 return;
161 }
211 162
212 ipc_channel_.reset(); 163 ipc_channel_ = IPC::Channel::CreateClient(
213 base::PlatformThread::Sleep(kPerIterationWaitTime); 164 mojo::edk::ConnectToPeerProcess(std::move(channel_handle_)).release(),
165 this);
166 if (ipc_channel_->Connect()) {
167 return;
214 } 168 }
169 ipc_channel_.reset();
215 170
216 if (!connection_error_callback_.is_null()) { 171 if (!connection_error_callback_.is_null()) {
217 base::ResetAndReturn(&connection_error_callback_).Run(); 172 base::ResetAndReturn(&connection_error_callback_).Run();
218 } 173 }
219 } 174 }
220 175
221 } // namespace remoting 176 } // 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