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 <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; | 94 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; |
95 const char kSuggestionsFieldTrialURLParam[] = "url"; | 95 const char kSuggestionsFieldTrialURLParam[] = "url"; |
96 const char kSuggestionsFieldTrialCommonParamsParam[] = "common_params"; | 96 const char kSuggestionsFieldTrialCommonParamsParam[] = "common_params"; |
97 const char kSuggestionsFieldTrialBlacklistPathParam[] = "blacklist_path"; | 97 const char kSuggestionsFieldTrialBlacklistPathParam[] = "blacklist_path"; |
98 const char kSuggestionsFieldTrialBlacklistUrlParam[] = "blacklist_url_param"; | 98 const char kSuggestionsFieldTrialBlacklistUrlParam[] = "blacklist_url_param"; |
99 const char kSuggestionsFieldTrialStateParam[] = "state"; | 99 const char kSuggestionsFieldTrialStateParam[] = "state"; |
100 const char kSuggestionsFieldTrialControlParam[] = "control"; | 100 const char kSuggestionsFieldTrialControlParam[] = "control"; |
101 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; | 101 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; |
102 const char kSuggestionsFieldTrialTimeoutMs[] = "timeout_ms"; | 102 const char kSuggestionsFieldTrialTimeoutMs[] = "timeout_ms"; |
103 | 103 |
104 const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour; | |
Mathieu
2014/08/04 19:54:55
Add a comment above saying that the default expiry
gayane -on leave until 09-2017
2014/08/05 14:39:15
Done.
gayane -on leave until 09-2017
2014/08/05 14:39:15
Done.
| |
105 | |
104 namespace { | 106 namespace { |
105 | 107 |
106 std::string GetBlacklistUrlPrefix() { | 108 std::string GetBlacklistUrlPrefix() { |
107 std::stringstream blacklist_url_prefix_stream; | 109 std::stringstream blacklist_url_prefix_stream; |
108 blacklist_url_prefix_stream | 110 blacklist_url_prefix_stream |
109 << GetExperimentParam(kSuggestionsFieldTrialURLParam) | 111 << GetExperimentParam(kSuggestionsFieldTrialURLParam) |
110 << GetExperimentParam(kSuggestionsFieldTrialBlacklistPathParam) << "?" | 112 << GetExperimentParam(kSuggestionsFieldTrialBlacklistPathParam) << "?" |
111 << GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam) << "&" | 113 << GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam) << "&" |
112 << GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam) << "="; | 114 << GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam) << "="; |
113 return blacklist_url_prefix_stream.str(); | 115 return blacklist_url_prefix_stream.str(); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 // Handle a successful blacklisting. | 299 // Handle a successful blacklisting. |
298 GURL blacklisted_url; | 300 GURL blacklisted_url; |
299 if (GetBlacklistedUrl(*source, &blacklisted_url)) { | 301 if (GetBlacklistedUrl(*source, &blacklisted_url)) { |
300 blacklist_store_->RemoveUrl(blacklisted_url); | 302 blacklist_store_->RemoveUrl(blacklisted_url); |
301 } | 303 } |
302 | 304 |
303 std::string suggestions_data; | 305 std::string suggestions_data; |
304 bool success = request->GetResponseAsString(&suggestions_data); | 306 bool success = request->GetResponseAsString(&suggestions_data); |
305 DCHECK(success); | 307 DCHECK(success); |
306 | 308 |
307 // Compute suggestions, and dispatch then to requestors. On error still | 309 // Compute suggestions, and dispatch them to requestors. On error still |
308 // dispatch empty suggestions. | 310 // dispatch empty suggestions. |
309 SuggestionsProfile suggestions; | 311 SuggestionsProfile suggestions; |
310 if (suggestions_data.empty()) { | 312 if (suggestions_data.empty()) { |
311 LogResponseState(RESPONSE_EMPTY); | 313 LogResponseState(RESPONSE_EMPTY); |
312 suggestions_store_->ClearSuggestions(); | 314 suggestions_store_->ClearSuggestions(); |
313 } else if (suggestions.ParseFromString(suggestions_data)) { | 315 } else if (suggestions.ParseFromString(suggestions_data)) { |
314 LogResponseState(RESPONSE_VALID); | 316 LogResponseState(RESPONSE_VALID); |
315 thumbnail_manager_->Initialize(suggestions); | 317 thumbnail_manager_->Initialize(suggestions); |
318 | |
319 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | |
320 .ToInternalValue(); | |
321 SetDefaultExpiryTimestamps(&suggestions, now_usec + kDefaultExpiryUsec); | |
316 suggestions_store_->StoreSuggestions(suggestions); | 322 suggestions_store_->StoreSuggestions(suggestions); |
317 } else { | 323 } else { |
318 LogResponseState(RESPONSE_INVALID); | 324 LogResponseState(RESPONSE_INVALID); |
319 suggestions_store_->LoadSuggestions(&suggestions); | 325 suggestions_store_->LoadSuggestions(&suggestions); |
320 } | 326 } |
321 | 327 |
322 FilterAndServe(&suggestions); | 328 FilterAndServe(&suggestions); |
323 ScheduleBlacklistUpload(true); | 329 ScheduleBlacklistUpload(true); |
324 } | 330 } |
325 | 331 |
332 void SuggestionsService::SetDefaultExpiryTimestamps( | |
333 SuggestionsProfile* suggestions, int64 default_timestamp_usec) { | |
334 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | |
335 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | |
336 // Do not set expiry if the server has already provided a more specific | |
337 // expiry time for this suggestions. | |
Mathieu
2014/08/04 19:54:54
*this suggestion
gayane -on leave until 09-2017
2014/08/05 14:39:15
Done.
| |
338 if (!suggestion->has_expiry_ts()){ | |
Mathieu
2014/08/04 19:54:54
space before {
| |
339 suggestion->set_expiry_ts(default_timestamp_usec); | |
340 } | |
341 } | |
342 } | |
343 | |
326 void SuggestionsService::Shutdown() { | 344 void SuggestionsService::Shutdown() { |
327 // Cancel pending request and timeout closure, then serve existing requestors | 345 // Cancel pending request and timeout closure, then serve existing requestors |
328 // from cache. | 346 // from cache. |
329 pending_request_.reset(NULL); | 347 pending_request_.reset(NULL); |
330 pending_timeout_closure_.reset(NULL); | 348 pending_timeout_closure_.reset(NULL); |
331 ServeFromCache(); | 349 ServeFromCache(); |
332 } | 350 } |
333 | 351 |
334 void SuggestionsService::ServeFromCache() { | 352 void SuggestionsService::ServeFromCache() { |
335 SuggestionsProfile suggestions; | 353 SuggestionsProfile suggestions; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 if (last_request_successful) { | 397 if (last_request_successful) { |
380 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; | 398 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; |
381 } else { | 399 } else { |
382 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; | 400 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; |
383 if (candidate_delay < kBlacklistMaxDelaySec) | 401 if (candidate_delay < kBlacklistMaxDelaySec) |
384 blacklist_delay_sec_ = candidate_delay; | 402 blacklist_delay_sec_ = candidate_delay; |
385 } | 403 } |
386 } | 404 } |
387 | 405 |
388 } // namespace suggestions | 406 } // namespace suggestions |
OLD | NEW |