| 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/pref_registry/pref_registry_syncable.h" | 16 #include "components/pref_registry/pref_registry_syncable.h" |
| 17 #include "components/suggestions/blacklist_store.h" | 17 #include "components/suggestions/blacklist_store.h" |
| 18 #include "components/suggestions/suggestions_store.h" | 18 #include "components/suggestions/suggestions_store.h" |
| 19 #include "components/variations/net/variations_http_header_provider.h" | 19 #include "components/variations/net/variations_http_header_provider.h" |
| 20 #include "components/variations/variations_associated_data.h" | |
| 21 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 22 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 23 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 24 #include "net/base/url_util.h" | 23 #include "net/base/url_util.h" |
| 25 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 26 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
| 27 #include "net/http/http_util.h" | 26 #include "net/http/http_util.h" |
| 28 #include "net/url_request/url_fetcher.h" | 27 #include "net/url_request/url_fetcher.h" |
| 29 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
| 30 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 44 RESPONSE_VALID, | 43 RESPONSE_VALID, |
| 45 RESPONSE_STATE_SIZE | 44 RESPONSE_STATE_SIZE |
| 46 }; | 45 }; |
| 47 | 46 |
| 48 // Will log the supplied response |state|. | 47 // Will log the supplied response |state|. |
| 49 void LogResponseState(SuggestionsResponseState state) { | 48 void LogResponseState(SuggestionsResponseState state) { |
| 50 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, | 49 UMA_HISTOGRAM_ENUMERATION("Suggestions.ResponseState", state, |
| 51 RESPONSE_STATE_SIZE); | 50 RESPONSE_STATE_SIZE); |
| 52 } | 51 } |
| 53 | 52 |
| 54 // Obtains the experiment parameter under the supplied |key|, or empty string | |
| 55 // if the parameter does not exist. | |
| 56 std::string GetExperimentParam(const std::string& key) { | |
| 57 return variations::GetVariationParamValue(kSuggestionsFieldTrialName, key); | |
| 58 } | |
| 59 | |
| 60 GURL BuildBlacklistRequestURL(const std::string& blacklist_url_prefix, | 53 GURL BuildBlacklistRequestURL(const std::string& blacklist_url_prefix, |
| 61 const GURL& candidate_url) { | 54 const GURL& candidate_url) { |
| 62 return GURL(blacklist_url_prefix + | 55 return GURL(blacklist_url_prefix + |
| 63 net::EscapeQueryParamValue(candidate_url.spec(), true)); | 56 net::EscapeQueryParamValue(candidate_url.spec(), true)); |
| 64 } | 57 } |
| 65 | 58 |
| 66 // Runs each callback in |requestors| on |suggestions|, then deallocates | 59 // Runs each callback in |requestors| on |suggestions|, then deallocates |
| 67 // |requestors|. | 60 // |requestors|. |
| 68 void DispatchRequestsAndClear( | 61 void DispatchRequestsAndClear( |
| 69 const SuggestionsProfile& suggestions, | 62 const SuggestionsProfile& suggestions, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 81 | 74 |
| 82 // Multiplier on the delay used when re-scheduling a failed request. | 75 // Multiplier on the delay used when re-scheduling a failed request. |
| 83 const int kSchedulingBackoffMultiplier = 2; | 76 const int kSchedulingBackoffMultiplier = 2; |
| 84 | 77 |
| 85 // Maximum valid delay for scheduling a request. Candidate delays larger than | 78 // Maximum valid delay for scheduling a request. Candidate delays larger than |
| 86 // this are rejected. This means the maximum backoff is at least 5 / 2 minutes. | 79 // this are rejected. This means the maximum backoff is at least 5 / 2 minutes. |
| 87 const int kSchedulingMaxDelaySec = 5 * 60; | 80 const int kSchedulingMaxDelaySec = 5 * 60; |
| 88 | 81 |
| 89 } // namespace | 82 } // namespace |
| 90 | 83 |
| 91 const char kSuggestionsFieldTrialName[] = "ChromeSuggestions"; | |
| 92 const char kSuggestionsFieldTrialControlParam[] = "control"; | |
| 93 const char kSuggestionsFieldTrialStateEnabled[] = "enabled"; | |
| 94 const char kSuggestionsFieldTrialStateParam[] = "state"; | |
| 95 | |
| 96 // TODO(mathp): Put this in TemplateURL. | 84 // TODO(mathp): Put this in TemplateURL. |
| 97 const char kSuggestionsURL[] = "https://www.google.com/chromesuggestions?t=2"; | 85 const char kSuggestionsURL[] = "https://www.google.com/chromesuggestions?t=2"; |
| 98 const char kSuggestionsBlacklistURLPrefix[] = | 86 const char kSuggestionsBlacklistURLPrefix[] = |
| 99 "https://www.google.com/chromesuggestions/blacklist?t=2&url="; | 87 "https://www.google.com/chromesuggestions/blacklist?t=2&url="; |
| 100 const char kSuggestionsBlacklistURLParam[] = "url"; | 88 const char kSuggestionsBlacklistURLParam[] = "url"; |
| 101 | 89 |
| 102 // The default expiry timeout is 72 hours. | 90 // The default expiry timeout is 72 hours. |
| 103 const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour; | 91 const int64 kDefaultExpiryUsec = 72 * base::Time::kMicrosecondsPerHour; |
| 104 | 92 |
| 105 SuggestionsService::SuggestionsService( | 93 SuggestionsService::SuggestionsService( |
| 106 net::URLRequestContextGetter* url_request_context, | 94 net::URLRequestContextGetter* url_request_context, |
| 107 scoped_ptr<SuggestionsStore> suggestions_store, | 95 scoped_ptr<SuggestionsStore> suggestions_store, |
| 108 scoped_ptr<ImageManager> thumbnail_manager, | 96 scoped_ptr<ImageManager> thumbnail_manager, |
| 109 scoped_ptr<BlacklistStore> blacklist_store) | 97 scoped_ptr<BlacklistStore> blacklist_store) |
| 110 : url_request_context_(url_request_context), | 98 : url_request_context_(url_request_context), |
| 111 suggestions_store_(suggestions_store.Pass()), | 99 suggestions_store_(suggestions_store.Pass()), |
| 112 thumbnail_manager_(thumbnail_manager.Pass()), | 100 thumbnail_manager_(thumbnail_manager.Pass()), |
| 113 blacklist_store_(blacklist_store.Pass()), | 101 blacklist_store_(blacklist_store.Pass()), |
| 114 scheduling_delay_(TimeDelta::FromSeconds(kDefaultSchedulingDelaySec)), | 102 scheduling_delay_(TimeDelta::FromSeconds(kDefaultSchedulingDelaySec)), |
| 115 suggestions_url_(kSuggestionsURL), | 103 suggestions_url_(kSuggestionsURL), |
| 116 blacklist_url_prefix_(kSuggestionsBlacklistURLPrefix), | 104 blacklist_url_prefix_(kSuggestionsBlacklistURLPrefix), |
| 117 weak_ptr_factory_(this) {} | 105 weak_ptr_factory_(this) {} |
| 118 | 106 |
| 119 SuggestionsService::~SuggestionsService() {} | 107 SuggestionsService::~SuggestionsService() {} |
| 120 | 108 |
| 121 // static | |
| 122 bool SuggestionsService::IsControlGroup() { | |
| 123 return GetExperimentParam(kSuggestionsFieldTrialControlParam) == | |
| 124 kSuggestionsFieldTrialStateEnabled; | |
| 125 } | |
| 126 | |
| 127 void SuggestionsService::FetchSuggestionsData( | 109 void SuggestionsService::FetchSuggestionsData( |
| 128 SyncState sync_state, | 110 SyncState sync_state, |
| 129 SuggestionsService::ResponseCallback callback) { | 111 SuggestionsService::ResponseCallback callback) { |
| 130 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 waiting_requestors_.push_back(callback); | 113 waiting_requestors_.push_back(callback); |
| 132 if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { | 114 if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) { |
| 133 // Cancel any ongoing request, to stop interacting with the server. | 115 // Cancel any ongoing request, to stop interacting with the server. |
| 134 pending_request_.reset(NULL); | 116 pending_request_.reset(NULL); |
| 135 suggestions_store_->ClearSuggestions(); | 117 suggestions_store_->ClearSuggestions(); |
| 136 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_); | 118 DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); | 358 scheduling_delay_ = TimeDelta::FromSeconds(kDefaultSchedulingDelaySec); |
| 377 } else { | 359 } else { |
| 378 TimeDelta candidate_delay = | 360 TimeDelta candidate_delay = |
| 379 scheduling_delay_ * kSchedulingBackoffMultiplier; | 361 scheduling_delay_ * kSchedulingBackoffMultiplier; |
| 380 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) | 362 if (candidate_delay < TimeDelta::FromSeconds(kSchedulingMaxDelaySec)) |
| 381 scheduling_delay_ = candidate_delay; | 363 scheduling_delay_ = candidate_delay; |
| 382 } | 364 } |
| 383 } | 365 } |
| 384 | 366 |
| 385 } // namespace suggestions | 367 } // namespace suggestions |
| OLD | NEW |