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()), | |
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.
| |
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. | |
Marc Treib
2016/07/20 12:58:27
nit: UMA, all caps
jkrcal
2016/07/20 14:20:17
Done.
| |
42 enum class RequestType : int { | |
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 : int { | |
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 DemandQuota(); | |
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 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.
| |
75 std::string GetCountPref() const; | |
76 | |
77 PrefService* pref_service_; | |
78 const RequestCounter::RequestType type_; | |
79 | |
80 int quota_; | |
81 | |
82 // The histogram for reporting all requests of the given |type_|. | |
83 base::HistogramBase* histogram_request_status_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
86 }; | |
87 | |
88 } // namespace ntp_snippets | |
89 | |
90 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
OLD | NEW |