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 <limits.h> | |
|
Marc Treib
2016/08/09 14:04:53
Move to the .cc
jkrcal
2016/08/10 10:27:26
Done.
| |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 | 12 |
| 12 class PrefRegistrySimple; | 13 class PrefRegistrySimple; |
| 13 class PrefService; | 14 class PrefService; |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 class HistogramBase; | 17 class HistogramBase; |
| 17 } // namespace base | 18 } // namespace base |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 33 // Implementation notes: When extending this class for a new RequestType, please | 34 // Implementation notes: When extending this class for a new RequestType, please |
| 34 // 1) define in request_counter.cc in kRequestTypeInfo | 35 // 1) define in request_counter.cc in kRequestTypeInfo |
| 35 // a) the string value for your |type| and | 36 // a) the string value for your |type| and |
| 36 // b) constants for day/count prefs; | 37 // b) constants for day/count prefs; |
| 37 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml | 38 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml |
| 38 // (with the same string value as in 1a)). | 39 // (with the same string value as in 1a)). |
| 39 class RequestThrottler { | 40 class RequestThrottler { |
| 40 public: | 41 public: |
| 41 // Enumeration listing all current applications of the request counter. | 42 // Enumeration listing all current applications of the request counter. |
| 42 enum class RequestType { | 43 enum class RequestType { |
| 43 CONTENT_SUGGESTION_FETCHER | 44 CONTENT_SUGGESTION_FETCHER, |
| 45 CONTENT_SUGGESTION_THUMBNAIL, | |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 RequestThrottler(PrefService* pref_service, RequestType type); | 48 RequestThrottler(PrefService* pref_service, RequestType type); |
| 47 | 49 |
| 48 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. | 50 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. |
| 49 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 51 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 50 | 52 |
| 51 // Returns whether quota is available for another request and reports this | 53 // Returns whether quota is available for another request and reports this |
| 52 // information to UMA. Forced requests always return true -- should be only | 54 // information to UMA. Interactive requests should be always granted (upon |
| 53 // used for requests initiated by the user (if it is safe to assume that all | 55 // standard conditions) and should be only used for requests initiated by the |
| 54 // users cannot generate an amount of requests we cannot handle). | 56 // user (if it is safe to assume that all users cannot generate an amount of |
| 55 bool DemandQuotaForRequest(bool force_request); | 57 // requests we cannot handle). |
| 58 bool DemandQuotaForRequest(bool interactive_request); | |
| 56 | 59 |
| 57 private: | 60 private: |
| 58 friend class RequestThrottlerTest; | 61 friend class RequestThrottlerTest; |
| 59 // Used internally for working with a RequestType. | 62 // Used internally for working with a RequestType. |
| 60 struct RequestTypeInfo; | 63 struct RequestTypeInfo; |
| 61 | 64 |
| 62 // The array of info entries - one per each RequestType. | 65 // The array of info entries - one per each RequestType. |
| 63 static const RequestTypeInfo kRequestTypeInfo[]; | 66 static const RequestTypeInfo kRequestTypeInfo[]; |
| 64 | 67 |
| 65 // Also emits the PerDay histogram if the day changed. | 68 // Also emits the PerDay histogram if the day changed. |
| 66 void ResetCounterIfDayChanged(); | 69 void ResetCounterIfDayChanged(); |
| 67 | 70 |
| 68 const char* GetRequestTypeAsString() const; | 71 const char* GetRequestTypeAsString() const; |
| 69 | 72 |
| 70 int GetCount() const; | 73 int GetQuota(bool interactive_request) const; |
| 71 void SetCount(int count); | 74 int GetCount(bool interactive_request) const; |
| 75 void SetCount(bool interactive_request, int count); | |
| 72 int GetDay() const; | 76 int GetDay() const; |
| 73 void SetDay(int day); | 77 void SetDay(int day); |
| 74 bool HasDay() const; | 78 bool HasDay() const; |
| 75 | 79 |
| 76 PrefService* pref_service_; | 80 PrefService* pref_service_; |
| 77 const RequestTypeInfo& type_info_; | 81 const RequestTypeInfo& type_info_; |
| 78 | 82 |
| 79 // It comes from a variation parameter or |default_quota| as a fallback. | 83 // The quotas come from variation parameters or default quotas as a fallback. |
|
Marc Treib
2016/08/09 14:04:53
I'd formulate this the other way around: The quota
jkrcal
2016/08/10 10:27:26
Done.
| |
| 80 int quota_; | 84 int quota_; |
| 85 int interactive_quota_; | |
| 81 | 86 |
| 82 // The histograms for reporting the requests of the given |type_|. | 87 // The histograms for reporting the requests of the given |type_|. |
| 83 base::HistogramBase* histogram_request_status_; | 88 base::HistogramBase* histogram_request_status_; |
| 84 base::HistogramBase* histogram_per_day_; | 89 base::HistogramBase* histogram_per_day_; |
| 85 | 90 |
| 86 DISALLOW_COPY_AND_ASSIGN(RequestThrottler); | 91 DISALLOW_COPY_AND_ASSIGN(RequestThrottler); |
| 87 }; | 92 }; |
| 88 | 93 |
| 89 } // namespace ntp_snippets | 94 } // namespace ntp_snippets |
| 90 | 95 |
| 91 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ | 96 #endif // COMPONENTS_NTP_SNIPPETS_REQUEST_THROTTLER_H_ |
| OLD | NEW |