xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 5 | #ifndef EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_ |
| 6 | #define EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_ |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 7 | |
| 8 | #include <map> |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 9 | #include <memory> |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 10 | #include <string> |
| 11 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 12 | #include "base/macros.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
| 15 | #include "extensions/renderer/extension_throttle_entry.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 16 | #include "url/gurl.h" |
| 17 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 18 | namespace blink { |
| 19 | class WebURLRequest; |
| 20 | } // namespace blink |
| 21 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 22 | namespace content { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 23 | class URLLoaderThrottle; |
| 24 | } // namespace content |
| 25 | |
| 26 | namespace net { |
| 27 | struct RedirectInfo; |
| 28 | } // namespace net |
| 29 | |
| 30 | namespace network { |
| 31 | struct ResourceResponseHead; |
| 32 | } // namespace network |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 33 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 34 | namespace extensions { |
| 35 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 36 | // This is a thread safe class that registers URL request throttler entries for |
| 37 | // URLs being accessed in order to supervise traffic. URL requests for HTTP |
| 38 | // contents should register their URLs in this manager on each request. |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 39 | // |
| 40 | // ExtensionThrottleManager maintains a map of URL IDs to URL request |
| 41 | // throttler entries. It creates URL request throttler entries when new URLs |
| 42 | // are registered, and does garbage collection from time to time in order to |
| 43 | // clean out outdated entries. URL ID consists of lowercased scheme, host, port |
| 44 | // and path. All URLs converted to the same ID will share the same entry. |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 45 | class ExtensionThrottleManager { |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 46 | public: |
| 47 | ExtensionThrottleManager(); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 48 | virtual ~ExtensionThrottleManager(); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 49 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 50 | // Creates a throttle which uses this class to prevent extensions from |
| 51 | // requesting a URL too often, if such a throttle is needed. |
| 52 | std::unique_ptr<content::URLLoaderThrottle> MaybeCreateURLLoaderThrottle( |
| 53 | const blink::WebURLRequest& request); |
| 54 | |
| 55 | // Determine if a request to |request_url| with the given |request_load_flags| |
| 56 | // (see net/base/load_flags.h) should be rejected. |
| 57 | bool ShouldRejectRequest(const GURL& request_url, int request_load_flags); |
| 58 | |
| 59 | // Determine if a redirect from the original |request_url| with the original |
| 60 | // |request_load_flags| (see net/base/load_flags.h) should be allowed to be |
| 61 | // redirected as specified by |redirect_info|. |
| 62 | bool ShouldRejectRedirect(const GURL& request_url, |
| 63 | int request_load_flags, |
| 64 | const net::RedirectInfo& redirect_info); |
| 65 | |
| 66 | // Must be called when the |response_head| for a request has been received. |
| 67 | void WillProcessResponse(const GURL& response_url, |
| 68 | const network::ResourceResponseHead& response_head); |
| 69 | |
| 70 | // Set the network status online state as specified in |is_online|. |
| 71 | void SetOnline(bool is_online); |
| 72 | |
| 73 | void SetBackoffPolicyForTests( |
| 74 | std::unique_ptr<net::BackoffEntry::Policy> policy); |
| 75 | |
| 76 | // Registers a new entry in this service and overrides the existing entry (if |
| 77 | // any) for the URL. The service will hold a reference to the entry. |
| 78 | // It is only used by unit tests. |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame^] | 79 | void OverrideEntryForTests(const GURL& url, |
| 80 | std::unique_ptr<ExtensionThrottleEntry> entry); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 81 | |
| 82 | // Sets whether to ignore net::LOAD_MAYBE_USER_GESTURE of the request for |
| 83 | // testing. Otherwise, requests will not be throttled when they may have been |
| 84 | // throttled in response to a recent user gesture, though they're still |
| 85 | // counted for the purpose of throttling other requests. |
| 86 | void SetIgnoreUserGestureLoadFlagForTests( |
| 87 | bool ignore_user_gesture_load_flag_for_tests); |
| 88 | |
| 89 | int GetNumberOfEntriesForTests() const { return url_entries_.size(); } |
| 90 | |
| 91 | protected: |
| 92 | // Method that allows us to transform a URL into an ID that can be used in our |
| 93 | // map. Resulting IDs will be lowercase and consist of the scheme, host, port |
| 94 | // and path (without query string, fragment, etc.). |
| 95 | // If the URL is invalid, the invalid spec will be returned, without any |
| 96 | // transformation. |
| 97 | std::string GetIdFromUrl(const GURL& url) const; |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 98 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 99 | // Must be called for every request, returns the URL request throttler entry |
| 100 | // associated with the URL. The caller must inform this entry of some events. |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame^] | 101 | // Please refer to extension_throttle_entry.h for further information. |
| 102 | ExtensionThrottleEntry* RegisterRequestUrl(const GURL& url); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 103 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 104 | // Method that does the actual work of garbage collecting. |
| 105 | void GarbageCollectEntries(); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 106 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 107 | private: |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 108 | // Explicitly erases an entry. |
| 109 | // This is useful to remove those entries which have got infinite lifetime and |
| 110 | // thus won't be garbage collected. |
| 111 | // It is only used by unit tests. |
| 112 | void EraseEntryForTests(const GURL& url); |
| 113 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 114 | // Whether throttling is enabled or not. |
| 115 | void set_enforce_throttling(bool enforce); |
| 116 | bool enforce_throttling(); |
| 117 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 118 | // Method that ensures the map gets cleaned from time to time. The period at |
| 119 | // which garbage collecting happens is adjustable with the |
| 120 | // kRequestBetweenCollecting constant. |
| 121 | void GarbageCollectEntriesIfNecessary(); |
| 122 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 123 | // Maximum number of entries that we are willing to collect in our map. |
| 124 | static const unsigned int kMaximumNumberOfEntries; |
| 125 | // Number of requests that will be made between garbage collection. |
| 126 | static const unsigned int kRequestsBetweenCollecting; |
| 127 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame^] | 128 | // Map that contains a list of URL ID (composed of the scheme, host, port and |
| 129 | // path) and their matching ExtensionThrottleEntry. |
| 130 | std::map<std::string, std::unique_ptr<ExtensionThrottleEntry>> url_entries_; |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 131 | |
| 132 | // This keeps track of how many requests have been made. Used with |
| 133 | // GarbageCollectEntries. |
| 134 | unsigned int requests_since_last_gc_; |
| 135 | |
| 136 | // Valid after construction. |
| 137 | GURL::Replacements url_id_replacements_; |
| 138 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 139 | bool ignore_user_gesture_load_flag_for_tests_; |
| 140 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame^] | 141 | // This is null when it is not set for tests. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 142 | std::unique_ptr<net::BackoffEntry::Policy> backoff_policy_for_tests_; |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 143 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 144 | // Used to synchronize all public methods. |
| 145 | base::Lock lock_; |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 146 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(ExtensionThrottleManager); |
| 148 | }; |
| 149 | |
| 150 | } // namespace extensions |
| 151 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 152 | #endif // EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_ |