OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "extensions/browser/quota_service.h" | 5 #include "extensions/browser/quota_service.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "extensions/browser/extension_function.h" | 9 #include "extensions/browser/extension_function.h" |
10 #include "extensions/common/error_utils.h" | 10 #include "extensions/common/error_utils.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if (!Apply(*i, event_time)) | 130 if (!Apply(*i, event_time)) |
131 return false; // It only takes one to spoil it for everyone. | 131 return false; // It only takes one to spoil it for everyone. |
132 } | 132 } |
133 return true; | 133 return true; |
134 } | 134 } |
135 | 135 |
136 std::string QuotaLimitHeuristic::GetError() const { | 136 std::string QuotaLimitHeuristic::GetError() const { |
137 return extensions::ErrorUtils::FormatErrorMessage(kOverQuotaError, name_); | 137 return extensions::ErrorUtils::FormatErrorMessage(kOverQuotaError, name_); |
138 } | 138 } |
139 | 139 |
140 QuotaService::SustainedLimit::SustainedLimit(const base::TimeDelta& sustain, | |
141 const Config& config, | |
142 BucketMapper* map, | |
143 const std::string& name) | |
144 : QuotaLimitHeuristic(config, map, name), | |
145 repeat_exhaustion_allowance_(sustain.InSeconds() / | |
146 config.refill_interval.InSeconds()), | |
147 num_available_repeat_exhaustions_(repeat_exhaustion_allowance_) {} | |
148 | |
149 bool QuotaService::TimedLimit::Apply(Bucket* bucket, | 140 bool QuotaService::TimedLimit::Apply(Bucket* bucket, |
150 const base::TimeTicks& event_time) { | 141 const base::TimeTicks& event_time) { |
151 if (event_time > bucket->expiration()) | 142 if (event_time > bucket->expiration()) |
152 bucket->Reset(config(), event_time); | 143 bucket->Reset(config(), event_time); |
153 | 144 |
154 return bucket->DeductToken(); | 145 return bucket->DeductToken(); |
155 } | 146 } |
156 | 147 |
157 bool QuotaService::SustainedLimit::Apply(Bucket* bucket, | |
158 const base::TimeTicks& event_time) { | |
159 if (event_time > bucket->expiration()) { | |
160 // We reset state for this item and start over again if this request breaks | |
161 // the bad cycle that was previously being tracked. This occurs if the | |
162 // state in the bucket expired recently (it has been long enough since the | |
163 // event that we don't care about the last event), but the bucket still has | |
164 // tokens (so pressure was not sustained over that time), OR we are more | |
165 // than 1 full refill interval away from the last event (so even if we used | |
166 // up all the tokens in the last bucket, nothing happened in the entire | |
167 // next refill interval, so it doesn't matter). | |
168 if (bucket->has_tokens() || | |
169 event_time > bucket->expiration() + config().refill_interval) { | |
170 bucket->Reset(config(), event_time); | |
171 num_available_repeat_exhaustions_ = repeat_exhaustion_allowance_; | |
172 } else if (--num_available_repeat_exhaustions_ > 0) { | |
173 // The last interval was saturated with requests, and this is the first | |
174 // event in the next interval. If this happens | |
175 // repeat_exhaustion_allowance_ times, it's a violation. Reset the bucket | |
176 // state to start timing from the end of the last interval (and we'll | |
177 // deduct the token below) so we can detect this each time it happens. | |
178 bucket->Reset(config(), bucket->expiration()); | |
179 } else { | |
180 // No allowances left; this request is a violation. | |
181 return false; | |
182 } | |
183 } | |
184 | |
185 // We can go negative since we check has_tokens when we get to *next* bucket, | |
186 // and for the small interval all that matters is whether we used up all the | |
187 // tokens (which is true if num_tokens_ <= 0). | |
188 bucket->DeductToken(); | |
189 return true; | |
190 } | |
191 | |
192 } // namespace extensions | 148 } // namespace extensions |
OLD | NEW |