| 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 // The QuotaService uses heuristics to limit abusive requests | 5 // The QuotaService 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 |
| 11 // arguments to a unique Bucket (the BucketMapper), and another to determine | 11 // arguments to a unique Bucket (the BucketMapper), and another to determine |
| 12 // if a new request involving such an item at a given time is a violation. | 12 // if a new request involving such an item at a given time is a violation. |
| 13 | 13 |
| 14 #ifndef EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 14 #ifndef EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
| 15 #define EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 15 #define EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
| 16 | 16 |
| 17 #include <stdint.h> |
| 18 |
| 17 #include <list> | 19 #include <list> |
| 18 #include <map> | 20 #include <map> |
| 19 #include <string> | 21 #include <string> |
| 20 | 22 |
| 21 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
| 22 #include "base/containers/hash_tables.h" | 24 #include "base/containers/hash_tables.h" |
| 25 #include "base/macros.h" |
| 23 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/threading/non_thread_safe.h" | 27 #include "base/threading/non_thread_safe.h" |
| 25 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 26 #include "base/timer/timer.h" | 29 #include "base/timer/timer.h" |
| 27 #include "base/values.h" | 30 #include "base/values.h" |
| 28 | 31 |
| 29 class ExtensionFunction; | 32 class ExtensionFunction; |
| 30 | 33 |
| 31 namespace extensions { | 34 namespace extensions { |
| 32 class QuotaLimitHeuristic; | 35 class QuotaLimitHeuristic; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // function arguments to corresponding Buckets for each input arg, and 2) a | 88 // function arguments to corresponding Buckets for each input arg, and 2) a |
| 86 // heuristic for determining if a new event involving a particular item | 89 // heuristic for determining if a new event involving a particular item |
| 87 // (represented by its Bucket) constitutes a quota violation. | 90 // (represented by its Bucket) constitutes a quota violation. |
| 88 class QuotaLimitHeuristic { | 91 class QuotaLimitHeuristic { |
| 89 public: | 92 public: |
| 90 // Parameters to configure the amount of tokens allotted to individual | 93 // Parameters to configure the amount of tokens allotted to individual |
| 91 // Bucket objects (see Below) and how often they are replenished. | 94 // Bucket objects (see Below) and how often they are replenished. |
| 92 struct Config { | 95 struct Config { |
| 93 // The maximum number of tokens a bucket can contain, and is refilled to | 96 // The maximum number of tokens a bucket can contain, and is refilled to |
| 94 // every epoch. | 97 // every epoch. |
| 95 int64 refill_token_count; | 98 int64_t refill_token_count; |
| 96 | 99 |
| 97 // Specifies how frequently the bucket is logically refilled with tokens. | 100 // Specifies how frequently the bucket is logically refilled with tokens. |
| 98 base::TimeDelta refill_interval; | 101 base::TimeDelta refill_interval; |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 // A Bucket is how the heuristic portrays an individual item (since quota | 104 // A Bucket is how the heuristic portrays an individual item (since quota |
| 102 // limits are per item) and all associated state for an item that needs to | 105 // limits are per item) and all associated state for an item that needs to |
| 103 // carry through multiple calls to Apply. It "holds" tokens, which are | 106 // carry through multiple calls to Apply. It "holds" tokens, which are |
| 104 // debited and credited in response to new events involving the item being | 107 // debited and credited in response to new events involving the item being |
| 105 // being represented. For convenience, instead of actually periodically | 108 // being represented. For convenience, instead of actually periodically |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 // valid from |start| until the first refill interval elapses and it needs | 122 // valid from |start| until the first refill interval elapses and it needs |
| 120 // to be reset again. | 123 // to be reset again. |
| 121 void Reset(const Config& config, const base::TimeTicks& start); | 124 void Reset(const Config& config, const base::TimeTicks& start); |
| 122 | 125 |
| 123 // The time at which the token count and next expiration should be reset, | 126 // The time at which the token count and next expiration should be reset, |
| 124 // via a call to Reset. | 127 // via a call to Reset. |
| 125 const base::TimeTicks& expiration() { return expiration_; } | 128 const base::TimeTicks& expiration() { return expiration_; } |
| 126 | 129 |
| 127 private: | 130 private: |
| 128 base::TimeTicks expiration_; | 131 base::TimeTicks expiration_; |
| 129 int64 num_tokens_; | 132 int64_t num_tokens_; |
| 130 DISALLOW_COPY_AND_ASSIGN(Bucket); | 133 DISALLOW_COPY_AND_ASSIGN(Bucket); |
| 131 }; | 134 }; |
| 132 typedef std::list<Bucket*> BucketList; | 135 typedef std::list<Bucket*> BucketList; |
| 133 | 136 |
| 134 // A helper interface to retrieve the bucket corresponding to |args| from | 137 // A helper interface to retrieve the bucket corresponding to |args| from |
| 135 // the set of buckets (which is typically stored in the BucketMapper itself) | 138 // the set of buckets (which is typically stored in the BucketMapper itself) |
| 136 // for this QuotaLimitHeuristic. | 139 // for this QuotaLimitHeuristic. |
| 137 class BucketMapper { | 140 class BucketMapper { |
| 138 public: | 141 public: |
| 139 virtual ~BucketMapper() {} | 142 virtual ~BucketMapper() {} |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 class QuotaService::TimedLimit : public QuotaLimitHeuristic { | 205 class QuotaService::TimedLimit : public QuotaLimitHeuristic { |
| 203 public: | 206 public: |
| 204 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) | 207 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) |
| 205 : QuotaLimitHeuristic(config, map, name) {} | 208 : QuotaLimitHeuristic(config, map, name) {} |
| 206 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; | 209 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; |
| 207 }; | 210 }; |
| 208 | 211 |
| 209 } // namespace extensions | 212 } // namespace extensions |
| 210 | 213 |
| 211 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 214 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
| OLD | NEW |