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.
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 CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_
24 #define CHROME_BROWSER_SIGNIN_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 "chrome/browser/profiles/profile.h"
36 #include "chrome/browser/signin/signin_internals_util.h"
37 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
38 #include "google_apis/gaia/google_service_auth_error.h"
44 // Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL.
45 // TODO(blundell): Eliminate this struct once crbug.com/333997 is fixed.
46 // A listener might use this to make note of a username / password
47 // pair for encryption keys.
48 struct GoogleServiceSigninSuccessDetails {
49 GoogleServiceSigninSuccessDetails(const std::string& in_username,
50 const std::string& in_password)
51 : username(in_username),
52 password(in_password) {}
57 // Details for the Notification type NOTIFICATION_GOOGLE_SIGNED_OUT.
58 // TODO(blundell): Eliminate this struct once crbug.com/333997 is fixed.
59 struct GoogleServiceSignoutDetails {
60 explicit GoogleServiceSignoutDetails(const std::string& in_username)
61 : username(in_username) {}
65 class SigninManagerBase : public BrowserContextKeyedService {
69 // Called when a user fails to sign into Google services such as sync.
70 virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) {}
72 // Called when a user signs into Google services such as sync.
73 virtual void GoogleSigninSucceeded(const std::string& username,
74 const std::string& password) {}
76 // Called when the currently signed-in user for a user has been signed out.
77 virtual void GoogleSignedOut(const std::string& username) {}
79 virtual ~Observer() {}
83 virtual ~SigninManagerBase();
85 // If user was signed in, load tokens from DB if available.
86 virtual void Initialize(Profile* profile, PrefService* local_state);
87 bool IsInitialized() const;
89 // Returns true if a signin to Chrome is allowed (by policy or pref).
90 // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
91 // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
92 // needed, but as is we provide this method to let all interested code
93 // code query the value in one way, versus half using PrefService directly
94 // and the other half using SigninManager.
95 virtual bool IsSigninAllowed() const;
97 // If a user has previously signed in (and has not signed out), this returns
98 // the normalized email address of the account. Otherwise, it returns an empty
100 const std::string& GetAuthenticatedUsername() const;
102 // If a user has previously signed in (and has not signed out), this returns
103 // the account id. Otherwise, it returns an empty string. This id can be used
104 // to uniquely identify an account, so for example can be used as a key to
105 // map accounts to data.
107 // TODO(rogerta): eventually the account id should be an obfuscated gaia id.
108 // For now though, this function returns the same value as
109 // GetAuthenticatedUsername() since lots of code assumes the unique id for an
110 // account is the username. For code that needs a unique id to represent the
111 // connected account, call this method. Example: the AccountInfoMap type
112 // in MutableProfileOAuth2TokenService. For code that needs to know the
113 // normalized email address of the connected account, use
114 // GetAuthenticatedUsername(). Example: to show the string "Signed in as XXX"
115 // in the hotdog menu.
116 const std::string& GetAuthenticatedAccountId() const;
118 // Sets the user name. Note: |username| should be already authenticated as
119 // this is a sticky operation (in contrast to StartSignIn).
120 // TODO(tim): Remove this in favor of passing username on construction by
121 // (by platform / depending on StartBehavior). Bug 88109.
122 void SetAuthenticatedUsername(const std::string& username);
124 // Returns true if there's a signin in progress.
125 virtual bool AuthInProgress() const;
127 // BrowserContextKeyedService implementation.
128 virtual void Shutdown() OVERRIDE;
130 // Methods to register or remove observers of signin.
131 void AddObserver(Observer* observer);
132 void RemoveObserver(Observer* observer);
134 // Methods to register or remove SigninDiagnosticObservers.
135 void AddSigninDiagnosticsObserver(
136 signin_internals_util::SigninDiagnosticsObserver* observer);
137 void RemoveSigninDiagnosticsObserver(
138 signin_internals_util::SigninDiagnosticsObserver* observer);
140 Profile* profile() { return profile_; }
143 // Used by subclass to clear authenticated_username_ instead of using
144 // SetAuthenticatedUsername, which enforces special preconditions due
145 // to the fact that it is part of the public API and called by clients.
146 void clear_authenticated_username();
148 // Pointer to parent profile (protected so FakeSigninManager can access
152 // List of observers to notify on signin events.
153 // Makes sure list is empty on destruction.
154 ObserverList<Observer, true> observer_list_;
156 // Helper methods to notify all registered diagnostics observers with.
157 void NotifyDiagnosticsObservers(
158 const signin_internals_util::UntimedSigninStatusField& field,
159 const std::string& value);
160 void NotifyDiagnosticsObservers(
161 const signin_internals_util::TimedSigninStatusField& field,
162 const std::string& value);
165 friend class FakeSigninManagerBase;
166 friend class FakeSigninManager;
168 // Actual username after successful authentication.
169 std::string authenticated_username_;
171 // The list of SigninDiagnosticObservers.
172 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 // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_