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/signin_internals_util.h"
37 #include "google_apis/gaia/google_service_auth_error.h"
41 // TODO(blundell): Eliminate this forward declaration once SigninManager no
42 // longer needs Profile. crbug.com/334209
46 // Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL.
47 // TODO(blundell): Eliminate this struct once crbug.com/333997 is fixed.
48 // A listener might use this to make note of a username / password
49 // pair for encryption keys.
50 struct GoogleServiceSigninSuccessDetails
{
51 GoogleServiceSigninSuccessDetails(const std::string
& in_username
,
52 const std::string
& in_password
)
53 : username(in_username
), password(in_password
) {}
58 // Details for the Notification type NOTIFICATION_GOOGLE_SIGNED_OUT.
59 // TODO(blundell): Eliminate this struct once crbug.com/333997 is fixed.
60 struct GoogleServiceSignoutDetails
{
61 explicit GoogleServiceSignoutDetails(const std::string
& in_username
)
62 : username(in_username
) {}
66 class SigninManagerBase
: public KeyedService
{
70 // Called when a user fails to sign into Google services such as sync.
71 virtual void GoogleSigninFailed(const GoogleServiceAuthError
& error
) {}
73 // Called when a user signs into Google services such as sync.
74 virtual void GoogleSigninSucceeded(const std::string
& username
,
75 const std::string
& password
) {}
77 // Called when the currently signed-in user for a user has been signed out.
78 virtual void GoogleSignedOut(const std::string
& username
) {}
81 virtual ~Observer() {}
84 SigninManagerBase(SigninClient
* client
);
85 virtual ~SigninManagerBase();
87 // If user was signed in, load tokens from DB if available.
88 // TODO(blundell): Eliminate the |profile| argument once SigninManager no
89 // longer needs Profile. crbug.com/334209
90 virtual void Initialize(Profile
* profile
, PrefService
* local_state
);
91 bool IsInitialized() const;
93 // Returns true if a signin to Chrome is allowed (by policy or pref).
94 // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
95 // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
96 // needed, but as is we provide this method to let all interested code
97 // code query the value in one way, versus half using PrefService directly
98 // and the other half using SigninManager.
99 virtual bool IsSigninAllowed() const;
101 // If a user has previously signed in (and has not signed out), this returns
102 // the normalized email address of the account. Otherwise, it returns an empty
104 const std::string
& GetAuthenticatedUsername() const;
106 // If a user has previously signed in (and has not signed out), this returns
107 // the account id. Otherwise, it returns an empty string. This id can be used
108 // to uniquely identify an account, so for example can be used as a key to
109 // map accounts to data.
111 // TODO(rogerta): eventually the account id should be an obfuscated gaia id.
112 // For now though, this function returns the same value as
113 // GetAuthenticatedUsername() since lots of code assumes the unique id for an
114 // account is the username. For code that needs a unique id to represent the
115 // connected account, call this method. Example: the AccountInfoMap type
116 // in MutableProfileOAuth2TokenService. For code that needs to know the
117 // normalized email address of the connected account, use
118 // GetAuthenticatedUsername(). Example: to show the string "Signed in as XXX"
119 // in the hotdog menu.
120 const std::string
& GetAuthenticatedAccountId() const;
122 // Sets the user name. Note: |username| should be already authenticated as
123 // this is a sticky operation (in contrast to StartSignIn).
124 // TODO(tim): Remove this in favor of passing username on construction by
125 // (by platform / depending on StartBehavior). Bug 88109.
126 void SetAuthenticatedUsername(const std::string
& username
);
128 // Returns true if there's a signin in progress.
129 virtual bool AuthInProgress() const;
131 // KeyedService implementation.
132 virtual void Shutdown() OVERRIDE
;
134 // Methods to register or remove observers of signin.
135 void AddObserver(Observer
* observer
);
136 void RemoveObserver(Observer
* observer
);
138 // Methods to register or remove SigninDiagnosticObservers.
139 void AddSigninDiagnosticsObserver(
140 signin_internals_util::SigninDiagnosticsObserver
* observer
);
141 void RemoveSigninDiagnosticsObserver(
142 signin_internals_util::SigninDiagnosticsObserver
* observer
);
145 // Used by subclass to clear authenticated_username_ instead of using
146 // SetAuthenticatedUsername, which enforces special preconditions due
147 // to the fact that it is part of the public API and called by clients.
148 void clear_authenticated_username();
150 // List of observers to notify on signin events.
151 // Makes sure list is empty on destruction.
152 ObserverList
<Observer
, true> observer_list_
;
154 // Helper methods to notify all registered diagnostics observers with.
155 void NotifyDiagnosticsObservers(
156 const signin_internals_util::UntimedSigninStatusField
& field
,
157 const std::string
& value
);
158 void NotifyDiagnosticsObservers(
159 const signin_internals_util::TimedSigninStatusField
& field
,
160 const std::string
& value
);
163 friend class FakeSigninManagerBase
;
164 friend class FakeSigninManager
;
166 SigninClient
* client_
;
169 // Actual username after successful authentication.
170 std::string authenticated_username_
;
172 // The list of SigninDiagnosticObservers.
173 ObserverList
<signin_internals_util::SigninDiagnosticsObserver
, true>
174 signin_diagnostics_observers_
;
176 base::WeakPtrFactory
<SigninManagerBase
> weak_pointer_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(SigninManagerBase
);
181 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_