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> | 17 #include <stdint.h> |
18 | 18 |
19 #include <list> | 19 #include <list> |
20 #include <map> | 20 #include <map> |
21 #include <memory> | 21 #include <memory> |
22 #include <string> | 22 #include <string> |
23 | 23 |
24 #include "base/compiler_specific.h" | 24 #include "base/compiler_specific.h" |
25 #include "base/containers/hash_tables.h" | 25 #include "base/containers/hash_tables.h" |
26 #include "base/macros.h" | 26 #include "base/macros.h" |
27 #include "base/threading/non_thread_safe.h" | 27 #include "base/sequence_checker.h" |
28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
29 #include "base/timer/timer.h" | 29 #include "base/timer/timer.h" |
30 #include "base/values.h" | 30 #include "base/values.h" |
31 #include "extensions/common/extension_id.h" | 31 #include "extensions/common/extension_id.h" |
32 | 32 |
33 class ExtensionFunction; | 33 class ExtensionFunction; |
34 | 34 |
35 namespace extensions { | 35 namespace extensions { |
36 class QuotaLimitHeuristic; | 36 class QuotaLimitHeuristic; |
37 | 37 |
38 using QuotaLimitHeuristics = std::list<std::unique_ptr<QuotaLimitHeuristic>>; | 38 using QuotaLimitHeuristics = std::list<std::unique_ptr<QuotaLimitHeuristic>>; |
39 | 39 |
40 // The QuotaService takes care that calls to certain extension | 40 // The QuotaService takes care that calls to certain extension |
41 // functions do not exceed predefined quotas. | 41 // functions do not exceed predefined quotas. |
42 // | 42 // |
43 // The QuotaService needs to live entirely on one thread, i.e. be created, | 43 // The QuotaService needs to live entirely on one thread, i.e. be created, |
44 // called and destroyed on the same thread, due to its use of a RepeatingTimer. | 44 // called and destroyed on the same thread, due to its use of a RepeatingTimer. |
45 // It is not a KeyedService because instances exist on both the UI | 45 // It is not a KeyedService because instances exist on both the UI |
46 // and IO threads. | 46 // and IO threads. |
Devlin
2017/05/30 19:06:47
This class is probably better suited for ThreadChe
gab
2017/05/31 17:52:28
Done.
| |
47 class QuotaService : public base::NonThreadSafe { | 47 class QuotaService { |
48 public: | 48 public: |
49 // Some concrete heuristics (declared below) that ExtensionFunctions can | 49 // Some concrete heuristics (declared below) that ExtensionFunctions can |
50 // use to help the service make decisions about quota violations. | 50 // use to help the service make decisions about quota violations. |
51 class TimedLimit; | 51 class TimedLimit; |
52 | 52 |
53 QuotaService(); | 53 QuotaService(); |
54 virtual ~QuotaService(); | 54 virtual ~QuotaService(); |
55 | 55 |
56 // Decide whether the invocation of |function| with argument |args| by the | 56 // Decide whether the invocation of |function| with argument |args| by the |
57 // extension specified by |extension_id| results in a quota limit violation. | 57 // extension specified by |extension_id| results in a quota limit violation. |
(...skipping 26 matching lines...) Expand all Loading... | |
84 void Purge(); | 84 void Purge(); |
85 base::RepeatingTimer purge_timer_; | 85 base::RepeatingTimer purge_timer_; |
86 | 86 |
87 // Our quota tracking state for extensions that have invoked quota limited | 87 // Our quota tracking state for extensions that have invoked quota limited |
88 // functions. Each extension is treated separately, so extension ids are the | 88 // functions. Each extension is treated separately, so extension ids are the |
89 // key for the mapping. As an extension invokes functions, the map keeps | 89 // key for the mapping. As an extension invokes functions, the map keeps |
90 // track of which functions it has invoked and the heuristics for each one. | 90 // track of which functions it has invoked and the heuristics for each one. |
91 // Each heuristic will be evaluated and ANDed together to get a final answer. | 91 // Each heuristic will be evaluated and ANDed together to get a final answer. |
92 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; | 92 std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; |
93 | 93 |
94 SEQUENCE_CHECKER(sequence_checker_); | |
95 | |
94 DISALLOW_COPY_AND_ASSIGN(QuotaService); | 96 DISALLOW_COPY_AND_ASSIGN(QuotaService); |
95 }; | 97 }; |
96 | 98 |
97 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension | 99 // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension |
98 // function arguments to corresponding Buckets for each input arg, and 2) a | 100 // function arguments to corresponding Buckets for each input arg, and 2) a |
99 // heuristic for determining if a new event involving a particular item | 101 // heuristic for determining if a new event involving a particular item |
100 // (represented by its Bucket) constitutes a quota violation. | 102 // (represented by its Bucket) constitutes a quota violation. |
101 class QuotaLimitHeuristic { | 103 class QuotaLimitHeuristic { |
102 public: | 104 public: |
103 // Parameters to configure the amount of tokens allotted to individual | 105 // Parameters to configure the amount of tokens allotted to individual |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 class QuotaService::TimedLimit : public QuotaLimitHeuristic { | 217 class QuotaService::TimedLimit : public QuotaLimitHeuristic { |
216 public: | 218 public: |
217 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) | 219 TimedLimit(const Config& config, BucketMapper* map, const std::string& name) |
218 : QuotaLimitHeuristic(config, map, name) {} | 220 : QuotaLimitHeuristic(config, map, name) {} |
219 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; | 221 bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; |
220 }; | 222 }; |
221 | 223 |
222 } // namespace extensions | 224 } // namespace extensions |
223 | 225 |
224 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ | 226 #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
OLD | NEW |