OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/suggestions/suggestions_service.h" | 5 #include "components/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "components/suggestions/blacklist_store.h" | 20 #include "components/suggestions/blacklist_store.h" |
20 #include "components/suggestions/suggestions_store.h" | 21 #include "components/suggestions/suggestions_store.h" |
21 #include "components/variations/net/variations_http_header_provider.h" | 22 #include "components/variations/net/variations_http_header_provider.h" |
22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
23 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/base/url_util.h" | 26 #include "net/base/url_util.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 // Multiplier on the delay used when re-scheduling a failed request. | 78 // Multiplier on the delay used when re-scheduling a failed request. |
78 const int kSchedulingBackoffMultiplier = 2; | 79 const int kSchedulingBackoffMultiplier = 2; |
79 | 80 |
80 // Maximum valid delay for scheduling a request. Candidate delays larger than | 81 // Maximum valid delay for scheduling a request. Candidate delays larger than |
81 // this are rejected. This means the maximum backoff is at least 5 / 2 minutes. | 82 // this are rejected. This means the maximum backoff is at least 5 / 2 minutes. |
82 const int kSchedulingMaxDelaySec = 5 * 60; | 83 const int kSchedulingMaxDelaySec = 5 * 60; |
83 | 84 |
84 } // namespace | 85 } // namespace |
85 | 86 |
86 // TODO(mathp): Put this in TemplateURL. | 87 // TODO(mathp): Put this in TemplateURL. |
88 // TODO(mathp): Add logic to decide the device type of the request. | |
Mathieu
2015/07/28 13:09:29
nice one ;) mathp -> fserb
Seriously though, can
fserb
2015/07/28 15:31:33
I mean, there's going to be an IFDEF somewhere, bu
| |
89 #if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS) | |
Mathieu
2015/07/28 13:09:29
ChromeOS can be lumped in the desktop case.
fserb
2015/07/28 15:31:33
Done.
| |
87 const char kSuggestionsURL[] = "https://www.google.com/chromesuggestions?t=2"; | 90 const char kSuggestionsURL[] = "https://www.google.com/chromesuggestions?t=2"; |
88 const char kSuggestionsBlacklistURLPrefix[] = | 91 const char kSuggestionsBlacklistURLPrefix[] = |
89 "https://www.google.com/chromesuggestions/blacklist?t=2&url="; | 92 "https://www.google.com/chromesuggestions/blacklist?t=2&url="; |
93 const char kSuggestionsBlacklistClearURL[] = | |
94 "https://www.google.com/chromesuggestions/blacklist/clear?t=2"; | |
95 #else | |
96 const char kSuggestionsURL[] = "https://www.google.com/chromesuggestions?t=1"; | |
97 const char kSuggestionsBlacklistURLPrefix[] = | |
98 "https://www.google.com/chromesuggestions/blacklist?t=1&url="; | |
99 const char kSuggestionsBlacklistClearURL[] = | |
100 "https://www.google.com/chromesuggestions/blacklist/clear?t=1"; | |
101 #endif | |
90 const char kSuggestionsBlacklistURLParam[] = "url"; | 102 const char kSuggestionsBlacklistURLParam[] = "url"; |
91 | 103 |
92 // The default expiry timeout is 72 hours. | 104 // The default expiry timeout is 168 hours. |
93 const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour; | 105 const int64 kDefaultExpiryUsec = 168 * base::Time::kMicrosecondsPerHour; |
94 | 106 |
95 SuggestionsService::SuggestionsService( | 107 SuggestionsService::SuggestionsService( |
96 net::URLRequestContextGetter* url_request_context, | 108 net::URLRequestContextGetter* url_request_context, |
97 scoped_ptr<SuggestionsStore> suggestions_store, | 109 scoped_ptr<SuggestionsStore> suggestions_store, |
98 scoped_ptr<ImageManager> thumbnail_manager, | 110 scoped_ptr<ImageManager> thumbnail_manager, |
99 scoped_ptr<BlacklistStore> blacklist_store) | 111 scoped_ptr<BlacklistStore> blacklist_store) |
100 : url_request_context_(url_request_context), | 112 : url_request_context_(url_request_context), |
101 suggestions_store_(suggestions_store.Pass()), | 113 suggestions_store_(suggestions_store.Pass()), |
102 thumbnail_manager_(thumbnail_manager.Pass()), | 114 thumbnail_manager_(thumbnail_manager.Pass()), |
103 blacklist_store_(blacklist_store.Pass()), | 115 blacklist_store_(blacklist_store.Pass()), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 blacklist_store_->RemoveUrl(url)) { | 180 blacklist_store_->RemoveUrl(url)) { |
169 // The URL was not yet candidate for upload to the server and could be | 181 // The URL was not yet candidate for upload to the server and could be |
170 // removed from the blacklist. | 182 // removed from the blacklist. |
171 waiting_requestors_.push_back(callback); | 183 waiting_requestors_.push_back(callback); |
172 ServeFromCache(); | 184 ServeFromCache(); |
173 return; | 185 return; |
174 } | 186 } |
175 fail_callback.Run(); | 187 fail_callback.Run(); |
176 } | 188 } |
177 | 189 |
190 void SuggestionsService::ClearBlacklist(const ResponseCallback& callback) { | |
191 DCHECK(thread_checker_.CalledOnValidThread()); | |
192 blacklist_store_->ClearBlacklist(); | |
193 IssueRequestIfNoneOngoing(GURL(kSuggestionsBlacklistClearURL)); | |
194 waiting_requestors_.push_back(callback); | |
195 ServeFromCache(); | |
196 } | |
197 | |
178 // static | 198 // static |
179 bool SuggestionsService::GetBlacklistedUrl(const net::URLFetcher& request, | 199 bool SuggestionsService::GetBlacklistedUrl(const net::URLFetcher& request, |
180 GURL* url) { | 200 GURL* url) { |
181 bool is_blacklist_request = base::StartsWith( | 201 bool is_blacklist_request = base::StartsWith( |
182 request.GetOriginalURL().spec(), kSuggestionsBlacklistURLPrefix, | 202 request.GetOriginalURL().spec(), kSuggestionsBlacklistURLPrefix, |
183 base::CompareCase::SENSITIVE); | 203 base::CompareCase::SENSITIVE); |
184 if (!is_blacklist_request) return false; | 204 if (!is_blacklist_request) return false; |
185 | 205 |
186 // Extract the blacklisted URL from the blacklist request. | 206 // Extract the blacklisted URL from the blacklist request. |
187 std::string blacklisted; | 207 std::string blacklisted; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 // in the case of invalid response. | 309 // in the case of invalid response. |
290 SuggestionsProfile suggestions; | 310 SuggestionsProfile suggestions; |
291 if (suggestions_data.empty()) { | 311 if (suggestions_data.empty()) { |
292 LogResponseState(RESPONSE_EMPTY); | 312 LogResponseState(RESPONSE_EMPTY); |
293 suggestions_store_->ClearSuggestions(); | 313 suggestions_store_->ClearSuggestions(); |
294 } else if (suggestions.ParseFromString(suggestions_data)) { | 314 } else if (suggestions.ParseFromString(suggestions_data)) { |
295 LogResponseState(RESPONSE_VALID); | 315 LogResponseState(RESPONSE_VALID); |
296 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 316 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) |
297 .ToInternalValue(); | 317 .ToInternalValue(); |
298 SetDefaultExpiryTimestamp(&suggestions, now_usec + kDefaultExpiryUsec); | 318 SetDefaultExpiryTimestamp(&suggestions, now_usec + kDefaultExpiryUsec); |
319 PopulateFavicons(suggestions); | |
299 suggestions_store_->StoreSuggestions(suggestions); | 320 suggestions_store_->StoreSuggestions(suggestions); |
300 } else { | 321 } else { |
301 LogResponseState(RESPONSE_INVALID); | 322 LogResponseState(RESPONSE_INVALID); |
302 } | 323 } |
303 | 324 |
304 UpdateBlacklistDelay(true); | 325 UpdateBlacklistDelay(true); |
305 ScheduleBlacklistUpload(); | 326 ScheduleBlacklistUpload(); |
306 } | 327 } |
307 | 328 |
329 void SuggestionsService::PopulateFavicons(SuggestionsProfile* suggestions) { | |
330 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | |
331 suggestions::ChromeSuggestion* s = suggestions.mutable_suggestions(i); | |
332 s->set_favicon_url( | |
333 base::StringPrintf("https://s2.googleusercontent.com/s2/" | |
Mathieu
2015/07/28 13:09:29
bring into a constant at the top
fserb
2015/07/28 15:31:33
Done.
| |
334 "favicons?domain_url=%s&alt=s&sz=32", | |
335 s->url().c_str())); | |
336 } | |
337 } | |
338 | |
308 void SuggestionsService::Shutdown() { | 339 void SuggestionsService::Shutdown() { |
309 // Cancel pending request, then serve existing requestors from cache. | 340 // Cancel pending request, then serve existing requestors from cache. |
310 pending_request_.reset(NULL); | 341 pending_request_.reset(NULL); |
311 ServeFromCache(); | 342 ServeFromCache(); |
312 } | 343 } |
313 | 344 |
314 void SuggestionsService::ServeFromCache() { | 345 void SuggestionsService::ServeFromCache() { |
315 SuggestionsProfile suggestions; | 346 SuggestionsProfile suggestions; |
316 // In case of empty cache or error, |suggestions| stays empty. | 347 // In case of empty cache or error, |suggestions| stays empty. |
317 suggestions_store_->LoadSuggestions(&suggestions); | 348 suggestions_store_->LoadSuggestions(&suggestions); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); | 392 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); |
362 } else { | 393 } else { |
363 TimeDelta candidate_delay = | 394 TimeDelta candidate_delay = |
364 scheduling_delay_ * kSchedulingBackoffMultiplier; | 395 scheduling_delay_ * kSchedulingBackoffMultiplier; |
365 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) | 396 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) |
366 scheduling_delay_ = candidate_delay; | 397 scheduling_delay_ = candidate_delay; |
367 } | 398 } |
368 } | 399 } |
369 | 400 |
370 } // namespace suggestions | 401 } // namespace suggestions |
OLD | NEW |