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 |
| 36 struct RequestThrottler::RequestTypeInfo { |
| 37 const char* name; |
| 38 const char* count_pref; |
| 39 const char* day_pref; |
| 40 const int default_quota; |
| 41 }; |
| 42 |
41 // When adding a new type here, extend also the "RequestCounterTypes" | 43 // When adding a new type here, extend also the "RequestCounterTypes" |
42 // <histogram_suffixes> in histograms.xml with the |name| string. | 44 // <histogram_suffixes> in histograms.xml with the |name| string. |
43 const RequestTypeInfo kRequestTypeInfo[] = { | 45 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { |
44 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, | 46 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
45 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, | 47 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, |
46 prefs::kSnippetFetcherQuotaDay}}; | 48 prefs::kSnippetFetcherQuotaDay, 50}}; |
47 | 49 |
48 } // namespace | 50 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), | 51 : pref_service_(pref_service), |
54 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { | 52 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
55 DCHECK(pref_service); | 53 DCHECK(pref_service); |
56 | 54 |
57 std::string quota = variations::GetVariationParamValue( | 55 std::string quota = variations::GetVariationParamValue( |
58 ntp_snippets::kStudyName, | 56 ntp_snippets::kStudyName, |
59 base::StringPrintf("quota_%s", GetRequestTypeAsString())); | 57 base::StringPrintf("quota_%s", GetRequestTypeAsString())); |
60 if (!base::StringToInt(quota, "a_)) { | 58 if (!base::StringToInt(quota, "a_)) { |
61 LOG_IF(WARNING, !quota.empty()) | 59 LOG_IF(WARNING, !quota.empty()) |
62 << "Invalid variation parameter for quota for " | 60 << "Invalid variation parameter for quota for " |
63 << GetRequestTypeAsString(); | 61 << GetRequestTypeAsString(); |
64 quota_ = default_quota; | 62 quota_ = type_info_.default_quota; |
65 } | 63 } |
66 | 64 |
67 // Since the histogram names are dynamic, we cannot use the standard macros | 65 // Since the histogram names are dynamic, we cannot use the standard macros |
68 // and we need to lookup the histograms, instead. | 66 // and we need to lookup the histograms, instead. |
69 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); | 67 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
70 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). | 68 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
71 histogram_request_status_ = base::LinearHistogram::FactoryGet( | 69 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
72 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", | 70 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", |
73 GetRequestTypeAsString()), | 71 GetRequestTypeAsString()), |
74 1, status_count, status_count + 1, | 72 1, status_count, status_count + 1, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 138 |
141 void RequestThrottler::SetDay(int day) { | 139 void RequestThrottler::SetDay(int day) { |
142 pref_service_->SetInteger(type_info_.day_pref, day); | 140 pref_service_->SetInteger(type_info_.day_pref, day); |
143 } | 141 } |
144 | 142 |
145 bool RequestThrottler::HasDay() const { | 143 bool RequestThrottler::HasDay() const { |
146 return pref_service_->HasPrefPath(type_info_.day_pref); | 144 return pref_service_->HasPrefPath(type_info_.day_pref); |
147 } | 145 } |
148 | 146 |
149 } // namespace ntp_snippets | 147 } // namespace ntp_snippets |
OLD | NEW |