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#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_TRACKER_H_
6#define COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_TRACKER_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "components/signin/core/browser/signin_manager.h"
10#include "google_apis/gaia/google_service_auth_error.h"
11#include "google_apis/gaia/merge_session_helper.h"
12#include "google_apis/gaia/oauth2_token_service.h"
13
14class AccountReconcilor;
15class ProfileOAuth2TokenService;
16class SigninClient;
17
18// The signin flow logic is spread across several classes with varying
19// responsibilities:
20//
21// SigninTracker (this class) - This class listens to notifications from various
22// services (SigninManager, OAuth2TokenService) and coalesces them into
23// notifications for the UI layer. This is the class that encapsulates the logic
24// that determines whether a user is fully logged in or not, and exposes
25// callbacks so various pieces of the UI (OneClickSyncStarter) can track the
26// current startup state.
27//
28// SyncSetupHandler - This class is primarily responsible for interacting with
29// the web UI for performing system login and sync configuration. Receives
30// callbacks from the UI when the user wishes to initiate a login, and
31// translates system state (login errors, etc) into the appropriate calls into
32// the UI to reflect this status to the user.
33//
34// LoginUIService - Our desktop UI flows rely on having only a single login flow
35// visible to the user at once. This is achieved via LoginUIService
36// (a KeyedService that keeps track of the currently visible
37// login UI).
38//
39// SigninManager - Records the currently-logged-in user and handles all
40// interaction with the GAIA backend during the signin process. Unlike
41// SigninTracker, SigninManager only knows about the GAIA login state and is
42// not aware of the state of any signed in services.
43//
44// OAuth2TokenService - Maintains and manages OAuth2 tokens for the accounts
45// connected to this profile.
46//
47// ProfileSyncService - Provides the external API for interacting with the
48// sync framework. Listens for notifications for tokens to know when to startup
49// sync, and provides an Observer interface to notify the UI layer of changes
50// in sync state so they can be reflected in the UI.
51class SigninTracker : public SigninManagerBase::Observer,
52                      public OAuth2TokenService::Observer,
53                      public MergeSessionHelper::Observer {
54 public:
55  class Observer {
56   public:
57    // The signin attempt failed, and the cause is passed in |error|.
58    virtual void SigninFailed(const GoogleServiceAuthError& error) = 0;
59
60    // The signin attempt succeeded.
61    virtual void SigninSuccess() = 0;
62
63    // The signed in account has been merged into the content area cookie jar.
64    // This will be called only after a call to SigninSuccess().
65    virtual void MergeSessionComplete(const GoogleServiceAuthError& error) = 0;
66  };
67
68  // Creates a SigninTracker that tracks the signin status on the passed
69  // classes, and notifies the |observer| on status changes. All of the
70  // instances with the exception of |account_reconcilor| must be non-null and
71  // must outlive the SigninTracker. |account_reconcilor| will be used if it is
72  // non-null.
73  SigninTracker(ProfileOAuth2TokenService* token_service,
74                SigninManagerBase* signin_manager,
75                AccountReconcilor* account_reconcilor,
76                SigninClient* client,
77                Observer* observer);
78  virtual ~SigninTracker();
79
80  // SigninManagerBase::Observer implementation.
81  virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
82
83  // OAuth2TokenService::Observer implementation.
84  virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
85  virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
86
87 private:
88  // Initializes this by adding notifications and observers.
89  void Initialize();
90
91  // MergeSessionHelper::Observer implementation.
92  virtual void MergeSessionCompleted(
93      const std::string& account_id,
94      const GoogleServiceAuthError& error) OVERRIDE;
95
96  // The classes whose collective signin status we are tracking.
97  ProfileOAuth2TokenService* token_service_;
98  SigninManagerBase* signin_manager_;
99  AccountReconcilor* account_reconcilor_;
100
101  // The client associated with this instance.
102  SigninClient* client_;
103
104  // Weak pointer to the observer we call when the signin state changes.
105  Observer* observer_;
106
107  DISALLOW_COPY_AND_ASSIGN(SigninTracker);
108};
109
110#endif  // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_TRACKER_H_
111