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 ntp_snippets { | |
16 | |
17 // Counts requests to external services, compares them to a daily quota, reports | |
18 // them to UMA. In the application code, create one local instance for each type | |
19 // of requests, identified by the |id| string. The request counter is based on: | |
20 // - daily quota from a variation param "quota_|id|" in the NTPSnippets trial; | |
21 // - prefs "ntp.requests_quota.|id|.day" and "ntp.requests_quota.|id|.count". | |
22 // Furthermore the counter reports | |
23 // - all requests to the "NewTabPage.RequestsTotal.|Id|" histogram | |
24 // - the daily number of requests per user in the | |
25 // "NewTabPage.RequestsPerUser.|Id|" histogram | |
Marc Treib
2016/07/19 09:06:17
RequestsPerUser and RequestTotal would only be dif
jkrcal
2016/07/20 09:40:40
Clarified.
| |
26 // (note that your "example_id" is autocapitalized to "ExampleId") | |
Marc Treib
2016/07/19 09:06:17
Why?
jkrcal
2016/07/20 09:40:40
Removed.
| |
27 // Thus, when you add an application of RequestCounter, you must also: | |
28 // - add a static function that calls RequestCounter::RegisterPrefs with the | |
29 // same |id| and call this function from browser_prefs.cc. | |
30 // - define the two histograms in histogram.xml (see previous applications). | |
Marc Treib
2016/07/19 09:06:17
Hmmmm, I don't really like this. So, there's a cen
Marc Treib
2016/07/19 09:06:17
histograms.xml has a concept of "histogram suffixe
jkrcal
2016/07/20 09:40:40
Done.
| |
31 class RequestCounter { | |
32 public: | |
33 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | |
34 // histogram, so do not change existing values. Insert new values at the end, | |
35 // and update the histogram definition. | |
36 enum class RequestType { | |
37 FORCED, | |
38 QUOTA_GRANTED, | |
39 QUOTA_EXCEEDED, | |
40 REQUEST_TYPE_MAX | |
41 }; | |
42 | |
43 // The |id| passed in here must be unique application-wide. | |
44 RequestCounter(PrefService* pref_service, | |
45 const std::string& id, | |
46 int default_quota); | |
47 | |
48 // Must be called with the same |id| as used in the constructor before any | |
49 // calls to IsQuotaAvailable. | |
50 static void RegisterProfilePrefs(PrefRegistrySimple* registry, | |
51 const std::string& id); | |
52 | |
53 // Returns whether quota is available for another request and reports this | |
54 // information to UMA. | |
55 bool IsQuotaAvailable(); | |
Marc Treib
2016/07/19 09:06:17
The name sounds like this is a pure getter that do
jkrcal
2016/07/20 09:40:40
Changed to GetQuota() which avoids overloading the
| |
56 | |
57 // Reports a forced request to UMA (without checking the quota). | |
58 void ReportForcedRequest(); | |
59 | |
60 private: | |
61 PrefService* pref_service_; | |
62 std::string id_; | |
63 std::string id_camel_case_; | |
64 | |
65 int counter_; | |
66 int quota_; | |
67 | |
68 void ResetCounterIfDayChanged(); | |
Marc Treib
2016/07/19 09:06:17
Methods come before data members.
jkrcal
2016/07/20 09:40:40
Done.
| |
69 | |
70 static std::string GetDaysPref(const std::string& id); | |
71 static std::string GetCountPref(const std::string& id); | |
72 | |
73 std::string GetTotalHistogramName(); | |
74 std::string GetPerUserHistogramName(); | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
77 }; | |
78 | |
79 } // namespace ntp_snippets | |
80 | |
81 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
OLD | NEW |