1 // Copyright 2015 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.
5 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_FETCHER_SERVICE_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_FETCHER_SERVICE_H_
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/timer/timer.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
17 class AccountInfoFetcher
;
18 class AccountTrackerService
;
19 class ChildAccountInfoFetcher
;
20 class OAuth2TokenService
;
21 class RefreshTokenAnnotationRequest
;
24 namespace invalidation
{
25 class InvalidationService
;
28 namespace user_prefs
{
29 class PrefRegistrySyncable
;
32 class AccountFetcherService
: public KeyedService
,
33 public OAuth2TokenService::Observer
,
34 public base::NonThreadSafe
{
36 // Name of the preference that tracks the int64 representation of the last
37 // time the AccountTrackerService was updated.
38 static const char kLastUpdatePref
[];
40 AccountFetcherService();
41 ~AccountFetcherService() override
;
43 // Registers the preferences used by AccountFetcherService.
44 static void RegisterPrefs(user_prefs::PrefRegistrySyncable
* user_prefs
);
46 void Initialize(SigninClient
* signin_client
,
47 OAuth2TokenService
* token_service
,
48 AccountTrackerService
* account_tracker_service
,
49 invalidation::InvalidationService
* invalidation_service
);
51 // KeyedService implementation
52 void Shutdown() override
;
54 // To be called after the Profile is fully initialized; permits network
55 // calls to be executed.
56 void EnableNetworkFetches();
58 // Indicates if all user information has been fetched. If the result is false,
59 // there are still unfininshed fetchers.
60 virtual bool IsAllUserInfoFetched() const;
62 void FetchUserInfoBeforeSignin(const std::string
& account_id
);
64 AccountTrackerService
* account_tracker_service() const {
65 return account_tracker_service_
;
68 // Called by ChildAccountInfoFetcher.
69 void SetIsChildAccount(const std::string
& account_id
, bool is_child_account
);
72 friend class AccountInfoFetcher
;
73 friend class ChildAccountInfoFetcherImpl
;
75 void RefreshAllAccountInfo(bool only_fetch_if_invalid
);
76 void RefreshAllAccountsAndScheduleNext();
77 void ScheduleNextRefresh();
79 // Called on all account state changes. Decides whether to fetch new child
80 // status information or reset old values that aren't valid now.
81 void UpdateChildInfo();
83 // Virtual so that tests can override the network fetching behaviour.
84 // Further the two fetches are managed by a different refresh logic and
85 // thus, can not be combined.
86 virtual void StartFetchingUserInfo(const std::string
& account_id
);
87 virtual void StartFetchingChildInfo(const std::string
& account_id
);
89 // If there is more than one account in a profile, we forcibly reset the
90 // child status for an account to be false.
91 void ResetChildInfo();
93 // Refreshes the AccountInfo associated with |account_id|.
94 void RefreshAccountInfo(const std::string
& account_id
,
95 bool only_fetch_if_invalid
);
97 // Virtual so that tests can override the network fetching behaviour.
98 virtual void SendRefreshTokenAnnotationRequest(const std::string
& account_id
);
99 void RefreshTokenAnnotationRequestDone(const std::string
& account_id
);
101 // Called by AccountInfoFetcher.
102 void OnUserInfoFetchSuccess(const std::string
& account_id
,
103 scoped_ptr
<base::DictionaryValue
> user_info
);
104 void OnUserInfoFetchFailure(const std::string
& account_id
);
106 // OAuth2TokenService::Observer implementation.
107 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
108 void OnRefreshTokenRevoked(const std::string
& account_id
) override
;
109 void OnRefreshTokensLoaded() override
;
111 AccountTrackerService
* account_tracker_service_
; // Not owned.
112 OAuth2TokenService
* token_service_
; // Not owned.
113 SigninClient
* signin_client_
; // Not owned.
114 invalidation::InvalidationService
* invalidation_service_
; // Not owned.
115 bool network_fetches_enabled_
;
116 std::list
<std::string
> pending_user_info_fetches_
;
117 base::Time last_updated_
;
118 base::OneShotTimer
<AccountFetcherService
> timer_
;
119 bool shutdown_called_
;
121 std::string child_request_account_id_
;
122 scoped_ptr
<ChildAccountInfoFetcher
> child_info_request_
;
124 // Holds references to account info fetchers keyed by account_id.
125 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<AccountInfoFetcher
>>
127 // Holds references to refresh token annotation requests keyed by account_id.
128 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<RefreshTokenAnnotationRequest
>>
129 refresh_token_annotation_requests_
;
131 DISALLOW_COPY_AND_ASSIGN(AccountFetcherService
);
134 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_FETCHER_SERVICE_H_