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
);
65 AccountTrackerService
* account_tracker_service() const {
66 return account_tracker_service_
;
69 // Called by ChildAccountInfoFetcher.
70 void SetIsChildAccount(const std::string
& account_id
, bool is_child_account
);
73 friend class AccountInfoFetcher
;
74 friend class ChildAccountInfoFetcherImpl
;
75 friend class ChildAccountInfoFetcherAndroid
;
77 void RefreshAllAccountInfo(bool only_fetch_if_invalid
);
78 void RefreshAllAccountsAndScheduleNext();
79 void ScheduleNextRefresh();
81 // Called on all account state changes. Decides whether to fetch new child
82 // status information or reset old values that aren't valid now.
83 void UpdateChildInfo();
85 // Virtual so that tests can override the network fetching behaviour.
86 // Further the two fetches are managed by a different refresh logic and
87 // thus, can not be combined.
88 virtual void StartFetchingUserInfo(const std::string
& account_id
);
89 virtual void StartFetchingChildInfo(const std::string
& account_id
);
91 // If there is more than one account in a profile, we forcibly reset the
92 // child status for an account to be false.
93 void ResetChildInfo();
95 // Refreshes the AccountInfo associated with |account_id|.
96 void RefreshAccountInfo(const std::string
& account_id
,
97 bool only_fetch_if_invalid
);
99 // Virtual so that tests can override the network fetching behaviour.
100 virtual void SendRefreshTokenAnnotationRequest(const std::string
& account_id
);
101 void RefreshTokenAnnotationRequestDone(const std::string
& account_id
);
103 // Called by AccountInfoFetcher.
104 void OnUserInfoFetchSuccess(const std::string
& account_id
,
105 scoped_ptr
<base::DictionaryValue
> user_info
);
106 void OnUserInfoFetchFailure(const std::string
& account_id
);
108 // OAuth2TokenService::Observer implementation.
109 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
110 void OnRefreshTokenRevoked(const std::string
& account_id
) override
;
111 void OnRefreshTokensLoaded() override
;
113 AccountTrackerService
* account_tracker_service_
; // Not owned.
114 OAuth2TokenService
* token_service_
; // Not owned.
115 SigninClient
* signin_client_
; // Not owned.
116 invalidation::InvalidationService
* invalidation_service_
; // Not owned.
117 bool network_fetches_enabled_
;
118 std::list
<std::string
> pending_user_info_fetches_
;
119 base::Time last_updated_
;
120 base::OneShotTimer
<AccountFetcherService
> timer_
;
121 bool shutdown_called_
;
123 std::string child_request_account_id_
;
124 scoped_ptr
<ChildAccountInfoFetcher
> child_info_request_
;
126 // Holds references to account info fetchers keyed by account_id.
127 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<AccountInfoFetcher
>>
129 // Holds references to refresh token annotation requests keyed by account_id.
130 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<RefreshTokenAnnotationRequest
>>
131 refresh_token_annotation_requests_
;
133 DISALLOW_COPY_AND_ASSIGN(AccountFetcherService
);
136 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_FETCHER_SERVICE_H_