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. |
70 // extension gets away with murder if it is timed right, but the extensions | |
71 // we are trying to limit are ones that consistently violate, so we'll | |
72 // converge to the correct set. | |
73 void Purge(); | 70 void Purge(); |
74 void PurgeFunctionHeuristicsMap(FunctionHeuristicsMap* map); | 71 void PurgeFunctionHeuristicsMap(FunctionHeuristicsMap* map); |
75 base::RepeatingTimer<QuotaService> purge_timer_; | 72 base::RepeatingTimer<QuotaService> purge_timer_; |
76 | 73 |
77 // Our quota tracking state for extensions that have invoked quota limited | 74 // Our quota tracking state for extensions that have invoked quota limited |
78 // functions. Each extension is treated separately, so extension ids are the | 75 // functions. Each extension is treated separately, so extension ids are the |
79 // key for the mapping. As an extension invokes functions, the map keeps | 76 // 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. | 77 // 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. | 78 // Each heuristic will be evaluated and ANDed together to get a final answer. |
82 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; | 79 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; |
83 | 80 |
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); | 81 DISALLOW_COPY_AND_ASSIGN(QuotaService); |
91 }; | 82 }; |
92 | 83 |
93 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension | 84 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension |
94 // function arguments to corresponding Buckets for each input arg, and 2) a | 85 // function arguments to corresponding Buckets for each input arg, and 2) a |
95 // heuristic for determining if a new event involving a particular item | 86 // heuristic for determining if a new event involving a particular item |
96 // (represented by its Bucket) constitutes a quota violation. | 87 // (represented by its Bucket) constitutes a quota violation. |
97 class QuotaLimitHeuristic { | 88 class QuotaLimitHeuristic { |
98 public: | 89 public: |
99 // Parameters to configure the amount of tokens allotted to individual | 90 // 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 { | 202 class QuotaService::TimedLimit : public QuotaLimitHeuristic { |
212 public: | 203 public: |
213 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) | 204 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) |
214 : QuotaLimitHeuristic(config, map, name) {} | 205 : QuotaLimitHeuristic(config, map, name) {} |
215 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; | 206 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; |
216 }; | 207 }; |
217 | 208 |
218 } // namespace extensions | 209 } // namespace extensions |
219 | 210 |
220 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 211 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
OLD | NEW |