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_NTP_SNIPPETS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 ~FetchedCategory(); // = default, in .cc | 58 ~FetchedCategory(); // = default, in .cc |
59 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc | 59 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc |
60 }; | 60 }; |
61 using FetchedCategoriesVector = std::vector<FetchedCategory>; | 61 using FetchedCategoriesVector = std::vector<FetchedCategory>; |
62 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>; | 62 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>; |
63 | 63 |
64 // |snippets| contains parsed snippets if a fetch succeeded. If problems | 64 // |snippets| contains parsed snippets if a fetch succeeded. If problems |
65 // occur, |snippets| contains no value (no actual vector in base::Optional). | 65 // occur, |snippets| contains no value (no actual vector in base::Optional). |
66 // Error details can be retrieved using last_status(). | 66 // Error details can be retrieved using last_status(). |
67 using SnippetsAvailableCallback = | 67 using SnippetsAvailableCallback = |
68 base::Callback<void(OptionalFetchedCategories fetched_categories)>; | 68 base::OnceCallback<void(OptionalFetchedCategories fetched_categories)>; |
69 | 69 |
70 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 70 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
71 // histograms, so do not change existing values. Insert new values at the end, | 71 // histograms, so do not change existing values. Insert new values at the end, |
72 // and update the histogram definition. | 72 // and update the histogram definition. |
73 enum class FetchResult { | 73 enum class FetchResult { |
74 SUCCESS, | 74 SUCCESS, |
75 DEPRECATED_EMPTY_HOSTS, | 75 DEPRECATED_EMPTY_HOSTS, |
76 URL_REQUEST_STATUS_ERROR, | 76 URL_REQUEST_STATUS_ERROR, |
77 HTTP_ERROR, | 77 HTTP_ERROR, |
78 JSON_PARSE_ERROR, | 78 JSON_PARSE_ERROR, |
(...skipping 26 matching lines...) Expand all Loading... |
105 // A set of suggestion IDs that should not be returned again. | 105 // A set of suggestion IDs that should not be returned again. |
106 std::set<std::string> excluded_ids; | 106 std::set<std::string> excluded_ids; |
107 | 107 |
108 // Maximum number of snippets to fetch. | 108 // Maximum number of snippets to fetch. |
109 int count_to_fetch = 0; | 109 int count_to_fetch = 0; |
110 | 110 |
111 // Whether this is an interactive request, i.e. triggered by an explicit | 111 // Whether this is an interactive request, i.e. triggered by an explicit |
112 // user action. Typically, non-interactive requests are subject to a daily | 112 // user action. Typically, non-interactive requests are subject to a daily |
113 // quota. | 113 // quota. |
114 bool interactive_request = false; | 114 bool interactive_request = false; |
| 115 |
| 116 // If set, the request is returning this category only; otherwise all |
| 117 // available categories are returned. |
| 118 base::Optional<Category> exclusive_category; |
| 119 |
| 120 // The callback to notify when new snippets get fetched. |
| 121 SnippetsAvailableCallback snippets_available_callback; |
115 }; | 122 }; |
116 | 123 |
117 NTPSnippetsFetcher( | 124 NTPSnippetsFetcher( |
118 SigninManagerBase* signin_manager, | 125 SigninManagerBase* signin_manager, |
119 OAuth2TokenService* token_service, | 126 OAuth2TokenService* token_service, |
120 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 127 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
121 PrefService* pref_service, | 128 PrefService* pref_service, |
122 CategoryFactory* category_factory, | 129 CategoryFactory* category_factory, |
123 translate::LanguageModel* language_model, | 130 translate::LanguageModel* language_model, |
124 const ParseJSONCallback& parse_json_callback, | 131 const ParseJSONCallback& parse_json_callback, |
125 const std::string& api_key, | 132 const std::string& api_key, |
126 const UserClassifier* user_classifier); | 133 const UserClassifier* user_classifier); |
127 ~NTPSnippetsFetcher() override; | 134 ~NTPSnippetsFetcher() override; |
128 | 135 |
129 // Set a callback that is called when a new set of snippets are downloaded, | |
130 // overriding any previously set callback. | |
131 void SetCallback(const SnippetsAvailableCallback& callback); | |
132 | |
133 // Initiates a fetch from the server. When done (successfully or not), calls | 136 // Initiates a fetch from the server. When done (successfully or not), calls |
134 // the subscriber of SetCallback(). | 137 // callback that was passed along with the |params|. |
135 // | 138 // |
136 // If an ongoing fetch exists, it will be silently abandoned and a new one | 139 // If an ongoing fetch exists, it will be silently abandoned and a new one |
137 // started, without triggering an additional callback (i.e. the callback will | 140 // started, without triggering an additional callback (i.e. the callback will |
138 // only be called once). | 141 // only be called once). |
139 void FetchSnippets(const Params& params); | 142 void FetchSnippets(const Params& params); |
140 | 143 |
141 // Debug string representing the status/result of the last fetch attempt. | 144 // Debug string representing the status/result of the last fetch attempt. |
142 const std::string& last_status() const { return last_status_; } | 145 const std::string& last_status() const { return last_status_; } |
143 | 146 |
144 // Returns the last JSON fetched from the server. | 147 // Returns the last JSON fetched from the server. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // URLFetcherDelegate implementation. | 223 // URLFetcherDelegate implementation. |
221 void OnURLFetchComplete(const net::URLFetcher* source) override; | 224 void OnURLFetchComplete(const net::URLFetcher* source) override; |
222 | 225 |
223 bool JsonToSnippets(const base::Value& parsed, | 226 bool JsonToSnippets(const base::Value& parsed, |
224 FetchedCategoriesVector* categories); | 227 FetchedCategoriesVector* categories); |
225 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 228 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
226 void OnJsonError(const std::string& error); | 229 void OnJsonError(const std::string& error); |
227 void FetchFinished(OptionalFetchedCategories fetched_categories, | 230 void FetchFinished(OptionalFetchedCategories fetched_categories, |
228 FetchResult result, | 231 FetchResult result, |
229 const std::string& extra_message); | 232 const std::string& extra_message); |
| 233 void FilterCategories(OptionalFetchedCategories* fetched) const; |
230 | 234 |
231 bool DemandQuotaForRequest(bool interactive_request); | 235 bool DemandQuotaForRequest(bool interactive_request); |
232 | 236 |
233 // Authentication for signed-in users. | 237 // Authentication for signed-in users. |
234 SigninManagerBase* signin_manager_; | 238 SigninManagerBase* signin_manager_; |
235 OAuth2TokenService* token_service_; | 239 OAuth2TokenService* token_service_; |
236 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 240 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
237 bool waiting_for_refresh_token_ = false; | 241 bool waiting_for_refresh_token_ = false; |
238 | 242 |
239 // When a token request gets canceled, we want to retry once. | 243 // When a token request gets canceled, we want to retry once. |
(...skipping 23 matching lines...) Expand all Loading... |
263 std::unique_ptr<base::TickClock> tick_clock_; | 267 std::unique_ptr<base::TickClock> tick_clock_; |
264 | 268 |
265 // Classifier that tells us how active the user is. Not owned. | 269 // Classifier that tells us how active the user is. Not owned. |
266 const UserClassifier* user_classifier_; | 270 const UserClassifier* user_classifier_; |
267 | 271 |
268 // Request throttlers for limiting requests for different classes of users. | 272 // Request throttlers for limiting requests for different classes of users. |
269 RequestThrottler request_throttler_rare_ntp_user_; | 273 RequestThrottler request_throttler_rare_ntp_user_; |
270 RequestThrottler request_throttler_active_ntp_user_; | 274 RequestThrottler request_throttler_active_ntp_user_; |
271 RequestThrottler request_throttler_active_suggestions_consumer_; | 275 RequestThrottler request_throttler_active_suggestions_consumer_; |
272 | 276 |
273 // The callback to notify when new snippets get fetched. | |
274 SnippetsAvailableCallback snippets_available_callback_; | |
275 | |
276 // The parameters for the current request. | 277 // The parameters for the current request. |
277 Params params_; | 278 Params params_; |
278 | 279 |
279 // The fetcher for downloading the snippets. Only non-null if a fetch is | 280 // The fetcher for downloading the snippets. Only non-null if a fetch is |
280 // currently ongoing. | 281 // currently ongoing. |
281 std::unique_ptr<net::URLFetcher> url_fetcher_; | 282 std::unique_ptr<net::URLFetcher> url_fetcher_; |
282 | 283 |
283 // When the current request was started, for logging purposes. | 284 // When the current request was started, for logging purposes. |
284 base::TimeTicks fetch_start_time_; | 285 base::TimeTicks fetch_start_time_; |
285 | 286 |
286 // Info on the last finished fetch. | 287 // Info on the last finished fetch. |
287 std::string last_status_; | 288 std::string last_status_; |
288 std::string last_fetch_json_; | 289 std::string last_fetch_json_; |
289 | 290 |
290 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 291 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
291 | 292 |
292 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 293 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
293 }; | 294 }; |
294 } // namespace ntp_snippets | 295 } // namespace ntp_snippets |
295 | 296 |
296 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 297 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
OLD | NEW |