blob: 2eb3a639c096ab4ead552e24c9875d9e65a5464f [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"
[email protected]0d02c332014-06-19 20:56:5014#include "google_apis/gaia/gaia_oauth_client.h"
Colin Blundell2b8d74782018-08-02 07:27:5215#include "services/identity/public/cpp/access_token_fetcher.h"
16#include "services/identity/public/cpp/identity_manager.h"
[email protected]0d02c332014-06-19 20:56:5017
18class GoogleServiceAuthError;
[email protected]0d02c332014-06-19 20:56:5019
20namespace net {
21class URLRequestContextGetter;
22}
23
Colin Blundell021e3e4d2018-04-19 01:03:2424namespace gcm {
[email protected]0d02c332014-06-19 20:56:5025
26struct AccountIds {
27 std::string account_key; // The account ID used by OAuth2TokenService.
28 std::string gaia;
29 std::string email;
30};
31
32class AccountIdFetcher;
33
34// The AccountTracker keeps track of what accounts exist on the
35// profile and the state of their credentials. The tracker fetches the
36// gaia ID of each account it knows about.
37//
38// The AccountTracker maintains these invariants:
39// 1. Events are only fired after the gaia ID has been fetched.
40// 2. Add/Remove and SignIn/SignOut pairs are always generated in order.
41// 3. SignIn follows Add, and there will be a SignOut between SignIn & Remove.
42// 4. If there is no primary account, there are no other accounts.
Colin Blundell2b8d74782018-08-02 07:27:5243class AccountTracker : public identity::IdentityManager::Observer {
[email protected]0d02c332014-06-19 20:56:5044 public:
Colin Blundell2b8d74782018-08-02 07:27:5245 AccountTracker(identity::IdentityManager* identity_manager,
[email protected]0d02c332014-06-19 20:56:5046 net::URLRequestContextGetter* request_context_getter);
dchengf93bb582014-10-21 16:11:5647 ~AccountTracker() override;
[email protected]0d02c332014-06-19 20:56:5048
49 class Observer {
50 public:
[email protected]0d02c332014-06-19 20:56:5051 virtual void OnAccountSignInChanged(const AccountIds& ids,
52 bool is_signed_in) = 0;
53 };
54
55 void Shutdown();
56
57 void AddObserver(Observer* observer);
58 void RemoveObserver(Observer* observer);
59
60 // Returns the list of accounts that are signed in, and for which gaia IDs
61 // have been fetched. The primary account for the profile will be first
62 // in the vector. Additional accounts will be in order of their gaia IDs.
63 std::vector<AccountIds> GetAccounts() const;
[email protected]0d02c332014-06-19 20:56:5064
Colin Blundell8f1112c12018-05-23 09:10:0965 // Indicates if all user information has been fetched. If the result is false,
66 // there are still unfinished fetchers.
67 virtual bool IsAllUserInfoFetched() const;
68
69 private:
70 friend class AccountIdFetcher;
71
72 struct AccountState {
73 AccountIds ids;
74 bool is_signed_in;
75 };
76
Colin Blundell2b8d74782018-08-02 07:27:5277 // identity::IdentityManager::Observer implementation.
78 void OnPrimaryAccountSet(const AccountInfo& primary_account_info) override;
79 void OnPrimaryAccountCleared(
80 const AccountInfo& previous_primary_account_info) override;
81 void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
82 bool is_valid) override;
83 void OnRefreshTokenRemovedForAccount(
84 const AccountInfo& account_info) override;
[email protected]0d02c332014-06-19 20:56:5085
86 void OnUserInfoFetchSuccess(AccountIdFetcher* fetcher,
87 const std::string& gaia_id);
88 void OnUserInfoFetchFailure(AccountIdFetcher* fetcher);
89
[email protected]0d02c332014-06-19 20:56:5090 void NotifySignInChanged(const AccountState& account);
91
ki.stfu07be8392015-09-28 08:25:4192 void UpdateSignInState(const std::string& account_key, bool is_signed_in);
[email protected]0d02c332014-06-19 20:56:5093
ki.stfu07be8392015-09-28 08:25:4194 void StartTrackingAccount(const std::string& account_key);
95
96 // Note: |account_key| is passed by value here, because the original
97 // object may be stored in |accounts_| and if so, it will be destroyed
98 // after erasing the key from the map.
[email protected]0d02c332014-06-19 20:56:5099 void StopTrackingAccount(const std::string account_key);
ki.stfu07be8392015-09-28 08:25:41100
[email protected]0d02c332014-06-19 20:56:50101 void StopTrackingAllAccounts();
ki.stfu07be8392015-09-28 08:25:41102 void StartFetchingUserInfo(const std::string& account_key);
[email protected]0d02c332014-06-19 20:56:50103 void DeleteFetcher(AccountIdFetcher* fetcher);
104
Colin Blundell2b8d74782018-08-02 07:27:52105 identity::IdentityManager* identity_manager_;
[email protected]0d02c332014-06-19 20:56:50106 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
avi144d04c2016-10-27 22:20:11107 std::map<std::string, std::unique_ptr<AccountIdFetcher>> user_info_requests_;
[email protected]0d02c332014-06-19 20:56:50108 std::map<std::string, AccountState> accounts_;
brettw236d3172015-06-03 16:31:43109 base::ObserverList<Observer> observer_list_;
[email protected]0d02c332014-06-19 20:56:50110 bool shutdown_called_;
111};
112
Colin Blundell2b8d74782018-08-02 07:27:52113class AccountIdFetcher : public gaia::GaiaOAuthClient::Delegate {
[email protected]0d02c332014-06-19 20:56:50114 public:
Colin Blundell2b8d74782018-08-02 07:27:52115 AccountIdFetcher(identity::IdentityManager* identity_manager,
[email protected]0d02c332014-06-19 20:56:50116 net::URLRequestContextGetter* request_context_getter,
117 AccountTracker* tracker,
118 const std::string& account_key);
dchengf93bb582014-10-21 16:11:56119 ~AccountIdFetcher() override;
[email protected]0d02c332014-06-19 20:56:50120
121 const std::string& account_key() { return account_key_; }
122
123 void Start();
124
Colin Blundell2b8d74782018-08-02 07:27:52125 void AccessTokenFetched(GoogleServiceAuthError error,
126 identity::AccessTokenInfo access_token_info);
[email protected]0d02c332014-06-19 20:56:50127
128 // gaia::GaiaOAuthClient::Delegate implementation.
dchengf93bb582014-10-21 16:11:56129 void OnGetUserIdResponse(const std::string& gaia_id) override;
130 void OnOAuthError() override;
131 void OnNetworkError(int response_code) override;
[email protected]0d02c332014-06-19 20:56:50132
133 private:
Colin Blundell2b8d74782018-08-02 07:27:52134 identity::IdentityManager* identity_manager_;
[email protected]0d02c332014-06-19 20:56:50135 net::URLRequestContextGetter* request_context_getter_;
136 AccountTracker* tracker_;
137 const std::string account_key_;
138
Colin Blundell2b8d74782018-08-02 07:27:52139 std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_;
dchengf064ccc2016-04-08 17:35:40140 std::unique_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
[email protected]0d02c332014-06-19 20:56:50141};
142
Colin Blundell021e3e4d2018-04-19 01:03:24143} // namespace gcm
[email protected]0d02c332014-06-19 20:56:50144
Colin Blundell021e3e4d2018-04-19 01:03:24145#endif // COMPONENTS_GCM_DRIVER_ACCOUNT_TRACKER_H_