Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1498)

Unified Diff: components/ntp_snippets/request_counter.h

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marc's comments #4 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3a91f38675121b892fa5c162e66d3fe998a1fa8c
--- /dev/null
+++ b/components/ntp_snippets/request_counter.h
@@ -0,0 +1,102 @@
+// 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;
Marc Treib 2016/07/20 16:15:14 s/days/day
jkrcal 2016/07/21 07:44:28 Done.
+// 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.
Marc Treib 2016/07/20 16:15:14 "Not used for UMA" is the default assumption, no n
jkrcal 2016/07/21 07:44:28 Done.
+ 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
+ };
+
+ // Used internally for working with a RequestType.
+ struct RequestTypeInfo {
Marc Treib 2016/07/20 16:15:14 You can just forward-declare this and keep the def
jkrcal 2016/07/21 07:44:28 Done.
+ const char* name;
+ const char* count_pref;
+ const char* day_pref;
+ };
+
+ RequestCounter(PrefService* pref_service,
+ RequestType type,
+ int default_quota);
+
+ // Registers 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:
+ // Also emmits the PerDay histogram if the day changed.
Marc Treib 2016/07/20 16:15:14 s/emmits/emits
jkrcal 2016/07/21 07:44:28 Done.
+ 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 RequestTypeInfo& type_info_;
+
+ 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_

Powered by Google App Engine
This is Rietveld 408576698