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

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: A minor fix #3 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..a6eebc6e00a5a9af6165935e268b78f2731d2243
--- /dev/null
+++ b/components/ntp_snippets/request_counter.h
@@ -0,0 +1,81 @@
+// 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 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 |id| string. The request counter is based on:
+// - daily quota from a variation param "quota_|id|" in the NTPSnippets trial;
+// - prefs "ntp.requests_quota.|id|.day" and "ntp.requests_quota.|id|.count".
+// Furthermore the counter reports
+// - all requests to the "NewTabPage.RequestsTotal.|Id|" histogram
+// - the daily number of requests per user in the
+// "NewTabPage.RequestsPerUser.|Id|" histogram
Marc Treib 2016/07/19 09:06:17 RequestsPerUser and RequestTotal would only be dif
jkrcal 2016/07/20 09:40:40 Clarified.
+// (note that your "example_id" is autocapitalized to "ExampleId")
Marc Treib 2016/07/19 09:06:17 Why?
jkrcal 2016/07/20 09:40:40 Removed.
+// Thus, when you add an application of RequestCounter, you must also:
+// - add a static function that calls RequestCounter::RegisterPrefs with the
+// same |id| and call this function from browser_prefs.cc.
+// - define the two histograms in histogram.xml (see previous applications).
Marc Treib 2016/07/19 09:06:17 Hmmmm, I don't really like this. So, there's a cen
Marc Treib 2016/07/19 09:06:17 histograms.xml has a concept of "histogram suffixe
jkrcal 2016/07/20 09:40:40 Done.
+class RequestCounter {
+ public:
+ // 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 RequestType {
+ FORCED,
+ QUOTA_GRANTED,
+ QUOTA_EXCEEDED,
+ REQUEST_TYPE_MAX
+ };
+
+ // The |id| passed in here must be unique application-wide.
+ RequestCounter(PrefService* pref_service,
+ const std::string& id,
+ int default_quota);
+
+ // Must be called with the same |id| as used in the constructor before any
+ // calls to IsQuotaAvailable.
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry,
+ const std::string& id);
+
+ // Returns whether quota is available for another request and reports this
+ // information to UMA.
+ bool IsQuotaAvailable();
Marc Treib 2016/07/19 09:06:17 The name sounds like this is a pure getter that do
jkrcal 2016/07/20 09:40:40 Changed to GetQuota() which avoids overloading the
+
+ // Reports a forced request to UMA (without checking the quota).
+ void ReportForcedRequest();
+
+ private:
+ PrefService* pref_service_;
+ std::string id_;
+ std::string id_camel_case_;
+
+ int counter_;
+ int quota_;
+
+ void ResetCounterIfDayChanged();
Marc Treib 2016/07/19 09:06:17 Methods come before data members.
jkrcal 2016/07/20 09:40:40 Done.
+
+ static std::string GetDaysPref(const std::string& id);
+ static std::string GetCountPref(const std::string& id);
+
+ std::string GetTotalHistogramName();
+ std::string GetPerUserHistogramName();
+
+ DISALLOW_COPY_AND_ASSIGN(RequestCounter);
+};
+
+} // namespace ntp_snippets
+
+#endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_

Powered by Google App Engine
This is Rietveld 408576698