Home | History | Annotate | Download | only in signin
      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 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_TRACKER_H_
      6 #define CHROME_BROWSER_SIGNIN_SIGNIN_TRACKER_H_
      7 
      8 #include "content/public/browser/notification_observer.h"
      9 #include "content/public/browser/notification_registrar.h"
     10 #include "content/public/browser/notification_types.h"
     11 #include "google_apis/gaia/google_service_auth_error.h"
     12 
     13 class Profile;
     14 
     15 // The signin flow logic is spread across several classes with varying
     16 // responsibilities:
     17 //
     18 // SigninTracker (this class) - This class listens to notifications from various
     19 // services (SigninManager, tokenService, etc) and coalesces them into
     20 // notifications for the UI layer. This is the class that encapsulates the logic
     21 // that determines whether a user is fully logged in or not, and exposes
     22 // callbacks so various pieces of the UI (OneClickSyncStarter, SyncSetupHandler)
     23 // can track the current startup state.
     24 //
     25 // SyncSetupHandler - This class is primarily responsible for interacting with
     26 // the web UI for performing system login and sync configuration. Receives
     27 // callbacks from the UI when the user wishes to initiate a login, and
     28 // translates system state (login errors, etc) into the appropriate calls into
     29 // the UI to reflect this status to the user.
     30 //
     31 // LoginUIService - Our desktop UI flows rely on having only a single login flow
     32 // visible to the user at once. This is achieved via LoginUIService
     33 // (a BrowserContextKeyedService that keeps track of the currently visible
     34 // login UI).
     35 //
     36 // SigninManager - Records the currently-logged-in user and handles all
     37 // interaction with the GAIA backend during the signin process. Unlike
     38 // SigninTracker, SigninManager only knows about the GAIA login state and is
     39 // not aware of the state of any signed in services.
     40 //
     41 // TokenService - Uses credentials provided by SigninManager to generate tokens
     42 // for all signed-in services in Chrome.
     43 //
     44 // ProfileSyncService - Provides the external API for interacting with the
     45 // sync framework. Listens for notifications from the TokenService to know
     46 // when to startup sync, and provides an Observer interface to notify the UI
     47 // layer of changes in sync state so they can be reflected in the UI.
     48 class SigninTracker : public content::NotificationObserver {
     49  public:
     50   class Observer {
     51    public:
     52     // The signin attempt failed, and the cause is passed in |error|.
     53     virtual void SigninFailed(const GoogleServiceAuthError& error) = 0;
     54 
     55     // The signin attempt succeeded.
     56     virtual void SigninSuccess() = 0;
     57   };
     58 
     59   // The various states the login process can be in.
     60   enum LoginState {
     61     WAITING_FOR_GAIA_VALIDATION,
     62     SERVICES_INITIALIZING,
     63     SIGNIN_COMPLETE
     64   };
     65 
     66   // Creates a SigninTracker that tracks the signin status on the passed
     67   // |profile|, and notifies the |observer| on status changes. |observer| must
     68   // be non-null and must outlive the SigninTracker.
     69   SigninTracker(Profile* profile, Observer* observer);
     70   virtual ~SigninTracker();
     71 
     72   // content::NotificationObserver implementation.
     73   virtual void Observe(int type,
     74                        const content::NotificationSource& source,
     75                        const content::NotificationDetails& details) OVERRIDE;
     76 
     77   // Returns true if the tokens are loaded for all signed-in services.
     78   static bool AreServiceTokensLoaded(Profile* profile);
     79 
     80   // Returns the sign in state for |profile|.  If the profile is not signed in,
     81   // or is authenticating with GAIA, WAITING_FOR_GAIA_VALIDATION is returned.
     82   // If SigninManager in has completed but TokenService is not ready,
     83   // SERVICES_INITIALIZING is returned. Otherwise SIGNIN_COMPLETE is returned.
     84   static LoginState GetSigninState(Profile* profile,
     85                                    GoogleServiceAuthError* error);
     86 
     87  private:
     88   // Initializes this by adding notifications and observers.
     89   void Initialize();
     90 
     91   // Invoked when one of the services potentially changed its signin status so
     92   // we can check to see whether we need to notify our observer.
     93   void HandleServiceStateChange();
     94 
     95   // The current state of the login process.
     96   LoginState state_;
     97 
     98   // The profile whose signin status we are tracking.
     99   Profile* profile_;
    100 
    101   // Weak pointer to the observer we call when the signin state changes.
    102   Observer* observer_;
    103 
    104   // Set to true when SigninManager has validated our credentials.
    105   bool credentials_valid_;
    106 
    107   // Used to listen to notifications from the SigninManager.
    108   content::NotificationRegistrar registrar_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(SigninTracker);
    111 };
    112 
    113 #endif  // CHROME_BROWSER_SIGNIN_SIGNIN_TRACKER_H_
    114