1// Copyright (c) 2011 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 CHROME_BROWSER_CHROMEOS_LOGIN_ONLINE_ATTEMPT_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_ONLINE_ATTEMPT_H_
7#pragma once
8
9#include <string>
10
11
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "chrome/browser/chromeos/login/login_status_consumer.h"
15#include "chrome/common/net/gaia/gaia_auth_consumer.h"
16#include "chrome/common/net/gaia/google_service_auth_error.h"
17
18class CancelableTask;
19class GaiaAuthFetcher;
20class Profile;
21
22namespace net {
23class URLRequestContextGetter;
24}
25
26namespace chromeos {
27class AuthAttemptState;
28class AuthAttemptStateResolver;
29
30class OnlineAttempt
31    : public base::RefCountedThreadSafe<OnlineAttempt>,
32      public GaiaAuthConsumer {
33 public:
34  OnlineAttempt(AuthAttemptState* current_attempt,
35                AuthAttemptStateResolver* callback);
36  virtual ~OnlineAttempt();
37
38  // Initiate the online login attempt.  Status will be recorded in
39  // |current_attempt|, and resolver_->Resolve() will be called on the
40  // IO thread when useful state is available.
41  // Must be called on the UI thread.
42  void Initiate(Profile* profile);
43
44  // Callbacks from GaiaAuthFetcher
45  virtual void OnClientLoginFailure(
46      const GoogleServiceAuthError& error);
47  virtual void OnClientLoginSuccess(
48      const GaiaAuthConsumer::ClientLoginResult& credentials);
49
50 private:
51  // Milliseconds until we timeout our attempt to hit ClientLogin.
52  static const int kClientLoginTimeoutMs;
53
54  void TryClientLogin();
55  void CancelClientLogin();
56
57  void TriggerResolve(const GaiaAuthConsumer::ClientLoginResult& credentials,
58                      const LoginFailure& outcome);
59
60  AuthAttemptState* const attempt_;
61  AuthAttemptStateResolver* const resolver_;
62
63  // Handles all net communications with Gaia.
64  scoped_ptr<GaiaAuthFetcher> gaia_authenticator_;
65  CancelableTask* fetch_canceler_;
66
67  // Whether we're willing to re-try the ClientLogin attempt.
68  bool try_again_;
69
70  friend class OnlineAttemptTest;
71  DISALLOW_COPY_AND_ASSIGN(OnlineAttempt);
72};
73
74}  // namespace chromeos
75
76#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_ONLINE_ATTEMPT_H_
77