| 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 // The default expiry timeout it 72 hours. |
| 105 const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour; |
| 106 |
| 104 namespace { | 107 namespace { |
| 105 | 108 |
| 106 std::string GetBlacklistUrlPrefix() { | 109 std::string GetBlacklistUrlPrefix() { |
| 107 std::stringstream blacklist_url_prefix_stream; | 110 std::stringstream blacklist_url_prefix_stream; |
| 108 blacklist_url_prefix_stream | 111 blacklist_url_prefix_stream |
| 109 << GetExperimentParam(kSuggestionsFieldTrialURLParam) | 112 << GetExperimentParam(kSuggestionsFieldTrialURLParam) |
| 110 << GetExperimentParam(kSuggestionsFieldTrialBlacklistPathParam) << "?" | 113 << GetExperimentParam(kSuggestionsFieldTrialBlacklistPathParam) << "?" |
| 111 << GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam) << "&" | 114 << GetExperimentParam(kSuggestionsFieldTrialCommonParamsParam) << "&" |
| 112 << GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam) << "="; | 115 << GetExperimentParam(kSuggestionsFieldTrialBlacklistUrlParam) << "="; |
| 113 return blacklist_url_prefix_stream.str(); | 116 return blacklist_url_prefix_stream.str(); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 // Handle a successful blacklisting. | 300 // Handle a successful blacklisting. |
| 298 GURL blacklisted_url; | 301 GURL blacklisted_url; |
| 299 if (GetBlacklistedUrl(*source, &blacklisted_url)) { | 302 if (GetBlacklistedUrl(*source, &blacklisted_url)) { |
| 300 blacklist_store_->RemoveUrl(blacklisted_url); | 303 blacklist_store_->RemoveUrl(blacklisted_url); |
| 301 } | 304 } |
| 302 | 305 |
| 303 std::string suggestions_data; | 306 std::string suggestions_data; |
| 304 bool success = request->GetResponseAsString(&suggestions_data); | 307 bool success = request->GetResponseAsString(&suggestions_data); |
| 305 DCHECK(success); | 308 DCHECK(success); |
| 306 | 309 |
| 307 // Compute suggestions, and dispatch then to requestors. On error still | 310 // Compute suggestions, and dispatch them to requestors. On error still |
| 308 // dispatch empty suggestions. | 311 // dispatch empty suggestions. |
| 309 SuggestionsProfile suggestions; | 312 SuggestionsProfile suggestions; |
| 310 if (suggestions_data.empty()) { | 313 if (suggestions_data.empty()) { |
| 311 LogResponseState(RESPONSE_EMPTY); | 314 LogResponseState(RESPONSE_EMPTY); |
| 312 suggestions_store_->ClearSuggestions(); | 315 suggestions_store_->ClearSuggestions(); |
| 313 } else if (suggestions.ParseFromString(suggestions_data)) { | 316 } else if (suggestions.ParseFromString(suggestions_data)) { |
| 314 LogResponseState(RESPONSE_VALID); | 317 LogResponseState(RESPONSE_VALID); |
| 315 thumbnail_manager_->Initialize(suggestions); | 318 thumbnail_manager_->Initialize(suggestions); |
| 319 |
| 320 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) |
| 321 .ToInternalValue(); |
| 322 SetDefaultExpiryTimestamp(&suggestions, now_usec + kDefaultExpiryUsec); |
| 316 suggestions_store_->StoreSuggestions(suggestions); | 323 suggestions_store_->StoreSuggestions(suggestions); |
| 317 } else { | 324 } else { |
| 318 LogResponseState(RESPONSE_INVALID); | 325 LogResponseState(RESPONSE_INVALID); |
| 319 suggestions_store_->LoadSuggestions(&suggestions); | 326 suggestions_store_->LoadSuggestions(&suggestions); |
| 320 } | 327 } |
| 321 | 328 |
| 322 FilterAndServe(&suggestions); | 329 FilterAndServe(&suggestions); |
| 323 ScheduleBlacklistUpload(true); | 330 ScheduleBlacklistUpload(true); |
| 324 } | 331 } |
| 325 | 332 |
| 333 void SuggestionsService::SetDefaultExpiryTimestamp( |
| 334 SuggestionsProfile* suggestions, int64 default_timestamp_usec) { |
| 335 for (int i = 0; i < suggestions->suggestions_size(); ++i) { |
| 336 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); |
| 337 // Do not set expiry if the server has already provided a more specific |
| 338 // expiry time for this suggestion. |
| 339 if (!suggestion->has_expiry_ts()) { |
| 340 suggestion->set_expiry_ts(default_timestamp_usec); |
| 341 } |
| 342 } |
| 343 } |
| 344 |
| 326 void SuggestionsService::Shutdown() { | 345 void SuggestionsService::Shutdown() { |
| 327 // Cancel pending request and timeout closure, then serve existing requestors | 346 // Cancel pending request and timeout closure, then serve existing requestors |
| 328 // from cache. | 347 // from cache. |
| 329 pending_request_.reset(NULL); | 348 pending_request_.reset(NULL); |
| 330 pending_timeout_closure_.reset(NULL); | 349 pending_timeout_closure_.reset(NULL); |
| 331 ServeFromCache(); | 350 ServeFromCache(); |
| 332 } | 351 } |
| 333 | 352 |
| 334 void SuggestionsService::ServeFromCache() { | 353 void SuggestionsService::ServeFromCache() { |
| 335 SuggestionsProfile suggestions; | 354 SuggestionsProfile suggestions; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 if (last_request_successful) { | 398 if (last_request_successful) { |
| 380 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; | 399 blacklist_delay_sec_ = kBlacklistDefaultDelaySec; |
| 381 } else { | 400 } else { |
| 382 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; | 401 int candidate_delay = blacklist_delay_sec_ * kBlacklistBackoffMultiplier; |
| 383 if (candidate_delay < kBlacklistMaxDelaySec) | 402 if (candidate_delay < kBlacklistMaxDelaySec) |
| 384 blacklist_delay_sec_ = candidate_delay; | 403 blacklist_delay_sec_ = candidate_delay; |
| 385 } | 404 } |
| 386 } | 405 } |
| 387 | 406 |
| 388 } // namespace suggestions | 407 } // namespace suggestions |
| OLD | NEW |