| 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" |
| 25 #include "net/url_request/url_fetcher_delegate.h" | 26 #include "net/url_request/url_fetcher_delegate.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 class OAuth2TokenService; | 29 class OAuth2TokenService; |
| 29 class SigninManagerBase; | 30 class SigninManagerBase; |
| 30 | 31 |
| 31 namespace net { | 32 namespace net { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 44 | 45 |
| 45 class BlacklistStore; | 46 class BlacklistStore; |
| 46 class ImageManager; | 47 class ImageManager; |
| 47 class SuggestionsStore; | 48 class SuggestionsStore; |
| 48 | 49 |
| 49 // Actual (non-test) implementation of the SuggestionsService interface. | 50 // Actual (non-test) implementation of the SuggestionsService interface. |
| 50 class SuggestionsServiceImpl : public SuggestionsService, | 51 class SuggestionsServiceImpl : public SuggestionsService, |
| 51 public net::URLFetcherDelegate, | 52 public net::URLFetcherDelegate, |
| 52 public syncer::SyncServiceObserver { | 53 public syncer::SyncServiceObserver { |
| 53 public: | 54 public: |
| 54 SuggestionsServiceImpl(const SigninManagerBase* signin_manager, | 55 SuggestionsServiceImpl(SigninManagerBase* signin_manager, |
| 55 OAuth2TokenService* token_service, | 56 OAuth2TokenService* token_service, |
| 56 syncer::SyncService* sync_service, | 57 syncer::SyncService* sync_service, |
| 57 net::URLRequestContextGetter* url_request_context, | 58 net::URLRequestContextGetter* url_request_context, |
| 58 std::unique_ptr<SuggestionsStore> suggestions_store, | 59 std::unique_ptr<SuggestionsStore> suggestions_store, |
| 59 std::unique_ptr<ImageManager> thumbnail_manager, | 60 std::unique_ptr<ImageManager> thumbnail_manager, |
| 60 std::unique_ptr<BlacklistStore> blacklist_store); | 61 std::unique_ptr<BlacklistStore> blacklist_store); |
| 61 ~SuggestionsServiceImpl() override; | 62 ~SuggestionsServiceImpl() override; |
| 62 | 63 |
| 63 // SuggestionsService implementation. | 64 // SuggestionsService implementation. |
| 64 bool FetchSuggestionsData() override; | 65 bool FetchSuggestionsData() override; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // syncer::SyncServiceObserver implementation. | 113 // syncer::SyncServiceObserver implementation. |
| 113 void OnStateChanged() override; | 114 void OnStateChanged() override; |
| 114 | 115 |
| 115 // Sets default timestamp for suggestions which do not have expiry timestamp. | 116 // Sets default timestamp for suggestions which do not have expiry timestamp. |
| 116 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions, | 117 void SetDefaultExpiryTimestamp(SuggestionsProfile* suggestions, |
| 117 int64_t timestamp_usec); | 118 int64_t timestamp_usec); |
| 118 | 119 |
| 119 // Issues a network request if there isn't already one happening. | 120 // Issues a network request if there isn't already one happening. |
| 120 void IssueRequestIfNoneOngoing(const GURL& url); | 121 void IssueRequestIfNoneOngoing(const GURL& url); |
| 121 | 122 |
| 123 // Called when an access token request completes (successfully or not). |
| 124 void AccessTokenAvailable(const GURL& url, const std::string& access_token); |
| 125 |
| 122 // Issues a network request for suggestions (fetch, blacklist, or clear | 126 // Issues a network request for suggestions (fetch, blacklist, or clear |
| 123 // blacklist, depending on |url|). |access_token| is used only if OAuth2 | 127 // blacklist, depending on |url|). |
| 124 // authentication is enabled. | |
| 125 void IssueSuggestionsRequest(const GURL& url, | 128 void IssueSuggestionsRequest(const GURL& url, |
| 126 const std::string& access_token); | 129 const std::string& access_token); |
| 127 | 130 |
| 128 // Creates a request to the suggestions service, properly setting headers. | 131 // Creates a request to the suggestions service, properly setting headers. |
| 129 // If OAuth2 authentication is enabled, |access_token| should be a valid | 132 // If OAuth2 authentication is enabled, |access_token| should be a valid |
| 130 // OAuth2 access token, and will be written into an auth header. | 133 // OAuth2 access token, and will be written into an auth header. |
| 131 std::unique_ptr<net::URLFetcher> CreateSuggestionsRequest( | 134 std::unique_ptr<net::URLFetcher> CreateSuggestionsRequest( |
| 132 const GURL& url, | 135 const GURL& url, |
| 133 const std::string& access_token); | 136 const std::string& access_token); |
| 134 | 137 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 | 155 |
| 153 // Adds extra data to suggestions profile. | 156 // Adds extra data to suggestions profile. |
| 154 void PopulateExtraData(SuggestionsProfile* suggestions); | 157 void PopulateExtraData(SuggestionsProfile* suggestions); |
| 155 | 158 |
| 156 // Test seams. | 159 // Test seams. |
| 157 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } | 160 base::TimeDelta blacklist_delay() const { return scheduling_delay_; } |
| 158 void set_blacklist_delay(base::TimeDelta delay) { scheduling_delay_ = delay; } | 161 void set_blacklist_delay(base::TimeDelta delay) { scheduling_delay_ = delay; } |
| 159 | 162 |
| 160 base::ThreadChecker thread_checker_; | 163 base::ThreadChecker thread_checker_; |
| 161 | 164 |
| 165 SigninManagerBase* signin_manager_; |
| 166 OAuth2TokenService* token_service_; |
| 167 |
| 162 syncer::SyncService* sync_service_; | 168 syncer::SyncService* sync_service_; |
| 163 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> | 169 ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver> |
| 164 sync_service_observer_; | 170 sync_service_observer_; |
| 165 | 171 |
| 166 net::URLRequestContextGetter* url_request_context_; | 172 net::URLRequestContextGetter* url_request_context_; |
| 167 | 173 |
| 168 // The cache for the suggestions. | 174 // The cache for the suggestions. |
| 169 std::unique_ptr<SuggestionsStore> suggestions_store_; | 175 std::unique_ptr<SuggestionsStore> suggestions_store_; |
| 170 | 176 |
| 171 // Used to obtain server thumbnails, if available. | 177 // Used to obtain server thumbnails, if available. |
| 172 std::unique_ptr<ImageManager> thumbnail_manager_; | 178 std::unique_ptr<ImageManager> thumbnail_manager_; |
| 173 | 179 |
| 174 // The local cache for temporary blacklist, until uploaded to the server. | 180 // The local cache for temporary blacklist, until uploaded to the server. |
| 175 std::unique_ptr<BlacklistStore> blacklist_store_; | 181 std::unique_ptr<BlacklistStore> blacklist_store_; |
| 176 | 182 |
| 177 // Delay used when scheduling a blacklisting task. | 183 // Delay used when scheduling a blacklisting task. |
| 178 base::TimeDelta scheduling_delay_; | 184 base::TimeDelta scheduling_delay_; |
| 179 | 185 |
| 180 // Helper for fetching OAuth2 access tokens. | 186 // Helper for fetching OAuth2 access tokens. This is non-null iff an access |
| 181 class AccessTokenFetcher; | 187 // token request is currently in progress. |
| 182 std::unique_ptr<AccessTokenFetcher> token_fetcher_; | 188 std::unique_ptr<AccessTokenFetcher> token_fetcher_; |
| 183 | 189 |
| 184 // Contains the current suggestions fetch request. Will only have a value | 190 // Contains the current suggestions fetch request. Will only have a value |
| 185 // while a request is pending, and will be reset by |OnURLFetchComplete| or | 191 // while a request is pending, and will be reset by |OnURLFetchComplete| or |
| 186 // if cancelled. | 192 // if cancelled. |
| 187 std::unique_ptr<net::URLFetcher> pending_request_; | 193 std::unique_ptr<net::URLFetcher> pending_request_; |
| 188 | 194 |
| 189 // The start time of the previous suggestions request. This is used to measure | 195 // The start time of the previous suggestions request. This is used to measure |
| 190 // the latency of requests. Initially zero. | 196 // the latency of requests. Initially zero. |
| 191 base::TimeTicks last_request_started_time_; | 197 base::TimeTicks last_request_started_time_; |
| 192 | 198 |
| 193 ResponseCallbackList callback_list_; | 199 ResponseCallbackList callback_list_; |
| 194 | 200 |
| 195 // For callbacks may be run after destruction. | 201 // For callbacks may be run after destruction. |
| 196 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; | 202 base::WeakPtrFactory<SuggestionsServiceImpl> weak_ptr_factory_; |
| 197 | 203 |
| 198 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); | 204 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceImpl); |
| 199 }; | 205 }; |
| 200 | 206 |
| 201 } // namespace suggestions | 207 } // namespace suggestions |
| 202 | 208 |
| 203 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ | 209 #endif // COMPONENTS_SUGGESTIONS_SUGGESTIONS_SERVICE_IMPL_H_ |
| OLD | NEW |