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
3 // found in the LICENSE file.
5 // The signin manager encapsulates some functionality tracking
6 // which user is signed in.
8 // **NOTE** on semantics of SigninManager:
10 // Once a signin is successful, the username becomes "established" and will not
11 // be cleared until a SignOut operation is performed (persists across
12 // restarts). Until that happens, the signin manager can still be used to
13 // refresh credentials, but changing the username is not permitted.
15 // On Chrome OS, because of the existence of other components that handle login
16 // and signin at a higher level, all that is needed from a SigninManager is
17 // caching / handling of the "authenticated username" field, and TokenService
18 // initialization, so that components that depend on these two things
19 // (i.e on desktop) can continue using it / don't need to change. For this
20 // reason, SigninManagerBase is all that exists on Chrome OS. For desktop,
21 // see signin/signin_manager.h.
23 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_
24 #define COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_
28 #include "base/compiler_specific.h"
29 #include "base/gtest_prod_util.h"
30 #include "base/logging.h"
31 #include "base/memory/scoped_ptr.h"
32 #include "base/observer_list.h"
33 #include "base/prefs/pref_change_registrar.h"
34 #include "base/prefs/pref_member.h"
35 #include "components/keyed_service/core/keyed_service.h"
36 #include "components/signin/core/browser/account_info.h"
37 #include "components/signin/core/browser/signin_internals_util.h"
38 #include "google_apis/gaia/google_service_auth_error.h"
40 class AccountTrackerService
;
41 class PrefRegistrySimple
;
45 namespace user_prefs
{
46 class PrefRegistrySyncable
;
49 class SigninManagerBase
: public KeyedService
{
53 // Called when a user fails to sign into Google services such as sync.
54 virtual void GoogleSigninFailed(const GoogleServiceAuthError
& error
) {}
56 // Called when a user signs into Google services such as sync.
57 virtual void GoogleSigninSucceeded(const std::string
& account_id
,
58 const std::string
& username
,
59 const std::string
& password
) {}
61 // Called when the currently signed-in user for a user has been signed out.
62 virtual void GoogleSignedOut(const std::string
& account_id
,
63 const std::string
& username
) {}
66 virtual ~Observer() {}
69 SigninManagerBase(SigninClient
* client
,
70 AccountTrackerService
* account_tracker_service
);
71 ~SigninManagerBase() override
;
73 // Registers per-profile prefs.
74 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
76 // Registers per-install prefs.
77 static void RegisterPrefs(PrefRegistrySimple
* registry
);
79 // If user was signed in, load tokens from DB if available.
80 virtual void Initialize(PrefService
* local_state
);
81 bool IsInitialized() const;
83 // Returns true if a signin to Chrome is allowed (by policy or pref).
84 // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
85 // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
86 // needed, but as is we provide this method to let all interested code
87 // code query the value in one way, versus half using PrefService directly
88 // and the other half using SigninManager.
89 virtual bool IsSigninAllowed() const;
91 // If a user has previously signed in (and has not signed out), this returns
92 // the know information of the account. Otherwise, it returns an empty struct.
93 AccountInfo
GetAuthenticatedAccountInfo() const;
95 // If a user has previously signed in (and has not signed out), this returns
96 // the account id. Otherwise, it returns an empty string. This id can be used
97 // to uniquely identify an account, so for example can be used as a key to
98 // map accounts to data.
100 // TODO(rogerta): eventually the account id should be an obfuscated gaia id.
101 // For now though, this function returns the same value as
102 // GetAuthenticatedAccountInfo().email since lots of code assumes the unique
103 // id for an account is the username. For code that needs a unique id to
104 // represent the connected account, call this method. Example: the
105 // AccountStatusMap type in MutableProfileOAuth2TokenService. For code that
106 // needs to know the normalized email address of the connected account, use
107 // GetAuthenticatedAccountInfo().email. Example: to show the string "Signed
108 // in as XXX" in the hotdog menu.
109 const std::string
& GetAuthenticatedAccountId() const;
111 // Sets the authenticated user's Gaia ID and display email. Internally,
112 // this will seed the account information in AccountTrackerService and pick
113 // the right account_id for this account.
114 void SetAuthenticatedAccountInfo(const std::string
& gaia_id
,
115 const std::string
& email
);
117 // Returns true if there is an authenticated user.
118 bool IsAuthenticated() const;
120 // Returns true if there's a signin in progress.
121 virtual bool AuthInProgress() const;
123 // KeyedService implementation.
124 void Shutdown() override
;
126 // Methods to register or remove observers of signin.
127 void AddObserver(Observer
* observer
);
128 void RemoveObserver(Observer
* observer
);
130 // Methods to register or remove SigninDiagnosticObservers.
131 void AddSigninDiagnosticsObserver(
132 signin_internals_util::SigninDiagnosticsObserver
* observer
);
133 void RemoveSigninDiagnosticsObserver(
134 signin_internals_util::SigninDiagnosticsObserver
* observer
);
137 AccountTrackerService
* account_tracker_service() const {
138 return account_tracker_service_
;
141 SigninClient
* signin_client() const { return client_
; }
143 // Sets the authenticated user's account id.
144 void SetAuthenticatedAccountId(const std::string
& account_id
);
146 // Used by subclass to clear the authenticated user instead of using
147 // SetAuthenticatedAccountId, which enforces special preconditions due
148 // to the fact that it is part of the public API and called by clients.
149 void clear_authenticated_user() { authenticated_account_id_
.clear(); }
151 // List of observers to notify on signin events.
152 // Makes sure list is empty on destruction.
153 base::ObserverList
<Observer
, true> observer_list_
;
155 // Helper method to notify all registered diagnostics observers with.
156 void NotifyDiagnosticsObservers(
157 const signin_internals_util::TimedSigninStatusField
& field
,
158 const std::string
& value
);
161 friend class FakeSigninManagerBase
;
162 friend class FakeSigninManager
;
164 SigninClient
* client_
;
165 AccountTrackerService
* account_tracker_service_
;
168 // Account id after successful authentication.
169 std::string authenticated_account_id_
;
171 // The list of SigninDiagnosticObservers.
172 base::ObserverList
<signin_internals_util::SigninDiagnosticsObserver
, true>
173 signin_diagnostics_observers_
;
175 base::WeakPtrFactory
<SigninManagerBase
> weak_pointer_factory_
;
177 DISALLOW_COPY_AND_ASSIGN(SigninManagerBase
);
180 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_