| 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_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/time/clock.h" |
| 17 #include "base/time/tick_clock.h" | 18 #include "base/time/tick_clock.h" |
| 18 #include "components/ntp_snippets/category.h" | 19 #include "components/ntp_snippets/category.h" |
| 19 #include "components/ntp_snippets/category_info.h" | 20 #include "components/ntp_snippets/category_info.h" |
| 20 #include "components/ntp_snippets/remote/json_request.h" | 21 #include "components/ntp_snippets/remote/json_request.h" |
| 21 #include "components/ntp_snippets/remote/remote_suggestion.h" | 22 #include "components/ntp_snippets/remote/remote_suggestion.h" |
| 22 #include "components/ntp_snippets/remote/request_params.h" | 23 #include "components/ntp_snippets/remote/request_params.h" |
| 23 #include "components/ntp_snippets/remote/request_throttler.h" | 24 #include "components/ntp_snippets/remote/request_throttler.h" |
| 24 #include "components/ntp_snippets/status.h" | 25 #include "components/ntp_snippets/status.h" |
| 25 #include "components/translate/core/browser/language_model.h" | 26 #include "components/translate/core/browser/language_model.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 27 #include "net/url_request/url_request_context_getter.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 // Debug string representing the status/result of the last fetch attempt. | 99 // Debug string representing the status/result of the last fetch attempt. |
| 99 const std::string& last_status() const { return last_status_; } | 100 const std::string& last_status() const { return last_status_; } |
| 100 | 101 |
| 101 // Returns the last JSON fetched from the server. | 102 // Returns the last JSON fetched from the server. |
| 102 const std::string& last_json() const { return last_fetch_json_; } | 103 const std::string& last_json() const { return last_fetch_json_; } |
| 103 | 104 |
| 104 // Returns the URL endpoint used by the fetcher. | 105 // Returns the URL endpoint used by the fetcher. |
| 105 const GURL& fetch_url() const { return fetch_url_; } | 106 const GURL& fetch_url() const { return fetch_url_; } |
| 106 | 107 |
| 107 // Overrides internal clock for testing purposes. | 108 // Overrides internal clock for testing purposes. |
| 108 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { | 109 void SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
| 109 tick_clock_ = std::move(tick_clock); | 110 clock_ = std::move(clock); |
| 110 } | 111 } |
| 111 | 112 |
| 112 private: | 113 private: |
| 113 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, | 114 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, |
| 114 BuildRequestAuthenticated); | 115 BuildRequestAuthenticated); |
| 115 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, | 116 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, |
| 116 BuildRequestUnauthenticated); | 117 BuildRequestUnauthenticated); |
| 117 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, | 118 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, |
| 118 BuildRequestExcludedIds); | 119 BuildRequestExcludedIds); |
| 119 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, | 120 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 SnippetsAvailableCallback callback, | 152 SnippetsAvailableCallback callback, |
| 152 std::unique_ptr<base::Value> result, | 153 std::unique_ptr<base::Value> result, |
| 153 internal::FetchResult status_code, | 154 internal::FetchResult status_code, |
| 154 const std::string& error_details); | 155 const std::string& error_details); |
| 155 void FetchFinished(OptionalFetchedCategories categories, | 156 void FetchFinished(OptionalFetchedCategories categories, |
| 156 SnippetsAvailableCallback callback, | 157 SnippetsAvailableCallback callback, |
| 157 internal::FetchResult status_code, | 158 internal::FetchResult status_code, |
| 158 const std::string& error_details); | 159 const std::string& error_details); |
| 159 | 160 |
| 160 bool JsonToSnippets(const base::Value& parsed, | 161 bool JsonToSnippets(const base::Value& parsed, |
| 161 FetchedCategoriesVector* categories); | 162 FetchedCategoriesVector* categories, |
| 163 const base::Time& fetch_time); |
| 162 | 164 |
| 163 bool DemandQuotaForRequest(bool interactive_request); | 165 bool DemandQuotaForRequest(bool interactive_request); |
| 164 | 166 |
| 165 // Authentication for signed-in users. | 167 // Authentication for signed-in users. |
| 166 SigninManagerBase* signin_manager_; | 168 SigninManagerBase* signin_manager_; |
| 167 OAuth2TokenService* token_service_; | 169 OAuth2TokenService* token_service_; |
| 168 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 170 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
| 169 bool waiting_for_refresh_token_ = false; | 171 bool waiting_for_refresh_token_ = false; |
| 170 | 172 |
| 171 // When a token request gets canceled, we want to retry once. | 173 // When a token request gets canceled, we want to retry once. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 185 const ParseJSONCallback parse_json_callback_; | 187 const ParseJSONCallback parse_json_callback_; |
| 186 | 188 |
| 187 // API endpoint for fetching suggestions. | 189 // API endpoint for fetching suggestions. |
| 188 const GURL fetch_url_; | 190 const GURL fetch_url_; |
| 189 // Which API to use | 191 // Which API to use |
| 190 const internal::FetchAPI fetch_api_; | 192 const internal::FetchAPI fetch_api_; |
| 191 | 193 |
| 192 // API key to use for non-authenticated requests. | 194 // API key to use for non-authenticated requests. |
| 193 const std::string api_key_; | 195 const std::string api_key_; |
| 194 | 196 |
| 195 // Allow for an injectable tick clock for testing. | 197 // Allow for an injectable clock for testing. |
| 196 std::unique_ptr<base::TickClock> tick_clock_; | 198 std::unique_ptr<base::Clock> clock_; |
| 197 | 199 |
| 198 // Classifier that tells us how active the user is. Not owned. | 200 // Classifier that tells us how active the user is. Not owned. |
| 199 const UserClassifier* user_classifier_; | 201 const UserClassifier* user_classifier_; |
| 200 | 202 |
| 201 // Request throttlers for limiting requests for different classes of users. | 203 // Request throttlers for limiting requests for different classes of users. |
| 202 RequestThrottler request_throttler_rare_ntp_user_; | 204 RequestThrottler request_throttler_rare_ntp_user_; |
| 203 RequestThrottler request_throttler_active_ntp_user_; | 205 RequestThrottler request_throttler_active_ntp_user_; |
| 204 RequestThrottler request_throttler_active_suggestions_consumer_; | 206 RequestThrottler request_throttler_active_suggestions_consumer_; |
| 205 | 207 |
| 206 // Info on the last finished fetch. | 208 // Info on the last finished fetch. |
| 207 std::string last_status_; | 209 std::string last_status_; |
| 208 std::string last_fetch_json_; | 210 std::string last_fetch_json_; |
| 209 | 211 |
| 210 base::WeakPtrFactory<RemoteSuggestionsFetcher> weak_ptr_factory_; | 212 base::WeakPtrFactory<RemoteSuggestionsFetcher> weak_ptr_factory_; |
| 211 | 213 |
| 212 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsFetcher); | 214 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsFetcher); |
| 213 }; | 215 }; |
| 214 | 216 |
| 215 } // namespace ntp_snippets | 217 } // namespace ntp_snippets |
| 216 | 218 |
| 217 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ | 219 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ |
| OLD | NEW |