OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/test/chromoting_instance.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "jingle/glue/thread_wrapper.h" | |
13 #include "net/base/request_priority.h" | |
14 #include "net/socket/client_socket_factory.h" | |
15 #include "remoting/base/url_request_context_getter.h" | |
16 #include "remoting/client/audio_player.h" | |
17 #include "remoting/client/chromoting_client.h" | |
18 #include "remoting/client/client_context.h" | |
19 #include "remoting/client/token_fetcher_proxy.h" | |
20 #include "remoting/protocol/authentication_method.h" | |
21 #include "remoting/protocol/chromium_port_allocator.h" | |
22 #include "remoting/protocol/host_stub.h" | |
23 #include "remoting/protocol/libjingle_transport_factory.h" | |
24 #include "remoting/protocol/negotiating_client_authenticator.h" | |
25 #include "remoting/protocol/network_settings.h" | |
26 #include "remoting/protocol/session_config.h" | |
27 #include "remoting/protocol/third_party_client_authenticator.h" | |
28 #include "remoting/signaling/xmpp_signal_strategy.h" | |
29 #include "remoting/test/remote_host_info_fetcher.h" | |
30 #include "remoting/test/video_renderer.h" | |
31 | |
32 namespace { | |
33 const char kRemotingCapabilities[] = | |
Sergey Ulanov
2015/03/09 06:44:40
nit: empty line above this one
joedow
2015/03/09 21:09:25
Done.
| |
34 "sendInitialResolution " | |
35 "rateLimitResizeRequests " | |
36 "googleDrive " | |
37 "videoRecorder " | |
38 "casting"; | |
39 | |
40 const char kXmppHostName[] = "talk.google.com"; | |
41 const int kXmppPortNumber = 5222; | |
42 | |
43 const char kConnectionStateInitializing[] = "INITIALIZING"; | |
Sergey Ulanov
2015/03/09 06:44:41
These consts are used only to convert enums to str
joedow
2015/03/09 21:09:25
Done.
| |
44 const char kConnectionStateConnecting[] = "CONNECTING"; | |
45 const char kConnectionStateAuthenticated[] = "AUTHENTICATED"; | |
46 const char kConnectionStateConnected[] = "CONNECTED"; | |
47 const char kConnectionStateClosed[] = "CLOSED"; | |
48 const char kConnectionStateFailed[] = "FAILED"; | |
49 const char kConnectionStateUnknown[] = "UNKNOWN"; | |
50 | |
51 const char kProtocolErrorCodeOK[] = "NONE"; | |
52 const char kProtocolErrorCodePeerIsOffline[] = "PEER_IS_OFFLINE"; | |
53 const char kProtocolErrorCodeSessionRejected[] = "SESSION_REJECTED"; | |
54 const char kProtocolErrorCodeAuthFailed[] = "AUTHENTICATION_FAILED"; | |
55 const char kProtocolErrorCodeIncompatibleProtocol[] = "INCOMPATIBLE_PROTOCOL"; | |
56 const char kProtocolErrorCodeHostOverload[] = "HOST_OVERLOAD"; | |
57 const char kProtocolErrorCodeChannelConnection[] = "CHANNEL_CONNECTION_ERROR"; | |
58 const char kProtocolErrorCodeSignalingError[] = "SIGNALING_ERROR"; | |
59 const char kProtocolErrorCodeSignalingTimeout[] = "SIGNALING_TIMEOUT"; | |
60 const char kProtocolErrorCodeUnknown[] = "UNKNOWN_ERROR"; | |
61 } // namespace | |
62 | |
63 namespace remoting { | |
64 namespace test { | |
65 | |
66 ChromotingInstance::ChromotingInstance() : | |
67 connection_to_host_state_(protocol::ConnectionToHost::INITIALIZING), | |
68 connection_error_code_(protocol::OK) {} | |
Sergey Ulanov
2015/03/09 06:44:41
Move } to a separate line. I believe clang-format
joedow
2015/03/09 21:09:25
Done.
| |
69 | |
70 ChromotingInstance::~ChromotingInstance() { | |
71 // Since we are being destroyed, all observers should have unregistered | |
72 // themselves already, but we will clear the list here just in case. | |
Sergey Ulanov
2015/03/09 06:44:40
DCHECK that the observer list is empty? Actually O
joedow
2015/03/09 21:09:25
Done.
| |
73 connection_observers_.Clear(); | |
74 | |
75 // Ensure any connections are closed and the members are destroyed in the | |
76 // appropriate order. | |
77 EndConnection(); | |
78 } | |
79 | |
80 void ChromotingInstance::StartConnection( | |
81 const std::string& user_name, | |
82 const std::string& access_token, | |
83 const RemoteHostInfo& remote_host_info) { | |
84 DCHECK(!user_name.empty()); | |
85 DCHECK(!access_token.empty()); | |
86 DCHECK(remote_host_info.IsReadyForConnection()); | |
87 // A valid message loop is required to create a connection. | |
Sergey Ulanov
2015/03/09 06:44:40
nit: I don't think this check is really useful. An
joedow
2015/03/09 21:09:25
Done.
| |
88 DCHECK(base::MessageLoop::current()); | |
89 | |
90 // Required to establish a connection to the host. | |
91 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | |
92 | |
93 scoped_refptr<remoting::URLRequestContextGetter> request_context_getter; | |
Sergey Ulanov
2015/03/09 06:44:41
here and everywhere else remove remoting::
joedow
2015/03/09 21:09:25
Done.
| |
94 request_context_getter = new remoting::URLRequestContextGetter( | |
95 base::ThreadTaskRunnerHandle::Get(), // network_runner | |
96 base::ThreadTaskRunnerHandle::Get()); // file_runner | |
97 | |
98 client_context_.reset( | |
99 new remoting::ClientContext(base::ThreadTaskRunnerHandle::Get())); | |
100 | |
101 video_renderer_.reset(new remoting::test::VideoRenderer()); | |
102 | |
103 chromoting_client_.reset(new remoting::ChromotingClient( | |
104 client_context_.get(), | |
105 this, // ClientUserInterface* client_user_interface. | |
106 video_renderer_.get(), | |
107 scoped_ptr<AudioPlayer>())); | |
108 | |
109 if (test_connection_to_host_) { | |
110 chromoting_client_->SetConnectionToHostForTests( | |
111 test_connection_to_host_.Pass()); | |
112 } | |
113 | |
114 XmppSignalStrategy::XmppServerConfig xmpp_server_config; | |
115 xmpp_server_config.host = kXmppHostName; | |
116 xmpp_server_config.port = kXmppPortNumber; | |
117 xmpp_server_config.use_tls = true; | |
118 xmpp_server_config.username = user_name; | |
119 xmpp_server_config.auth_token = access_token; | |
120 | |
121 // Set up the signal strategy. This must outlive the client object. | |
122 signal_strategy_.reset(new remoting::XmppSignalStrategy( | |
123 net::ClientSocketFactory::GetDefaultFactory(), | |
124 request_context_getter, | |
125 xmpp_server_config)); | |
126 | |
127 protocol::NetworkSettings network_settings( | |
128 protocol::NetworkSettings::NAT_TRAVERSAL_FULL); | |
129 | |
130 scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( | |
131 protocol::ChromiumPortAllocator::Create( | |
132 request_context_getter, | |
133 network_settings)); | |
Sergey Ulanov
2015/03/09 06:44:40
nit: these arguments can fit on one line. Please u
joedow
2015/03/09 21:09:25
Done.
| |
134 | |
135 scoped_ptr<protocol::TransportFactory> transport_factory( | |
136 new protocol::LibjingleTransportFactory( | |
137 signal_strategy_.get(), | |
138 port_allocator.Pass(), | |
139 network_settings)); | |
140 | |
141 scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> | |
142 token_fetcher(new remoting::TokenFetcherProxy( | |
143 base::Bind(&ChromotingInstance::FetchThirdPartyToken, | |
144 remote_host_info.authorization_code, | |
145 remote_host_info.shared_secret), | |
146 "FAKE_HOST_PUBLIC_KEY")); | |
147 | |
148 fetch_secret_callback_ = base::Bind( | |
149 &ChromotingInstance::FetchSecretFromString, | |
150 remote_host_info.shared_secret); | |
151 | |
152 std::vector<protocol::AuthenticationMethod> auth_methods; | |
153 auth_methods.push_back(protocol::AuthenticationMethod::ThirdParty()); | |
154 | |
155 scoped_ptr<protocol::Authenticator> authenticator( | |
156 new protocol::NegotiatingClientAuthenticator( | |
157 std::string(), // const std::string& client_pairing_id | |
Sergey Ulanov
2015/03/09 06:44:41
don't need to specify type in the comment.
joedow
2015/03/09 21:09:25
Done.
| |
158 std::string(), // const std::string& shared_secret | |
159 std::string(), // const std::string& authentication_tag | |
160 fetch_secret_callback_, | |
161 token_fetcher.Pass(), | |
162 auth_methods)); | |
163 | |
164 chromoting_client_->Start( | |
165 signal_strategy_.get(), | |
166 authenticator.Pass(), | |
167 transport_factory.Pass(), | |
168 remote_host_info.host_jid, | |
169 kRemotingCapabilities); | |
170 } | |
171 | |
172 void ChromotingInstance::EndConnection() { | |
173 // Clearing out the client will close the connection. | |
174 chromoting_client_.reset(); | |
175 | |
176 // The signal strategy object must outlive the client so destroy it next. | |
177 signal_strategy_.reset(); | |
178 | |
179 // The connection state should been updated when the chromoting client was | |
180 // destroyed however we will verify and update the connection state if not. | |
181 if (connection_to_host_state_ != protocol::ConnectionToHost::CLOSED && | |
182 connection_to_host_state_ != protocol::ConnectionToHost::FAILED && | |
183 connection_error_code_ == protocol::OK) { | |
184 OnConnectionState(protocol::ConnectionToHost::CLOSED, protocol::OK); | |
185 } | |
186 } | |
187 | |
188 void ChromotingInstance::AddRemoteConnectionObserver( | |
189 RemoteConnectionObserver* observer) { | |
190 DCHECK(observer); | |
191 DCHECK(!connection_observers_.HasObserver(observer)); | |
Sergey Ulanov
2015/03/09 06:44:40
Don't need this. ObserverList checks for it.
I wo
joedow
2015/03/09 21:09:25
Done.
| |
192 | |
193 connection_observers_.AddObserver(observer); | |
194 } | |
195 | |
196 void ChromotingInstance::RemoveRemoteConnectionObserver( | |
197 RemoteConnectionObserver* observer) { | |
198 DCHECK(observer); | |
199 DCHECK(connection_observers_.HasObserver(observer)); | |
Sergey Ulanov
2015/03/09 06:44:40
Don't need this DCHECK.
joedow
2015/03/09 21:09:25
Done.
| |
200 | |
201 connection_observers_.RemoveObserver(observer); | |
202 } | |
203 | |
204 void ChromotingInstance::SetConnectionToHostForTests( | |
205 scoped_ptr<protocol::ConnectionToHost> connection_to_host) { | |
206 test_connection_to_host_ = connection_to_host.Pass(); | |
207 } | |
208 | |
209 void ChromotingInstance::OnConnectionState( | |
210 protocol::ConnectionToHost::State state, | |
211 protocol::ErrorCode error_code) { | |
212 DVLOG(1) << "ChromotingInstance::OnConnectionState() Called"; | |
Sergey Ulanov
2015/03/09 06:44:40
does this need to be DVLOG, given that this is tes
joedow
2015/03/09 21:09:25
I've been using DVLOG in my other test objects. I
| |
213 DVLOG(2) << "--State: " << ConnectionStateToFriendlyString(state); | |
214 DVLOG(2) << "--ErrorCode: " << ProtocolErrorToFriendlyString(error_code); | |
215 | |
216 connection_error_code_ = error_code; | |
217 connection_to_host_state_ = state; | |
218 | |
219 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
220 connection_observers_, | |
221 ConnectionStateChanged(state, error_code)); | |
222 | |
223 if (connection_error_code_ != protocol::OK || | |
224 connection_to_host_state_ == protocol::ConnectionToHost::CLOSED) { | |
225 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
226 connection_observers_, | |
227 DisconnectedFromRemoteHost()); | |
228 } | |
229 } | |
230 | |
231 void ChromotingInstance::OnConnectionReady(bool ready) { | |
232 DVLOG(1) << "ChromotingInstance::OnConnectionReady() Called"; | |
233 DVLOG(2) << "--ready:" << ready; | |
234 | |
235 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
236 connection_observers_, | |
237 ConnectionReady(ready)); | |
238 | |
239 if (ready) { | |
240 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
241 connection_observers_, | |
242 ConnectedToRemoteHost()); | |
243 } | |
244 } | |
245 | |
246 void ChromotingInstance::OnRouteChanged( | |
247 const std::string& channel_name, | |
248 const protocol::TransportRoute& route) { | |
249 DVLOG(1) << "ChromotingInstance::OnRouteChanged() Called"; | |
250 DVLOG(2) << "--channel_name:" << channel_name; | |
251 DVLOG(2) << "--route:" << protocol::TransportRoute::GetTypeString(route.type); | |
252 | |
253 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
254 connection_observers_, | |
255 RouteChanged(channel_name, route)); | |
256 } | |
257 | |
258 void ChromotingInstance::SetCapabilities(const std::string& capabilities) { | |
259 DVLOG(1) << "ChromotingInstance::SetCapabilities() Called"; | |
260 DVLOG(2) << "--Capabilities: " << capabilities; | |
261 | |
262 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
263 connection_observers_, | |
264 CapabilitiesSet(capabilities)); | |
265 } | |
266 | |
267 void ChromotingInstance::SetPairingResponse( | |
268 const protocol::PairingResponse& pairing_response) { | |
269 DVLOG(1) << "ChromotingInstance::SetPairingResponse() Called"; | |
270 DVLOG(2) << "--client_id: " << pairing_response.client_id(); | |
271 DVLOG(2) << "--shared_secret: " << pairing_response.shared_secret(); | |
272 | |
273 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
274 connection_observers_, | |
275 PairingResponseSet(pairing_response)); | |
276 } | |
277 | |
278 void ChromotingInstance::DeliverHostMessage( | |
279 const protocol::ExtensionMessage& message) { | |
280 DVLOG(1) << "ChromotingInstance::DeliverHostMessage() called"; | |
281 DVLOG(2) << "--type: " << message.type(); | |
282 DVLOG(2) << "--data: " << message.data(); | |
283 | |
284 FOR_EACH_OBSERVER(RemoteConnectionObserver, | |
285 connection_observers_, | |
286 HostMessageReceived(message)); | |
287 } | |
288 | |
289 protocol::ClipboardStub* ChromotingInstance::GetClipboardStub() { | |
290 DVLOG(1) << "ChromotingInstance::GetClipboardStub() Called"; | |
291 return this; | |
292 } | |
293 | |
294 protocol::CursorShapeStub* ChromotingInstance::GetCursorShapeStub() { | |
295 DVLOG(1) << "ChromotingInstance::GetCursorShapeStub() Called"; | |
296 return this; | |
297 } | |
298 | |
299 void ChromotingInstance::InjectClipboardEvent( | |
300 const protocol::ClipboardEvent& event) { | |
301 DVLOG(1) << "ChromotingInstance::InjectClipboardEvent() Called"; | |
302 } | |
303 | |
304 void ChromotingInstance::SetCursorShape( | |
305 const protocol::CursorShapeInfo& cursor_shape) { | |
306 DVLOG(1) << "ChromotingInstance::SetCursorShape() Called"; | |
307 } | |
308 | |
309 void ChromotingInstance::FetchSecretFromString( | |
310 const std::string& shared_secret, | |
311 bool pairing_supported, | |
312 const protocol::SecretFetchedCallback& secret_fetched_callback) { | |
313 DVLOG(1) << "ChromotingInstance::FetchSecretFromString Called"; | |
314 secret_fetched_callback.Run(shared_secret); | |
315 } | |
316 | |
317 void ChromotingInstance::FetchThirdPartyToken( | |
318 const std::string& authorization_token, | |
319 const std::string& shared_secret, | |
320 const GURL& token_url, | |
321 const std::string& host_public_key, | |
322 const std::string& scope, | |
323 base::WeakPtr<remoting::TokenFetcherProxy> token_fetcher_proxy) { | |
324 DVLOG(1) << "ChromotingInstance::FetchThirdPartyToken Called"; | |
325 DVLOG(2) << "--token_url: " << token_url; | |
326 DVLOG(2) << "--host_public_key: " << host_public_key; | |
327 DVLOG(2) << "--scope: " << scope; | |
328 | |
329 if (token_fetcher_proxy) { | |
330 token_fetcher_proxy->OnTokenFetched(authorization_token, shared_secret); | |
331 token_fetcher_proxy.reset(); | |
332 } else { | |
333 LOG(ERROR) << "Invalid token fetcher proxy passed in"; | |
334 } | |
335 } | |
336 | |
337 const char* ChromotingInstance::ConnectionStateToFriendlyString( | |
338 protocol::ConnectionToHost::State state) { | |
339 switch (state) { | |
340 case protocol::ConnectionToHost::INITIALIZING: | |
341 return kConnectionStateInitializing; | |
342 | |
343 case protocol::ConnectionToHost::CONNECTING: | |
344 return kConnectionStateConnecting; | |
345 | |
346 case protocol::ConnectionToHost::AUTHENTICATED: | |
347 return kConnectionStateAuthenticated; | |
348 | |
349 case protocol::ConnectionToHost::CONNECTED: | |
350 return kConnectionStateConnected; | |
351 | |
352 case protocol::ConnectionToHost::CLOSED: | |
353 return kConnectionStateClosed; | |
354 | |
355 case protocol::ConnectionToHost::FAILED: | |
356 return kConnectionStateFailed; | |
357 | |
358 default: | |
359 LOG(ERROR) << "Unknown connection state: '" << state << "'"; | |
360 return kConnectionStateUnknown; | |
361 } | |
362 } | |
363 | |
364 const char* ChromotingInstance::ProtocolErrorToFriendlyString( | |
365 protocol::ErrorCode error_code) { | |
366 switch (error_code) { | |
367 case protocol::OK: | |
368 return kProtocolErrorCodeOK; | |
369 | |
370 case protocol::PEER_IS_OFFLINE: | |
371 return kProtocolErrorCodePeerIsOffline; | |
372 | |
373 case protocol::SESSION_REJECTED: | |
374 return kProtocolErrorCodeSessionRejected; | |
375 | |
376 case protocol::AUTHENTICATION_FAILED: | |
377 return kProtocolErrorCodeAuthFailed; | |
378 | |
379 case protocol::INCOMPATIBLE_PROTOCOL: | |
380 return kProtocolErrorCodeIncompatibleProtocol; | |
381 | |
382 case protocol::HOST_OVERLOAD: | |
383 return kProtocolErrorCodeHostOverload; | |
384 | |
385 case protocol::CHANNEL_CONNECTION_ERROR: | |
386 return kProtocolErrorCodeChannelConnection; | |
387 | |
388 case protocol::SIGNALING_ERROR: | |
389 return kProtocolErrorCodeSignalingError; | |
390 | |
391 case protocol::SIGNALING_TIMEOUT: | |
392 return kProtocolErrorCodeSignalingTimeout; | |
393 | |
394 case protocol::UNKNOWN_ERROR: | |
395 return kProtocolErrorCodeUnknown; | |
396 | |
397 default: | |
398 LOG(ERROR) << "Unrecognized error code: '" << error_code << "'"; | |
399 return kProtocolErrorCodeUnknown; | |
400 } | |
401 } | |
402 | |
403 } // namespace test | |
404 } // namespace remoting | |
OLD | NEW |