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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 RESULT_MAX | 83 RESULT_MAX |
84 }; | 84 }; |
85 | 85 |
86 // Enumeration listing all possible variants of dealing with personalization. | 86 // Enumeration listing all possible variants of dealing with personalization. |
87 enum class Personalization { | 87 enum class Personalization { |
88 kPersonal, | 88 kPersonal, |
89 kNonPersonal, | 89 kNonPersonal, |
90 kBoth | 90 kBoth |
91 }; | 91 }; |
92 | 92 |
| 93 // Contains all the parameters for one fetch. |
| 94 struct Params { |
| 95 Params(); |
| 96 Params(const Params&); |
| 97 ~Params(); |
| 98 |
| 99 // If non-empty, restricts the result to the given set of hosts. |
| 100 std::set<std::string> hosts; |
| 101 |
| 102 // BCP 47 language code specifying the user's UI language. |
| 103 std::string language_code; |
| 104 |
| 105 // A set of suggestion IDs that should not be returned again. |
| 106 std::set<std::string> excluded_ids; |
| 107 |
| 108 // Maximum number of snippets to fetch. |
| 109 int count_to_fetch = 0; |
| 110 |
| 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 |
| 113 // quota. |
| 114 bool interactive_request = false; |
| 115 }; |
| 116 |
93 NTPSnippetsFetcher( | 117 NTPSnippetsFetcher( |
94 SigninManagerBase* signin_manager, | 118 SigninManagerBase* signin_manager, |
95 OAuth2TokenService* token_service, | 119 OAuth2TokenService* token_service, |
96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 120 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
97 PrefService* pref_service, | 121 PrefService* pref_service, |
98 CategoryFactory* category_factory, | 122 CategoryFactory* category_factory, |
99 translate::LanguageModel* language_model, | 123 translate::LanguageModel* language_model, |
100 const ParseJSONCallback& parse_json_callback, | 124 const ParseJSONCallback& parse_json_callback, |
101 const std::string& api_key, | 125 const std::string& api_key, |
102 const UserClassifier* user_classifier); | 126 const UserClassifier* user_classifier); |
103 ~NTPSnippetsFetcher() override; | 127 ~NTPSnippetsFetcher() override; |
104 | 128 |
105 // Set a callback that is called when a new set of snippets are downloaded, | 129 // Set a callback that is called when a new set of snippets are downloaded, |
106 // overriding any previously set callback. | 130 // overriding any previously set callback. |
107 void SetCallback(const SnippetsAvailableCallback& callback); | 131 void SetCallback(const SnippetsAvailableCallback& callback); |
108 | 132 |
109 // Fetches snippets from the server. |hosts| restricts the results to a set of | 133 // Initiates a fetch from the server. When done (successfully or not), calls |
110 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are | 134 // the subscriber of SetCallback(). |
111 // applied. | |
112 // | 135 // |
113 // |excluded_ids| will be reported to the server; the server should not return | 136 // If an ongoing fetch exists, it will be silently abandoned and a new one |
114 // suggestions with those IDs. | 137 // started, without triggering an additional callback (i.e. the callback will |
115 // | 138 // only be called once). |
116 // If an ongoing fetch exists, it will be cancelled and a new one started, | 139 void FetchSnippets(const Params& params); |
117 // without triggering an additional callback (i.e. not noticeable by | |
118 // subscriber of SetCallback()). | |
119 // | |
120 // Fetches snippets only if the daily quota not exceeded, unless | |
121 // |interactive_request| is set to true (use only for user-initiated fetches). | |
122 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | |
123 const std::string& language_code, | |
124 const std::set<std::string>& excluded_ids, | |
125 int count, | |
126 bool interactive_request); | |
127 | 140 |
128 // Debug string representing the status/result of the last fetch attempt. | 141 // Debug string representing the status/result of the last fetch attempt. |
129 const std::string& last_status() const { return last_status_; } | 142 const std::string& last_status() const { return last_status_; } |
130 | 143 |
131 // Returns the last JSON fetched from the server. | 144 // Returns the last JSON fetched from the server. |
132 const std::string& last_json() const { | 145 const std::string& last_json() const { |
133 return last_fetch_json_; | 146 return last_fetch_json_; |
134 } | 147 } |
135 | 148 |
136 // Returns the personalization setting of the fetcher. | 149 // Returns the personalization setting of the fetcher. |
137 Personalization personalization() const { return personalization_; } | 150 Personalization personalization() const { return personalization_; } |
138 | 151 |
139 // Returns the URL endpoint used by the fetcher. | 152 // Returns the URL endpoint used by the fetcher. |
140 GURL fetch_url() const { return fetch_url_; } | 153 const GURL& fetch_url() const { return fetch_url_; } |
141 | 154 |
142 // Does the fetcher use authentication to get personalized results? | 155 // Does the fetcher use authentication to get personalized results? |
143 bool UsesAuthentication() const; | 156 bool UsesAuthentication() const; |
144 | 157 |
145 // Overrides internal clock for testing purposes. | 158 // Overrides internal clock for testing purposes. |
146 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { | 159 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { |
147 tick_clock_ = std::move(tick_clock); | 160 tick_clock_ = std::move(tick_clock); |
148 } | 161 } |
149 | 162 |
150 void SetPersonalizationForTesting(Personalization personalization) { | 163 void SetPersonalizationForTesting(Personalization personalization) { |
(...skipping 10 matching lines...) Expand all Loading... |
161 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | 174 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, |
162 BuildRequestWithUILanguageOnly); | 175 BuildRequestWithUILanguageOnly); |
163 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | 176 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, |
164 BuildRequestWithOtherLanguageOnly); | 177 BuildRequestWithOtherLanguageOnly); |
165 | 178 |
166 enum FetchAPI { | 179 enum FetchAPI { |
167 CHROME_READER_API, | 180 CHROME_READER_API, |
168 CHROME_CONTENT_SUGGESTIONS_API, | 181 CHROME_CONTENT_SUGGESTIONS_API, |
169 }; | 182 }; |
170 | 183 |
171 struct RequestParams { | 184 struct RequestBuilder { |
172 FetchAPI fetch_api; | 185 Params params; |
| 186 FetchAPI fetch_api = CHROME_READER_API; |
173 std::string obfuscated_gaia_id; | 187 std::string obfuscated_gaia_id; |
174 bool only_return_personalized_results; | 188 bool only_return_personalized_results = false; |
175 std::string user_locale; | |
176 std::set<std::string> host_restricts; | |
177 std::set<std::string> excluded_ids; | |
178 int count_to_fetch; | |
179 bool interactive_request; | |
180 std::string user_class; | 189 std::string user_class; |
181 translate::LanguageModel::LanguageInfo ui_language; | 190 translate::LanguageModel::LanguageInfo ui_language{"", 0.0f}; |
182 translate::LanguageModel::LanguageInfo other_top_language; | 191 translate::LanguageModel::LanguageInfo other_top_language{"", 0.0f}; |
183 | 192 |
184 RequestParams(); | 193 RequestBuilder(); |
185 ~RequestParams(); | 194 RequestBuilder(RequestBuilder&&); |
| 195 ~RequestBuilder(); |
186 | 196 |
187 std::string BuildRequest(); | 197 std::string BuildRequest(); |
188 }; | 198 }; |
189 | 199 |
| 200 RequestBuilder MakeRequestBuilder() const; |
| 201 |
190 void FetchSnippetsImpl(const GURL& url, | 202 void FetchSnippetsImpl(const GURL& url, |
191 const std::string& auth_header, | 203 const std::string& auth_header, |
192 const std::string& request); | 204 const std::string& request); |
193 void SetUpCommonFetchingParameters(RequestParams* params) const; | |
194 void FetchSnippetsNonAuthenticated(); | 205 void FetchSnippetsNonAuthenticated(); |
195 void FetchSnippetsAuthenticated(const std::string& account_id, | 206 void FetchSnippetsAuthenticated(const std::string& account_id, |
196 const std::string& oauth_access_token); | 207 const std::string& oauth_access_token); |
197 void StartTokenRequest(); | 208 void StartTokenRequest(); |
198 | 209 |
199 // OAuth2TokenService::Consumer overrides: | 210 // OAuth2TokenService::Consumer overrides: |
200 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 211 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
201 const std::string& access_token, | 212 const std::string& access_token, |
202 const base::Time& expiration_time) override; | 213 const base::Time& expiration_time) override; |
203 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 214 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
204 const GoogleServiceAuthError& error) override; | 215 const GoogleServiceAuthError& error) override; |
205 | 216 |
206 // OAuth2TokenService::Observer overrides: | 217 // OAuth2TokenService::Observer overrides: |
207 void OnRefreshTokenAvailable(const std::string& account_id) override; | 218 void OnRefreshTokenAvailable(const std::string& account_id) override; |
208 | 219 |
209 // URLFetcherDelegate implementation. | 220 // URLFetcherDelegate implementation. |
210 void OnURLFetchComplete(const net::URLFetcher* source) override; | 221 void OnURLFetchComplete(const net::URLFetcher* source) override; |
211 | 222 |
212 bool JsonToSnippets(const base::Value& parsed, | 223 bool JsonToSnippets(const base::Value& parsed, |
213 FetchedCategoriesVector* categories); | 224 FetchedCategoriesVector* categories); |
214 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 225 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
215 void OnJsonError(const std::string& error); | 226 void OnJsonError(const std::string& error); |
216 void FetchFinished(OptionalFetchedCategories fetched_categories, | 227 void FetchFinished(OptionalFetchedCategories fetched_categories, |
217 FetchResult result, | 228 FetchResult result, |
218 const std::string& extra_message); | 229 const std::string& extra_message); |
219 | 230 |
220 bool DemandQuotaForRequest(bool interactive_request); | 231 bool DemandQuotaForRequest(bool interactive_request); |
221 | 232 |
222 // Authorization for signed-in users. | 233 // Authentication for signed-in users. |
223 SigninManagerBase* signin_manager_; | 234 SigninManagerBase* signin_manager_; |
224 OAuth2TokenService* token_service_; | 235 OAuth2TokenService* token_service_; |
225 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 236 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
226 bool waiting_for_refresh_token_; | 237 bool waiting_for_refresh_token_ = false; |
| 238 |
| 239 // When a token request gets canceled, we want to retry once. |
| 240 bool oauth_token_retried_ = false; |
227 | 241 |
228 // Holds the URL request context. | 242 // Holds the URL request context. |
229 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 243 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
230 | 244 |
231 // Weak references, not owned. | 245 // Weak references, not owned. |
232 CategoryFactory* const category_factory_; | 246 CategoryFactory* const category_factory_; |
233 translate::LanguageModel* const language_model_; | 247 translate::LanguageModel* const language_model_; |
234 | 248 |
235 const ParseJSONCallback parse_json_callback_; | 249 const ParseJSONCallback parse_json_callback_; |
236 base::TimeTicks fetch_start_time_; | |
237 std::string last_status_; | |
238 std::string last_fetch_json_; | |
239 | |
240 // Hosts to restrict the snippets to. | |
241 std::set<std::string> hosts_; | |
242 | |
243 // Snippets to exclude from the results. | |
244 std::set<std::string> excluded_ids_; | |
245 | |
246 // Count of snippets to fetch. | |
247 int count_to_fetch_; | |
248 | |
249 // Language code to restrict to for personalized results. | |
250 std::string locale_; | |
251 | 250 |
252 // API endpoint for fetching snippets. | 251 // API endpoint for fetching snippets. |
253 const GURL fetch_url_; | 252 const GURL fetch_url_; |
254 // Which API to use | 253 // Which API to use |
255 const FetchAPI fetch_api_; | 254 const FetchAPI fetch_api_; |
256 | 255 |
257 // The fetcher for downloading the snippets. | |
258 std::unique_ptr<net::URLFetcher> url_fetcher_; | |
259 | |
260 // The callback to notify when new snippets get fetched. | |
261 SnippetsAvailableCallback snippets_available_callback_; | |
262 | |
263 // API key to use for non-authenticated requests. | 256 // API key to use for non-authenticated requests. |
264 const std::string api_key_; | 257 const std::string api_key_; |
265 | 258 |
266 // The variant of the fetching to use, loaded from variation parameters. | 259 // The variant of the fetching to use, loaded from variation parameters. |
267 Personalization personalization_; | 260 Personalization personalization_; |
268 | 261 |
269 // Is the request user initiated? | |
270 bool interactive_request_; | |
271 | |
272 // Allow for an injectable tick clock for testing. | 262 // Allow for an injectable tick clock for testing. |
273 std::unique_ptr<base::TickClock> tick_clock_; | 263 std::unique_ptr<base::TickClock> tick_clock_; |
274 | 264 |
275 // Classifier that tells us how active the user is. Not owned. | 265 // Classifier that tells us how active the user is. Not owned. |
276 const UserClassifier* user_classifier_; | 266 const UserClassifier* user_classifier_; |
277 | 267 |
278 // Request throttlers for limiting requests for different classes of users. | 268 // Request throttlers for limiting requests for different classes of users. |
279 RequestThrottler request_throttler_rare_ntp_user_; | 269 RequestThrottler request_throttler_rare_ntp_user_; |
280 RequestThrottler request_throttler_active_ntp_user_; | 270 RequestThrottler request_throttler_active_ntp_user_; |
281 RequestThrottler request_throttler_active_suggestions_consumer_; | 271 RequestThrottler request_throttler_active_suggestions_consumer_; |
282 | 272 |
283 // When a token request gets canceled, we want to retry once. | 273 // The callback to notify when new snippets get fetched. |
284 bool oauth_token_retried_; | 274 SnippetsAvailableCallback snippets_available_callback_; |
| 275 |
| 276 // The parameters for the current request. |
| 277 Params params_; |
| 278 |
| 279 // The fetcher for downloading the snippets. Only non-null if a fetch is |
| 280 // currently ongoing. |
| 281 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 282 |
| 283 // When the current request was started, for logging purposes. |
| 284 base::TimeTicks fetch_start_time_; |
| 285 |
| 286 // Info on the last finished fetch. |
| 287 std::string last_status_; |
| 288 std::string last_fetch_json_; |
285 | 289 |
286 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 290 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
287 | 291 |
288 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 292 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
289 }; | 293 }; |
290 } // namespace ntp_snippets | 294 } // namespace ntp_snippets |
291 | 295 |
292 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 296 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
OLD | NEW |