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