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.
5 #ifndef JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_
6 #define JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_
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"
24 class XmppClientSettings
;
25 class XmppTaskParentInterface
;
29 class URLRequestContextGetter
;
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.
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
{
48 // Called when a connection has been successfully established.
49 virtual void OnConnect(
50 base::WeakPtr
<buzz::XmppTaskParentInterface
> base_task
) = 0;
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;
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
61 virtual void OnCredentialsRejected() = 0;
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
);
77 // Starts connecting (or forces a reconnection if we're backed off).
78 void StartConnection();
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
);
85 // net::NetworkChangeNotifier::IPAddressObserver implementation.
86 virtual void OnIPAddressChanged() OVERRIDE
;
88 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
89 virtual void OnConnectionTypeChanged(
90 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
92 // net::NetworkChangeNotifier::DNSObserver implementation.
93 virtual void OnDNSChanged() OVERRIDE
;
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
;
103 // Called by the various network notifications.
104 void OnNetworkEvent();
106 // Stops any existing reconnect timer and sets an initial reconnect
108 void ResetReconnectState();
110 // Tries to reconnect in some point in the future. If called
111 // repeatedly, will wait longer and longer until reconnecting.
114 // The actual function (called by |reconnect_timer_|) that does the
118 Delegate
* const delegate_
;
119 LoginSettings login_settings_
;
120 scoped_ptr
<SingleLoginAttempt
> single_attempt_
;
122 // reconnection state.
123 base::TimeDelta reconnect_interval_
;
124 base::OneShotTimer
<Login
> reconnect_timer_
;
126 DISALLOW_COPY_AND_ASSIGN(Login
);
129 } // namespace notifier
131 #endif // JINGLE_NOTIFIER_COMMUNICATOR_LOGIN_H_