| 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 // 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 ExtensionsQuotaService(); | 50 ExtensionsQuotaService(); |
| 51 virtual ~ExtensionsQuotaService(); | 51 virtual ~ExtensionsQuotaService(); |
| 52 | 52 |
| 53 // Decide whether the invocation of |function| with argument |args| by the | 53 // Decide whether the invocation of |function| with argument |args| by the |
| 54 // extension specified by |extension_id| results in a quota limit violation. | 54 // extension specified by |extension_id| results in a quota limit violation. |
| 55 // Returns an error message representing the failure if quota was exceeded, | 55 // Returns an error message representing the failure if quota was exceeded, |
| 56 // or empty-string if the request is fine and can proceed. | 56 // or empty-string if the request is fine and can proceed. |
| 57 std::string Assess(const std::string& extension_id, | 57 std::string Assess(const std::string& extension_id, |
| 58 ExtensionFunction* function, | 58 ExtensionFunction* function, |
| 59 const ListValue* args, | 59 const base::ListValue* args, |
| 60 const base::TimeTicks& event_time); | 60 const base::TimeTicks& event_time); |
| 61 | 61 |
| 62 private: | 62 private: |
| 63 friend class extensions::TestResetQuotaFunction; | 63 friend class extensions::TestResetQuotaFunction; |
| 64 typedef std::string ExtensionId; | 64 typedef std::string ExtensionId; |
| 65 typedef std::string FunctionName; | 65 typedef std::string FunctionName; |
| 66 // All QuotaLimitHeuristic instances in this map are owned by us. | 66 // All QuotaLimitHeuristic instances in this map are owned by us. |
| 67 typedef std::map<FunctionName, QuotaLimitHeuristics> FunctionHeuristicsMap; | 67 typedef std::map<FunctionName, QuotaLimitHeuristics> FunctionHeuristicsMap; |
| 68 | 68 |
| 69 // Purge resets all accumulated data (except |violation_errors_|) as if the | 69 // Purge resets all accumulated data (except |violation_errors_|) as if the |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // the set of buckets (which is typically stored in the BucketMapper itself) | 145 // the set of buckets (which is typically stored in the BucketMapper itself) |
| 146 // for this QuotaLimitHeuristic. | 146 // for this QuotaLimitHeuristic. |
| 147 class BucketMapper { | 147 class BucketMapper { |
| 148 public: | 148 public: |
| 149 virtual ~BucketMapper() {} | 149 virtual ~BucketMapper() {} |
| 150 // In most cases, this should simply extract item IDs from the arguments | 150 // In most cases, this should simply extract item IDs from the arguments |
| 151 // (e.g for bookmark operations involving an existing item). If a problem | 151 // (e.g for bookmark operations involving an existing item). If a problem |
| 152 // occurs while parsing |args|, the function aborts - buckets may be non- | 152 // occurs while parsing |args|, the function aborts - buckets may be non- |
| 153 // empty). The expectation is that invalid args and associated errors are | 153 // empty). The expectation is that invalid args and associated errors are |
| 154 // handled by the ExtensionFunction itself so we don't concern ourselves. | 154 // handled by the ExtensionFunction itself so we don't concern ourselves. |
| 155 virtual void GetBucketsForArgs(const ListValue* args, | 155 virtual void GetBucketsForArgs(const base::ListValue* args, |
| 156 BucketList* buckets) = 0; | 156 BucketList* buckets) = 0; |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 // Maps all calls to the same bucket, regardless of |args|, for this | 159 // Maps all calls to the same bucket, regardless of |args|, for this |
| 160 // QuotaLimitHeuristic. | 160 // QuotaLimitHeuristic. |
| 161 class SingletonBucketMapper : public BucketMapper { | 161 class SingletonBucketMapper : public BucketMapper { |
| 162 public: | 162 public: |
| 163 SingletonBucketMapper() {} | 163 SingletonBucketMapper() {} |
| 164 virtual ~SingletonBucketMapper() {} | 164 virtual ~SingletonBucketMapper() {} |
| 165 virtual void GetBucketsForArgs(const ListValue* args, | 165 virtual void GetBucketsForArgs(const base::ListValue* args, |
| 166 BucketList* buckets) OVERRIDE; | 166 BucketList* buckets) OVERRIDE; |
| 167 | 167 |
| 168 private: | 168 private: |
| 169 Bucket bucket_; | 169 Bucket bucket_; |
| 170 DISALLOW_COPY_AND_ASSIGN(SingletonBucketMapper); | 170 DISALLOW_COPY_AND_ASSIGN(SingletonBucketMapper); |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 // Ownership of |map| is given to the new QuotaLimitHeuristic. | 173 // Ownership of |map| is given to the new QuotaLimitHeuristic. |
| 174 QuotaLimitHeuristic(const Config& config, | 174 QuotaLimitHeuristic(const Config& config, |
| 175 BucketMapper* map, | 175 BucketMapper* map, |
| 176 const std::string& name); | 176 const std::string& name); |
| 177 virtual ~QuotaLimitHeuristic(); | 177 virtual ~QuotaLimitHeuristic(); |
| 178 | 178 |
| 179 // Determines if sufficient quota exists (according to the Apply | 179 // Determines if sufficient quota exists (according to the Apply |
| 180 // implementation of a derived class) to perform an operation with |args|, | 180 // implementation of a derived class) to perform an operation with |args|, |
| 181 // based on the history of similar operations with similar arguments (which | 181 // based on the history of similar operations with similar arguments (which |
| 182 // is retrieved using the BucketMapper). | 182 // is retrieved using the BucketMapper). |
| 183 bool ApplyToArgs(const ListValue* args, const base::TimeTicks& event_time); | 183 bool ApplyToArgs(const base::ListValue* args, |
| 184 const base::TimeTicks& event_time); |
| 184 | 185 |
| 185 // Returns an error formatted according to this heuristic. | 186 // Returns an error formatted according to this heuristic. |
| 186 std::string GetError() const; | 187 std::string GetError() const; |
| 187 | 188 |
| 188 protected: | 189 protected: |
| 189 const Config& config() { return config_; } | 190 const Config& config() { return config_; } |
| 190 | 191 |
| 191 // Determine if the new event occurring at |event_time| involving |bucket| | 192 // Determine if the new event occurring at |event_time| involving |bucket| |
| 192 // constitutes a quota violation according to this heuristic. | 193 // constitutes a quota violation according to this heuristic. |
| 193 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time) = 0; | 194 virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time) = 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 virtual bool Apply(Bucket* bucket, | 229 virtual bool Apply(Bucket* bucket, |
| 229 const base::TimeTicks& event_time) OVERRIDE; | 230 const base::TimeTicks& event_time) OVERRIDE; |
| 230 private: | 231 private: |
| 231 // Specifies how long exhaustion of buckets is allowed to continue before | 232 // Specifies how long exhaustion of buckets is allowed to continue before |
| 232 // denying requests. | 233 // denying requests. |
| 233 const int64 repeat_exhaustion_allowance_; | 234 const int64 repeat_exhaustion_allowance_; |
| 234 int64 num_available_repeat_exhaustions_; | 235 int64 num_available_repeat_exhaustions_; |
| 235 }; | 236 }; |
| 236 | 237 |
| 237 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_QUOTA_SERVICE_H_ | 238 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSIONS_QUOTA_SERVICE_H_ |
| OLD | NEW |