Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / signin / core / browser / account_fetcher_service.h
blob415ca3d9516d5aeca4d9486b62910a1b9982b2d2
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_
8 #include <list>
9 #include <map>
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;
22 class SigninClient;
24 namespace invalidation {
25 class InvalidationService;
28 class AccountFetcherService : public KeyedService,
29 public OAuth2TokenService::Observer,
30 public base::NonThreadSafe {
31 public:
32 // Name of the preference that tracks the int64 representation of the last
33 // time the AccountTrackerService was updated.
34 static const char kLastUpdatePref[];
36 AccountFetcherService();
37 ~AccountFetcherService() override;
39 void Initialize(SigninClient* signin_client,
40 OAuth2TokenService* token_service,
41 AccountTrackerService* account_tracker_service,
42 invalidation::InvalidationService* invalidation_service);
44 // KeyedService implementation
45 void Shutdown() override;
47 // To be called after the Profile is fully initialized; permits network
48 // calls to be executed.
49 void EnableNetworkFetches();
51 // Indicates if all user information has been fetched. If the result is false,
52 // there are still unfininshed fetchers.
53 virtual bool IsAllUserInfoFetched() const;
55 protected:
56 AccountTrackerService* account_tracker_service() const {
57 return account_tracker_service_;
60 // Called by ChildAccountInfoFetcher.
61 void SetIsChildAccount(const std::string& account_id, bool is_child_account);
63 private:
64 friend class AccountInfoFetcher;
65 friend class ChildAccountInfoFetcherImpl;
66 friend class ChildAccountInfoFetcherAndroid;
68 void RefreshAllAccountInfo(bool only_fetch_if_invalid);
69 void RefreshAllAccountsAndScheduleNext();
70 void ScheduleNextRefresh();
72 // Called on all account state changes. Decides whether to fetch new child
73 // status information or reset old values that aren't valid now.
74 void UpdateChildInfo();
76 // Virtual so that tests can override the network fetching behaviour.
77 // Further the two fetches are managed by a different refresh logic and
78 // thus, can not be combined.
79 virtual void StartFetchingUserInfo(const std::string& account_id);
80 virtual void StartFetchingChildInfo(const std::string& account_id);
82 // If there is more than one account in a profile, we forcibly reset the
83 // child status for an account to be false.
84 void ResetChildInfo();
86 // Refreshes the AccountInfo associated with |account_id|.
87 void RefreshAccountInfo(const std::string& account_id,
88 bool only_fetch_if_invalid);
90 // Virtual so that tests can override the network fetching behaviour.
91 virtual void SendRefreshTokenAnnotationRequest(const std::string& account_id);
92 void RefreshTokenAnnotationRequestDone(const std::string& account_id);
94 // Called by AccountInfoFetcher.
95 void OnUserInfoFetchSuccess(const std::string& account_id,
96 scoped_ptr<base::DictionaryValue> user_info);
97 void OnUserInfoFetchFailure(const std::string& account_id);
99 // OAuth2TokenService::Observer implementation.
100 void OnRefreshTokenAvailable(const std::string& account_id) override;
101 void OnRefreshTokenRevoked(const std::string& account_id) override;
102 void OnRefreshTokensLoaded() override;
104 AccountTrackerService* account_tracker_service_; // Not owned.
105 OAuth2TokenService* token_service_; // Not owned.
106 SigninClient* signin_client_; // Not owned.
107 invalidation::InvalidationService* invalidation_service_; // Not owned.
108 bool network_fetches_enabled_;
109 std::list<std::string> pending_user_info_fetches_;
110 base::Time last_updated_;
111 base::OneShotTimer<AccountFetcherService> timer_;
112 bool shutdown_called_;
114 std::string child_request_account_id_;
115 scoped_ptr<ChildAccountInfoFetcher> child_info_request_;
117 // Holds references to account info fetchers keyed by account_id.
118 base::ScopedPtrHashMap<std::string, scoped_ptr<AccountInfoFetcher>>
119 user_info_requests_;
120 // Holds references to refresh token annotation requests keyed by account_id.
121 base::ScopedPtrHashMap<std::string, scoped_ptr<RefreshTokenAnnotationRequest>>
122 refresh_token_annotation_requests_;
124 DISALLOW_COPY_AND_ASSIGN(AccountFetcherService);
127 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_FETCHER_SERVICE_H_