OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/request_counter.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" |
| 14 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 15 #include "components/ntp_snippets/pref_names.h" |
| 16 #include "components/prefs/pref_registry_simple.h" |
| 17 #include "components/prefs/pref_service.h" |
| 18 #include "components/variations/variations_associated_data.h" |
| 19 |
| 20 namespace ntp_snippets { |
| 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 { |
| 30 |
| 31 // When adding a new type here, extend also the "RequestCounterTypes" |
| 32 // <histogram_suffixes> in histograms.xml with the |name| string. |
| 33 const RequestTypeInfo kRequestTypeInfo[] = { |
| 34 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
| 35 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, |
| 36 prefs::kSnippetFetcherQuotaDay}}; |
| 37 |
| 38 } // namespace |
| 39 |
| 40 RequestCounter::RequestCounter(PrefService* pref_service, |
| 41 RequestType type, |
| 42 int default_quota) |
| 43 : pref_service_(pref_service), |
| 44 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
| 45 DCHECK(pref_service); |
| 46 |
| 47 std::string quota = variations::GetVariationParamValue( |
| 48 ntp_snippets::kStudyName, |
| 49 base::StringPrintf("quota_%s", GetRequestTypeAsString())); |
| 50 if (!base::StringToInt(quota, "a_)) { |
| 51 LOG_IF(WARNING, !quota.empty()) |
| 52 << "Invalid variation parameter for quota for " |
| 53 << GetRequestTypeAsString(); |
| 54 quota_ = default_quota; |
| 55 } |
| 56 |
| 57 // Since the histogram names are dynamic, we cannot use the standard macros |
| 58 // and we need to lookup the histograms, instead. |
| 59 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
| 60 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
| 61 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
| 62 base::StringPrintf("NewTabPage.RequestCounter.RequestStatus_%s", |
| 63 GetRequestTypeAsString()), |
| 64 1, status_count, status_count + 1, |
| 65 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 66 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
| 67 histogram_per_day_ = base::Histogram::FactoryGet( |
| 68 base::StringPrintf("NewTabPage.RequestCounter.PerDay_%s", |
| 69 GetRequestTypeAsString()), |
| 70 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 71 } |
| 72 |
| 73 // static |
| 74 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 75 for (const RequestTypeInfo& info : kRequestTypeInfo) { |
| 76 registry->RegisterIntegerPref(info.count_pref, 0); |
| 77 registry->RegisterIntegerPref(info.day_pref, 0); |
| 78 } |
| 79 } |
| 80 |
| 81 bool RequestCounter::DemandQuotaForRequest(bool forced_request) { |
| 82 ResetCounterIfDayChanged(); |
| 83 |
| 84 if (forced_request) { |
| 85 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); |
| 86 return true; |
| 87 } |
| 88 |
| 89 int new_count = GetCount() + 1; |
| 90 SetCount(new_count); |
| 91 bool available = (new_count <= quota_); |
| 92 |
| 93 histogram_request_status_->Add( |
| 94 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED |
| 95 : RequestStatus::QUOTA_EXCEEDED)); |
| 96 return available; |
| 97 } |
| 98 |
| 99 void RequestCounter::ResetCounterIfDayChanged() { |
| 100 // The count of days since a fixed reference (the Unix epoch). |
| 101 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
| 102 |
| 103 if (!HasDay()) { |
| 104 // The counter is used for the first time in this profile. |
| 105 SetDay(now_day); |
| 106 } else if (now_day != GetDay()) { |
| 107 // Day has changed - report the number of requests from the previous day. |
| 108 histogram_per_day_->Add(GetCount()); |
| 109 // Reset the counter. |
| 110 SetCount(0); |
| 111 SetDay(now_day); |
| 112 } |
| 113 } |
| 114 |
| 115 const char* RequestCounter::GetRequestTypeAsString() const { |
| 116 return type_info_.name; |
| 117 } |
| 118 |
| 119 int RequestCounter::GetCount() const { |
| 120 return pref_service_->GetInteger(type_info_.count_pref); |
| 121 } |
| 122 |
| 123 void RequestCounter::SetCount(int count) { |
| 124 pref_service_->SetInteger(type_info_.count_pref, count); |
| 125 } |
| 126 |
| 127 int RequestCounter::GetDay() const { |
| 128 return pref_service_->GetInteger(type_info_.day_pref); |
| 129 } |
| 130 |
| 131 void RequestCounter::SetDay(int day) { |
| 132 pref_service_->SetInteger(type_info_.day_pref, day); |
| 133 } |
| 134 |
| 135 bool RequestCounter::HasDay() const { |
| 136 return pref_service_->HasPrefPath(type_info_.day_pref); |
| 137 } |
| 138 |
| 139 } // namespace ntp_snippets |
OLD | NEW |