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 // | |
34 // Implementation notes: When extending this class for a new RequestType, please | |
35 // 1) define in request_counter.cc in kRequestTypeInfo | |
36 // a) the string value for your |type| and | |
37 // b) constants for day/count prefs; | |
38 // 2) define a new RequestCounterTypes histogram suffix in histogram.xml (with | |
39 // the same string value as in 1a)). | |
40 class RequestCounter { | |
41 public: | |
42 // Enumeration listing all current applications of the request counter. | |
43 enum class RequestType { | |
44 CONTENT_SUGGESTION_FETCHER | |
tschumann
2016/07/21 18:27:45
are there plans for further types? if not, we migh
jkrcal
2016/07/21 19:17:38
Sure there are:
- Fetching thumbnails,
- Fetchi
tschumann
2016/07/22 07:49:08
ok. Let's have a quick in-person chat about those
jkrcal
2016/07/22 09:10:03
Acknowledged.
| |
45 }; | |
46 | |
47 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | |
48 // histogram, so do not change existing values. Insert new values at the end, | |
49 // and update the histogram definition. | |
50 enum class RequestStatus { | |
tschumann
2016/07/21 18:27:45
why is this exposed as part of the public interfac
jkrcal
2016/07/21 19:17:38
Oh, thanks for a pair of fresh eyes. I could not s
| |
51 FORCED, | |
52 QUOTA_GRANTED, | |
53 QUOTA_EXCEEDED, | |
54 REQUEST_STATUS_COUNT | |
55 }; | |
56 | |
57 RequestCounter(PrefService* pref_service, | |
58 RequestType type, | |
59 int default_quota); | |
60 | |
61 // Registers profile prefs for all RequestTypes. | |
62 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
63 | |
64 // Returns whether quota is available for another request and reports this | |
65 // information to UMA. Forced requests do not count against the quota and | |
tschumann
2016/07/21 18:27:45
i consider the counting aspect an implementation d
jkrcal
2016/07/21 19:17:38
I do not think this is an implementation detail. I
tschumann
2016/07/22 07:49:08
My point would be that places which use this class
jkrcal
2016/07/22 09:10:03
Thanks for explaining. Now I tend to agree (not to
| |
66 // return always true -- useful for requests initiated by the user. | |
67 bool DemandQuotaForRequest(bool force_request); | |
68 | |
69 private: | |
70 // Also emits the PerDay histogram if the day changed. | |
71 void ResetCounterIfDayChanged(); | |
72 | |
73 const char* 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 RequestTypeInfo& type_info_; | |
83 | |
84 // It comes from a variation parameter or |default_quota| as a fallback. | |
85 int quota_; | |
86 | |
87 // The histograms for reporting the requests of the given |type_|. | |
88 base::HistogramBase* histogram_request_status_; | |
89 base::HistogramBase* histogram_per_day_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
92 }; | |
93 | |
94 } // namespace ntp_snippets | |
95 | |
96 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
OLD | NEW |