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; | |
Marc Treib
2016/07/20 16:15:14
s/days/day
jkrcal
2016/07/21 07:44:28
Done.
| |
35 // 2) define a new RequestCounterTypes histogram suffix in histogram.xml (with | |
36 // the same string value as in 1a)). | |
37 class RequestCounter { | |
38 public: | |
39 // Enumeration listing all current applications of the request counter. Not | |
40 // 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.
| |
41 enum class RequestType { | |
42 ARTICLE_CONTENT_FETCHER | |
43 }; | |
44 | |
45 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | |
46 // histogram, so do not change existing values. Insert new values at the end, | |
47 // and update the histogram definition. | |
48 enum class RequestStatus { | |
49 FORCED, | |
50 QUOTA_GRANTED, | |
51 QUOTA_EXCEEDED, | |
52 REQUEST_STATUS_COUNT | |
53 }; | |
54 | |
55 // Used internally for working with a RequestType. | |
56 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.
| |
57 const char* name; | |
58 const char* count_pref; | |
59 const char* day_pref; | |
60 }; | |
61 | |
62 RequestCounter(PrefService* pref_service, | |
63 RequestType type, | |
64 int default_quota); | |
65 | |
66 // Registers profile prefs for all RequestTypes. | |
67 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
68 | |
69 // Returns whether quota is available for another request and reports this | |
70 // information to UMA. | |
71 bool DemandQuotaForRequest(); | |
72 | |
73 // Reports a forced request to UMA (without checking the quota). | |
74 void ReportForcedRequest() const; | |
75 | |
76 private: | |
77 // 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.
| |
78 void ResetCounterIfDayChanged(); | |
79 | |
80 std::string GetRequestTypeAsString() const; | |
81 | |
82 int GetCount() const; | |
83 void SetCount(int count); | |
84 int GetDay() const; | |
85 void SetDay(int day); | |
86 bool HasDay() const; | |
87 | |
88 PrefService* pref_service_; | |
89 const RequestTypeInfo& type_info_; | |
90 | |
91 int quota_; | |
92 | |
93 // The histograms for reporting the requests of the given |type_|. | |
94 base::HistogramBase* histogram_request_status_; | |
95 base::HistogramBase* histogram_per_day_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
98 }; | |
99 | |
100 } // namespace ntp_snippets | |
101 | |
102 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
OLD | NEW |