[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 5 | // The QuotaService uses heuristics to limit abusive requests |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 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 |
| 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 |
| 10 | // quota violations'. A heuristic is two functions: one mapping input |
| 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. |
| 13 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 14 | #ifndef EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
| 15 | #define EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 16 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 19 | #include <list> |
| 20 | #include <map> |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 21 | #include <memory> |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 22 | #include <string> |
| 23 | |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 24 | #include "base/compiler_specific.h" |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 25 | #include "base/containers/hash_tables.h" |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 26 | #include "base/macros.h" |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 27 | #include "base/threading/thread_checker.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 28 | #include "base/time/time.h" |
| 29 | #include "base/timer/timer.h" |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 30 | #include "base/values.h" |
lazyboy | 0222b36 | 2016-11-12 02:00:09 | [diff] [blame] | 31 | #include "extensions/common/extension_id.h" |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 32 | |
| 33 | class ExtensionFunction; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 34 | |
[email protected] | ae54db1c | 2012-07-11 12:33:35 | [diff] [blame] | 35 | namespace extensions { |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 36 | class QuotaLimitHeuristic; |
[email protected] | ae54db1c | 2012-07-11 12:33:35 | [diff] [blame] | 37 | |
avi | 14f2974 | 2016-11-03 23:39:50 | [diff] [blame] | 38 | using QuotaLimitHeuristics = std::list<std::unique_ptr<QuotaLimitHeuristic>>; |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 39 | |
| 40 | // The QuotaService takes care that calls to certain extension |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 41 | // functions do not exceed predefined quotas. |
| 42 | // |
[email protected] | aab2310 | 2014-02-05 18:57:55 | [diff] [blame] | 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. |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 45 | // It is not a KeyedService because instances exist on both the UI |
[email protected] | aab2310 | 2014-02-05 18:57:55 | [diff] [blame] | 46 | // and IO threads. |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 47 | class QuotaService { |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 48 | public: |
| 49 | // Some concrete heuristics (declared below) that ExtensionFunctions can |
| 50 | // use to help the service make decisions about quota violations. |
| 51 | class TimedLimit; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 52 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 53 | QuotaService(); |
| 54 | virtual ~QuotaService(); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 55 | |
| 56 | // Decide whether the invocation of |function| with argument |args| by the |
| 57 | // extension specified by |extension_id| results in a quota limit violation. |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 58 | // Returns an error message representing the failure if quota was exceeded, |
| 59 | // or empty-string if the request is fine and can proceed. |
| 60 | std::string Assess(const std::string& extension_id, |
| 61 | ExtensionFunction* function, |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 62 | const base::ListValue* args, |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 63 | const base::TimeTicks& event_time); |
| 64 | |
fdoray | 4cfe32a | 2016-06-29 19:45:52 | [diff] [blame] | 65 | // An active ScopedDisablePurgeForTesting prevents QuotaService's constructor |
| 66 | // from starting a purge timer. |
| 67 | class ScopedDisablePurgeForTesting { |
| 68 | public: |
| 69 | ScopedDisablePurgeForTesting(); |
| 70 | ~ScopedDisablePurgeForTesting(); |
| 71 | |
| 72 | private: |
| 73 | DISALLOW_COPY_AND_ASSIGN(ScopedDisablePurgeForTesting); |
| 74 | }; |
| 75 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 76 | private: |
avi | 14f2974 | 2016-11-03 23:39:50 | [diff] [blame] | 77 | using FunctionName = std::string; |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 78 | // All QuotaLimitHeuristic instances in this map are owned by us. |
avi | 14f2974 | 2016-11-03 23:39:50 | [diff] [blame] | 79 | using FunctionHeuristicsMap = std::map<FunctionName, QuotaLimitHeuristics>; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 80 | |
kalman | 2610ea1 | 2014-11-05 00:18:18 | [diff] [blame] | 81 | // Purge resets all accumulated data as if the service was just created. |
| 82 | // Called periodically so we don't consume an unbounded amount of memory |
| 83 | // while tracking quota. |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 84 | void Purge(); |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 85 | base::RepeatingTimer purge_timer_; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 86 | |
| 87 | // Our quota tracking state for extensions that have invoked quota limited |
| 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 |
| 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. |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 92 | std::map<ExtensionId, FunctionHeuristicsMap> function_heuristics_; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 93 | |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 94 | THREAD_CHECKER(thread_checker_); |
| 95 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 96 | DISALLOW_COPY_AND_ASSIGN(QuotaService); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | // A QuotaLimitHeuristic is two things: 1, A heuristic to map extension |
| 100 | // function arguments to corresponding Buckets for each input arg, and 2) a |
| 101 | // heuristic for determining if a new event involving a particular item |
| 102 | // (represented by its Bucket) constitutes a quota violation. |
| 103 | class QuotaLimitHeuristic { |
| 104 | public: |
| 105 | // Parameters to configure the amount of tokens allotted to individual |
| 106 | // Bucket objects (see Below) and how often they are replenished. |
| 107 | struct Config { |
| 108 | // The maximum number of tokens a bucket can contain, and is refilled to |
| 109 | // every epoch. |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 110 | int64_t refill_token_count; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 111 | |
| 112 | // Specifies how frequently the bucket is logically refilled with tokens. |
| 113 | base::TimeDelta refill_interval; |
| 114 | }; |
| 115 | |
| 116 | // A Bucket is how the heuristic portrays an individual item (since quota |
| 117 | // limits are per item) and all associated state for an item that needs to |
| 118 | // carry through multiple calls to Apply. It "holds" tokens, which are |
| 119 | // debited and credited in response to new events involving the item being |
| 120 | // being represented. For convenience, instead of actually periodically |
| 121 | // refilling buckets they are just 'Reset' on-demand (e.g. when new events |
| 122 | // come in). So, a bucket has an expiration to denote it has becomes stale. |
| 123 | class Bucket { |
| 124 | public: |
[email protected] | dd1e41a | 2009-12-04 04:16:22 | [diff] [blame] | 125 | Bucket() : num_tokens_(0) {} |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 126 | // Removes a token from this bucket, and returns true if the bucket had |
| 127 | // any tokens in the first place. |
| 128 | bool DeductToken() { return num_tokens_-- > 0; } |
| 129 | |
| 130 | // Returns true if this bucket has tokens to deduct. |
| 131 | bool has_tokens() const { return num_tokens_ > 0; } |
| 132 | |
| 133 | // Reset this bucket to specification (from internal configuration), to be |
| 134 | // valid from |start| until the first refill interval elapses and it needs |
| 135 | // to be reset again. |
| 136 | void Reset(const Config& config, const base::TimeTicks& start); |
| 137 | |
| 138 | // The time at which the token count and next expiration should be reset, |
| 139 | // via a call to Reset. |
| 140 | const base::TimeTicks& expiration() { return expiration_; } |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 141 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 142 | private: |
| 143 | base::TimeTicks expiration_; |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 144 | int64_t num_tokens_; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(Bucket); |
| 146 | }; |
avi | 14f2974 | 2016-11-03 23:39:50 | [diff] [blame] | 147 | using BucketList = std::list<Bucket*>; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 148 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 149 | // A helper interface to retrieve the bucket corresponding to |args| from |
| 150 | // the set of buckets (which is typically stored in the BucketMapper itself) |
| 151 | // for this QuotaLimitHeuristic. |
| 152 | class BucketMapper { |
| 153 | public: |
[email protected] | dd1e41a | 2009-12-04 04:16:22 | [diff] [blame] | 154 | virtual ~BucketMapper() {} |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 155 | // In most cases, this should simply extract item IDs from the arguments |
| 156 | // (e.g for bookmark operations involving an existing item). If a problem |
| 157 | // occurs while parsing |args|, the function aborts - buckets may be non- |
| 158 | // empty). The expectation is that invalid args and associated errors are |
| 159 | // handled by the ExtensionFunction itself so we don't concern ourselves. |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 160 | virtual void GetBucketsForArgs(const base::ListValue* args, |
[email protected] | 438c97d | 2010-05-21 23:30:15 | [diff] [blame] | 161 | BucketList* buckets) = 0; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 162 | }; |
| 163 | |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 164 | // Maps all calls to the same bucket, regardless of |args|, for this |
| 165 | // QuotaLimitHeuristic. |
| 166 | class SingletonBucketMapper : public BucketMapper { |
| 167 | public: |
| 168 | SingletonBucketMapper() {} |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 169 | ~SingletonBucketMapper() override {} |
| 170 | void GetBucketsForArgs(const base::ListValue* args, |
| 171 | BucketList* buckets) override; |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | Bucket bucket_; |
| 175 | DISALLOW_COPY_AND_ASSIGN(SingletonBucketMapper); |
| 176 | }; |
| 177 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 178 | // Ownership of |map| is given to the new QuotaLimitHeuristic. |
| 179 | QuotaLimitHeuristic(const Config& config, |
| 180 | BucketMapper* map, |
| 181 | const std::string& name); |
[email protected] | 2858bbf | 2010-10-05 23:46:02 | [diff] [blame] | 182 | virtual ~QuotaLimitHeuristic(); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 183 | |
| 184 | // Determines if sufficient quota exists (according to the Apply |
| 185 | // implementation of a derived class) to perform an operation with |args|, |
| 186 | // based on the history of similar operations with similar arguments (which |
| 187 | // is retrieved using the BucketMapper). |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 188 | bool ApplyToArgs(const base::ListValue* args, |
| 189 | const base::TimeTicks& event_time); |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 190 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 191 | // Returns an error formatted according to this heuristic. |
| 192 | std::string GetError() const; |
| 193 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 194 | protected: |
| 195 | const Config& config() { return config_; } |
| 196 | |
| 197 | // Determine if the new event occurring at |event_time| involving |bucket| |
| 198 | // constitutes a quota violation according to this heuristic. |
| 199 | virtual bool Apply(Bucket* bucket, const base::TimeTicks& event_time) = 0; |
| 200 | |
| 201 | private: |
| 202 | friend class QuotaLimitHeuristicTest; |
| 203 | |
| 204 | const Config config_; |
| 205 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 206 | // The mapper used in Map. Cannot be NULL. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 207 | std::unique_ptr<BucketMapper> bucket_mapper_; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 208 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 209 | // The name of the heuristic for formatting error messages. |
| 210 | std::string name_; |
| 211 | |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 212 | DISALLOW_COPY_AND_ASSIGN(QuotaLimitHeuristic); |
| 213 | }; |
| 214 | |
| 215 | // A simple per-item heuristic to limit the number of events that can occur in |
| 216 | // a given period of time; e.g "no more than 100 events in an hour". |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 217 | class QuotaService::TimedLimit : public QuotaLimitHeuristic { |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 218 | public: |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 219 | TimedLimit(const Config& config, BucketMapper* map, const std::string& name) |
| 220 | : QuotaLimitHeuristic(config, map, name) {} |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 221 | bool Apply(Bucket* bucket, const base::TimeTicks& event_time) override; |
[email protected] | d13950e | 2009-12-04 01:43:02 | [diff] [blame] | 222 | }; |
| 223 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 224 | } // namespace extensions |
| 225 | |
| 226 | #endif // EXTENSIONS_BROWSER_QUOTA_SERVICE_H_ |