| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "jingle/notifier/communicator/login.h" | 5 #include "jingle/notifier/communicator/login.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 13 #include "talk/base/common.h" | 13 #include "talk/base/common.h" |
| 14 #include "talk/base/firewallsocketserver.h" | 14 #include "talk/base/firewallsocketserver.h" |
| 15 #include "talk/base/logging.h" | 15 #include "talk/base/logging.h" |
| 16 #include "talk/base/physicalsocketserver.h" | 16 #include "talk/base/physicalsocketserver.h" |
| 17 #include "talk/base/taskrunner.h" | 17 #include "talk/base/taskrunner.h" |
| 18 #include "talk/xmllite/xmlelement.h" | 18 #include "talk/xmllite/xmlelement.h" |
| 19 #include "talk/xmpp/asyncsocket.h" | 19 #include "talk/xmpp/asyncsocket.h" |
| 20 #include "talk/xmpp/prexmppauth.h" | 20 #include "talk/xmpp/prexmppauth.h" |
| 21 #include "talk/xmpp/xmppclient.h" | 21 #include "talk/xmpp/xmppclient.h" |
| 22 #include "talk/xmpp/xmppclientsettings.h" | 22 #include "talk/xmpp/xmppclientsettings.h" |
| 23 #include "talk/xmpp/xmppengine.h" | 23 #include "talk/xmpp/xmppengine.h" |
| 24 | 24 |
| 25 namespace notifier { | 25 namespace notifier { |
| 26 | 26 |
| 27 // Redirect valid for 5 minutes. | 27 // Redirect valid for 5 minutes. |
| 28 static const int kRedirectTimeoutMinutes = 5; | 28 static const int kRedirectTimeoutMinutes = 5; |
| 29 | 29 |
| 30 Login::Delegate::~Delegate() {} |
| 31 |
| 30 Login::Login(Delegate* delegate, | 32 Login::Login(Delegate* delegate, |
| 31 const buzz::XmppClientSettings& user_settings, | 33 const buzz::XmppClientSettings& user_settings, |
| 32 const scoped_refptr<net::URLRequestContextGetter>& | 34 const scoped_refptr<net::URLRequestContextGetter>& |
| 33 request_context_getter, | 35 request_context_getter, |
| 34 const ServerList& servers, | 36 const ServerList& servers, |
| 35 bool try_ssltcp_first, | 37 bool try_ssltcp_first, |
| 36 const std::string& auth_mechanism) | 38 const std::string& auth_mechanism) |
| 37 : delegate_(delegate), | 39 : delegate_(delegate), |
| 38 login_settings_(user_settings, | 40 login_settings_(user_settings, |
| 39 request_context_getter, | 41 request_context_getter, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 50 | 52 |
| 51 void Login::StartConnection() { | 53 void Login::StartConnection() { |
| 52 VLOG(1) << "Starting connection..."; | 54 VLOG(1) << "Starting connection..."; |
| 53 single_attempt_.reset(new SingleLoginAttempt(login_settings_, this)); | 55 single_attempt_.reset(new SingleLoginAttempt(login_settings_, this)); |
| 54 } | 56 } |
| 55 | 57 |
| 56 void Login::UpdateXmppSettings(const buzz::XmppClientSettings& user_settings) { | 58 void Login::UpdateXmppSettings(const buzz::XmppClientSettings& user_settings) { |
| 57 login_settings_.set_user_settings(user_settings); | 59 login_settings_.set_user_settings(user_settings); |
| 58 } | 60 } |
| 59 | 61 |
| 62 // In the code below, we assume that calling a delegate method may end |
| 63 // up in ourselves being deleted, so we always call it last. |
| 64 // |
| 65 // TODO(akalin): Add unit tests to enforce the behavior above. |
| 66 |
| 60 void Login::OnConnect(base::WeakPtr<buzz::XmppTaskParentInterface> base_task) { | 67 void Login::OnConnect(base::WeakPtr<buzz::XmppTaskParentInterface> base_task) { |
| 61 ResetReconnectState(); | 68 ResetReconnectState(); |
| 62 delegate_->OnConnect(base_task); | 69 delegate_->OnConnect(base_task); |
| 63 } | 70 } |
| 64 | 71 |
| 65 void Login::OnNeedReconnect() { | |
| 66 TryReconnect(); | |
| 67 } | |
| 68 | |
| 69 void Login::OnRedirect(const ServerInformation& redirect_server) { | 72 void Login::OnRedirect(const ServerInformation& redirect_server) { |
| 70 login_settings_.SetRedirectServer(redirect_server); | 73 login_settings_.SetRedirectServer(redirect_server); |
| 71 // Drop the current connection, and start the login process again. | 74 // Drop the current connection, and start the login process again. |
| 72 StartConnection(); | 75 StartConnection(); |
| 76 delegate_->OnTransientDisconnection(); |
| 77 } |
| 78 |
| 79 void Login::OnCredentialsRejected() { |
| 80 TryReconnect(); |
| 81 delegate_->OnCredentialsRejected(); |
| 82 } |
| 83 |
| 84 void Login::OnSettingsExhausted() { |
| 85 TryReconnect(); |
| 86 delegate_->OnTransientDisconnection(); |
| 73 } | 87 } |
| 74 | 88 |
| 75 void Login::OnIPAddressChanged() { | 89 void Login::OnIPAddressChanged() { |
| 76 VLOG(1) << "Detected IP address change"; | 90 VLOG(1) << "Detected IP address change"; |
| 77 // Reconnect in 1 to 9 seconds (vary the time a little to try to | 91 // Reconnect in 1 to 9 seconds (vary the time a little to try to |
| 78 // avoid spikey behavior on network hiccups). | 92 // avoid spikey behavior on network hiccups). |
| 79 reconnect_interval_ = base::TimeDelta::FromSeconds(base::RandInt(1, 9)); | 93 reconnect_interval_ = base::TimeDelta::FromSeconds(base::RandInt(1, 9)); |
| 80 TryReconnect(); | 94 TryReconnect(); |
| 95 delegate_->OnTransientDisconnection(); |
| 81 } | 96 } |
| 82 | 97 |
| 83 void Login::ResetReconnectState() { | 98 void Login::ResetReconnectState() { |
| 84 reconnect_interval_ = | 99 reconnect_interval_ = |
| 85 base::TimeDelta::FromSeconds(base::RandInt(5, 25)); | 100 base::TimeDelta::FromSeconds(base::RandInt(5, 25)); |
| 86 reconnect_timer_.Stop(); | 101 reconnect_timer_.Stop(); |
| 87 } | 102 } |
| 88 | 103 |
| 89 void Login::TryReconnect() { | 104 void Login::TryReconnect() { |
| 90 DCHECK_GT(reconnect_interval_.InSeconds(), 0); | 105 DCHECK_GT(reconnect_interval_.InSeconds(), 0); |
| 91 single_attempt_.reset(); | 106 single_attempt_.reset(); |
| 92 reconnect_timer_.Stop(); | 107 reconnect_timer_.Stop(); |
| 93 VLOG(1) << "Reconnecting in " | 108 VLOG(1) << "Reconnecting in " |
| 94 << reconnect_interval_.InSeconds() << " seconds"; | 109 << reconnect_interval_.InSeconds() << " seconds"; |
| 95 reconnect_timer_.Start( | 110 reconnect_timer_.Start( |
| 96 FROM_HERE, reconnect_interval_, this, &Login::DoReconnect); | 111 FROM_HERE, reconnect_interval_, this, &Login::DoReconnect); |
| 97 delegate_->OnDisconnect(); | |
| 98 } | 112 } |
| 99 | 113 |
| 100 void Login::DoReconnect() { | 114 void Login::DoReconnect() { |
| 101 // Double reconnect time up to 30 minutes. | 115 // Double reconnect time up to 30 minutes. |
| 102 const base::TimeDelta kMaxReconnectInterval = | 116 const base::TimeDelta kMaxReconnectInterval = |
| 103 base::TimeDelta::FromMinutes(30); | 117 base::TimeDelta::FromMinutes(30); |
| 104 reconnect_interval_ *= 2; | 118 reconnect_interval_ *= 2; |
| 105 if (reconnect_interval_ > kMaxReconnectInterval) | 119 if (reconnect_interval_ > kMaxReconnectInterval) |
| 106 reconnect_interval_ = kMaxReconnectInterval; | 120 reconnect_interval_ = kMaxReconnectInterval; |
| 107 VLOG(1) << "Reconnecting..."; | 121 VLOG(1) << "Reconnecting..."; |
| 108 StartConnection(); | 122 StartConnection(); |
| 109 } | 123 } |
| 110 | 124 |
| 111 } // namespace notifier | 125 } // namespace notifier |
| OLD | NEW |