Chromium Code Reviews| 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..3657c03f21156763023a5b17af1c0182a5fccafc |
| --- /dev/null |
| +++ b/components/ntp_snippets/request_counter.h |
| @@ -0,0 +1,97 @@ |
| +// 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 { |
| + |
| +struct RequestTypeInfo; |
| + |
| +// 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: |
|
tschumann
2016/07/21 11:37:09
it would be nice to clearly separate implementatio
jkrcal
2016/07/21 18:17:57
Done.
|
| +// 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 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. |
| + enum class RequestType { |
| + CONTENT_SUGGESTION_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); |
| + |
| + // 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; |
|
tschumann
2016/07/21 11:37:09
as discussed offline, it would be nice to have a m
Marc Treib
2016/07/21 11:43:50
+1, I like this idea.
jkrcal
2016/07/21 18:17:58
Done. I documented the current behaviour. I guess
|
| + |
| + private: |
| + // Also emits the PerDay histogram if the day changed. |
| + 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_; |
| + |
| + // 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(RequestCounter); |
| +}; |
| + |
| +} // namespace ntp_snippets |
| + |
| +#endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |