Chromium Code Reviews| 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.Total_|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 the string value for your |type| in GetRequestTypeAsString(), | |
| 33 // 2) add constants for days/count prefs, add them to GetCountPref() and | |
| 34 // GetDayPref() functions, and register them in RegisterProfilePrefs(), | |
| 35 // 3) define a new RequestCounterTypes histogram suffix in histogram.xml (with | |
| 36 // the same string value as in 1)). | |
| 37 class RequestCounter { | |
| 38 public: | |
| 39 // Enumeration listing all current applications of the request counter. Not | |
| 40 // used for uma. | |
| 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 { | |
|
Marc Treib
2016/07/20 10:19:05
I think if you add " : int" here (before the "{"),
jkrcal
2016/07/20 12:09:27
No, it does not compile without the static cast.
Marc Treib
2016/07/20 12:58:27
Alright :( Then no need to add the ": int" I gues
| |
| 49 FORCED, | |
| 50 QUOTA_GRANTED, | |
| 51 QUOTA_EXCEEDED, | |
| 52 REQUEST_STATUS_MAX | |
|
Marc Treib
2016/07/20 10:19:06
This is a COUNT, not a MAX.
jkrcal
2016/07/20 12:09:28
Done.
| |
| 53 }; | |
| 54 | |
| 55 RequestCounter(PrefService* pref_service, | |
| 56 RequestCounter::RequestType type, | |
|
Marc Treib
2016/07/20 10:19:05
nit: "RequestCounter::" not needed
jkrcal
2016/07/20 12:09:27
Done.
| |
| 57 int default_quota); | |
| 58 | |
| 59 // Register profile prefs for all RequestTypes. | |
| 60 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
| 61 | |
| 62 // Returns whether quota is available for another request and reports this | |
| 63 // information to UMA. | |
| 64 bool GetQuota(); | |
|
Marc Treib
2016/07/20 10:19:06
Now it sounds like "tell me the quota"...
jkrcal
2016/07/20 12:09:27
Huh, you are right. I've forgotten about |quota_|.
Marc Treib
2016/07/20 12:58:27
Yeah, this is much better, thanks!
Maybe add "ForR
| |
| 65 | |
| 66 // Reports a forced request to UMA (without checking the quota). | |
| 67 void ReportForcedRequest() const; | |
| 68 | |
| 69 private: | |
| 70 void ResetCounterIfDayChanged(); | |
| 71 | |
| 72 std::string GetRequestTypeAsString() const; | |
| 73 std::string GetDaysPref() const; | |
| 74 std::string GetCountPref() const; | |
| 75 | |
| 76 PrefService* pref_service_; | |
| 77 RequestCounter::RequestType type_; | |
|
Marc Treib
2016/07/20 10:19:06
const, to make it clear?
jkrcal
2016/07/20 12:09:28
Done.
| |
| 78 | |
| 79 int counter_; | |
|
Marc Treib
2016/07/20 10:19:06
Do you need to store this? Might as well just make
jkrcal
2016/07/20 12:09:27
Done.
| |
| 80 int quota_; | |
| 81 | |
| 82 // The histogram for reporting all requests of the given |type_|. | |
| 83 base::HistogramBase* histogram_total_; | |
|
Marc Treib
2016/07/20 10:19:05
If you do this for one histogram, then you should
jkrcal
2016/07/20 12:09:28
This histogram is used in each request and on two
Marc Treib
2016/07/20 12:58:27
Ah, so it's for performance reasons? Okay, I guess
| |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(RequestCounter); | |
| 86 }; | |
| 87 | |
| 88 } // namespace ntp_snippets | |
| 89 | |
| 90 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_COUNTER_H_ | |
| OLD | NEW |