OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extensions_quota_service.h" | 5 #include "chrome/browser/extensions/extensions_quota_service.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/extensions/extension_function.h" | 9 #include "chrome/browser/extensions/extension_function.h" |
10 #include "extensions/common/error_utils.h" | 10 #include "extensions/common/error_utils.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 ExtensionsQuotaService::~ExtensionsQuotaService() { | 31 ExtensionsQuotaService::~ExtensionsQuotaService() { |
32 DCHECK(CalledOnValidThread()); | 32 DCHECK(CalledOnValidThread()); |
33 purge_timer_.Stop(); | 33 purge_timer_.Stop(); |
34 Purge(); | 34 Purge(); |
35 } | 35 } |
36 | 36 |
37 std::string ExtensionsQuotaService::Assess( | 37 std::string ExtensionsQuotaService::Assess( |
38 const std::string& extension_id, | 38 const std::string& extension_id, |
39 ExtensionFunction* function, | 39 ExtensionFunction* function, |
40 const ListValue* args, | 40 const base::ListValue* args, |
41 const base::TimeTicks& event_time) { | 41 const base::TimeTicks& event_time) { |
42 DCHECK(CalledOnValidThread()); | 42 DCHECK(CalledOnValidThread()); |
43 | 43 |
44 if (function->ShouldSkipQuotaLimiting()) | 44 if (function->ShouldSkipQuotaLimiting()) |
45 return std::string(); | 45 return std::string(); |
46 | 46 |
47 // Lookup function list for extension. | 47 // Lookup function list for extension. |
48 FunctionHeuristicsMap& functions = function_heuristics_[extension_id]; | 48 FunctionHeuristicsMap& functions = function_heuristics_[extension_id]; |
49 | 49 |
50 // Lookup heuristics for function, create if necessary. | 50 // Lookup heuristics for function, create if necessary. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 PurgeFunctionHeuristicsMap(&it->second); | 99 PurgeFunctionHeuristicsMap(&it->second); |
100 } | 100 } |
101 | 101 |
102 void QuotaLimitHeuristic::Bucket::Reset(const Config& config, | 102 void QuotaLimitHeuristic::Bucket::Reset(const Config& config, |
103 const base::TimeTicks& start) { | 103 const base::TimeTicks& start) { |
104 num_tokens_ = config.refill_token_count; | 104 num_tokens_ = config.refill_token_count; |
105 expiration_ = start + config.refill_interval; | 105 expiration_ = start + config.refill_interval; |
106 } | 106 } |
107 | 107 |
108 void QuotaLimitHeuristic::SingletonBucketMapper::GetBucketsForArgs( | 108 void QuotaLimitHeuristic::SingletonBucketMapper::GetBucketsForArgs( |
109 const ListValue* args, | 109 const base::ListValue* args, |
110 BucketList* buckets) { | 110 BucketList* buckets) { |
111 buckets->push_back(&bucket_); | 111 buckets->push_back(&bucket_); |
112 } | 112 } |
113 | 113 |
114 QuotaLimitHeuristic::QuotaLimitHeuristic(const Config& config, | 114 QuotaLimitHeuristic::QuotaLimitHeuristic(const Config& config, |
115 BucketMapper* map, | 115 BucketMapper* map, |
116 const std::string& name) | 116 const std::string& name) |
117 : config_(config), bucket_mapper_(map), name_(name) { | 117 : config_(config), bucket_mapper_(map), name_(name) { |
118 } | 118 } |
119 | 119 |
120 QuotaLimitHeuristic::~QuotaLimitHeuristic() {} | 120 QuotaLimitHeuristic::~QuotaLimitHeuristic() {} |
121 | 121 |
122 bool QuotaLimitHeuristic::ApplyToArgs(const ListValue* args, | 122 bool QuotaLimitHeuristic::ApplyToArgs(const base::ListValue* args, |
123 const base::TimeTicks& event_time) { | 123 const base::TimeTicks& event_time) { |
124 BucketList buckets; | 124 BucketList buckets; |
125 bucket_mapper_->GetBucketsForArgs(args, &buckets); | 125 bucket_mapper_->GetBucketsForArgs(args, &buckets); |
126 for (BucketList::iterator i = buckets.begin(); i != buckets.end(); ++i) { | 126 for (BucketList::iterator i = buckets.begin(); i != buckets.end(); ++i) { |
127 if ((*i)->expiration().is_null()) // A brand new bucket. | 127 if ((*i)->expiration().is_null()) // A brand new bucket. |
128 (*i)->Reset(config_, event_time); | 128 (*i)->Reset(config_, event_time); |
129 if (!Apply(*i, event_time)) | 129 if (!Apply(*i, event_time)) |
130 return false; // It only takes one to spoil it for everyone. | 130 return false; // It only takes one to spoil it for everyone. |
131 } | 131 } |
132 return true; | 132 return true; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 return false; | 182 return false; |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 // We can go negative since we check has_tokens when we get to *next* bucket, | 186 // We can go negative since we check has_tokens when we get to *next* bucket, |
187 // and for the small interval all that matters is whether we used up all the | 187 // and for the small interval all that matters is whether we used up all the |
188 // tokens (which is true if num_tokens_ <= 0). | 188 // tokens (which is true if num_tokens_ <= 0). |
189 bucket->DeductToken(); | 189 bucket->DeductToken(); |
190 return true; | 190 return true; |
191 } | 191 } |
OLD | NEW |