| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // The ExtensionsQuotaService uses heuristics to limit abusive requests | 5 // The ExtensionsQuotaService uses heuristics to limit abusive requests |
| 6 // made by extensions. In this model 'items' (e.g individual bookmarks) are | 6 // made by extensions. In this model 'items' (e.g individual bookmarks) are |
| 7 // represented by a 'Bucket' that holds state for that item for one single | 7 // represented by a 'Bucket' that holds state for that item for one single |
| 8 // interval of time. The interval of time is defined as 'how long we need to | 8 // interval of time. The interval of time is defined as 'how long we need to |
| 9 // watch an item (for a particular heuristic) before making a decision about | 9 // watch an item (for a particular heuristic) before making a decision about |
| 10 // quota violations'. A heuristic is two functions: one mapping input | 10 // quota violations'. A heuristic is two functions: one mapping input |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // In most cases, this should simply extract item IDs from the arguments | 135 // In most cases, this should simply extract item IDs from the arguments |
| 136 // (e.g for bookmark operations involving an existing item). If a problem | 136 // (e.g for bookmark operations involving an existing item). If a problem |
| 137 // occurs while parsing |args|, the function aborts - buckets may be non- | 137 // occurs while parsing |args|, the function aborts - buckets may be non- |
| 138 // empty). The expectation is that invalid args and associated errors are | 138 // empty). The expectation is that invalid args and associated errors are |
| 139 // handled by the ExtensionFunction itself so we don't concern ourselves. | 139 // handled by the ExtensionFunction itself so we don't concern ourselves. |
| 140 virtual void GetBucketsForArgs(const ListValue* args, | 140 virtual void GetBucketsForArgs(const ListValue* args, |
| 141 BucketList* buckets) = 0; | 141 BucketList* buckets) = 0; |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 // Ownership of |mapper| is given to the new QuotaLimitHeuristic. | 144 // Ownership of |mapper| is given to the new QuotaLimitHeuristic. |
| 145 explicit QuotaLimitHeuristic(const Config& config, BucketMapper* map); | 145 QuotaLimitHeuristic(const Config& config, BucketMapper* map); |
| 146 virtual ~QuotaLimitHeuristic(); | 146 virtual ~QuotaLimitHeuristic(); |
| 147 | 147 |
| 148 // Determines if sufficient quota exists (according to the Apply | 148 // Determines if sufficient quota exists (according to the Apply |
| 149 // implementation of a derived class) to perform an operation with |args|, | 149 // implementation of a derived class) to perform an operation with |args|, |
| 150 // based on the history of similar operations with similar arguments (which | 150 // based on the history of similar operations with similar arguments (which |
| 151 // is retrieved using the BucketMapper). | 151 // is retrieved using the BucketMapper). |
| 152 bool ApplyToArgs(const ListValue* args, const base::TimeTicks& event_time); | 152 bool ApplyToArgs(const ListValue* args, const base::TimeTicks& event_time); |
| 153 | 153 |
| 154 protected: | 154 protected: |
| 155 const Config& config() { return config_; } | 155 const Config& config() { return config_; } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 166 // The mapper used in Map. Cannot be NULL. | 166 // The mapper used in Map. Cannot be NULL. |
| 167 scoped_ptr<BucketMapper> bucket_mapper_; | 167 scoped_ptr<BucketMapper> bucket_mapper_; |
| 168 | 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(QuotaLimitHeuristic); | 169 DISALLOW_COPY_AND_ASSIGN(QuotaLimitHeuristic); |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 // A simple per-item heuristic to limit the number of events that can occur in | 172 // A simple per-item heuristic to limit the number of events that can occur in |
| 173 // a given period of time; e.g "no more than 100 events in an hour". | 173 // a given period of time; e.g "no more than 100 events in an hour". |
| 174 class ExtensionsQuotaService::TimedLimit : public QuotaLimitHeuristic { | 174 class ExtensionsQuotaService::TimedLimit : public QuotaLimitHeuristic { |
| 175 public: | 175 public: |
| 176 explicit TimedLimit(const Config& config, BucketMapper* map) | 176 TimedLimit(const Config& config, BucketMapper* map) |
| 177 : QuotaLimitHeuristic(config, map) {} | 177 : QuotaLimitHeuristic(config, map) {} |
| 178 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time); | 178 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time); |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // A per-item heuristic to limit the number of events that can occur in a | 181 // A per-item heuristic to limit the number of events that can occur in a |
| 182 // period of time over a sustained longer interval. E.g "no more than two | 182 // period of time over a sustained longer interval. E.g "no more than two |
| 183 // events per minute, sustained over 10 minutes". | 183 // events per minute, sustained over 10 minutes". |
| 184 class ExtensionsQuotaService::SustainedLimit : public QuotaLimitHeuristic { | 184 class ExtensionsQuotaService::SustainedLimit : public QuotaLimitHeuristic { |
| 185 public: | 185 public: |
| 186 SustainedLimit(const base::TimeDelta& sustain, | 186 SustainedLimit(const base::TimeDelta& sustain, |
| 187 const Config& config, | 187 const Config& config, |
| 188 BucketMapper* map); | 188 BucketMapper* map); |
| 189 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time); | 189 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time); |
| 190 private: | 190 private: |
| 191 // Specifies how long exhaustion of buckets is allowed to continue before | 191 // Specifies how long exhaustion of buckets is allowed to continue before |
| 192 // denying requests. | 192 // denying requests. |
| 193 const int64 repeat_exhaustion_allowance_; | 193 const int64 repeat_exhaustion_allowance_; |
| 194 int64 num_available_repeat_exhaustions_; | 194 int64 num_available_repeat_exhaustions_; |
| 195 }; | 195 }; |
| 196 | 196 |
| 197 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_QUOTA_SERVICE_H_ | 197 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_QUOTA_SERVICE_H_ |
| OLD | NEW |