blob: d6a7e3451491f0b8f3945ae19e1230c3c8ab9a9c [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"
[email protected]0d02c332014-06-19 20:56:5016#include "google_apis/gaia/oauth2_token_service.h"
17
18class GoogleServiceAuthError;
Colin Blundellf8789932018-05-22 11:35:5619class ProfileOAuth2TokenService;
[email protected]0d02c332014-06-19 20:56:5020
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,
Colin Blundellf8789932018-05-22 11:35:5648 ProfileOAuth2TokenService* token_service,
[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
Colin Blundell8f1112c12018-05-23 09:10:0968 // Indicates if all user information has been fetched. If the result is false,
69 // there are still unfinished fetchers.
70 virtual bool IsAllUserInfoFetched() const;
71
72 private:
73 friend class AccountIdFetcher;
74
75 struct AccountState {
76 AccountIds ids;
77 bool is_signed_in;
78 };
79
[email protected]0d02c332014-06-19 20:56:5080 // OAuth2TokenService::Observer implementation.
dchengf93bb582014-10-21 16:11:5681 void OnRefreshTokenAvailable(const std::string& account_key) override;
82 void OnRefreshTokenRevoked(const std::string& account_key) override;
[email protected]0d02c332014-06-19 20:56:5083
84 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
85 const std::string& gaia_id);
86 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
87
Colin Blundellbf6d4c5f2018-05-16 11:12:2988 // SigninManagerBase::Observer implementation.
89 void GoogleSigninSucceeded(const std::string& account_id,
90 const std::string& username) override;
91 void GoogleSignedOut(const std::string& account_id,
92 const std::string& username) override;
[email protected]0d02c332014-06-19 20:56:5093
[email protected]0d02c332014-06-19 20:56:5094 void NotifySignInChanged(const AccountState& account);
95
ki.stfu07be8392015-09-28 08:25:4196 void UpdateSignInState(const std::string& account_key, bool is_signed_in);
[email protected]0d02c332014-06-19 20:56:5097
ki.stfu07be8392015-09-28 08:25:4198 void StartTrackingAccount(const std::string& account_key);
99
100 // Note: |account_key| is passed by value here, because the original
101 // object may be stored in |accounts_| and if so, it will be destroyed
102 // after erasing the key from the map.
[email protected]0d02c332014-06-19 20:56:50103 void StopTrackingAccount(const std::string account_key);
ki.stfu07be8392015-09-28 08:25:41104
[email protected]0d02c332014-06-19 20:56:50105 void StopTrackingAllAccounts();
ki.stfu07be8392015-09-28 08:25:41106 void StartFetchingUserInfo(const std::string& account_key);
[email protected]0d02c332014-06-19 20:56:50107 void DeleteFetcher(AccountIdFetcher* fetcher);
108
Colin Blundellbf6d4c5f2018-05-16 11:12:29109 SigninManagerBase* signin_manager_;
Colin Blundellf8789932018-05-22 11:35:56110 ProfileOAuth2TokenService* token_service_;
[email protected]0d02c332014-06-19 20:56:50111 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
avi144d04c2016-10-27 22:20:11112 std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_;
[email protected]0d02c332014-06-19 20:56:50113 std::map<std::string, AccountState> accounts_;
brettw236d3172015-06-03 16:31:43114 base::ObserverList<Observer> observer_list_;
[email protected]0d02c332014-06-19 20:56:50115 bool shutdown_called_;
116};
117
118class AccountIdFetcher : public OAuth2TokenService::Consumer,
119 public gaia::GaiaOAuthClient::Delegate {
120 public:
121 AccountIdFetcher(OAuth2TokenService* token_service,
122 net::URLRequestContextGetter* request_context_getter,
123 AccountTracker* tracker,
124 const std::string& account_key);
dchengf93bb582014-10-21 16:11:56125 ~AccountIdFetcher() override;
[email protected]0d02c332014-06-19 20:56:50126
127 const std::string& account_key() { return account_key_; }
128
129 void Start();
130
131 // OAuth2TokenService::Consumer implementation.
dchengf93bb582014-10-21 16:11:56132 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
133 const std::string& access_token,
134 const base::Time& expiration_time) override;
135 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
136 const GoogleServiceAuthError& error) override;
[email protected]0d02c332014-06-19 20:56:50137
138 // gaia::GaiaOAuthClient::Delegate implementation.
dchengf93bb582014-10-21 16:11:56139 void OnGetUserIdResponse(const std::string& gaia_id) override;
140 void OnOAuthError() override;
141 void OnNetworkError(int response_code) override;
[email protected]0d02c332014-06-19 20:56:50142
143 private:
144 OAuth2TokenService* token_service_;
145 net::URLRequestContextGetter* request_context_getter_;
146 AccountTracker* tracker_;
147 const std::string account_key_;
148
dchengf064ccc2016-04-08 17:35:40149 std::unique_ptr<OAuth2TokenService::Request> login_token_request_;
150 std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
[email protected]0d02c332014-06-19 20:56:50151};
152
Colin Blundell021e3e4d2018-04-19 01:03:24153} // namespace gcm
[email protected]0d02c332014-06-19 20:56:50154
Colin Blundell021e3e4d2018-04-19 01:03:24155#endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_