blob: a80de25ceabeb6bf461730aa5451a644b8501b13 [file] [log] [blame]
[email protected]0d02c332014-06-19 20:56:501// 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
Colin Blundell021e3e4d2018-04-19 01:03:245#ifndef COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
6#define COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_
[email protected]0d02c332014-06-19 20:56:507
8#include <map>
dchengf064ccc2016-04-08 17:35:409#include <memory>
[email protected]0d02c332014-06-19 20:56:5010#include <string>
11#include <vector>
12
[email protected]0d02c332014-06-19 20:56:5013#include "base/observer_list.h"
Colin Blundellbf6d4c5f2018-05-16 11:12:2914#include "components/signin/core/browser/signin_manager.h"
[email protected]0d02c332014-06-19 20:56:5015#include "google_apis/gaia/gaia_oauth_client.h"
16#include "google_apis/gaia/identity_provider.h"
17#include "google_apis/gaia/oauth2_token_service.h"
18
19class GoogleServiceAuthError;
20
21namespace net {
22class URLRequestContextGetter;
23}
24
Colin Blundell021e3e4d2018-04-19 01:03:2425namespace gcm {
[email protected]0d02c332014-06-19 20:56:5026
27struct AccountIds {
28 std::string account_key; // The account ID used by OAuth2TokenService.
29 std::string gaia;
30 std::string email;
31};
32
33class AccountIdFetcher;
34
35// The AccountTracker keeps track of what accounts exist on the
36// profile and the state of their credentials. The tracker fetches the
37// gaia ID of each account it knows about.
38//
39// The AccountTracker maintains these invariants:
40// 1. Events are only fired after the gaia ID has been fetched.
41// 2. Add/Remove and SignIn/SignOut pairs are always generated in order.
42// 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove.
43// 4. If there is no primary account, there are no other accounts.
44class AccountTracker : public OAuth2TokenService::Observer,
Colin Blundellbf6d4c5f2018-05-16 11:12:2945 public SigninManagerBase::Observer {
[email protected]0d02c332014-06-19 20:56:5046 public:
Colin Blundellbf6d4c5f2018-05-16 11:12:2947 AccountTracker(SigninManagerBase* signin_manager,
48 IdentityProvider* identity_provider,
[email protected]0d02c332014-06-19 20:56:5049 net::URLRequestContextGetter* request_context_getter);
dchengf93bb582014-10-21 16:11:5650 ~AccountTracker() override;
[email protected]0d02c332014-06-19 20:56:5051
52 class Observer {
53 public:
[email protected]0d02c332014-06-19 20:56:5054 virtual void OnAccountSignInChanged(const AccountIds& ids,
55 bool is_signed_in) = 0;
56 };
57
58 void Shutdown();
59
60 void AddObserver(Observer* observer);
61 void RemoveObserver(Observer* observer);
62
63 // Returns the list of accounts that are signed in, and for which gaia IDs
64 // have been fetched. The primary account for the profile will be first
65 // in the vector. Additional accounts will be in order of their gaia IDs.
66 std::vector<AccountIds> GetAccounts() const;
[email protected]0d02c332014-06-19 20:56:5067
68 // OAuth2TokenService::Observer implementation.
dchengf93bb582014-10-21 16:11:5669 void OnRefreshTokenAvailable(const std::string& account_key) override;
70 void OnRefreshTokenRevoked(const std::string& account_key) override;
[email protected]0d02c332014-06-19 20:56:5071
72 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
73 const std::string& gaia_id);
74 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
75
Colin Blundellbf6d4c5f2018-05-16 11:12:2976 // SigninManagerBase::Observer implementation.
77 void GoogleSigninSucceeded(const std::string& account_id,
78 const std::string& username) override;
79 void GoogleSignedOut(const std::string& account_id,
80 const std::string& username) override;
[email protected]0d02c332014-06-19 20:56:5081
82 // Sets the state of an account. Does not fire notifications.
83 void SetAccountStateForTest(AccountIds ids, bool is_signed_in);
84
85 IdentityProvider* identity_provider() { return identity_provider_; }
86
[email protected]641c8cc72014-07-02 00:33:0687 // Indicates if all user information has been fetched. If the result is false,
avi144d04c2016-10-27 22:20:1188 // there are still unfinished fetchers.
[email protected]641c8cc72014-07-02 00:33:0689 virtual bool IsAllUserInfoFetched() const;
90
[email protected]0d02c332014-06-19 20:56:5091 private:
92 struct AccountState {
93 AccountIds ids;
94 bool is_signed_in;
95 };
96
[email protected]0d02c332014-06-19 20:56:5097 void NotifySignInChanged(const AccountState& account);
98
ki.stfu07be8392015-09-28 08:25:4199 void UpdateSignInState(const std::string& account_key, bool is_signed_in);
[email protected]0d02c332014-06-19 20:56:50100
ki.stfu07be8392015-09-28 08:25:41101 void StartTrackingAccount(const std::string& account_key);
102
103 // Note: |account_key| is passed by value here, because the original
104 // object may be stored in |accounts_| and if so, it will be destroyed
105 // after erasing the key from the map.
[email protected]0d02c332014-06-19 20:56:50106 void StopTrackingAccount(const std::string account_key);
ki.stfu07be8392015-09-28 08:25:41107
[email protected]0d02c332014-06-19 20:56:50108 void StopTrackingAllAccounts();
ki.stfu07be8392015-09-28 08:25:41109 void StartFetchingUserInfo(const std::string& account_key);
[email protected]0d02c332014-06-19 20:56:50110 void DeleteFetcher(AccountIdFetcher* fetcher);
111
Colin Blundellbf6d4c5f2018-05-16 11:12:29112 SigninManagerBase* signin_manager_;
113 IdentityProvider* identity_provider_;
[email protected]0d02c332014-06-19 20:56:50114 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
avi144d04c2016-10-27 22:20:11115 std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_;
[email protected]0d02c332014-06-19 20:56:50116 std::map<std::string, AccountState> accounts_;
brettw236d3172015-06-03 16:31:43117 base::ObserverList<Observer> observer_list_;
[email protected]0d02c332014-06-19 20:56:50118 bool shutdown_called_;
119};
120
121class AccountIdFetcher : public OAuth2TokenService::Consumer,
122 public gaia::GaiaOAuthClient::Delegate {
123 public:
124 AccountIdFetcher(OAuth2TokenService* token_service,
125 net::URLRequestContextGetter* request_context_getter,
126 AccountTracker* tracker,
127 const std::string& account_key);
dchengf93bb582014-10-21 16:11:56128 ~AccountIdFetcher() override;
[email protected]0d02c332014-06-19 20:56:50129
130 const std::string& account_key() { return account_key_; }
131
132 void Start();
133
134 // OAuth2TokenService::Consumer implementation.
dchengf93bb582014-10-21 16:11:56135 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
136 const std::string& access_token,
137 const base::Time& expiration_time) override;
138 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
139 const GoogleServiceAuthError& error) override;
[email protected]0d02c332014-06-19 20:56:50140
141 // gaia::GaiaOAuthClient::Delegate implementation.
dchengf93bb582014-10-21 16:11:56142 void OnGetUserIdResponse(const std::string& gaia_id) override;
143 void OnOAuthError() override;
144 void OnNetworkError(int response_code) override;
[email protected]0d02c332014-06-19 20:56:50145
146 private:
147 OAuth2TokenService* token_service_;
148 net::URLRequestContextGetter* request_context_getter_;
149 AccountTracker* tracker_;
150 const std::string account_key_;
151
dchengf064ccc2016-04-08 17:35:40152 std::unique_ptr<OAuth2TokenService::Request> login_token_request_;
153 std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
[email protected]0d02c332014-06-19 20:56:50154};
155
Colin Blundell021e3e4d2018-04-19 01:03:24156} // namespace gcm
[email protected]0d02c332014-06-19 20:56:50157
Colin Blundell021e3e4d2018-04-19 01:03:24158#endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_