• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // The signin manager encapsulates some functionality tracking
6 // which user is signed in. When a user is signed in, a ClientLogin
7 // request is run on their behalf. Auth tokens are fetched from Google
8 // and the results are stored in the TokenService.
9 
10 #ifndef CHROME_BROWSER_SYNC_SIGNIN_MANAGER_H_
11 #define CHROME_BROWSER_SYNC_SIGNIN_MANAGER_H_
12 #pragma once
13 
14 #include <string>
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
18 #include "chrome/common/net/gaia/google_service_auth_error.h"
19 
20 class GaiaAuthFetcher;
21 class Profile;
22 class PrefService;
23 
24 // Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL.
25 // A listener might use this to make note of a username / password
26 // pair for encryption keys.
27 struct GoogleServiceSigninSuccessDetails {
GoogleServiceSigninSuccessDetailsGoogleServiceSigninSuccessDetails28   GoogleServiceSigninSuccessDetails(const std::string& in_username,
29                                     const std::string& in_password)
30       : username(in_username),
31         password(in_password) {}
32   std::string username;
33   std::string password;
34 };
35 
36 class SigninManager : public GaiaAuthConsumer {
37  public:
38   SigninManager();
39   virtual ~SigninManager();
40 
41   // Call to register our prefs.
42   static void RegisterUserPrefs(PrefService* user_prefs);
43 
44   // If user was signed in, load tokens from DB if available.
45   void Initialize(Profile* profile);
46 
47   // If a user is signed in, this will return their name.
48   // Otherwise, it will return an empty string.
49   const std::string& GetUsername();
50 
51   // Sets the user name.  Used for migrating credentials from previous system.
52   void SetUsername(const std::string& username);
53 
54   // Attempt to sign in this user. If successful, set a preference indicating
55   // the signed in user and send out a notification, then start fetching tokens
56   // for the user.
57   void StartSignIn(const std::string& username,
58                    const std::string& password,
59                    const std::string& login_token,
60                    const std::string& login_captcha);
61 
62   // Used when a second factor access code was required to complete a signin
63   // attempt.
64   void ProvideSecondFactorAccessCode(const std::string& access_code);
65 
66   // Sign a user out, removing the preference, erasing all keys
67   // associated with the user, and canceling all auth in progress.
68   void SignOut();
69 
70   // GaiaAuthConsumer
71   virtual void OnClientLoginSuccess(const ClientLoginResult& result);
72   virtual void OnClientLoginFailure(const GoogleServiceAuthError& error);
73   virtual void OnGetUserInfoSuccess(const std::string& key,
74                                     const std::string& value);
75   virtual void OnGetUserInfoKeyNotFound(const std::string& key);
76   virtual void OnGetUserInfoFailure(const GoogleServiceAuthError& error);
77 
78  private:
79   Profile* profile_;
80   std::string username_;
81   std::string password_;  // This is kept empty whenever possible.
82   bool had_two_factor_error_;
83 
84   // Result of the last client login, kept pending the lookup of the
85   // canonical email.
86   ClientLoginResult last_result_;
87 
88   // Actual client login handler.
89   scoped_ptr<GaiaAuthFetcher> client_login_;
90 };
91 
92 #endif  // CHROME_BROWSER_SYNC_SIGNIN_MANAGER_H_
93