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" |
(...skipping 22 matching lines...) Expand all Loading... |
33 // Implementation notes: When extending this class for a new RequestType, please | 33 // Implementation notes: When extending this class for a new RequestType, please |
34 // 1) define in request_counter.cc in kRequestTypeInfo | 34 // 1) define in request_counter.cc in kRequestTypeInfo |
35 // a) the string value for your |type| and | 35 // a) the string value for your |type| and |
36 // b) constants for day/count prefs; | 36 // b) constants for day/count prefs; |
37 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml | 37 // 2) define a new RequestThrottlerTypes histogram suffix in histogram.xml |
38 // (with the same string value as in 1a)). | 38 // (with the same string value as in 1a)). |
39 class RequestThrottler { | 39 class RequestThrottler { |
40 public: | 40 public: |
41 // Enumeration listing all current applications of the request counter. | 41 // Enumeration listing all current applications of the request counter. |
42 enum class RequestType { | 42 enum class RequestType { |
43 CONTENT_SUGGESTION_FETCHER | 43 CONTENT_SUGGESTION_FETCHER, |
| 44 CONTENT_SUGGESTION_THUMBNAIL, |
44 }; | 45 }; |
45 | 46 |
46 RequestThrottler(PrefService* pref_service, RequestType type); | 47 RequestThrottler(PrefService* pref_service, RequestType type); |
47 | 48 |
48 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. | 49 // Registers profile prefs for all RequestTypes. Called from browser_prefs.cc. |
49 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 50 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
50 | 51 |
51 // Returns whether quota is available for another request and reports this | 52 // Returns whether quota is available for another request and reports this |
52 // information to UMA. Forced requests always return true -- should be only | 53 // 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 | 54 // standard conditions) and should be only used for requests initiated by the |
54 // users cannot generate an amount of requests we cannot handle). | 55 // user (if it is safe to assume that all users cannot generate an amount of |
55 bool DemandQuotaForRequest(bool force_request); | 56 // requests we cannot handle). |
| 57 bool DemandQuotaForRequest(bool interactive_request); |
56 | 58 |
57 private: | 59 private: |
58 friend class RequestThrottlerTest; | 60 friend class RequestThrottlerTest; |
59 // Used internally for working with a RequestType. | 61 // Used internally for working with a RequestType. |
60 struct RequestTypeInfo; | 62 struct RequestTypeInfo; |
61 | 63 |
62 // The array of info entries - one per each RequestType. | 64 // The array of info entries - one per each RequestType. |
63 static const RequestTypeInfo kRequestTypeInfo[]; | 65 static const RequestTypeInfo kRequestTypeInfo[]; |
64 | 66 |
65 // Also emits the PerDay histogram if the day changed. | 67 // Also emits the PerDay histogram if the day changed. |
66 void ResetCounterIfDayChanged(); | 68 void ResetCounterIfDayChanged(); |
67 | 69 |
68 const char* GetRequestTypeAsString() const; | 70 const char* GetRequestTypeAsString() const; |
69 | 71 |
70 int GetCount() const; | 72 int GetQuota(bool interactive_request) const; |
71 void SetCount(int count); | 73 int GetCount(bool interactive_request) const; |
| 74 void SetCount(bool interactive_request, int count); |
72 int GetDay() const; | 75 int GetDay() const; |
73 void SetDay(int day); | 76 void SetDay(int day); |
74 bool HasDay() const; | 77 bool HasDay() const; |
75 | 78 |
76 PrefService* pref_service_; | 79 PrefService* pref_service_; |
77 const RequestTypeInfo& type_info_; | 80 const RequestTypeInfo& type_info_; |
78 | 81 |
79 // It comes from a variation parameter or |default_quota| as a fallback. | 82 // The quotas are hardcoded, but can be overridden by variation params. |
80 int quota_; | 83 int quota_; |
| 84 int interactive_quota_; |
81 | 85 |
82 // The histograms for reporting the requests of the given |type_|. | 86 // The histograms for reporting the requests of the given |type_|. |
83 base::HistogramBase* histogram_request_status_; | 87 base::HistogramBase* histogram_request_status_; |
84 base::HistogramBase* histogram_per_day_; | 88 base::HistogramBase* histogram_per_day_background_; |
| 89 base::HistogramBase* histogram_per_day_interactive_; |
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 |