Index: components/ntp_snippets/request_throttler.h |
diff --git a/components/ntp_snippets/request_throttler.h b/components/ntp_snippets/request_throttler.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f908825814b62fcb0790aca1a1a2702aeb91013f |
--- /dev/null |
+++ b/components/ntp_snippets/request_throttler.h |
@@ -0,0 +1,88 @@ |
+// 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_THROTTLER_H_ |
+#define COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |
+ |
+#include <string> |
+ |
+#include "base/macros.h" |
+ |
+class PrefRegistrySimple; |
+class PrefService; |
+ |
+namespace base { |
+class HistogramBase; |
+} // namespace base |
+ |
+namespace ntp_snippets { |
+ |
+struct RequestTypeInfo; |
Bernhard Bauer
2016/07/22 13:58:24
This type could be made a private member of the cl
jkrcal
2016/07/25 10:02:36
I would keep it there as I did already move it bac
Marc Treib
2016/07/25 10:06:16
Generally, keep the scope as small as possible. Th
jkrcal
2016/07/25 10:14:45
Thanks!
If it is a private member, how should I d
Marc Treib
2016/07/25 10:22:15
Ah, right - that would need to become a static mem
Bernhard Bauer
2016/07/25 11:15:03
Yeah, you would still declare it in the header as
|
+ |
+// 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.request_throttler.|type|.count" to store the current counter, |
+// - pref "ntp.request_throttler.|type|.day" to store current day to which the |
+// current counter value applies. |
+// Furthermore the counter reports to histograms: |
+// - "NewTabPage.RequestThrottler.RequestStatus_|type|" - status of each |
+// request; |
+// - "NewTabPage.RequestThrottler.PerDay_|type|" - the daily count of requests. |
+// |
+// Implementation notes: When extending this class for a new RequestType, please |
+// 1) define in request_counter.cc in kRequestTypeInfo |
+// a) the string value for your |type| and |
+// b) constants for day/count prefs; |
+// 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml |
+// (with the same string value as in 1a)). |
+class RequestThrottler { |
+ public: |
+ // Enumeration listing all current applications of the request counter. |
+ enum class RequestType { |
+ CONTENT_SUGGESTION_FETCHER |
+ }; |
+ |
+ RequestThrottler(PrefService* pref_service, |
+ RequestType type, |
Bernhard Bauer
2016/07/22 13:58:24
Alignment
jkrcal
2016/07/25 10:02:36
Done (in the follow-up CL).
|
+ int default_quota); |
+ |
+ // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. |
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
+ |
+ // Returns whether quota is available for another request and reports this |
+ // information to UMA. Forced requests always return true -- should be only |
+ // used for requests initiated by the user (if it is safe to assume that all |
+ // users cannot generate an amount of requests we cannot handle). |
+ bool DemandQuotaForRequest(bool force_request); |
+ |
+ private: |
+ // Also emits the PerDay histogram if the day changed. |
+ void ResetCounterIfDayChanged(); |
+ |
+ const char* GetRequestTypeAsString() const; |
+ |
+ int GetCount() const; |
+ void SetCount(int count); |
+ int GetDay() const; |
+ void SetDay(int day); |
+ bool HasDay() const; |
+ |
+ PrefService* pref_service_; |
+ const RequestTypeInfo& type_info_; |
+ |
+ // It comes from a variation parameter or |default_quota| as a fallback. |
+ 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(RequestThrottler); |
+}; |
+ |
+} // namespace ntp_snippets |
+ |
+#endif // COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |