b3698151fe35947ffeaec6205e5151f3c3148cc8
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_manager_base.h
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.
4 //
5 // The signin manager encapsulates some functionality tracking
6 // which user is signed in.
7 //
8 // **NOTE** on semantics of SigninManager:
9 //
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.
14 //
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.
22
23 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_
24 #define CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_
25
26 #include <string>
27
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"
39
40 class CookieSettings;
41 class ProfileIOData;
42 class PrefService;
43
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) {}
53   std::string username;
54   std::string password;
55 };
56
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) {}
62   std::string username;
63 };
64
65 class SigninManagerBase : public BrowserContextKeyedService {
66  public:
67   class Observer {
68    public:
69     // Called when a user fails to sign into Google services such as sync.
70     virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) {}
71
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) {}
75
76     // Called when the currently signed-in user for a user has been signed out.
77     virtual void GoogleSignedOut(const std::string& username) {}
78    protected:
79     virtual ~Observer() {}
80   };
81
82   SigninManagerBase();
83   virtual ~SigninManagerBase();
84
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;
88
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;
96
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
99   // string.
100   const std::string& GetAuthenticatedUsername() const;
101
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.
106   //
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;
117
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);
123
124   // Returns true if there's a signin in progress.
125   virtual bool AuthInProgress() const;
126
127   // BrowserContextKeyedService implementation.
128   virtual void Shutdown() OVERRIDE;
129
130   // Methods to register or remove observers of signin.
131   void AddObserver(Observer* observer);
132   void RemoveObserver(Observer* observer);
133
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);
139
140   Profile* profile() { return profile_; }
141
142  protected:
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();
147
148   // Pointer to parent profile (protected so FakeSigninManager can access
149   // it).
150   Profile* profile_;
151
152   // List of observers to notify on signin events.
153   // Makes sure list is empty on destruction.
154   ObserverList<Observer, true> observer_list_;
155
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);
163
164  private:
165   friend class FakeSigninManagerBase;
166   friend class FakeSigninManager;
167
168   // Actual username after successful authentication.
169   std::string authenticated_username_;
170
171   // The list of SigninDiagnosticObservers.
172   ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
173       signin_diagnostics_observers_;
174
175   base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_;
176
177   DISALLOW_COPY_AND_ASSIGN(SigninManagerBase);
178 };
179
180 #endif  // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_