Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 | 11 |
| 12 class PrefRegistrySimple; | 12 class PrefRegistrySimple; |
| 13 class PrefService; | 13 class PrefService; |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class HistogramBase; | 16 class HistogramBase; |
| 17 } // namespace base | 17 } // namespace base |
| 18 | 18 |
| 19 namespace ntp_snippets { | 19 namespace ntp_snippets { |
| 20 | 20 |
| 21 struct RequestTypeInfo; | |
| 22 | |
| 23 // Counts requests to external services, compares them to a daily quota, reports | 21 // 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 | 22 // 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: | 23 // 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 | 24 // - daily quota from a variation param "quota_|type|" in the NTPSnippets trial |
| 27 // - pref "ntp.request_throttler.|type|.count" to store the current counter, | 25 // - pref "ntp.request_throttler.|type|.count" to store the current counter, |
| 28 // - pref "ntp.request_throttler.|type|.day" to store current day to which the | 26 // - pref "ntp.request_throttler.|type|.day" to store current day to which the |
| 29 // current counter value applies. | 27 // current counter value applies. |
| 30 // Furthermore the counter reports to histograms: | 28 // Furthermore the counter reports to histograms: |
| 31 // - "NewTabPage.RequestThrottler.RequestStatus_|type|" - status of each | 29 // - "NewTabPage.RequestThrottler.RequestStatus_|type|" - status of each |
| 32 // request; | 30 // request; |
| 33 // - "NewTabPage.RequestThrottler.PerDay_|type|" - the daily count of requests. | 31 // - "NewTabPage.RequestThrottler.PerDay_|type|" - the daily count of requests. |
| 34 // | 32 // |
| 35 // Implementation notes: When extending this class for a new RequestType, please | 33 // Implementation notes: When extending this class for a new RequestType, please |
| 36 // 1) define in request_counter.cc in kRequestTypeInfo | 34 // 1) define in request_counter.cc in kRequestTypeInfo |
| 37 // a) the string value for your |type| and | 35 // a) the string value for your |type| and |
| 38 // b) constants for day/count prefs; | 36 // b) constants for day/count prefs; |
| 39 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml | 37 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml |
| 40 // (with the same string value as in 1a)). | 38 // (with the same string value as in 1a)). |
| 41 class RequestThrottler { | 39 class RequestThrottler { |
| 42 public: | 40 public: |
| 43 // Enumeration listing all current applications of the request counter. | 41 // Enumeration listing all current applications of the request counter. |
| 44 enum class RequestType { | 42 enum class RequestType { |
| 45 CONTENT_SUGGESTION_FETCHER | 43 CONTENT_SUGGESTION_FETCHER |
| 46 }; | 44 }; |
| 47 | 45 |
| 48 RequestThrottler(PrefService* pref_service, | 46 RequestThrottler(PrefService* pref_service, RequestType type); |
| 49 RequestType type, | |
| 50 int default_quota); | |
| 51 | 47 |
| 52 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. | 48 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. |
| 53 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 49 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 54 | 50 |
| 55 // Returns whether quota is available for another request and reports this | 51 // Returns whether quota is available for another request and reports this |
| 56 // information to UMA. Forced requests always return true -- should be only | 52 // information to UMA. Forced requests always return true -- should be only |
| 57 // used for requests initiated by the user (if it is safe to assume that all | 53 // used for requests initiated by the user (if it is safe to assume that all |
| 58 // users cannot generate an amount of requests we cannot handle). | 54 // users cannot generate an amount of requests we cannot handle). |
| 59 bool DemandQuotaForRequest(bool force_request); | 55 bool DemandQuotaForRequest(bool force_request); |
| 60 | 56 |
| 61 private: | 57 private: |
| 58 // Used internally for working with a RequestType. | |
| 59 struct RequestTypeInfo { | |
|
Bernhard Bauer
2016/07/26 09:52:45
Do you need to define the struct here or would a d
jkrcal
2016/07/26 10:05:07
Done.
| |
| 60 const char* name; | |
| 61 const char* count_pref; | |
| 62 const char* day_pref; | |
| 63 const int default_quota; | |
| 64 }; | |
| 65 | |
| 66 // The array of info entries - one per each RequestType. | |
| 67 static const RequestTypeInfo kRequestTypeInfo[1]; | |
|
Bernhard Bauer
2016/07/26 09:52:45
Same here for the array size -- could you just lea
jkrcal
2016/07/26 10:05:07
Ah, right!
| |
| 68 | |
| 62 // Also emits the PerDay histogram if the day changed. | 69 // Also emits the PerDay histogram if the day changed. |
| 63 void ResetCounterIfDayChanged(); | 70 void ResetCounterIfDayChanged(); |
| 64 | 71 |
| 65 const char* GetRequestTypeAsString() const; | 72 const char* GetRequestTypeAsString() const; |
| 66 | 73 |
| 67 int GetCount() const; | 74 int GetCount() const; |
| 68 void SetCount(int count); | 75 void SetCount(int count); |
| 69 int GetDay() const; | 76 int GetDay() const; |
| 70 void SetDay(int day); | 77 void SetDay(int day); |
| 71 bool HasDay() const; | 78 bool HasDay() const; |
| 72 | 79 |
| 73 PrefService* pref_service_; | 80 PrefService* pref_service_; |
| 74 const RequestTypeInfo& type_info_; | 81 const RequestTypeInfo& type_info_; |
| 75 | 82 |
| 76 // It comes from a variation parameter or |default_quota| as a fallback. | 83 // It comes from a variation parameter or |default_quota| as a fallback. |
| 77 int quota_; | 84 int quota_; |
| 78 | 85 |
| 79 // The histograms for reporting the requests of the given |type_|. | 86 // The histograms for reporting the requests of the given |type_|. |
| 80 base::HistogramBase* histogram_request_status_; | 87 base::HistogramBase* histogram_request_status_; |
| 81 base::HistogramBase* histogram_per_day_; | 88 base::HistogramBase* histogram_per_day_; |
| 82 | 89 |
| 83 DISALLOW_COPY_AND_ASSIGN(RequestThrottler); | 90 DISALLOW_COPY_AND_ASSIGN(RequestThrottler); |
| 84 }; | 91 }; |
| 85 | 92 |
| 86 } // namespace ntp_snippets | 93 } // namespace ntp_snippets |
| 87 | 94 |
| 88 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ | 95 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |
| OLD | NEW |