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/test_chromoting_client.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/test_video_renderer.h" |
| 31 |
| 32 namespace { |
| 33 |
| 34 const char kXmppHostName[] = "talk.google.com"; |
| 35 const int kXmppPortNumber = 5222; |
| 36 |
| 37 // Used as the TokenFetcherCallback for App Remoting sessions. |
| 38 void FetchThirdPartyToken( |
| 39 const std::string& authorization_token, |
| 40 const std::string& shared_secret, |
| 41 const GURL& token_url, |
| 42 const std::string& host_public_key, |
| 43 const std::string& scope, |
| 44 base::WeakPtr<remoting::TokenFetcherProxy> token_fetcher_proxy) { |
| 45 DVLOG(1) << "FetchThirdPartyToken(" |
| 46 << "token_url: " << token_url << ", " |
| 47 << "host_public_key: " << host_public_key << ", " |
| 48 << "scope: " << scope << ") Called"; |
| 49 |
| 50 if (token_fetcher_proxy) { |
| 51 token_fetcher_proxy->OnTokenFetched(authorization_token, shared_secret); |
| 52 token_fetcher_proxy.reset(); |
| 53 } else { |
| 54 LOG(ERROR) << "Invalid token fetcher proxy passed in"; |
| 55 } |
| 56 } |
| 57 |
| 58 const char* ConnectionStateToFriendlyString( |
| 59 remoting::protocol::ConnectionToHost::State state) { |
| 60 switch (state) { |
| 61 case remoting::protocol::ConnectionToHost::INITIALIZING: |
| 62 return "INITIALIZING"; |
| 63 |
| 64 case remoting::protocol::ConnectionToHost::CONNECTING: |
| 65 return "CONNECTING"; |
| 66 |
| 67 case remoting::protocol::ConnectionToHost::AUTHENTICATED: |
| 68 return "AUTHENTICATED"; |
| 69 |
| 70 case remoting::protocol::ConnectionToHost::CONNECTED: |
| 71 return "CONNECTED"; |
| 72 |
| 73 case remoting::protocol::ConnectionToHost::CLOSED: |
| 74 return "CLOSED"; |
| 75 |
| 76 case remoting::protocol::ConnectionToHost::FAILED: |
| 77 return "FAILED"; |
| 78 |
| 79 default: |
| 80 LOG(ERROR) << "Unknown connection state: '" << state << "'"; |
| 81 return "UNKNOWN"; |
| 82 } |
| 83 } |
| 84 |
| 85 const char* ProtocolErrorToFriendlyString( |
| 86 remoting::protocol::ErrorCode error_code) { |
| 87 switch (error_code) { |
| 88 case remoting::protocol::OK: |
| 89 return "NONE"; |
| 90 |
| 91 case remoting::protocol::PEER_IS_OFFLINE: |
| 92 return "PEER_IS_OFFLINE"; |
| 93 |
| 94 case remoting::protocol::SESSION_REJECTED: |
| 95 return "SESSION_REJECTED"; |
| 96 |
| 97 case remoting::protocol::AUTHENTICATION_FAILED: |
| 98 return "AUTHENTICATION_FAILED"; |
| 99 |
| 100 case remoting::protocol::INCOMPATIBLE_PROTOCOL: |
| 101 return "INCOMPATIBLE_PROTOCOL"; |
| 102 |
| 103 case remoting::protocol::HOST_OVERLOAD: |
| 104 return "HOST_OVERLOAD"; |
| 105 |
| 106 case remoting::protocol::CHANNEL_CONNECTION_ERROR: |
| 107 return "CHANNEL_CONNECTION_ERROR"; |
| 108 |
| 109 case remoting::protocol::SIGNALING_ERROR: |
| 110 return "SIGNALING_ERROR"; |
| 111 |
| 112 case remoting::protocol::SIGNALING_TIMEOUT: |
| 113 return "SIGNALING_TIMEOUT"; |
| 114 |
| 115 case remoting::protocol::UNKNOWN_ERROR: |
| 116 return "UNKNOWN_ERROR"; |
| 117 |
| 118 default: |
| 119 LOG(ERROR) << "Unrecognized error code: '" << error_code << "'"; |
| 120 return "UNKNOWN_ERROR"; |
| 121 } |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 |
| 126 namespace remoting { |
| 127 namespace test { |
| 128 |
| 129 TestChromotingClient::TestChromotingClient() |
| 130 : connection_to_host_state_(protocol::ConnectionToHost::INITIALIZING), |
| 131 connection_error_code_(protocol::OK) { |
| 132 } |
| 133 |
| 134 TestChromotingClient::~TestChromotingClient() { |
| 135 // Ensure any connections are closed and the members are destroyed in the |
| 136 // appropriate order. |
| 137 EndConnection(); |
| 138 } |
| 139 |
| 140 void TestChromotingClient::StartConnection( |
| 141 const std::string& user_name, |
| 142 const std::string& access_token, |
| 143 const RemoteHostInfo& remote_host_info) { |
| 144 DCHECK(!user_name.empty()); |
| 145 DCHECK(!access_token.empty()); |
| 146 DCHECK(remote_host_info.IsReadyForConnection()); |
| 147 |
| 148 // Required to establish a connection to the host. |
| 149 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 150 |
| 151 scoped_refptr<URLRequestContextGetter> request_context_getter; |
| 152 request_context_getter = new URLRequestContextGetter( |
| 153 base::ThreadTaskRunnerHandle::Get(), // network_runner |
| 154 base::ThreadTaskRunnerHandle::Get()); // file_runner |
| 155 |
| 156 client_context_.reset(new ClientContext(base::ThreadTaskRunnerHandle::Get())); |
| 157 |
| 158 video_renderer_.reset(new TestVideoRenderer()); |
| 159 |
| 160 chromoting_client_.reset(new ChromotingClient(client_context_.get(), |
| 161 this, // client_user_interface. |
| 162 video_renderer_.get(), |
| 163 nullptr // audio_player |
| 164 )); |
| 165 |
| 166 if (test_connection_to_host_) { |
| 167 chromoting_client_->SetConnectionToHostForTests( |
| 168 test_connection_to_host_.Pass()); |
| 169 } |
| 170 |
| 171 XmppSignalStrategy::XmppServerConfig xmpp_server_config; |
| 172 xmpp_server_config.host = kXmppHostName; |
| 173 xmpp_server_config.port = kXmppPortNumber; |
| 174 xmpp_server_config.use_tls = true; |
| 175 xmpp_server_config.username = user_name; |
| 176 xmpp_server_config.auth_token = access_token; |
| 177 |
| 178 // Set up the signal strategy. This must outlive the client object. |
| 179 signal_strategy_.reset( |
| 180 new XmppSignalStrategy(net::ClientSocketFactory::GetDefaultFactory(), |
| 181 request_context_getter, xmpp_server_config)); |
| 182 |
| 183 protocol::NetworkSettings network_settings( |
| 184 protocol::NetworkSettings::NAT_TRAVERSAL_FULL); |
| 185 |
| 186 scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( |
| 187 protocol::ChromiumPortAllocator::Create(request_context_getter, |
| 188 network_settings)); |
| 189 |
| 190 scoped_ptr<protocol::TransportFactory> transport_factory( |
| 191 new protocol::LibjingleTransportFactory( |
| 192 signal_strategy_.get(), port_allocator.Pass(), network_settings)); |
| 193 |
| 194 scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> |
| 195 token_fetcher(new TokenFetcherProxy( |
| 196 base::Bind(&FetchThirdPartyToken, remote_host_info.authorization_code, |
| 197 remote_host_info.shared_secret), |
| 198 "FAKE_HOST_PUBLIC_KEY")); |
| 199 |
| 200 std::vector<protocol::AuthenticationMethod> auth_methods; |
| 201 auth_methods.push_back(protocol::AuthenticationMethod::ThirdParty()); |
| 202 |
| 203 // FetchSecretCallback is used for PIN based auth which we aren't using so we |
| 204 // can pass a null callback here. |
| 205 protocol::FetchSecretCallback fetch_secret_callback; |
| 206 scoped_ptr<protocol::Authenticator> authenticator( |
| 207 new protocol::NegotiatingClientAuthenticator( |
| 208 std::string(), // client_pairing_id |
| 209 std::string(), // shared_secret |
| 210 std::string(), // authentication_tag |
| 211 fetch_secret_callback, token_fetcher.Pass(), auth_methods)); |
| 212 |
| 213 chromoting_client_->Start(signal_strategy_.get(), authenticator.Pass(), |
| 214 transport_factory.Pass(), remote_host_info.host_jid, |
| 215 std::string() // capabilities |
| 216 ); |
| 217 } |
| 218 |
| 219 void TestChromotingClient::EndConnection() { |
| 220 // Clearing out the client will close the connection. |
| 221 chromoting_client_.reset(); |
| 222 |
| 223 // The signal strategy object must outlive the client so destroy it next. |
| 224 signal_strategy_.reset(); |
| 225 |
| 226 // The connection state will be updated when the chromoting client was |
| 227 // destroyed if an active connection was established, but not in other cases. |
| 228 // We should be consistent in either case so we will set the state if needed. |
| 229 if (connection_to_host_state_ != protocol::ConnectionToHost::CLOSED && |
| 230 connection_to_host_state_ != protocol::ConnectionToHost::FAILED && |
| 231 connection_error_code_ == protocol::OK) { |
| 232 OnConnectionState(protocol::ConnectionToHost::CLOSED, protocol::OK); |
| 233 } |
| 234 } |
| 235 |
| 236 void TestChromotingClient::AddRemoteConnectionObserver( |
| 237 RemoteConnectionObserver* observer) { |
| 238 DCHECK(observer); |
| 239 |
| 240 connection_observers_.AddObserver(observer); |
| 241 } |
| 242 |
| 243 void TestChromotingClient::RemoveRemoteConnectionObserver( |
| 244 RemoteConnectionObserver* observer) { |
| 245 DCHECK(observer); |
| 246 |
| 247 connection_observers_.RemoveObserver(observer); |
| 248 } |
| 249 |
| 250 void TestChromotingClient::SetConnectionToHostForTests( |
| 251 scoped_ptr<protocol::ConnectionToHost> connection_to_host) { |
| 252 test_connection_to_host_ = connection_to_host.Pass(); |
| 253 } |
| 254 |
| 255 void TestChromotingClient::OnConnectionState( |
| 256 protocol::ConnectionToHost::State state, |
| 257 protocol::ErrorCode error_code) { |
| 258 DVLOG(1) << "TestChromotingClient::OnConnectionState(" |
| 259 << "state: " << ConnectionStateToFriendlyString(state) << ", " |
| 260 << "error_code: " << ProtocolErrorToFriendlyString(error_code) |
| 261 << ") Called"; |
| 262 |
| 263 connection_error_code_ = error_code; |
| 264 connection_to_host_state_ = state; |
| 265 |
| 266 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 267 ConnectionStateChanged(state, error_code)); |
| 268 } |
| 269 |
| 270 void TestChromotingClient::OnConnectionReady(bool ready) { |
| 271 DVLOG(1) << "TestChromotingClient::OnConnectionReady(" |
| 272 << "ready:" << ready << ") Called"; |
| 273 |
| 274 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 275 ConnectionReady(ready)); |
| 276 } |
| 277 |
| 278 void TestChromotingClient::OnRouteChanged( |
| 279 const std::string& channel_name, |
| 280 const protocol::TransportRoute& route) { |
| 281 DVLOG(1) << "TestChromotingClient::OnRouteChanged(" |
| 282 << "channel_name:" << channel_name << ", " |
| 283 << "route:" << protocol::TransportRoute::GetTypeString(route.type) |
| 284 << ") Called"; |
| 285 |
| 286 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 287 RouteChanged(channel_name, route)); |
| 288 } |
| 289 |
| 290 void TestChromotingClient::SetCapabilities(const std::string& capabilities) { |
| 291 DVLOG(1) << "TestChromotingClient::SetCapabilities(" |
| 292 << "capabilities: " << capabilities << ") Called"; |
| 293 |
| 294 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 295 CapabilitiesSet(capabilities)); |
| 296 } |
| 297 |
| 298 void TestChromotingClient::SetPairingResponse( |
| 299 const protocol::PairingResponse& pairing_response) { |
| 300 DVLOG(1) << "TestChromotingClient::SetPairingResponse(" |
| 301 << "client_id: " << pairing_response.client_id() << ", " |
| 302 << "shared_secret: " << pairing_response.shared_secret() |
| 303 << ") Called"; |
| 304 |
| 305 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 306 PairingResponseSet(pairing_response)); |
| 307 } |
| 308 |
| 309 void TestChromotingClient::DeliverHostMessage( |
| 310 const protocol::ExtensionMessage& message) { |
| 311 DVLOG(1) << "TestChromotingClient::DeliverHostMessage(" |
| 312 << "type: " << message.type() << ", " |
| 313 << "data: " << message.data() << ") Called"; |
| 314 |
| 315 FOR_EACH_OBSERVER(RemoteConnectionObserver, connection_observers_, |
| 316 HostMessageReceived(message)); |
| 317 } |
| 318 |
| 319 protocol::ClipboardStub* TestChromotingClient::GetClipboardStub() { |
| 320 DVLOG(1) << "TestChromotingClient::GetClipboardStub() Called"; |
| 321 return this; |
| 322 } |
| 323 |
| 324 protocol::CursorShapeStub* TestChromotingClient::GetCursorShapeStub() { |
| 325 DVLOG(1) << "TestChromotingClient::GetCursorShapeStub() Called"; |
| 326 return this; |
| 327 } |
| 328 |
| 329 void TestChromotingClient::InjectClipboardEvent( |
| 330 const protocol::ClipboardEvent& event) { |
| 331 DVLOG(1) << "TestChromotingClient::InjectClipboardEvent() Called"; |
| 332 } |
| 333 |
| 334 void TestChromotingClient::SetCursorShape( |
| 335 const protocol::CursorShapeInfo& cursor_shape) { |
| 336 DVLOG(1) << "TestChromotingClient::SetCursorShape() Called"; |
| 337 } |
| 338 |
| 339 } // namespace test |
| 340 } // namespace remoting |
OLD | NEW |