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..5b643cad4973b443b1634c1c0bc5fcff96089d04 |
| --- /dev/null |
| +++ b/components/ntp_snippets/request_counter.h |
| @@ -0,0 +1,90 @@ |
| +// 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()), |
|
Marc Treib
2016/07/20 12:58:27
Can't RegisterProfilePrefs just loop over the arra
jkrcal
2016/07/20 14:20:17
Done.
Marc Treib
2016/07/20 15:13:57
Great! Please update the comment too :)
jkrcal
2016/07/20 15:46:22
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 12:58:27
nit: UMA, all caps
jkrcal
2016/07/20 14:20:17
Done.
|
| + enum class RequestType : int { |
| + 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 : int { |
| + 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 DemandQuota(); |
| + |
| + // Reports a forced request to UMA (without checking the quota). |
| + void ReportForcedRequest() const; |
| + |
| + private: |
| + void ResetCounterIfDayChanged(); |
| + |
| + std::string GetRequestTypeAsString() const; |
| + std::string GetDaysPref() const; |
|
Marc Treib
2016/07/20 12:58:27
GetDayPref
I'd just have made "GetDay", "SetDay"
jkrcal
2016/07/20 14:20:17
Done.
|
| + std::string GetCountPref() const; |
| + |
| + PrefService* pref_service_; |
| + const RequestCounter::RequestType type_; |
| + |
| + int quota_; |
| + |
| + // The histogram for reporting all requests of the given |type_|. |
| + base::HistogramBase* histogram_request_status_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RequestCounter); |
| +}; |
| + |
| +} // namespace ntp_snippets |
| + |
| +#endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |