| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ | 5 #ifndef COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
| 6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ | 6 #define COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/callback_list.h" | 14 #include "base/callback_list.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/optional.h" | 18 #include "base/optional.h" |
| 19 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "components/signin/core/browser/access_token_fetcher.h" |
| 22 #include "components/suggestions/proto/suggestions.pb.h" | 23 #include "components/suggestions/proto/suggestions.pb.h" |
| 23 #include "components/suggestions/suggestions_service.h" | 24 #include "components/suggestions/suggestions_service.h" |
| 24 #include "components/sync/driver/sync_service_observer.h" | 25 #include "components/sync/driver/sync_service_observer.h" |
| 26 #include "google_apis/gaia/google_service_auth_error.h" |
| 25 #include "net/url_request/url_fetcher_delegate.h" | 27 #include "net/url_request/url_fetcher_delegate.h" |
| 26 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 27 | 29 |
| 28 class OAuth2TokenService; | 30 class OAuth2TokenService; |
| 29 class SigninManagerBase; | 31 class SigninManagerBase; |
| 30 | 32 |
| 31 namespace net { | 33 namespace net { |
| 32 class URLRequestContextGetter; | 34 class URLRequestContextGetter; |
| 33 } // namespace net | 35 } // namespace net |
| 34 | 36 |
| 35 namespace syncer { | 37 namespace syncer { |
| 36 class SyncService; | 38 class SyncService; |
| 37 } // namespace syncer | 39 } // namespace syncer |
| 38 | 40 |
| 39 namespace user_prefs { | 41 namespace user_prefs { |
| 40 class PrefRegistrySyncable; | 42 class PrefRegistrySyncable; |
| 41 } // namespace user_prefs | 43 } // namespace user_prefs |
| 42 | 44 |
| 43 namespace suggestions { | 45 namespace suggestions { |
| 44 | 46 |
| 45 class BlacklistStore; | 47 class BlacklistStore; |
| 46 class ImageManager; | 48 class ImageManager; |
| 47 class SuggestionsStore; | 49 class SuggestionsStore; |
| 48 | 50 |
| 49 // Actual (non-test) implementation of the SuggestionsService interface. | 51 // Actual (non-test) implementation of the SuggestionsService interface. |
| 50 class SuggestionsServiceImpl : public SuggestionsService, | 52 class SuggestionsServiceImpl : public SuggestionsService, |
| 51 public net::URLFetcherDelegate, | 53 public net::URLFetcherDelegate, |
| 52 public syncer::SyncServiceObserver { | 54 public syncer::SyncServiceObserver { |
| 53 public: | 55 public: |
| 54 SuggestionsServiceImpl(const SigninManagerBase* signin_manager, | 56 SuggestionsServiceImpl(SigninManagerBase* signin_manager, |
| 55 OAuth2TokenService* token_service, | 57 OAuth2TokenService* token_service, |
| 56 syncer::SyncService* sync_service, | 58 syncer::SyncService* sync_service, |
| 57 net::URLRequestContextGetter* url_request_context, | 59 net::URLRequestContextGetter* url_request_context, |
| 58 std::unique_ptr<SuggestionsStore> suggestions_store, | 60 std::unique_ptr<SuggestionsStore> suggestions_store, |
| 59 std::unique_ptr<ImageManager> thumbnail_manager, | 61 std::unique_ptr<ImageManager> thumbnail_manager, |
| 60 std::unique_ptr<BlacklistStore> blacklist_store); | 62 std::unique_ptr<BlacklistStore> blacklist_store); |
| 61 ~SuggestionsServiceImpl() override; | 63 ~SuggestionsServiceImpl() override; |
| 62 | 64 |
| 63 // SuggestionsService implementation. | 65 // SuggestionsService implementation. |
| 64 bool FetchSuggestionsData() override; | 66 bool FetchSuggestionsData() override; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // syncer::SyncServiceObserver implementation. | 114 // syncer::SyncServiceObserver implementation. |
| 113 void OnStateChanged() override; | 115 void OnStateChanged() override; |
| 114 | 116 |
| 115 // Sets default timestamp for suggestions which do not have expiry timestamp. | 117 // Sets default timestamp for suggestions which do not have expiry timestamp. |
| 116 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions, | 118 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions, |
| 117 int64_t timestamp_usec); | 119 int64_t timestamp_usec); |
| 118 | 120 |
| 119 // Issues a network request if there isn't already one happening. | 121 // Issues a network request if there isn't already one happening. |
| 120 void IssueRequestIfNoneOngoing(const GURL& url); | 122 void IssueRequestIfNoneOngoing(const GURL& url); |
| 121 | 123 |
| 124 // Called when an access token request completes (successfully or not). |
| 125 void AccessTokenAvailable(const GURL& url, |
| 126 const GoogleServiceAuthError& error, |
| 127 const std::string& access_token); |
| 128 |
| 122 // Issues a network request for suggestions (fetch, blacklist, or clear | 129 // Issues a network request for suggestions (fetch, blacklist, or clear |
| 123 // blacklist, depending on |url|). |access_token| is used only if OAuth2 | 130 // blacklist, depending on |url|). |
| 124 // authentication is enabled. | |
| 125 void IssueSuggestionsRequest(const GURL& url, | 131 void IssueSuggestionsRequest(const GURL& url, |
| 126 const std::string& access_token); | 132 const std::string& access_token); |
| 127 | 133 |
| 128 // Creates a request to the suggestions service, properly setting headers. | 134 // Creates a request to the suggestions service, properly setting headers. |
| 129 // If OAuth2 authentication is enabled, |access_token| should be a valid | 135 // If OAuth2 authentication is enabled, |access_token| should be a valid |
| 130 // OAuth2 access token, and will be written into an auth header. | 136 // OAuth2 access token, and will be written into an auth header. |
| 131 std::unique_ptr<net::URLFetcher> CreateSuggestionsRequest( | 137 std::unique_ptr<net::URLFetcher> CreateSuggestionsRequest( |
| 132 const GURL& url, | 138 const GURL& url, |
| 133 const std::string& access_token); | 139 const std::string& access_token); |
| 134 | 140 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 | 158 |
| 153 // Adds extra data to suggestions profile. | 159 // Adds extra data to suggestions profile. |
| 154 void PopulateExtraData(SuggestionsProfile* suggestions); | 160 void PopulateExtraData(SuggestionsProfile* suggestions); |
| 155 | 161 |
| 156 // Test seams. | 162 // Test seams. |
| 157 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } | 163 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } |
| 158 void set_blacklist_delay(base::TimeDelta delay) { scheduling_delay_ = delay; } | 164 void set_blacklist_delay(base::TimeDelta delay) { scheduling_delay_ = delay; } |
| 159 | 165 |
| 160 base::ThreadChecker thread_checker_; | 166 base::ThreadChecker thread_checker_; |
| 161 | 167 |
| 168 SigninManagerBase* signin_manager_; |
| 169 OAuth2TokenService* token_service_; |
| 170 |
| 162 syncer::SyncService* sync_service_; | 171 syncer::SyncService* sync_service_; |
| 163 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> | 172 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> |
| 164 sync_service_observer_; | 173 sync_service_observer_; |
| 165 | 174 |
| 166 net::URLRequestContextGetter* url_request_context_; | 175 net::URLRequestContextGetter* url_request_context_; |
| 167 | 176 |
| 168 // The cache for the suggestions. | 177 // The cache for the suggestions. |
| 169 std::unique_ptr<SuggestionsStore> suggestions_store_; | 178 std::unique_ptr<SuggestionsStore> suggestions_store_; |
| 170 | 179 |
| 171 // Used to obtain server thumbnails, if available. | 180 // Used to obtain server thumbnails, if available. |
| 172 std::unique_ptr<ImageManager> thumbnail_manager_; | 181 std::unique_ptr<ImageManager> thumbnail_manager_; |
| 173 | 182 |
| 174 // The local cache for temporary blacklist, until uploaded to the server. | 183 // The local cache for temporary blacklist, until uploaded to the server. |
| 175 std::unique_ptr<BlacklistStore> blacklist_store_; | 184 std::unique_ptr<BlacklistStore> blacklist_store_; |
| 176 | 185 |
| 177 // Delay used when scheduling a blacklisting task. | 186 // Delay used when scheduling a blacklisting task. |
| 178 base::TimeDelta scheduling_delay_; | 187 base::TimeDelta scheduling_delay_; |
| 179 | 188 |
| 180 // Helper for fetching OAuth2 access tokens. | 189 // Helper for fetching OAuth2 access tokens. This is non-null iff an access |
| 181 class AccessTokenFetcher; | 190 // token request is currently in progress. |
| 182 std::unique_ptr<AccessTokenFetcher> token_fetcher_; | 191 std::unique_ptr<AccessTokenFetcher> token_fetcher_; |
| 183 | 192 |
| 184 // Contains the current suggestions fetch request. Will only have a value | 193 // Contains the current suggestions fetch request. Will only have a value |
| 185 // while a request is pending, and will be reset by |OnURLFetchComplete| or | 194 // while a request is pending, and will be reset by |OnURLFetchComplete| or |
| 186 // if cancelled. | 195 // if cancelled. |
| 187 std::unique_ptr<net::URLFetcher> pending_request_; | 196 std::unique_ptr<net::URLFetcher> pending_request_; |
| 188 | 197 |
| 189 // The start time of the previous suggestions request. This is used to measure | 198 // The start time of the previous suggestions request. This is used to measure |
| 190 // the latency of requests. Initially zero. | 199 // the latency of requests. Initially zero. |
| 191 base::TimeTicks last_request_started_time_; | 200 base::TimeTicks last_request_started_time_; |
| 192 | 201 |
| 193 ResponseCallbackList callback_list_; | 202 ResponseCallbackList callback_list_; |
| 194 | 203 |
| 195 // For callbacks may be run after destruction. | 204 // For callbacks may be run after destruction. |
| 196 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; | 205 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; |
| 197 | 206 |
| 198 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); | 207 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); |
| 199 }; | 208 }; |
| 200 | 209 |
| 201 } // namespace suggestions | 210 } // namespace suggestions |
| 202 | 211 |
| 203 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ | 212 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
| OLD | NEW |