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 struct RequestTypeInfo; | |
22 | |
23 // Counts requests to external services, compares them to a daily quota, reports | |
24 // them to UMA. In the application code, create one local instance for each type | |
25 // of requests, identified by the RequestType. The request counter is based on: | |
26 // - daily quota from a variation param "quota_|type|" in the NTPSnippets trial | |
27 // - pref "ntp.requests_quota.|type|.count" to store the current counter value, | |
28 // - pref "ntp.requests_quota.|type|.day" to store current day to which the | |
29 // current counter value applies. | |
30 // Furthermore the counter reports to histograms: | |
31 // - "NewTabPage.RequestCounter.RequestStatus_|type|" - status of each request; | |
32 // - "NewTabPage.RequestCounter.PerDay_|type|" - the daily number of requests. | |
33 // 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.
| |
34 // 1) define in request_counter.cc in kRequestTypeInfo | |
35 // a) the string value for your |type| and | |
36 // b) constants for day/count prefs; | |
37 // 2) define a new RequestCounterTypes histogram suffix in histogram.xml (with | |
38 // the same string value as in 1a)). | |
39 class RequestCounter { | |
40 public: | |
41 // Enumeration listing all current applications of the request counter. | |
42 enum class RequestType { | |
43 CONTENT_SUGGESTION_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 // Registers 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; | |
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
| |
69 | |
70 private: | |
71 // Also emits the PerDay histogram if the day changed. | |
72 void ResetCounterIfDayChanged(); | |
73 | |
74 std::string GetRequestTypeAsString() const; | |
75 | |
76 int GetCount() const; | |
77 void SetCount(int count); | |
78 int GetDay() const; | |
79 void SetDay(int day); | |
80 bool HasDay() const; | |
81 | |
82 PrefService* pref_service_; | |
83 const RequestTypeInfo& type_info_; | |
84 | |
85 // It comes from a variation parameter or |default_quota| as a fallback. | |
86 int quota_; | |
87 | |
88 // The histograms for reporting the requests of the given |type_|. | |
89 base::HistogramBase* histogram_request_status_; | |
90 base::HistogramBase* histogram_per_day_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
93 }; | |
94 | |
95 } // namespace ntp_snippets | |
96 | |
97 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
OLD | NEW |