| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/signaling/xmpp_signal_strategy.h" | 5 #include "remoting/signaling/xmpp_signal_strategy.h" |
| 6 | 6 |
| 7 #include <vector> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/observer_list.h" |
| 13 #include "base/rand_util.h" |
| 10 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/string_util.h" | 15 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 13 #include "jingle/glue/chrome_async_socket.h" | 17 #include "base/threading/thread_checker.h" |
| 14 #include "jingle/glue/task_pump.h" | 18 #include "base/time/time.h" |
| 15 #include "jingle/glue/xmpp_client_socket_factory.h" | 19 #include "base/timer/timer.h" |
| 16 #include "jingle/notifier/base/gaia_constants.h" | 20 #include "jingle/glue/proxy_resolving_client_socket.h" |
| 17 #include "jingle/notifier/base/gaia_token_pre_xmpp_auth.h" | 21 #include "net/cert/cert_verifier.h" |
| 22 #include "net/http/transport_security_state.h" |
| 18 #include "net/socket/client_socket_factory.h" | 23 #include "net/socket/client_socket_factory.h" |
| 24 #include "net/socket/client_socket_handle.h" |
| 25 #include "net/socket/ssl_client_socket.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "third_party/webrtc/base/thread.h" | 27 #include "remoting/base/buffered_socket_writer.h" |
| 21 #include "third_party/webrtc/libjingle/xmpp/prexmppauth.h" | 28 #include "remoting/signaling/xmpp_login_handler.h" |
| 22 #include "third_party/webrtc/libjingle/xmpp/saslcookiemechanism.h" | 29 #include "remoting/signaling/xmpp_stream_parser.h" |
| 30 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 23 | 31 |
| 24 const char kDefaultResourceName[] = "chromoting"; | 32 // Use 50 seconds keep-alive interval, in case routers terminate |
| 25 | |
| 26 // Use 58 seconds keep-alive interval, in case routers terminate | |
| 27 // connections that are idle for more than a minute. | 33 // connections that are idle for more than a minute. |
| 28 const int kKeepAliveIntervalSeconds = 50; | 34 const int kKeepAliveIntervalSeconds = 50; |
| 29 | 35 |
| 30 // Read buffer size used by ChromeAsyncSocket for read and write buffers. | 36 const int kReadBufferSize = 4096; |
| 31 // | |
| 32 // TODO(sergeyu): Currently jingle::ChromeAsyncSocket fails Write() when the | |
| 33 // write buffer is full and talk::XmppClient just ignores the error. As result | |
| 34 // chunks of data sent to the server are dropped (and they may not be full XMPP | |
| 35 // stanzas). The problem needs to be fixed either in XmppClient on | |
| 36 // ChromeAsyncSocket (e.g. ChromeAsyncSocket could close the connection when | |
| 37 // buffer is full). | |
| 38 const size_t kReadBufferSize = 64 * 1024; | |
| 39 const size_t kWriteBufferSize = 64 * 1024; | |
| 40 | 37 |
| 41 const int kDefaultXmppPort = 5222; | 38 const int kDefaultXmppPort = 5222; |
| 42 const int kDefaultHttpsPort = 443; | 39 const int kDefaultHttpsPort = 443; |
| 43 | 40 |
| 44 namespace remoting { | 41 namespace remoting { |
| 45 | 42 |
| 46 XmppSignalStrategy::XmppServerConfig::XmppServerConfig() {} | 43 XmppSignalStrategy::XmppServerConfig::XmppServerConfig() |
| 47 XmppSignalStrategy::XmppServerConfig::~XmppServerConfig() {} | 44 : port(kDefaultXmppPort), use_tls(true) { |
| 45 } |
| 48 | 46 |
| 49 XmppSignalStrategy::XmppSignalStrategy( | 47 XmppSignalStrategy::XmppServerConfig::~XmppServerConfig() { |
| 48 } |
| 49 |
| 50 class XmppSignalStrategy::Core : public XmppLoginHandler::Delegate { |
| 51 public: |
| 52 Core( |
| 53 net::ClientSocketFactory* socket_factory, |
| 54 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 55 const XmppServerConfig& xmpp_server_config); |
| 56 ~Core() override; |
| 57 |
| 58 void Connect(); |
| 59 void Disconnect(); |
| 60 State GetState() const; |
| 61 Error GetError() const; |
| 62 std::string GetLocalJid() const; |
| 63 void AddListener(Listener* listener); |
| 64 void RemoveListener(Listener* listener); |
| 65 bool SendStanza(scoped_ptr<buzz::XmlElement> stanza); |
| 66 |
| 67 void SetAuthInfo(const std::string& username, |
| 68 const std::string& auth_token); |
| 69 |
| 70 void VerifyNoListeners(); |
| 71 |
| 72 private: |
| 73 void OnSocketConnected(int result); |
| 74 void OnTlsConnected(int result); |
| 75 |
| 76 void ReadSocket(); |
| 77 void OnReadResult(int result); |
| 78 void HandleReadResult(int result); |
| 79 |
| 80 // XmppLoginHandler::Delegate interface. |
| 81 void SendMessage(const std::string& message) override; |
| 82 void StartTls() override; |
| 83 void OnHandshakeDone(const std::string& jid, |
| 84 scoped_ptr<XmppStreamParser> parser) override; |
| 85 void OnLoginHandlerError(SignalStrategy::Error error) override; |
| 86 |
| 87 // Event handlers for XmppStreamParser. |
| 88 void OnStanza(const scoped_ptr<buzz::XmlElement> stanza); |
| 89 void OnParserError(); |
| 90 |
| 91 void OnNetworkError(int error); |
| 92 |
| 93 void SendKeepAlive(); |
| 94 |
| 95 net::ClientSocketFactory* socket_factory_; |
| 96 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 97 XmppServerConfig xmpp_server_config_; |
| 98 |
| 99 // Used by the |socket_|. |
| 100 scoped_ptr<net::CertVerifier> cert_verifier_; |
| 101 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
| 102 |
| 103 scoped_ptr<net::StreamSocket> socket_; |
| 104 scoped_ptr<BufferedSocketWriter> writer_; |
| 105 scoped_refptr<net::IOBuffer> read_buffer_; |
| 106 bool read_pending_; |
| 107 bool tls_pending_; |
| 108 |
| 109 scoped_ptr<XmppLoginHandler> login_handler_; |
| 110 scoped_ptr<XmppStreamParser> stream_parser_; |
| 111 std::string jid_; |
| 112 |
| 113 Error error_; |
| 114 |
| 115 ObserverList<Listener, true> listeners_; |
| 116 |
| 117 base::Timer keep_alive_timer_; |
| 118 |
| 119 base::ThreadChecker thread_checker_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(Core); |
| 122 }; |
| 123 |
| 124 XmppSignalStrategy::Core::Core( |
| 50 net::ClientSocketFactory* socket_factory, | 125 net::ClientSocketFactory* socket_factory, |
| 51 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 126 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 52 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config) | 127 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config) |
| 53 : socket_factory_(socket_factory), | 128 : socket_factory_(socket_factory), |
| 54 request_context_getter_(request_context_getter), | 129 request_context_getter_(request_context_getter), |
| 55 resource_name_(kDefaultResourceName), | |
| 56 xmpp_client_(nullptr), | |
| 57 xmpp_server_config_(xmpp_server_config), | 130 xmpp_server_config_(xmpp_server_config), |
| 58 state_(DISCONNECTED), | 131 read_pending_(false), |
| 59 error_(OK) { | 132 tls_pending_(false), |
| 133 error_(OK), |
| 134 keep_alive_timer_( |
| 135 FROM_HERE, |
| 136 base::TimeDelta::FromSeconds(kKeepAliveIntervalSeconds), |
| 137 base::Bind(&Core::SendKeepAlive, base::Unretained(this)), |
| 138 true) { |
| 60 #if defined(NDEBUG) | 139 #if defined(NDEBUG) |
| 140 // Non-secure connections are allowed only for debugging. |
| 61 CHECK(xmpp_server_config_.use_tls); | 141 CHECK(xmpp_server_config_.use_tls); |
| 62 #endif | 142 #endif |
| 63 } | |
| 64 | 143 |
| 65 XmppSignalStrategy::~XmppSignalStrategy() { | 144 // TODO(sergeyu): Support for direct connections without TLS is not |
| 66 Disconnect(); | 145 // implemented yet. |
| 67 | 146 if (!xmpp_server_config_.use_tls) |
| 68 // Destroying task runner will destroy XmppClient, but XmppClient may be on | 147 NOTIMPLEMENTED(); |
| 69 // the stack and it doesn't handle this case properly, so we need to delay | |
| 70 // destruction. | |
| 71 base::ThreadTaskRunnerHandle::Get()->DeleteSoon( | |
| 72 FROM_HERE, task_runner_.release()); | |
| 73 } | |
| 74 | |
| 75 void XmppSignalStrategy::Connect() { | |
| 76 DCHECK(CalledOnValidThread()); | |
| 77 | |
| 78 // Disconnect first if we are currently connected. | |
| 79 Disconnect(); | |
| 80 | |
| 81 buzz::XmppClientSettings settings; | |
| 82 buzz::Jid login_jid(xmpp_server_config_.username); | |
| 83 settings.set_user(login_jid.node()); | |
| 84 settings.set_host(login_jid.domain()); | |
| 85 settings.set_resource(resource_name_); | |
| 86 settings.set_token_service("oauth2"); | |
| 87 settings.set_auth_token(buzz::AUTH_MECHANISM_GOOGLE_TOKEN, | |
| 88 xmpp_server_config_.auth_token); | |
| 89 | |
| 90 int port = xmpp_server_config_.port; | |
| 91 | 148 |
| 92 // Port 5222 may be blocked by firewall. talk.google.com allows connections on | 149 // Port 5222 may be blocked by firewall. talk.google.com allows connections on |
| 93 // port 443 which can be used instead of 5222. The webapp still requests to | 150 // port 443 which can be used instead of 5222. The webapp still requests to |
| 94 // use port 5222 for backwards compatibility with older versions of the host | 151 // use port 5222 for backwards compatibility with older versions of the host |
| 95 // that do not pass correct |use_fake_ssl_client_socket| value to | 152 // that do not pass correct |use_fake_ssl_client_socket| value to |
| 96 // XmppClientSocketFactory (and so cannot connect to port 443). In case we are | 153 // XmppClientSocketFactory (and so cannot connect to port 443). In case we are |
| 97 // connecting to talk.google.com try to use port 443 anyway. | 154 // connecting to talk.google.com try to use port 443 anyway. |
| 98 // | 155 // |
| 99 // TODO(sergeyu): Once all hosts support connections on port 443 | 156 // TODO(sergeyu): Once all hosts support connections on port 443 |
| 100 // the webapp needs to be updated to request port 443 and these 2 lines can be | 157 // the webapp needs to be updated to request port 443 and these 2 lines can be |
| 101 // removed. crbug.com/443384 | 158 // removed. crbug.com/443384 |
| 102 if (xmpp_server_config_.host == "talk.google.com" && port == kDefaultXmppPort) | 159 if (xmpp_server_config_.host == "talk.google.com" && |
| 103 port = kDefaultHttpsPort; | 160 xmpp_server_config_.port == kDefaultXmppPort) { |
| 104 | 161 xmpp_server_config_.port = kDefaultHttpsPort; |
| 105 settings.set_server( | 162 } |
| 106 rtc::SocketAddress(xmpp_server_config_.host, port)); | 163 } |
| 107 settings.set_use_tls( | 164 |
| 108 xmpp_server_config_.use_tls ? buzz::TLS_ENABLED : buzz::TLS_DISABLED); | 165 XmppSignalStrategy::Core::~Core() { |
| 109 | 166 Disconnect(); |
| 110 // Enable fake SSL handshake when connecting over HTTPS port. | 167 } |
| 111 bool use_fake_ssl_client_socket = (port == kDefaultHttpsPort); | 168 |
| 112 | 169 void XmppSignalStrategy::Core::Connect() { |
| 113 scoped_ptr<jingle_glue::XmppClientSocketFactory> xmpp_socket_factory( | 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 new jingle_glue::XmppClientSocketFactory(socket_factory_, | 171 |
| 115 net::SSLConfig(), | 172 // Disconnect first if we are currently connected. |
| 116 request_context_getter_, | 173 Disconnect(); |
| 117 use_fake_ssl_client_socket)); | 174 |
| 118 buzz::AsyncSocket* socket = new jingle_glue::ChromeAsyncSocket( | 175 error_ = OK; |
| 119 xmpp_socket_factory.release(), kReadBufferSize, kWriteBufferSize); | 176 |
| 120 | 177 FOR_EACH_OBSERVER(Listener, listeners_, |
| 121 task_runner_.reset(new jingle_glue::TaskPump()); | 178 OnSignalStrategyStateChange(CONNECTING)); |
| 122 xmpp_client_ = new buzz::XmppClient(task_runner_.get()); | 179 |
| 123 xmpp_client_->Connect( | 180 socket_.reset(new jingle_glue::ProxyResolvingClientSocket( |
| 124 settings, std::string(), socket, CreatePreXmppAuth(settings)); | 181 socket_factory_, request_context_getter_, net::SSLConfig(), |
| 125 xmpp_client_->SignalStateChange | 182 net::HostPortPair(xmpp_server_config_.host, xmpp_server_config_.port))); |
| 126 .connect(this, &XmppSignalStrategy::OnConnectionStateChanged); | 183 |
| 127 xmpp_client_->engine()->AddStanzaHandler(this, buzz::XmppEngine::HL_TYPE); | 184 int result = socket_->Connect(base::Bind( |
| 128 xmpp_client_->Start(); | 185 &Core::OnSocketConnected, base::Unretained(this))); |
| 129 | 186 if (result != net::ERR_IO_PENDING) |
| 130 SetState(CONNECTING); | 187 OnSocketConnected(result); |
| 188 } |
| 189 |
| 190 void XmppSignalStrategy::Core::Disconnect() { |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 |
| 193 if (socket_) { |
| 194 login_handler_.reset(); |
| 195 stream_parser_.reset(); |
| 196 writer_.reset(); |
| 197 socket_.reset(); |
| 198 |
| 199 FOR_EACH_OBSERVER(Listener, listeners_, |
| 200 OnSignalStrategyStateChange(DISCONNECTED)); |
| 201 } |
| 202 } |
| 203 |
| 204 SignalStrategy::State XmppSignalStrategy::Core::GetState() const { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 |
| 207 if (stream_parser_) { |
| 208 DCHECK(socket_); |
| 209 return CONNECTED; |
| 210 } else if (socket_) { |
| 211 return CONNECTING; |
| 212 } else { |
| 213 return DISCONNECTED; |
| 214 } |
| 215 } |
| 216 |
| 217 SignalStrategy::Error XmppSignalStrategy::Core::GetError() const { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 return error_; |
| 220 } |
| 221 |
| 222 std::string XmppSignalStrategy::Core::GetLocalJid() const { |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 return jid_; |
| 225 } |
| 226 |
| 227 void XmppSignalStrategy::Core::AddListener(Listener* listener) { |
| 228 DCHECK(thread_checker_.CalledOnValidThread()); |
| 229 listeners_.AddObserver(listener); |
| 230 } |
| 231 |
| 232 void XmppSignalStrategy::Core::RemoveListener(Listener* listener) { |
| 233 DCHECK(thread_checker_.CalledOnValidThread()); |
| 234 listeners_.RemoveObserver(listener); |
| 235 } |
| 236 |
| 237 bool XmppSignalStrategy::Core::SendStanza(scoped_ptr<buzz::XmlElement> stanza) { |
| 238 DCHECK(thread_checker_.CalledOnValidThread()); |
| 239 |
| 240 if (!stream_parser_) { |
| 241 VLOG(0) << "Dropping signalling message because XMPP " |
| 242 "connection has been terminated."; |
| 243 return false; |
| 244 } |
| 245 |
| 246 SendMessage(stanza->Str()); |
| 247 return true; |
| 248 } |
| 249 |
| 250 void XmppSignalStrategy::Core::SetAuthInfo(const std::string& username, |
| 251 const std::string& auth_token) { |
| 252 DCHECK(thread_checker_.CalledOnValidThread()); |
| 253 xmpp_server_config_.username = username; |
| 254 xmpp_server_config_.auth_token = auth_token; |
| 255 } |
| 256 |
| 257 void XmppSignalStrategy::Core::SendMessage(const std::string& message) { |
| 258 DCHECK(thread_checker_.CalledOnValidThread()); |
| 259 scoped_refptr<net::IOBufferWithSize> buffer = |
| 260 new net::IOBufferWithSize(message.size()); |
| 261 memcpy(buffer->data(), message.data(), message.size()); |
| 262 writer_->Write(buffer, base::Closure()); |
| 263 } |
| 264 |
| 265 void XmppSignalStrategy::Core::StartTls() { |
| 266 DCHECK(thread_checker_.CalledOnValidThread()); |
| 267 DCHECK(login_handler_); |
| 268 |
| 269 // Reset the writer so we don't try to write to the raw socket anymore. |
| 270 DCHECK_EQ(writer_->GetBufferSize(), 0); |
| 271 writer_.reset(); |
| 272 |
| 273 DCHECK(!read_pending_); |
| 274 |
| 275 scoped_ptr<net::ClientSocketHandle> socket_handle( |
| 276 new net::ClientSocketHandle()); |
| 277 socket_handle->SetSocket(socket_.Pass()); |
| 278 |
| 279 cert_verifier_.reset(net::CertVerifier::CreateDefault()); |
| 280 transport_security_state_.reset(new net::TransportSecurityState()); |
| 281 net::SSLClientSocketContext context; |
| 282 context.cert_verifier = cert_verifier_.get(); |
| 283 context.transport_security_state = transport_security_state_.get(); |
| 284 |
| 285 socket_ = socket_factory_->CreateSSLClientSocket( |
| 286 socket_handle.Pass(), |
| 287 net::HostPortPair(xmpp_server_config_.host, kDefaultHttpsPort), |
| 288 net::SSLConfig(), context); |
| 289 |
| 290 tls_pending_ = true; |
| 291 int result = socket_->Connect( |
| 292 base::Bind(&Core::OnTlsConnected, base::Unretained(this))); |
| 293 if (result != net::ERR_IO_PENDING) |
| 294 OnTlsConnected(result); |
| 295 } |
| 296 |
| 297 void XmppSignalStrategy::Core::OnHandshakeDone( |
| 298 const std::string& jid, |
| 299 scoped_ptr<XmppStreamParser> parser) { |
| 300 DCHECK(thread_checker_.CalledOnValidThread()); |
| 301 |
| 302 jid_ = jid; |
| 303 stream_parser_ = parser.Pass(); |
| 304 stream_parser_->SetCallbacks( |
| 305 base::Bind(&Core::OnStanza, base::Unretained(this)), |
| 306 base::Bind(&Core::OnParserError, base::Unretained(this))); |
| 307 |
| 308 // Don't need |login_handler_| anymore. |
| 309 login_handler_.reset(); |
| 310 |
| 311 FOR_EACH_OBSERVER(Listener, listeners_, |
| 312 OnSignalStrategyStateChange(CONNECTED)); |
| 313 } |
| 314 |
| 315 void XmppSignalStrategy::Core::OnLoginHandlerError( |
| 316 SignalStrategy::Error error) { |
| 317 DCHECK(thread_checker_.CalledOnValidThread()); |
| 318 |
| 319 error_ = error; |
| 320 Disconnect(); |
| 321 } |
| 322 |
| 323 void XmppSignalStrategy::Core::OnStanza( |
| 324 const scoped_ptr<buzz::XmlElement> stanza) { |
| 325 DCHECK(thread_checker_.CalledOnValidThread()); |
| 326 |
| 327 ObserverListBase<Listener>::Iterator it(&listeners_); |
| 328 for (Listener* listener = it.GetNext(); listener; listener = it.GetNext()) { |
| 329 if (listener->OnSignalStrategyIncomingStanza(stanza.get())) |
| 330 return; |
| 331 } |
| 332 } |
| 333 |
| 334 void XmppSignalStrategy::Core::OnParserError() { |
| 335 DCHECK(thread_checker_.CalledOnValidThread()); |
| 336 |
| 337 error_ = NETWORK_ERROR; |
| 338 Disconnect(); |
| 339 } |
| 340 |
| 341 void XmppSignalStrategy::Core::OnSocketConnected(int result) { |
| 342 DCHECK(thread_checker_.CalledOnValidThread()); |
| 343 |
| 344 if (result != net::OK) { |
| 345 OnNetworkError(result); |
| 346 return; |
| 347 } |
| 348 |
| 349 writer_.reset(new BufferedSocketWriter()); |
| 350 writer_->Init(socket_.get(), base::Bind(&Core::OnNetworkError, |
| 351 base::Unretained(this))); |
| 352 |
| 353 XmppLoginHandler::TlsMode tls_mode; |
| 354 if (xmpp_server_config_.use_tls) { |
| 355 tls_mode = (xmpp_server_config_.port == kDefaultXmppPort) |
| 356 ? XmppLoginHandler::TlsMode::WITH_HANDSHAKE |
| 357 : XmppLoginHandler::TlsMode::WITHOUT_HANDSHAKE; |
| 358 } else { |
| 359 tls_mode = XmppLoginHandler::TlsMode::NO_TLS; |
| 360 } |
| 361 |
| 362 // The server name is passed as to attribute in the <stream>. When connecting |
| 363 // to talk.google.com it affects the certificate the server will use for TLS: |
| 364 // talk.google.com uses gmail certificate when specified server is gmail.com |
| 365 // or googlemail.com and google.com cert otherwise. In the same time it |
| 366 // doesn't accept talk.google.com as target server. Here we use google.com |
| 367 // server name when authenticating to talk.google.com. This ensures that the |
| 368 // server will use google.com cert which will be accepted by the TLS |
| 369 // implementation in Chrome (TLS API doesn't allow specifying domain other |
| 370 // than the one that was passed to connect()). |
| 371 std::string server = xmpp_server_config_.host; |
| 372 if (server == "talk.google.com") |
| 373 server = "google.com"; |
| 374 |
| 375 login_handler_.reset( |
| 376 new XmppLoginHandler(server, xmpp_server_config_.username, |
| 377 xmpp_server_config_.auth_token, tls_mode, this)); |
| 378 login_handler_->Start(); |
| 379 |
| 380 ReadSocket(); |
| 381 } |
| 382 |
| 383 void XmppSignalStrategy::Core::OnTlsConnected(int result) { |
| 384 DCHECK(thread_checker_.CalledOnValidThread()); |
| 385 DCHECK(tls_pending_); |
| 386 tls_pending_ = false; |
| 387 |
| 388 if (result != net::OK) { |
| 389 OnNetworkError(result); |
| 390 return; |
| 391 } |
| 392 |
| 393 writer_.reset(new BufferedSocketWriter()); |
| 394 writer_->Init(socket_.get(), base::Bind(&Core::OnNetworkError, |
| 395 base::Unretained(this))); |
| 396 |
| 397 login_handler_->OnTlsStarted(); |
| 398 |
| 399 ReadSocket(); |
| 400 } |
| 401 |
| 402 void XmppSignalStrategy::Core::ReadSocket() { |
| 403 DCHECK(thread_checker_.CalledOnValidThread()); |
| 404 |
| 405 while (socket_ && !read_pending_ && !tls_pending_) { |
| 406 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 407 int result = socket_->Read( |
| 408 read_buffer_.get(), kReadBufferSize, |
| 409 base::Bind(&Core::OnReadResult, base::Unretained(this))); |
| 410 HandleReadResult(result); |
| 411 } |
| 412 } |
| 413 |
| 414 void XmppSignalStrategy::Core::OnReadResult(int result) { |
| 415 DCHECK(thread_checker_.CalledOnValidThread()); |
| 416 DCHECK(read_pending_); |
| 417 read_pending_ = false; |
| 418 HandleReadResult(result); |
| 419 ReadSocket(); |
| 420 } |
| 421 |
| 422 void XmppSignalStrategy::Core::HandleReadResult(int result) { |
| 423 DCHECK(thread_checker_.CalledOnValidThread()); |
| 424 |
| 425 if (result == net::ERR_IO_PENDING) { |
| 426 read_pending_ = true; |
| 427 return; |
| 428 } |
| 429 |
| 430 if (result < 0) { |
| 431 OnNetworkError(result); |
| 432 return; |
| 433 } |
| 434 |
| 435 if (result == 0) { |
| 436 // Connection was closed by the server. |
| 437 error_ = OK; |
| 438 Disconnect(); |
| 439 return; |
| 440 } |
| 441 |
| 442 if (stream_parser_) { |
| 443 stream_parser_->AppendData(std::string(read_buffer_->data(), result)); |
| 444 } else { |
| 445 login_handler_->OnDataReceived(std::string(read_buffer_->data(), result)); |
| 446 } |
| 447 } |
| 448 |
| 449 void XmppSignalStrategy::Core::OnNetworkError(int error) { |
| 450 DCHECK(thread_checker_.CalledOnValidThread()); |
| 451 |
| 452 LOG(ERROR) << "XMPP socket error " << error; |
| 453 error_ = NETWORK_ERROR; |
| 454 Disconnect(); |
| 455 } |
| 456 |
| 457 void XmppSignalStrategy::Core::SendKeepAlive() { |
| 458 DCHECK(thread_checker_.CalledOnValidThread()); |
| 459 |
| 460 if (GetState() == CONNECTED) |
| 461 SendMessage(" "); |
| 462 } |
| 463 |
| 464 XmppSignalStrategy::XmppSignalStrategy( |
| 465 net::ClientSocketFactory* socket_factory, |
| 466 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 467 const XmppServerConfig& xmpp_server_config) |
| 468 : core_(new Core(socket_factory, |
| 469 request_context_getter, |
| 470 xmpp_server_config)) { |
| 471 } |
| 472 |
| 473 XmppSignalStrategy::~XmppSignalStrategy() { |
| 474 // All listeners should be removed at this point, so it's safe to detach |
| 475 // |core_|. |
| 476 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, core_.release()); |
| 477 } |
| 478 |
| 479 void XmppSignalStrategy::Connect() { |
| 480 core_->Connect(); |
| 131 } | 481 } |
| 132 | 482 |
| 133 void XmppSignalStrategy::Disconnect() { | 483 void XmppSignalStrategy::Disconnect() { |
| 134 DCHECK(CalledOnValidThread()); | 484 core_->Disconnect(); |
| 135 | |
| 136 if (xmpp_client_) { | |
| 137 xmpp_client_->engine()->RemoveStanzaHandler(this); | |
| 138 | |
| 139 xmpp_client_->Disconnect(); | |
| 140 | |
| 141 // |xmpp_client_| should be set to nullptr in OnConnectionStateChanged() | |
| 142 // in response to Disconnect() call above. | |
| 143 DCHECK(xmpp_client_ == nullptr); | |
| 144 } | |
| 145 } | 485 } |
| 146 | 486 |
| 147 SignalStrategy::State XmppSignalStrategy::GetState() const { | 487 SignalStrategy::State XmppSignalStrategy::GetState() const { |
| 148 DCHECK(CalledOnValidThread()); | 488 return core_->GetState(); |
| 149 return state_; | |
| 150 } | 489 } |
| 151 | 490 |
| 152 SignalStrategy::Error XmppSignalStrategy::GetError() const { | 491 SignalStrategy::Error XmppSignalStrategy::GetError() const { |
| 153 DCHECK(CalledOnValidThread()); | 492 return core_->GetError(); |
| 154 return error_; | |
| 155 } | 493 } |
| 156 | 494 |
| 157 std::string XmppSignalStrategy::GetLocalJid() const { | 495 std::string XmppSignalStrategy::GetLocalJid() const { |
| 158 DCHECK(CalledOnValidThread()); | 496 return core_->GetLocalJid(); |
| 159 return xmpp_client_->jid().Str(); | |
| 160 } | 497 } |
| 161 | 498 |
| 162 void XmppSignalStrategy::AddListener(Listener* listener) { | 499 void XmppSignalStrategy::AddListener(Listener* listener) { |
| 163 DCHECK(CalledOnValidThread()); | 500 core_->AddListener(listener); |
| 164 listeners_.AddObserver(listener); | |
| 165 } | 501 } |
| 166 | 502 |
| 167 void XmppSignalStrategy::RemoveListener(Listener* listener) { | 503 void XmppSignalStrategy::RemoveListener(Listener* listener) { |
| 168 DCHECK(CalledOnValidThread()); | 504 core_->RemoveListener(listener); |
| 169 listeners_.RemoveObserver(listener); | 505 } |
| 170 } | |
| 171 | |
| 172 bool XmppSignalStrategy::SendStanza(scoped_ptr<buzz::XmlElement> stanza) { | 506 bool XmppSignalStrategy::SendStanza(scoped_ptr<buzz::XmlElement> stanza) { |
| 173 DCHECK(CalledOnValidThread()); | 507 return core_->SendStanza(stanza.Pass()); |
| 174 if (!xmpp_client_) { | |
| 175 VLOG(0) << "Dropping signalling message because XMPP " | |
| 176 "connection has been terminated."; | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 buzz::XmppReturnStatus status = xmpp_client_->SendStanza(stanza.release()); | |
| 181 return status == buzz::XMPP_RETURN_OK || status == buzz::XMPP_RETURN_PENDING; | |
| 182 } | 508 } |
| 183 | 509 |
| 184 std::string XmppSignalStrategy::GetNextId() { | 510 std::string XmppSignalStrategy::GetNextId() { |
| 185 DCHECK(CalledOnValidThread()); | 511 return base::Uint64ToString(base::RandUint64()); |
| 186 if (!xmpp_client_) { | |
| 187 // If the connection has been terminated then it doesn't matter | |
| 188 // what Id we return. | |
| 189 return std::string(); | |
| 190 } | |
| 191 return xmpp_client_->NextId(); | |
| 192 } | |
| 193 | |
| 194 bool XmppSignalStrategy::HandleStanza(const buzz::XmlElement* stanza) { | |
| 195 DCHECK(CalledOnValidThread()); | |
| 196 ObserverListBase<Listener>::Iterator it(&listeners_); | |
| 197 Listener* listener; | |
| 198 while ((listener = it.GetNext()) != nullptr) { | |
| 199 if (listener->OnSignalStrategyIncomingStanza(stanza)) | |
| 200 return true; | |
| 201 } | |
| 202 return false; | |
| 203 } | 512 } |
| 204 | 513 |
| 205 void XmppSignalStrategy::SetAuthInfo(const std::string& username, | 514 void XmppSignalStrategy::SetAuthInfo(const std::string& username, |
| 206 const std::string& auth_token) { | 515 const std::string& auth_token) { |
| 207 DCHECK(CalledOnValidThread()); | 516 core_->SetAuthInfo(username, auth_token); |
| 208 xmpp_server_config_.username = username; | |
| 209 xmpp_server_config_.auth_token = auth_token; | |
| 210 } | |
| 211 | |
| 212 void XmppSignalStrategy::SetResourceName(const std::string &resource_name) { | |
| 213 DCHECK(CalledOnValidThread()); | |
| 214 resource_name_ = resource_name; | |
| 215 } | |
| 216 | |
| 217 void XmppSignalStrategy::OnConnectionStateChanged( | |
| 218 buzz::XmppEngine::State state) { | |
| 219 DCHECK(CalledOnValidThread()); | |
| 220 | |
| 221 if (state == buzz::XmppEngine::STATE_OPEN) { | |
| 222 keep_alive_timer_.Start( | |
| 223 FROM_HERE, base::TimeDelta::FromSeconds(kKeepAliveIntervalSeconds), | |
| 224 this, &XmppSignalStrategy::SendKeepAlive); | |
| 225 SetState(CONNECTED); | |
| 226 } else if (state == buzz::XmppEngine::STATE_CLOSED) { | |
| 227 // Make sure we dump errors to the log. | |
| 228 int subcode; | |
| 229 buzz::XmppEngine::Error error = xmpp_client_->GetError(&subcode); | |
| 230 VLOG(0) << "XMPP connection was closed: error=" << error | |
| 231 << ", subcode=" << subcode; | |
| 232 | |
| 233 keep_alive_timer_.Stop(); | |
| 234 | |
| 235 // Client is destroyed by the TaskRunner after the client is | |
| 236 // closed. Reset the pointer so we don't try to use it later. | |
| 237 xmpp_client_ = nullptr; | |
| 238 | |
| 239 switch (error) { | |
| 240 case buzz::XmppEngine::ERROR_UNAUTHORIZED: | |
| 241 case buzz::XmppEngine::ERROR_AUTH: | |
| 242 case buzz::XmppEngine::ERROR_MISSING_USERNAME: | |
| 243 error_ = AUTHENTICATION_FAILED; | |
| 244 break; | |
| 245 | |
| 246 default: | |
| 247 error_ = NETWORK_ERROR; | |
| 248 } | |
| 249 | |
| 250 SetState(DISCONNECTED); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 void XmppSignalStrategy::SetState(State new_state) { | |
| 255 if (state_ != new_state) { | |
| 256 state_ = new_state; | |
| 257 FOR_EACH_OBSERVER(Listener, listeners_, | |
| 258 OnSignalStrategyStateChange(new_state)); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 void XmppSignalStrategy::SendKeepAlive() { | |
| 263 xmpp_client_->SendRaw(" "); | |
| 264 } | |
| 265 | |
| 266 // static | |
| 267 buzz::PreXmppAuth* XmppSignalStrategy::CreatePreXmppAuth( | |
| 268 const buzz::XmppClientSettings& settings) { | |
| 269 buzz::Jid jid(settings.user(), settings.host(), buzz::STR_EMPTY); | |
| 270 return new notifier::GaiaTokenPreXmppAuth( | |
| 271 jid.Str(), settings.auth_token(), settings.token_service(), "X-OAUTH2"); | |
| 272 } | 517 } |
| 273 | 518 |
| 274 } // namespace remoting | 519 } // namespace remoting |
| OLD | NEW |