Home | History | Annotate | Download | only in communicator
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_
      6 #define JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/time/time.h"
     15 #include "base/timer/timer.h"
     16 #include "jingle/notifier/base/server_information.h"
     17 #include "jingle/notifier/communicator/login_settings.h"
     18 #include "jingle/notifier/communicator/single_login_attempt.h"
     19 #include "net/base/network_change_notifier.h"
     20 #include "talk/xmpp/xmppengine.h"
     21 
     22 namespace buzz {
     23 class XmppClient;
     24 class XmppClientSettings;
     25 class XmppTaskParentInterface;
     26 }  // namespace buzz
     27 
     28 namespace net {
     29 class URLRequestContextGetter;
     30 }  // namespace net
     31 
     32 namespace notifier {
     33 
     34 class LoginSettings;
     35 
     36 // Does the login, keeps it alive (with refreshing cookies and
     37 // reattempting login when disconnected), and figures out what actions
     38 // to take on the various errors that may occur.
     39 //
     40 // TODO(akalin): Make this observe proxy config changes also.
     41 class Login : public net::NetworkChangeNotifier::IPAddressObserver,
     42               public net::NetworkChangeNotifier::ConnectionTypeObserver,
     43               public net::NetworkChangeNotifier::DNSObserver,
     44               public SingleLoginAttempt::Delegate {
     45  public:
     46   class Delegate {
     47    public:
     48     // Called when a connection has been successfully established.
     49     virtual void OnConnect(
     50         base::WeakPtr<buzz::XmppTaskParentInterface> base_task) = 0;
     51 
     52     // Called when there's no connection to the server but we expect
     53     // it to come back come back eventually.  The connection will be
     54     // retried with exponential backoff.
     55     virtual void OnTransientDisconnection() = 0;
     56 
     57     // Called when the current login credentials have been rejected.
     58     // The connection will still be retried with exponential backoff;
     59     // it's up to the delegate to stop connecting and/or prompt for
     60     // new credentials.
     61     virtual void OnCredentialsRejected() = 0;
     62 
     63    protected:
     64     virtual ~Delegate();
     65   };
     66 
     67   // Does not take ownership of |delegate|, which must not be NULL.
     68   Login(Delegate* delegate,
     69         const buzz::XmppClientSettings& user_settings,
     70         const scoped_refptr<net::URLRequestContextGetter>&
     71             request_context_getter,
     72         const ServerList& servers,
     73         bool try_ssltcp_first,
     74         const std::string& auth_mechanism);
     75   virtual ~Login();
     76 
     77   // Starts connecting (or forces a reconnection if we're backed off).
     78   void StartConnection();
     79 
     80   // The updated settings take effect only the next time when a
     81   // connection is attempted (either via reconnection or a call to
     82   // StartConnection()).
     83   void UpdateXmppSettings(const buzz::XmppClientSettings& user_settings);
     84 
     85   // net::NetworkChangeNotifier::IPAddressObserver implementation.
     86   virtual void OnIPAddressChanged() OVERRIDE;
     87 
     88   // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
     89   virtual void OnConnectionTypeChanged(
     90       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
     91 
     92   // net::NetworkChangeNotifier::DNSObserver implementation.
     93   virtual void OnDNSChanged() OVERRIDE;
     94 
     95   // SingleLoginAttempt::Delegate implementation.
     96   virtual void OnConnect(
     97       base::WeakPtr<buzz::XmppTaskParentInterface> base_task) OVERRIDE;
     98   virtual void OnRedirect(const ServerInformation& redirect_server) OVERRIDE;
     99   virtual void OnCredentialsRejected() OVERRIDE;
    100   virtual void OnSettingsExhausted() OVERRIDE;
    101 
    102  private:
    103   // Called by the various network notifications.
    104   void OnNetworkEvent();
    105 
    106   // Stops any existing reconnect timer and sets an initial reconnect
    107   // interval.
    108   void ResetReconnectState();
    109 
    110   // Tries to reconnect in some point in the future.  If called
    111   // repeatedly, will wait longer and longer until reconnecting.
    112   void TryReconnect();
    113 
    114   // The actual function (called by |reconnect_timer_|) that does the
    115   // reconnection.
    116   void DoReconnect();
    117 
    118   Delegate* const delegate_;
    119   LoginSettings login_settings_;
    120   scoped_ptr<SingleLoginAttempt> single_attempt_;
    121 
    122   // reconnection state.
    123   base::TimeDelta reconnect_interval_;
    124   base::OneShotTimer<Login> reconnect_timer_;
    125 
    126   DISALLOW_COPY_AND_ASSIGN(Login);
    127 };
    128 
    129 }  // namespace notifier
    130 
    131 #endif  // JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_
    132