5b609d625ebf4dcf173d14176253795645b02bb9
[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
79    protected:
80     virtual ~Observer() {}
81   };
82
83   SigninManagerBase();
84   virtual ~SigninManagerBase();
85
86   // If user was signed in, load tokens from DB if available.
87   virtual void Initialize(Profile* profile, PrefService* local_state);
88   bool IsInitialized() const;
89
90   // Returns true if a signin to Chrome is allowed (by policy or pref).
91   // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
92   // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
93   // needed, but as is we provide this method to let all interested code
94   // code query the value in one way, versus half using PrefService directly
95   // and the other half using SigninManager.
96   virtual bool IsSigninAllowed() const;
97
98   // If a user has previously signed in (and has not signed out), this returns
99   // the normalized email address of the account. Otherwise, it returns an empty
100   // string.
101   const std::string& GetAuthenticatedUsername() const;
102
103   // If a user has previously signed in (and has not signed out), this returns
104   // the account id. Otherwise, it returns an empty string.  This id can be used
105   // to uniquely identify an account, so for example can be used as a key to
106   // map accounts to data.
107   //
108   // TODO(rogerta): eventually the account id should be an obfuscated gaia id.
109   // For now though, this function returns the same value as
110   // GetAuthenticatedUsername() since lots of code assumes the unique id for an
111   // account is the username.  For code that needs a unique id to represent the
112   // connected account, call this method. Example: the AccountInfoMap type
113   // in MutableProfileOAuth2TokenService.  For code that needs to know the
114   // normalized email address of the connected account, use
115   // GetAuthenticatedUsername().  Example: to show the string "Signed in as XXX"
116   // in the hotdog menu.
117   const std::string& GetAuthenticatedAccountId() const;
118
119   // Sets the user name.  Note: |username| should be already authenticated as
120   // this is a sticky operation (in contrast to StartSignIn).
121   // TODO(tim): Remove this in favor of passing username on construction by
122   // (by platform / depending on StartBehavior). Bug 88109.
123   void SetAuthenticatedUsername(const std::string& username);
124
125   // Returns true if there's a signin in progress.
126   virtual bool AuthInProgress() const;
127
128   // BrowserContextKeyedService implementation.
129   virtual void Shutdown() OVERRIDE;
130
131   // Methods to register or remove observers of signin.
132   void AddObserver(Observer* observer);
133   void RemoveObserver(Observer* observer);
134
135   // Methods to register or remove SigninDiagnosticObservers.
136   void AddSigninDiagnosticsObserver(
137       signin_internals_util::SigninDiagnosticsObserver* observer);
138   void RemoveSigninDiagnosticsObserver(
139       signin_internals_util::SigninDiagnosticsObserver* observer);
140
141   Profile* profile() { return profile_; }
142
143  protected:
144   // Used by subclass to clear authenticated_username_ instead of using
145   // SetAuthenticatedUsername, which enforces special preconditions due
146   // to the fact that it is part of the public API and called by clients.
147   void clear_authenticated_username();
148
149   // Pointer to parent profile (protected so FakeSigninManager can access
150   // it).
151   Profile* profile_;
152
153   // List of observers to notify on signin events.
154   // Makes sure list is empty on destruction.
155   ObserverList<Observer, true> observer_list_;
156
157   // Helper methods to notify all registered diagnostics observers with.
158   void NotifyDiagnosticsObservers(
159       const signin_internals_util::UntimedSigninStatusField& field,
160       const std::string& value);
161   void NotifyDiagnosticsObservers(
162       const signin_internals_util::TimedSigninStatusField& field,
163       const std::string& value);
164
165  private:
166   friend class FakeSigninManagerBase;
167   friend class FakeSigninManager;
168
169   // Actual username after successful authentication.
170   std::string authenticated_username_;
171
172   // The list of SigninDiagnosticObservers.
173   ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
174       signin_diagnostics_observers_;
175
176   base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_;
177
178   DISALLOW_COPY_AND_ASSIGN(SigninManagerBase);
179 };
180
181 #endif  // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_