Home | History | Annotate | Download | only in browser
      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.
      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 COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_
     24 #define COMPONENTS_SIGNIN_CORE_BROWSER_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 "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"
     38 
     39 class PrefService;
     40 
     41 class SigninClient;
     42 
     43 class SigninManagerBase : public KeyedService {
     44  public:
     45   class Observer {
     46    public:
     47     // Called when a user fails to sign into Google services such as sync.
     48     virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) {}
     49 
     50     // Called when a user signs into Google services such as sync.
     51     virtual void GoogleSigninSucceeded(const std::string& username,
     52                                        const std::string& password) {}
     53 
     54     // Called when the currently signed-in user for a user has been signed out.
     55     virtual void GoogleSignedOut(const std::string& username) {}
     56 
     57    protected:
     58     virtual ~Observer() {}
     59   };
     60 
     61   SigninManagerBase(SigninClient* client);
     62   virtual ~SigninManagerBase();
     63 
     64   // If user was signed in, load tokens from DB if available.
     65   virtual void Initialize(PrefService* local_state);
     66   bool IsInitialized() const;
     67 
     68   // Returns true if a signin to Chrome is allowed (by policy or pref).
     69   // TODO(tim): kSigninAllowed is defined for all platforms in pref_names.h.
     70   // If kSigninAllowed pref was non-Chrome OS-only, this method wouldn't be
     71   // needed, but as is we provide this method to let all interested code
     72   // code query the value in one way, versus half using PrefService directly
     73   // and the other half using SigninManager.
     74   virtual bool IsSigninAllowed() const;
     75 
     76   // If a user has previously signed in (and has not signed out), this returns
     77   // the normalized email address of the account. Otherwise, it returns an empty
     78   // string.
     79   const std::string& GetAuthenticatedUsername() const;
     80 
     81   // If a user has previously signed in (and has not signed out), this returns
     82   // the account id. Otherwise, it returns an empty string.  This id can be used
     83   // to uniquely identify an account, so for example can be used as a key to
     84   // map accounts to data.
     85   //
     86   // TODO(rogerta): eventually the account id should be an obfuscated gaia id.
     87   // For now though, this function returns the same value as
     88   // GetAuthenticatedUsername() since lots of code assumes the unique id for an
     89   // account is the username.  For code that needs a unique id to represent the
     90   // connected account, call this method. Example: the AccountInfoMap type
     91   // in MutableProfileOAuth2TokenService.  For code that needs to know the
     92   // normalized email address of the connected account, use
     93   // GetAuthenticatedUsername().  Example: to show the string "Signed in as XXX"
     94   // in the hotdog menu.
     95   const std::string& GetAuthenticatedAccountId() const;
     96 
     97   // Sets the user name.  Note: |username| should be already authenticated as
     98   // this is a sticky operation (in contrast to StartSignIn).
     99   // TODO(tim): Remove this in favor of passing username on construction by
    100   // (by platform / depending on StartBehavior). Bug 88109.
    101   void SetAuthenticatedUsername(const std::string& username);
    102 
    103   // Returns true if there's a signin in progress.
    104   virtual bool AuthInProgress() const;
    105 
    106   // KeyedService implementation.
    107   virtual void Shutdown() OVERRIDE;
    108 
    109   // Methods to register or remove observers of signin.
    110   void AddObserver(Observer* observer);
    111   void RemoveObserver(Observer* observer);
    112 
    113   // Methods to register or remove SigninDiagnosticObservers.
    114   void AddSigninDiagnosticsObserver(
    115       signin_internals_util::SigninDiagnosticsObserver* observer);
    116   void RemoveSigninDiagnosticsObserver(
    117       signin_internals_util::SigninDiagnosticsObserver* observer);
    118 
    119  protected:
    120   // Used by subclass to clear authenticated_username_ instead of using
    121   // SetAuthenticatedUsername, which enforces special preconditions due
    122   // to the fact that it is part of the public API and called by clients.
    123   void clear_authenticated_username();
    124 
    125   // List of observers to notify on signin events.
    126   // Makes sure list is empty on destruction.
    127   ObserverList<Observer, true> observer_list_;
    128 
    129   // Helper methods to notify all registered diagnostics observers with.
    130   void NotifyDiagnosticsObservers(
    131       const signin_internals_util::UntimedSigninStatusField& field,
    132       const std::string& value);
    133   void NotifyDiagnosticsObservers(
    134       const signin_internals_util::TimedSigninStatusField& field,
    135       const std::string& value);
    136 
    137  private:
    138   friend class FakeSigninManagerBase;
    139   friend class FakeSigninManager;
    140 
    141   SigninClient* client_;
    142   bool initialized_;
    143 
    144   // Actual username after successful authentication.
    145   std::string authenticated_username_;
    146 
    147   // The list of SigninDiagnosticObservers.
    148   ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
    149       signin_diagnostics_observers_;
    150 
    151   base::WeakPtrFactory<SigninManagerBase> weak_pointer_factory_;
    152 
    153   DISALLOW_COPY_AND_ASSIGN(SigninManagerBase);
    154 };
    155 
    156 #endif  // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_MANAGER_BASE_H_
    157