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
39class CookieSettings;
40class ProfileIOData;
41class PrefService;
42
43// Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL.
44// A listener might use this to make note of a username / password
45// pair for encryption keys.
46struct GoogleServiceSigninSuccessDetails {
47  GoogleServiceSigninSuccessDetails(const std::string& in_username,
48                                    const std::string& in_password)
49      : username(in_username),
50        password(in_password) {}
51  std::string username;
52  std::string password;
53};
54
55// Details for the Notification type NOTIFICATION_GOOGLE_SIGNED_OUT.
56struct GoogleServiceSignoutDetails {
57  explicit GoogleServiceSignoutDetails(const std::string& in_username)
58      : username(in_username) {}
59  std::string username;
60};
61
62class SigninManagerBase : public BrowserContextKeyedService {
63 public:
64  SigninManagerBase();
65  virtual ~SigninManagerBase();
66
67  // If user was signed in, load tokens from DB if available.
68  virtual void Initialize(Profile* profile, PrefService* local_state);
69  bool IsInitialized() const;
70
71  // Returns true if a signin to Chrome is allowed (by policy or pref).
72  // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
73  // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
74  // needed, but as is we provide this method to let all interested code
75  // code query the value in one way, versus half using PrefService directly
76  // and the other half using SigninManager.
77  virtual bool IsSigninAllowed() const;
78
79  // If a user has previously established a username and SignOut has not been
80  // called, this will return the username.
81  // Otherwise, it will return an empty string.
82  const std::string& GetAuthenticatedUsername() const;
83
84  // Sets the user name.  Note: |username| should be already authenticated as
85  // this is a sticky operation (in contrast to StartSignIn).
86  // TODO(tim): Remove this in favor of passing username on construction by
87  // (by platform / depending on StartBehavior). Bug 88109.
88  void SetAuthenticatedUsername(const std::string& username);
89
90  // Returns true if there's a signin in progress.
91  virtual bool AuthInProgress() const;
92
93  // BrowserContextKeyedService implementation.
94  virtual void Shutdown() OVERRIDE;
95
96    // Methods to register or remove SigninDiagnosticObservers
97  void AddSigninDiagnosticsObserver(
98      signin_internals_util::SigninDiagnosticsObserver* observer);
99  void RemoveSigninDiagnosticsObserver(
100      signin_internals_util::SigninDiagnosticsObserver* observer);
101
102 protected:
103  // Used by subclass to clear authenticated_username_ instead of using
104  // SetAuthenticatedUsername, which enforces special preconditions due
105  // to the fact that it is part of the public API and called by clients.
106  void clear_authenticated_username();
107
108  // Pointer to parent profile (protected so FakeSigninManager can access
109  // it).
110  Profile* profile_;
111
112  // Helper methods to notify all registered diagnostics observers with.
113  void NotifyDiagnosticsObservers(
114      const signin_internals_util::UntimedSigninStatusField& field,
115      const std::string& value);
116  void NotifyDiagnosticsObservers(
117      const signin_internals_util::TimedSigninStatusField& field,
118      const std::string& value);
119
120 private:
121  friend class FakeSigninManagerBase;
122  friend class FakeSigninManager;
123
124  // Actual username after successful authentication.
125  std::string authenticated_username_;
126
127  // The list of SigninDiagnosticObservers.
128  ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
129      signin_diagnostics_observers_;
130
131  base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_;
132
133  DISALLOW_COPY_AND_ASSIGN(SigninManagerBase);
134};
135
136#endif  // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_BASE_H_
137