| 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 #include "components/ntp_snippets/request_throttler.h" | 5 #include "components/ntp_snippets/request_throttler.h" |
| 6 | 6 |
| 7 #include <limits.h> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "components/ntp_snippets/ntp_snippets_constants.h" | 15 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 15 #include "components/ntp_snippets/pref_names.h" | 16 #include "components/ntp_snippets/pref_names.h" |
| 16 #include "components/prefs/pref_registry_simple.h" | 17 #include "components/prefs/pref_registry_simple.h" |
| 17 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
| 18 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
| 19 | 20 |
| 20 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 25 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
| 25 // histogram, so do not change existing values. Insert new values at the end, | 26 // histogram, so do not change existing values. Insert new values at the end, |
| 26 // and update the histogram definition. | 27 // and update the histogram definition. |
| 27 enum class RequestStatus { | 28 enum class RequestStatus { |
| 28 FORCED, | 29 INTERACTIVE_QUOTA_GRANTED, |
| 29 QUOTA_GRANTED, | 30 BACKGROUND_QUOTA_GRANTED, |
| 30 QUOTA_EXCEEDED, | 31 BACKGROUND_QUOTA_EXCEEDED, |
| 32 INTERACTIVE_QUOTA_EXCEEDED, |
| 31 REQUEST_STATUS_COUNT | 33 REQUEST_STATUS_COUNT |
| 32 }; | 34 }; |
| 33 | 35 |
| 36 // Quota value to use if no quota should be applied (by default). |
| 37 const int kUnlimitedQuota = INT_MAX; |
| 38 |
| 34 } // namespace | 39 } // namespace |
| 35 | 40 |
| 36 struct RequestThrottler::RequestTypeInfo { | 41 struct RequestThrottler::RequestTypeInfo { |
| 37 const char* name; | 42 const char* name; |
| 38 const char* count_pref; | 43 const char* count_pref; |
| 44 const char* interactive_count_pref; |
| 39 const char* day_pref; | 45 const char* day_pref; |
| 40 const int default_quota; | 46 const int default_quota; |
| 47 const int default_interactive_quota; |
| 41 }; | 48 }; |
| 42 | 49 |
| 43 // When adding a new type here, extend also the "RequestCounterTypes" | 50 // When adding a new type here, extend also the "RequestThrottlerTypes" |
| 44 // <histogram_suffixes> in histograms.xml with the |name| string. | 51 // <histogram_suffixes> in histograms.xml with the |name| string. |
| 45 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { | 52 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { |
| 46 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, | 53 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
| 47 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, | 54 {"SuggestionFetcher", prefs::kSnippetFetcherRequestCount, |
| 48 prefs::kSnippetFetcherQuotaDay, 50}}; | 55 prefs::kSnippetFetcherInteractiveRequestCount, |
| 56 prefs::kSnippetFetcherRequestsDay, 50, kUnlimitedQuota}, |
| 57 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL, |
| 58 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount, |
| 59 prefs::kSnippetThumbnailsInteractiveRequestCount, |
| 60 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}}; |
| 49 | 61 |
| 50 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) | 62 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) |
| 51 : pref_service_(pref_service), | 63 : pref_service_(pref_service), |
| 52 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { | 64 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
| 53 DCHECK(pref_service); | 65 DCHECK(pref_service); |
| 54 | 66 |
| 55 std::string quota = variations::GetVariationParamValue( | 67 std::string quota = variations::GetVariationParamValue( |
| 56 ntp_snippets::kStudyName, | 68 ntp_snippets::kStudyName, |
| 57 base::StringPrintf("quota_%s", GetRequestTypeAsString())); | 69 base::StringPrintf("quota_%s", GetRequestTypeName())); |
| 58 if (!base::StringToInt(quota, "a_)) { | 70 if (!base::StringToInt(quota, "a_)) { |
| 59 LOG_IF(WARNING, !quota.empty()) | 71 LOG_IF(WARNING, !quota.empty()) |
| 60 << "Invalid variation parameter for quota for " | 72 << "Invalid variation parameter for quota for " |
| 61 << GetRequestTypeAsString(); | 73 << GetRequestTypeName(); |
| 62 quota_ = type_info_.default_quota; | 74 quota_ = type_info_.default_quota; |
| 63 } | 75 } |
| 64 | 76 |
| 77 std::string interactive_quota = variations::GetVariationParamValue( |
| 78 ntp_snippets::kStudyName, |
| 79 base::StringPrintf("interactive_quota_%s", GetRequestTypeName())); |
| 80 if (!base::StringToInt(interactive_quota, &interactive_quota_)) { |
| 81 LOG_IF(WARNING, !interactive_quota.empty()) |
| 82 << "Invalid variation parameter for interactive quota for " |
| 83 << GetRequestTypeName(); |
| 84 interactive_quota_ = type_info_.default_interactive_quota; |
| 85 } |
| 86 |
| 65 // Since the histogram names are dynamic, we cannot use the standard macros | 87 // Since the histogram names are dynamic, we cannot use the standard macros |
| 66 // and we need to lookup the histograms, instead. | 88 // and we need to lookup the histograms, instead. |
| 67 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); | 89 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
| 68 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). | 90 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
| 69 histogram_request_status_ = base::LinearHistogram::FactoryGet( | 91 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
| 70 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", | 92 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", |
| 71 GetRequestTypeAsString()), | 93 GetRequestTypeName()), |
| 72 1, status_count, status_count + 1, | 94 1, status_count, status_count + 1, |
| 73 base::HistogramBase::kUmaTargetedHistogramFlag); | 95 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 74 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). | 96 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
| 75 histogram_per_day_ = base::Histogram::FactoryGet( | 97 histogram_per_day_background_ = base::Histogram::FactoryGet( |
| 76 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", | 98 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", |
| 77 GetRequestTypeAsString()), | 99 GetRequestTypeName()), |
| 100 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 101 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
| 102 histogram_per_day_interactive_ = base::Histogram::FactoryGet( |
| 103 base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s", |
| 104 GetRequestTypeName()), |
| 78 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 105 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 79 } | 106 } |
| 80 | 107 |
| 81 // static | 108 // static |
| 82 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 109 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 83 for (const RequestTypeInfo& info : kRequestTypeInfo) { | 110 for (const RequestTypeInfo& info : kRequestTypeInfo) { |
| 84 registry->RegisterIntegerPref(info.count_pref, 0); | 111 registry->RegisterIntegerPref(info.count_pref, 0); |
| 112 registry->RegisterIntegerPref(info.interactive_count_pref, 0); |
| 85 registry->RegisterIntegerPref(info.day_pref, 0); | 113 registry->RegisterIntegerPref(info.day_pref, 0); |
| 86 } | 114 } |
| 87 } | 115 } |
| 88 | 116 |
| 89 bool RequestThrottler::DemandQuotaForRequest(bool forced_request) { | 117 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { |
| 90 ResetCounterIfDayChanged(); | 118 ResetCounterIfDayChanged(); |
| 91 | 119 |
| 92 if (forced_request) { | 120 int new_count = GetCount(interactive_request) + 1; |
| 93 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); | 121 SetCount(interactive_request, new_count); |
| 94 return true; | 122 bool available = (new_count <= GetQuota(interactive_request)); |
| 123 |
| 124 if (interactive_request) { |
| 125 histogram_request_status_->Add(static_cast<int>( |
| 126 available ? RequestStatus::INTERACTIVE_QUOTA_GRANTED |
| 127 : RequestStatus::INTERACTIVE_QUOTA_EXCEEDED)); |
| 128 } else { |
| 129 histogram_request_status_->Add( |
| 130 static_cast<int>(available ? RequestStatus::BACKGROUND_QUOTA_GRANTED |
| 131 : RequestStatus::BACKGROUND_QUOTA_EXCEEDED)); |
| 95 } | 132 } |
| 96 | |
| 97 int new_count = GetCount() + 1; | |
| 98 SetCount(new_count); | |
| 99 bool available = (new_count <= quota_); | |
| 100 | |
| 101 histogram_request_status_->Add( | |
| 102 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED | |
| 103 : RequestStatus::QUOTA_EXCEEDED)); | |
| 104 return available; | 133 return available; |
| 105 } | 134 } |
| 106 | 135 |
| 107 void RequestThrottler::ResetCounterIfDayChanged() { | 136 void RequestThrottler::ResetCounterIfDayChanged() { |
| 108 // The count of days since a fixed reference (the Unix epoch). | 137 // The count of days since a fixed reference (the Unix epoch). |
| 109 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); | 138 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
| 110 | 139 |
| 111 if (!HasDay()) { | 140 if (!HasDay()) { |
| 112 // The counter is used for the first time in this profile. | 141 // The counter is used for the first time in this profile. |
| 113 SetDay(now_day); | 142 SetDay(now_day); |
| 114 } else if (now_day != GetDay()) { | 143 } else if (now_day != GetDay()) { |
| 115 // Day has changed - report the number of requests from the previous day. | 144 // Day has changed - report the number of requests from the previous day. |
| 116 histogram_per_day_->Add(GetCount()); | 145 histogram_per_day_background_->Add(GetCount(/*interactive_request=*/false)); |
| 117 // Reset the counter. | 146 histogram_per_day_interactive_->Add(GetCount(/*interactive_request=*/true)); |
| 118 SetCount(0); | 147 // Reset the counters. |
| 148 SetCount(/*interactive_request=*/false, 0); |
| 149 SetCount(/*interactive_request=*/true, 0); |
| 119 SetDay(now_day); | 150 SetDay(now_day); |
| 120 } | 151 } |
| 121 } | 152 } |
| 122 | 153 |
| 123 const char* RequestThrottler::GetRequestTypeAsString() const { | 154 const char* RequestThrottler::GetRequestTypeName() const { |
| 124 return type_info_.name; | 155 return type_info_.name; |
| 125 } | 156 } |
| 126 | 157 |
| 127 int RequestThrottler::GetCount() const { | 158 // TODO(jkrcal): turn RequestTypeInfo into a proper class, move those methods |
| 128 return pref_service_->GetInteger(type_info_.count_pref); | 159 // onto the class and hide the members. |
| 160 int RequestThrottler::GetQuota(bool interactive_request) const { |
| 161 return interactive_request ? interactive_quota_ : quota_; |
| 129 } | 162 } |
| 130 | 163 |
| 131 void RequestThrottler::SetCount(int count) { | 164 int RequestThrottler::GetCount(bool interactive_request) const { |
| 132 pref_service_->SetInteger(type_info_.count_pref, count); | 165 return pref_service_->GetInteger(interactive_request |
| 166 ? type_info_.interactive_count_pref |
| 167 : type_info_.count_pref); |
| 168 } |
| 169 |
| 170 void RequestThrottler::SetCount(bool interactive_request, int count) { |
| 171 pref_service_->SetInteger(interactive_request |
| 172 ? type_info_.interactive_count_pref |
| 173 : type_info_.count_pref, |
| 174 count); |
| 133 } | 175 } |
| 134 | 176 |
| 135 int RequestThrottler::GetDay() const { | 177 int RequestThrottler::GetDay() const { |
| 136 return pref_service_->GetInteger(type_info_.day_pref); | 178 return pref_service_->GetInteger(type_info_.day_pref); |
| 137 } | 179 } |
| 138 | 180 |
| 139 void RequestThrottler::SetDay(int day) { | 181 void RequestThrottler::SetDay(int day) { |
| 140 pref_service_->SetInteger(type_info_.day_pref, day); | 182 pref_service_->SetInteger(type_info_.day_pref, day); |
| 141 } | 183 } |
| 142 | 184 |
| 143 bool RequestThrottler::HasDay() const { | 185 bool RequestThrottler::HasDay() const { |
| 144 return pref_service_->HasPrefPath(type_info_.day_pref); | 186 return pref_service_->HasPrefPath(type_info_.day_pref); |
| 145 } | 187 } |
| 146 | 188 |
| 147 } // namespace ntp_snippets | 189 } // namespace ntp_snippets |
| OLD | NEW |