| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/ntp_snippets/ntp_snippets_constants.h" | 14 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 15 #include "components/ntp_snippets/pref_names.h" | 15 #include "components/ntp_snippets/pref_names.h" |
| 16 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
| 17 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 18 #include "components/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
| 19 | 19 |
| 20 namespace ntp_snippets { | 20 namespace ntp_snippets { |
| 21 | 21 |
| 22 // Used internally for working with a RequestType. | |
| 23 struct RequestTypeInfo { | |
| 24 const char* name; | |
| 25 const char* count_pref; | |
| 26 const char* day_pref; | |
| 27 }; | |
| 28 | |
| 29 namespace { | 22 namespace { |
| 30 | 23 |
| 31 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
| 32 // histogram, so do not change existing values. Insert new values at the end, | 25 // histogram, so do not change existing values. Insert new values at the end, |
| 33 // and update the histogram definition. | 26 // and update the histogram definition. |
| 34 enum class RequestStatus { | 27 enum class RequestStatus { |
| 35 FORCED, | 28 FORCED, |
| 36 QUOTA_GRANTED, | 29 QUOTA_GRANTED, |
| 37 QUOTA_EXCEEDED, | 30 QUOTA_EXCEEDED, |
| 38 REQUEST_STATUS_COUNT | 31 REQUEST_STATUS_COUNT |
| 39 }; | 32 }; |
| 40 | 33 |
| 34 } // namespace |
| 35 |
| 41 // When adding a new type here, extend also the "RequestCounterTypes" | 36 // When adding a new type here, extend also the "RequestCounterTypes" |
| 42 // <histogram_suffixes> in histograms.xml with the |name| string. | 37 // <histogram_suffixes> in histograms.xml with the |name| string. |
| 43 const RequestTypeInfo kRequestTypeInfo[] = { | 38 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { |
| 44 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, | 39 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
| 45 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, | 40 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, |
| 46 prefs::kSnippetFetcherQuotaDay}}; | 41 prefs::kSnippetFetcherQuotaDay, 50}}; |
| 47 | 42 |
| 48 } // namespace | 43 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) |
| 49 | |
| 50 RequestThrottler::RequestThrottler(PrefService* pref_service, | |
| 51 RequestType type, | |
| 52 int default_quota) | |
| 53 : pref_service_(pref_service), | 44 : pref_service_(pref_service), |
| 54 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { | 45 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
| 55 DCHECK(pref_service); | 46 DCHECK(pref_service); |
| 56 | 47 |
| 57 std::string quota = variations::GetVariationParamValue( | 48 std::string quota = variations::GetVariationParamValue( |
| 58 ntp_snippets::kStudyName, | 49 ntp_snippets::kStudyName, |
| 59 base::StringPrintf("quota_%s", GetRequestTypeAsString())); | 50 base::StringPrintf("quota_%s", GetRequestTypeAsString())); |
| 60 if (!base::StringToInt(quota, "a_)) { | 51 if (!base::StringToInt(quota, "a_)) { |
| 61 LOG_IF(WARNING, !quota.empty()) | 52 LOG_IF(WARNING, !quota.empty()) |
| 62 << "Invalid variation parameter for quota for " | 53 << "Invalid variation parameter for quota for " |
| 63 << GetRequestTypeAsString(); | 54 << GetRequestTypeAsString(); |
| 64 quota_ = default_quota; | 55 quota_ = type_info_.default_quota; |
| 65 } | 56 } |
| 66 | 57 |
| 67 // Since the histogram names are dynamic, we cannot use the standard macros | 58 // Since the histogram names are dynamic, we cannot use the standard macros |
| 68 // and we need to lookup the histograms, instead. | 59 // and we need to lookup the histograms, instead. |
| 69 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); | 60 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
| 70 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). | 61 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
| 71 histogram_request_status_ = base::LinearHistogram::FactoryGet( | 62 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
| 72 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", | 63 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", |
| 73 GetRequestTypeAsString()), | 64 GetRequestTypeAsString()), |
| 74 1, status_count, status_count + 1, | 65 1, status_count, status_count + 1, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 131 |
| 141 void RequestThrottler::SetDay(int day) { | 132 void RequestThrottler::SetDay(int day) { |
| 142 pref_service_->SetInteger(type_info_.day_pref, day); | 133 pref_service_->SetInteger(type_info_.day_pref, day); |
| 143 } | 134 } |
| 144 | 135 |
| 145 bool RequestThrottler::HasDay() const { | 136 bool RequestThrottler::HasDay() const { |
| 146 return pref_service_->HasPrefPath(type_info_.day_pref); | 137 return pref_service_->HasPrefPath(type_info_.day_pref); |
| 147 } | 138 } |
| 148 | 139 |
| 149 } // namespace ntp_snippets | 140 } // namespace ntp_snippets |
| OLD | NEW |