| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // The signin manager encapsulates some functionality tracking | 5 // The signin manager encapsulates some functionality tracking |
| 6 // which user is signed in. | 6 // which user is signed in. |
| 7 // | 7 // |
| 8 // **NOTE** on semantics of SigninManager: | 8 // **NOTE** on semantics of SigninManager: |
| 9 // | 9 // |
| 10 // Once a signin is successful, the username becomes "established" and will not | 10 // Once a signin is successful, the username becomes "established" and will not |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "base/gtest_prod_util.h" | 29 #include "base/gtest_prod_util.h" |
| 30 #include "base/logging.h" | 30 #include "base/logging.h" |
| 31 #include "base/memory/scoped_ptr.h" | 31 #include "base/memory/scoped_ptr.h" |
| 32 #include "base/observer_list.h" | 32 #include "base/observer_list.h" |
| 33 #include "base/prefs/pref_change_registrar.h" | 33 #include "base/prefs/pref_change_registrar.h" |
| 34 #include "base/prefs/pref_member.h" | 34 #include "base/prefs/pref_member.h" |
| 35 #include "components/keyed_service/core/keyed_service.h" | 35 #include "components/keyed_service/core/keyed_service.h" |
| 36 #include "components/signin/core/browser/signin_internals_util.h" | 36 #include "components/signin/core/browser/signin_internals_util.h" |
| 37 #include "google_apis/gaia/google_service_auth_error.h" | 37 #include "google_apis/gaia/google_service_auth_error.h" |
| 38 | 38 |
| 39 class AccountTrackerService; |
| 39 class PrefService; | 40 class PrefService; |
| 40 | |
| 41 class SigninClient; | 41 class SigninClient; |
| 42 | 42 |
| 43 class SigninManagerBase : public KeyedService { | 43 class SigninManagerBase : public KeyedService { |
| 44 public: | 44 public: |
| 45 class Observer { | 45 class Observer { |
| 46 public: | 46 public: |
| 47 // Called when a user fails to sign into Google services such as sync. | 47 // Called when a user fails to sign into Google services such as sync. |
| 48 virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) {} | 48 virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) {} |
| 49 | 49 |
| 50 // Called when a user signs into Google services such as sync. | 50 // Called when a user signs into Google services such as sync. |
| 51 virtual void GoogleSigninSucceeded(const std::string& account_id, | 51 virtual void GoogleSigninSucceeded(const std::string& account_id, |
| 52 const std::string& username, | 52 const std::string& username, |
| 53 const std::string& password) {} | 53 const std::string& password) {} |
| 54 | 54 |
| 55 // Called when the currently signed-in user for a user has been signed out. | 55 // Called when the currently signed-in user for a user has been signed out. |
| 56 virtual void GoogleSignedOut(const std::string& account_id, | 56 virtual void GoogleSignedOut(const std::string& account_id, |
| 57 const std::string& username) {} | 57 const std::string& username) {} |
| 58 | 58 |
| 59 protected: | 59 protected: |
| 60 virtual ~Observer() {} | 60 virtual ~Observer() {} |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 SigninManagerBase(SigninClient* client); | 63 SigninManagerBase(SigninClient* client, |
| 64 AccountTrackerService* account_tracker_service); |
| 64 ~SigninManagerBase() override; | 65 ~SigninManagerBase() override; |
| 65 | 66 |
| 66 // If user was signed in, load tokens from DB if available. | 67 // If user was signed in, load tokens from DB if available. |
| 67 virtual void Initialize(PrefService* local_state); | 68 virtual void Initialize(PrefService* local_state); |
| 68 bool IsInitialized() const; | 69 bool IsInitialized() const; |
| 69 | 70 |
| 70 // Returns true if a signin to Chrome is allowed (by policy or pref). | 71 // Returns true if a signin to Chrome is allowed (by policy or pref). |
| 71 // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h. | 72 // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h. |
| 72 // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be | 73 // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be |
| 73 // needed, but as is we provide this method to let all interested code | 74 // needed, but as is we provide this method to let all interested code |
| 74 // code query the value in one way, versus half using PrefService directly | 75 // code query the value in one way, versus half using PrefService directly |
| 75 // and the other half using SigninManager. | 76 // and the other half using SigninManager. |
| 76 virtual bool IsSigninAllowed() const; | 77 virtual bool IsSigninAllowed() const; |
| 77 | 78 |
| 78 // If a user has previously signed in (and has not signed out), this returns | 79 // If a user has previously signed in (and has not signed out), this returns |
| 79 // the normalized email address of the account. Otherwise, it returns an empty | 80 // the normalized email address of the account. Otherwise, it returns an empty |
| 80 // string. | 81 // string. |
| 81 const std::string& GetAuthenticatedUsername() const; | 82 std::string GetAuthenticatedUsername() const; |
| 82 | 83 |
| 83 // If a user has previously signed in (and has not signed out), this returns | 84 // If a user has previously signed in (and has not signed out), this returns |
| 84 // the account id. Otherwise, it returns an empty string. This id can be used | 85 // the account id. Otherwise, it returns an empty string. This id can be used |
| 85 // to uniquely identify an account, so for example can be used as a key to | 86 // to uniquely identify an account, so for example can be used as a key to |
| 86 // map accounts to data. | 87 // map accounts to data. |
| 87 // | 88 // |
| 88 // TODO(rogerta): eventually the account id should be an obfuscated gaia id. | 89 // TODO(rogerta): eventually the account id should be an obfuscated gaia id. |
| 89 // For now though, this function returns the same value as | 90 // For now though, this function returns the same value as |
| 90 // GetAuthenticatedUsername() since lots of code assumes the unique id for an | 91 // GetAuthenticatedUsername() since lots of code assumes the unique id for an |
| 91 // account is the username. For code that needs a unique id to represent the | 92 // account is the username. For code that needs a unique id to represent the |
| 92 // connected account, call this method. Example: the AccountInfoMap type | 93 // connected account, call this method. Example: the AccountInfoMap type |
| 93 // in MutableProfileOAuth2TokenService. For code that needs to know the | 94 // in MutableProfileOAuth2TokenService. For code that needs to know the |
| 94 // normalized email address of the connected account, use | 95 // normalized email address of the connected account, use |
| 95 // GetAuthenticatedUsername(). Example: to show the string "Signed in as XXX" | 96 // GetAuthenticatedUsername(). Example: to show the string "Signed in as XXX" |
| 96 // in the hotdog menu. | 97 // in the hotdog menu. |
| 97 const std::string& GetAuthenticatedAccountId() const; | 98 const std::string& GetAuthenticatedAccountId() const; |
| 98 | 99 |
| 99 // Sets the user name. Note: |username| should be already authenticated as | 100 // Sets the authenticated user's Gaia ID and display email. Internally, |
| 100 // this is a sticky operation (in contrast to StartSignIn). | 101 // this will seed the account information in AccountTrackerService and pick |
| 101 // TODO(tim): Remove this in favor of passing username on construction by | 102 // the right account_id for this account. |
| 102 // (by platform / depending on StartBehavior). Bug 88109. | 103 void SetAuthenticatedAccountInfo(const std::string& gaia_id, |
| 103 void SetAuthenticatedUsername(const std::string& username); | 104 const std::string& email); |
| 104 | 105 |
| 105 // Returns true if there is an authenticated user. | 106 // Returns true if there is an authenticated user. |
| 106 bool IsAuthenticated() const; | 107 bool IsAuthenticated() const; |
| 107 | 108 |
| 108 // Returns true if there's a signin in progress. | 109 // Returns true if there's a signin in progress. |
| 109 virtual bool AuthInProgress() const; | 110 virtual bool AuthInProgress() const; |
| 110 | 111 |
| 111 // KeyedService implementation. | 112 // KeyedService implementation. |
| 112 void Shutdown() override; | 113 void Shutdown() override; |
| 113 | 114 |
| 114 // Methods to register or remove observers of signin. | 115 // Methods to register or remove observers of signin. |
| 115 void AddObserver(Observer* observer); | 116 void AddObserver(Observer* observer); |
| 116 void RemoveObserver(Observer* observer); | 117 void RemoveObserver(Observer* observer); |
| 117 | 118 |
| 118 // Methods to register or remove SigninDiagnosticObservers. | 119 // Methods to register or remove SigninDiagnosticObservers. |
| 119 void AddSigninDiagnosticsObserver( | 120 void AddSigninDiagnosticsObserver( |
| 120 signin_internals_util::SigninDiagnosticsObserver* observer); | 121 signin_internals_util::SigninDiagnosticsObserver* observer); |
| 121 void RemoveSigninDiagnosticsObserver( | 122 void RemoveSigninDiagnosticsObserver( |
| 122 signin_internals_util::SigninDiagnosticsObserver* observer); | 123 signin_internals_util::SigninDiagnosticsObserver* observer); |
| 123 | 124 |
| 124 protected: | 125 protected: |
| 125 // Used by subclass to clear authenticated_username_ instead of using | 126 AccountTrackerService* account_tracker_service() const { |
| 126 // SetAuthenticatedUsername, which enforces special preconditions due | 127 return account_tracker_service_; |
| 128 } |
| 129 |
| 130 SigninClient* signin_client() const { return client_; } |
| 131 |
| 132 // Sets the authenticated user's account id. |
| 133 void SetAuthenticatedAccountId(const std::string& account_id); |
| 134 |
| 135 // Used by subclass to clear the authenticated user instead of using |
| 136 // SetAuthenticatedAccountId, which enforces special preconditions due |
| 127 // to the fact that it is part of the public API and called by clients. | 137 // to the fact that it is part of the public API and called by clients. |
| 128 void ClearAuthenticatedUsername(); | 138 void clear_authenticated_user() { authenticated_account_id_.clear(); } |
| 129 | 139 |
| 130 // List of observers to notify on signin events. | 140 // List of observers to notify on signin events. |
| 131 // Makes sure list is empty on destruction. | 141 // Makes sure list is empty on destruction. |
| 132 ObserverList<Observer, true> observer_list_; | 142 ObserverList<Observer, true> observer_list_; |
| 133 | 143 |
| 134 // Helper method to notify all registered diagnostics observers with. | 144 // Helper method to notify all registered diagnostics observers with. |
| 135 void NotifyDiagnosticsObservers( | 145 void NotifyDiagnosticsObservers( |
| 136 const signin_internals_util::TimedSigninStatusField& field, | 146 const signin_internals_util::TimedSigninStatusField& field, |
| 137 const std::string& value); | 147 const std::string& value); |
| 138 | 148 |
| 139 private: | 149 private: |
| 140 friend class FakeSigninManagerBase; | 150 friend class FakeSigninManagerBase; |
| 141 friend class FakeSigninManager; | 151 friend class FakeSigninManager; |
| 142 | 152 |
| 143 SigninClient* client_; | 153 SigninClient* client_; |
| 154 AccountTrackerService* account_tracker_service_; |
| 144 bool initialized_; | 155 bool initialized_; |
| 145 | 156 |
| 146 // Actual username and account_id after successful authentication. | 157 // Account id after successful authentication. |
| 147 std::string authenticated_username_; | |
| 148 std::string authenticated_account_id_; | 158 std::string authenticated_account_id_; |
| 149 | 159 |
| 150 // The list of SigninDiagnosticObservers. | 160 // The list of SigninDiagnosticObservers. |
| 151 ObserverList<signin_internals_util::SigninDiagnosticsObserver, true> | 161 ObserverList<signin_internals_util::SigninDiagnosticsObserver, true> |
| 152 signin_diagnostics_observers_; | 162 signin_diagnostics_observers_; |
| 153 | 163 |
| 154 base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_; | 164 base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_; |
| 155 | 165 |
| 156 DISALLOW_COPY_AND_ASSIGN(SigninManagerBase); | 166 DISALLOW_COPY_AND_ASSIGN(SigninManagerBase); |
| 157 }; | 167 }; |
| 158 | 168 |
| 159 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_ | 169 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_ |
| OLD | NEW |