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