OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/macros.h" |
| 11 |
| 12 class PrefRegistrySimple; |
| 13 class PrefService; |
| 14 |
| 15 namespace base { |
| 16 class HistogramBase; |
| 17 } // namespace base |
| 18 |
| 19 namespace ntp_snippets { |
| 20 |
| 21 // Counts requests to external services, compares them to a daily quota, reports |
| 22 // them to UMA. In the application code, create one local instance for each type |
| 23 // of requests, identified by the RequestType. The request counter is based on: |
| 24 // - daily quota from a variation param "quota_|type|" in the NTPSnippets trial |
| 25 // - pref "ntp.requests_quota.|type|.count" to store the current counter value, |
| 26 // - pref "ntp.requests_quota.|type|.day" to store current day to which the |
| 27 // current counter value applies. |
| 28 // Furthermore the counter reports to histograms: |
| 29 // - "NewTabPage.RequestCounter.RequestStatus_|type|" - status of each request; |
| 30 // - "NewTabPage.RequestCounter.PerDay_|type|" - the daily number of requests. |
| 31 // Thus, when you add a new RequestType of RequestCounter, you must also: |
| 32 // 1) define in request_counter.cc in kRequestTypeInfo |
| 33 // a) the string value for your |type| and |
| 34 // b) constants for days/count prefs, (and register them in |
| 35 // RegisterProfilePrefs()), |
| 36 // 2) define a new RequestCounterTypes histogram suffix in histogram.xml (with |
| 37 // the same string value as in 1a)). |
| 38 class RequestCounter { |
| 39 public: |
| 40 // Enumeration listing all current applications of the request counter. Not |
| 41 // used for UMA. |
| 42 enum class RequestType { |
| 43 ARTICLE_CONTENT_FETCHER |
| 44 }; |
| 45 |
| 46 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
| 47 // histogram, so do not change existing values. Insert new values at the end, |
| 48 // and update the histogram definition. |
| 49 enum class RequestStatus { |
| 50 FORCED, |
| 51 QUOTA_GRANTED, |
| 52 QUOTA_EXCEEDED, |
| 53 REQUEST_STATUS_COUNT |
| 54 }; |
| 55 |
| 56 RequestCounter(PrefService* pref_service, |
| 57 RequestType type, |
| 58 int default_quota); |
| 59 |
| 60 // Register profile prefs for all RequestTypes. |
| 61 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 62 |
| 63 // Returns whether quota is available for another request and reports this |
| 64 // information to UMA. |
| 65 bool DemandQuotaForRequest(); |
| 66 |
| 67 // Reports a forced request to UMA (without checking the quota). |
| 68 void ReportForcedRequest() const; |
| 69 |
| 70 private: |
| 71 void ResetCounterIfDayChanged(); |
| 72 |
| 73 std::string GetRequestTypeAsString() const; |
| 74 |
| 75 int GetCount() const; |
| 76 void SetCount(int count); |
| 77 int GetDay() const; |
| 78 void SetDay(int day); |
| 79 bool HasDay() const; |
| 80 |
| 81 PrefService* pref_service_; |
| 82 const RequestCounter::RequestType type_; |
| 83 |
| 84 int quota_; |
| 85 |
| 86 // The histograms for reporting the requests of the given |type_|. |
| 87 base::HistogramBase* histogram_request_status_; |
| 88 base::HistogramBase* histogram_per_day_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(RequestCounter); |
| 91 }; |
| 92 |
| 93 } // namespace ntp_snippets |
| 94 |
| 95 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ |
OLD | NEW |