Chromium Code Reviews| 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 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 ExtensionFunction* function, | 57 ExtensionFunction* function, |
| 58 const base::ListValue* args, | 58 const base::ListValue* args, |
| 59 const base::TimeTicks& event_time); | 59 const base::TimeTicks& event_time); |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 typedef std::string ExtensionId; | 62 typedef std::string ExtensionId; |
| 63 typedef std::string FunctionName; | 63 typedef std::string FunctionName; |
| 64 // All QuotaLimitHeuristic instances in this map are owned by us. | 64 // All QuotaLimitHeuristic instances in this map are owned by us. |
| 65 typedef std::map<FunctionName, QuotaLimitHeuristics> FunctionHeuristicsMap; | 65 typedef std::map<FunctionName, QuotaLimitHeuristics> FunctionHeuristicsMap; |
| 66 | 66 |
| 67 // Purge resets all accumulated data (except |violation_errors_|) as if the | 67 // Purge resets all accumulated data as if the service was just created. |
| 68 // service was just created. Called periodically so we don't consume an | 68 // Called periodically so we don't consume an unbounded amount of memory |
| 69 // unbounded amount of memory while tracking quota. Yes, this could mean an | 69 // while tracking quota. Yes, this could mean an extension gets away with |
| 70 // extension gets away with murder if it is timed right, but the extensions | 70 // murder if it is timed right, but the extensions we are trying to limit are |
| 71 // we are trying to limit are ones that consistently violate, so we'll | 71 // ones that consistently violate, so we'll converge to the correct set. |
|
Nicolas Zea
2014/11/04 20:33:10
Is this comment still accurate (particularly the l
not at google - send to devlin
2014/11/04 22:07:25
Good point, fixed. Of the last sentence: the last
| |
| 72 // converge to the correct set. | |
| 73 void Purge(); | 72 void Purge(); |
| 74 void PurgeFunctionHeuristicsMap(FunctionHeuristicsMap* map); | 73 void PurgeFunctionHeuristicsMap(FunctionHeuristicsMap* map); |
| 75 base::RepeatingTimer<QuotaService> purge_timer_; | 74 base::RepeatingTimer<QuotaService> purge_timer_; |
| 76 | 75 |
| 77 // Our quota tracking state for extensions that have invoked quota limited | 76 // Our quota tracking state for extensions that have invoked quota limited |
| 78 // functions. Each extension is treated separately, so extension ids are the | 77 // functions. Each extension is treated separately, so extension ids are the |
| 79 // key for the mapping. As an extension invokes functions, the map keeps | 78 // key for the mapping. As an extension invokes functions, the map keeps |
| 80 // track of which functions it has invoked and the heuristics for each one. | 79 // track of which functions it has invoked and the heuristics for each one. |
| 81 // Each heuristic will be evaluated and ANDed together to get a final answer. | 80 // Each heuristic will be evaluated and ANDed together to get a final answer. |
| 82 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; | 81 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; |
| 83 | 82 |
| 84 // For now, as soon as an extension violates quota, we don't allow it to | |
| 85 // make any more requests to quota limited functions. This provides a quick | |
| 86 // lookup for these extensions that is only stored in memory. | |
| 87 typedef std::map<std::string, std::string> ViolationErrorMap; | |
| 88 ViolationErrorMap violation_errors_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(QuotaService); | 83 DISALLOW_COPY_AND_ASSIGN(QuotaService); |
| 91 }; | 84 }; |
| 92 | 85 |
| 93 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension | 86 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension |
| 94 // function arguments to corresponding Buckets for each input arg, and 2) a | 87 // function arguments to corresponding Buckets for each input arg, and 2) a |
| 95 // heuristic for determining if a new event involving a particular item | 88 // heuristic for determining if a new event involving a particular item |
| 96 // (represented by its Bucket) constitutes a quota violation. | 89 // (represented by its Bucket) constitutes a quota violation. |
| 97 class QuotaLimitHeuristic { | 90 class QuotaLimitHeuristic { |
| 98 public: | 91 public: |
| 99 // Parameters to configure the amount of tokens allotted to individual | 92 // Parameters to configure the amount of tokens allotted to individual |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 class QuotaService::TimedLimit : public QuotaLimitHeuristic { | 204 class QuotaService::TimedLimit : public QuotaLimitHeuristic { |
| 212 public: | 205 public: |
| 213 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) | 206 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) |
| 214 : QuotaLimitHeuristic(config, map, name) {} | 207 : QuotaLimitHeuristic(config, map, name) {} |
| 215 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; | 208 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; |
| 216 }; | 209 }; |
| 217 | 210 |
| 218 } // namespace extensions | 211 } // namespace extensions |
| 219 | 212 |
| 220 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 213 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
| OLD | NEW |