Index: components/ntp_snippets/request_counter.h |
diff --git a/components/ntp_snippets/request_counter.h b/components/ntp_snippets/request_counter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7a78cb9d8d5f9d3e4c2979c8610a8184c31af9c8 |
--- /dev/null |
+++ b/components/ntp_snippets/request_counter.h |
@@ -0,0 +1,95 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |
+#define COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |
+ |
+#include <string> |
+ |
+#include "base/macros.h" |
+ |
+class PrefRegistrySimple; |
+class PrefService; |
+ |
+namespace base { |
+class HistogramBase; |
+} // namespace base |
+ |
+namespace ntp_snippets { |
+ |
+// Counts requests to external services, compares them to a daily quota, reports |
+// them to UMA. In the application code, create one local instance for each type |
+// of requests, identified by the RequestType. The request counter is based on: |
+// - daily quota from a variation param "quota_|type|" in the NTPSnippets trial |
+// - pref "ntp.requests_quota.|type|.count" to store the current counter value, |
+// - pref "ntp.requests_quota.|type|.day" to store current day to which the |
+// current counter value applies. |
+// Furthermore the counter reports to histograms: |
+// - "NewTabPage.RequestCounter.RequestStatus_|type|" - status of each request; |
+// - "NewTabPage.RequestCounter.PerDay_|type|" - the daily number of requests. |
+// Thus, when you add a new RequestType of RequestCounter, you must also: |
+// 1) define in request_counter.cc in kRequestTypeInfo |
+// a) the string value for your |type| and |
+// b) constants for days/count prefs, (and register them in |
+// RegisterProfilePrefs()), |
+// 2) define a new RequestCounterTypes histogram suffix in histogram.xml (with |
+// the same string value as in 1a)). |
+class RequestCounter { |
+ public: |
+ // Enumeration listing all current applications of the request counter. Not |
+ // used for UMA. |
+ enum class RequestType { |
+ ARTICLE_CONTENT_FETCHER |
+ }; |
+ |
+ // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
+ // histogram, so do not change existing values. Insert new values at the end, |
+ // and update the histogram definition. |
+ enum class RequestStatus { |
+ FORCED, |
+ QUOTA_GRANTED, |
+ QUOTA_EXCEEDED, |
+ REQUEST_STATUS_COUNT |
+ }; |
+ |
+ RequestCounter(PrefService* pref_service, |
+ RequestType type, |
+ int default_quota); |
+ |
+ // Register profile prefs for all RequestTypes. |
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
+ |
+ // Returns whether quota is available for another request and reports this |
+ // information to UMA. |
+ bool DemandQuotaForRequest(); |
+ |
+ // Reports a forced request to UMA (without checking the quota). |
+ void ReportForcedRequest() const; |
+ |
+ private: |
+ void ResetCounterIfDayChanged(); |
+ |
+ std::string GetRequestTypeAsString() const; |
+ |
+ int GetCount() const; |
+ void SetCount(int count); |
+ int GetDay() const; |
+ void SetDay(int day); |
+ bool HasDay() const; |
+ |
+ PrefService* pref_service_; |
+ const RequestCounter::RequestType type_; |
+ |
+ int quota_; |
+ |
+ // The histograms for reporting the requests of the given |type_|. |
+ base::HistogramBase* histogram_request_status_; |
+ base::HistogramBase* histogram_per_day_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RequestCounter); |
+}; |
+ |
+} // namespace ntp_snippets |
+ |
+#endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |