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