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 16 matching lines...) Expand all Loading... | |
95 OAuth2TokenService* token_service, | 95 OAuth2TokenService* token_service, |
96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
97 PrefService* pref_service, | 97 PrefService* pref_service, |
98 CategoryFactory* category_factory, | 98 CategoryFactory* category_factory, |
99 translate::LanguageModel* language_model, | 99 translate::LanguageModel* language_model, |
100 const ParseJSONCallback& parse_json_callback, | 100 const ParseJSONCallback& parse_json_callback, |
101 const std::string& api_key, | 101 const std::string& api_key, |
102 const UserClassifier* user_classifier); | 102 const UserClassifier* user_classifier); |
103 ~NTPSnippetsFetcher() override; | 103 ~NTPSnippetsFetcher() override; |
104 | 104 |
105 // Set a callback that is called when a new set of snippets are downloaded, | |
106 // overriding any previously set callback. | |
107 void SetCallback(const SnippetsAvailableCallback& callback); | |
108 | |
109 // Fetches snippets from the server. |hosts| restricts the results to a set of | 105 // Fetches snippets from the server. |hosts| restricts the results to a set of |
110 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are | 106 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are |
111 // applied. | 107 // applied. |
112 // | 108 // |
113 // |excluded_ids| will be reported to the server; the server should not return | 109 // |excluded_ids| will be reported to the server; the server should not return |
114 // suggestions with those IDs. | 110 // suggestions with those IDs. |
115 // | 111 // |
116 // If an ongoing fetch exists, it will be cancelled and a new one started, | 112 // If an ongoing fetch exists, it will be cancelled and a new one started, |
117 // without triggering an additional callback (i.e. not noticeable by | 113 // without triggering an additional callback (i.e. not noticeable by |
118 // subscriber of SetCallback()). | 114 // the owner of |callback|). |
Marc Treib
2016/10/20 16:51:39
This is not accurate anymore: The owner of the pre
fhorschig
2016/11/02 05:05:27
As discussed offline, this behavior will change wi
| |
119 // | 115 // |
120 // Fetches snippets only if the daily quota not exceeded, unless | 116 // Fetches snippets only if the daily quota not exceeded, unless |
121 // |interactive_request| is set to true (use only for user-initiated fetches). | 117 // |interactive_request| is set to true (use only for user-initiated fetches). |
118 // | |
119 // If an |exclusive_category| was defined, the categories passed to the | |
120 // callback will only contain this category. | |
122 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 121 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
123 const std::string& language_code, | 122 const std::string& language_code, |
124 const std::set<std::string>& excluded_ids, | 123 const std::set<std::string>& excluded_ids, |
125 int count, | 124 int count, |
126 bool interactive_request); | 125 bool interactive_request, |
126 SnippetsAvailableCallback callback, | |
Marc Treib
2016/10/20 16:51:39
nit: I'd put callback last (all the others are inp
fhorschig
2016/11/02 05:05:27
Done in CL 2466863003.
| |
127 base::Optional<Category> exclusive_category); | |
127 | 128 |
128 // Debug string representing the status/result of the last fetch attempt. | 129 // Debug string representing the status/result of the last fetch attempt. |
129 const std::string& last_status() const { return last_status_; } | 130 const std::string& last_status() const { return last_status_; } |
130 | 131 |
131 // Returns the last JSON fetched from the server. | 132 // Returns the last JSON fetched from the server. |
132 const std::string& last_json() const { | 133 const std::string& last_json() const { |
133 return last_fetch_json_; | 134 return last_fetch_json_; |
134 } | 135 } |
135 | 136 |
136 // Returns the personalization setting of the fetcher. | 137 // Returns the personalization setting of the fetcher. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 // URLFetcherDelegate implementation. | 210 // URLFetcherDelegate implementation. |
210 void OnURLFetchComplete(const net::URLFetcher* source) override; | 211 void OnURLFetchComplete(const net::URLFetcher* source) override; |
211 | 212 |
212 bool JsonToSnippets(const base::Value& parsed, | 213 bool JsonToSnippets(const base::Value& parsed, |
213 FetchedCategoriesVector* categories); | 214 FetchedCategoriesVector* categories); |
214 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 215 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
215 void OnJsonError(const std::string& error); | 216 void OnJsonError(const std::string& error); |
216 void FetchFinished(OptionalFetchedCategories fetched_categories, | 217 void FetchFinished(OptionalFetchedCategories fetched_categories, |
217 FetchResult result, | 218 FetchResult result, |
218 const std::string& extra_message); | 219 const std::string& extra_message); |
220 void FilterCategories(OptionalFetchedCategories* fetched); | |
Marc Treib
2016/10/20 16:51:39
nit: const (or you could convert it to a non-membe
Marc Treib
2016/10/28 14:49:49
Done.
| |
219 | 221 |
220 bool DemandQuotaForRequest(bool interactive_request); | 222 bool DemandQuotaForRequest(bool interactive_request); |
221 | 223 |
222 // Authorization for signed-in users. | 224 // Authorization for signed-in users. |
223 SigninManagerBase* signin_manager_; | 225 SigninManagerBase* signin_manager_; |
224 OAuth2TokenService* token_service_; | 226 OAuth2TokenService* token_service_; |
225 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 227 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
226 bool waiting_for_refresh_token_; | 228 bool waiting_for_refresh_token_; |
227 | 229 |
228 // Holds the URL request context. | 230 // Holds the URL request context. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 | 264 |
263 // API key to use for non-authenticated requests. | 265 // API key to use for non-authenticated requests. |
264 const std::string api_key_; | 266 const std::string api_key_; |
265 | 267 |
266 // The variant of the fetching to use, loaded from variation parameters. | 268 // The variant of the fetching to use, loaded from variation parameters. |
267 Personalization personalization_; | 269 Personalization personalization_; |
268 | 270 |
269 // Is the request user initiated? | 271 // Is the request user initiated? |
270 bool interactive_request_; | 272 bool interactive_request_; |
271 | 273 |
274 // Is the request supposed to fetch one category excusively? Which one? | |
Marc Treib
2016/10/20 16:51:40
Typo: excusively (but I'm not a fan of the questio
Marc Treib
2016/10/28 14:49:49
Done.
| |
275 base::Optional<Category> exclusive_category_; | |
276 | |
272 // Allow for an injectable tick clock for testing. | 277 // Allow for an injectable tick clock for testing. |
273 std::unique_ptr<base::TickClock> tick_clock_; | 278 std::unique_ptr<base::TickClock> tick_clock_; |
274 | 279 |
275 // Classifier that tells us how active the user is. Not owned. | 280 // Classifier that tells us how active the user is. Not owned. |
276 const UserClassifier* user_classifier_; | 281 const UserClassifier* user_classifier_; |
277 | 282 |
278 // Request throttlers for limiting requests for different classes of users. | 283 // Request throttlers for limiting requests for different classes of users. |
279 RequestThrottler request_throttler_rare_ntp_user_; | 284 RequestThrottler request_throttler_rare_ntp_user_; |
280 RequestThrottler request_throttler_active_ntp_user_; | 285 RequestThrottler request_throttler_active_ntp_user_; |
281 RequestThrottler request_throttler_active_suggestions_consumer_; | 286 RequestThrottler request_throttler_active_suggestions_consumer_; |
282 | 287 |
283 // When a token request gets canceled, we want to retry once. | 288 // When a token request gets canceled, we want to retry once. |
284 bool oauth_token_retried_; | 289 bool oauth_token_retried_; |
285 | 290 |
286 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 291 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
287 | 292 |
288 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 293 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
289 }; | 294 }; |
290 } // namespace ntp_snippets | 295 } // namespace ntp_snippets |
291 | 296 |
292 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 297 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
OLD | NEW |