blob: f22c99cc2fd21076bf5e2248f9b687f09fe6c287 [file] [log] [blame]
xunjieli413a68782015-06-16 17:15:431// 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 Mumford3f0eda92018-07-23 14:51:175#ifndef EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_
6#define EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_
xunjieli413a68782015-06-16 17:15:437
8#include <map>
dchengf5d241082016-04-21 03:43:119#include <memory>
xunjieli413a68782015-06-16 17:15:4310#include <string>
11
avic9cec102015-12-23 00:39:2612#include "base/macros.h"
xunjieli413a68782015-06-16 17:15:4313#include "base/memory/ref_counted.h"
Chris Mumford3f0eda92018-07-23 14:51:1714#include "base/synchronization/lock.h"
15#include "extensions/renderer/extension_throttle_entry.h"
xunjieli413a68782015-06-16 17:15:4316#include "url/gurl.h"
17
Chris Mumford3f0eda92018-07-23 14:51:1718namespace blink {
Minggang Wangf6840ecf2019-07-29 05:15:0219class URLLoaderThrottle;
Chris Mumford3f0eda92018-07-23 14:51:1720class WebURLRequest;
21} // namespace blink
22
Chris Mumford3f0eda92018-07-23 14:51:1723namespace net {
24struct RedirectInfo;
25} // namespace net
26
27namespace network {
28struct ResourceResponseHead;
29} // namespace network
xunjieli413a68782015-06-16 17:15:4330
xunjieli413a68782015-06-16 17:15:4331namespace extensions {
32
Chris Mumford3f0eda92018-07-23 14:51:1733// This is a thread safe class that registers URL request throttler entries for
34// URLs being accessed in order to supervise traffic. URL requests for HTTP
35// contents should register their URLs in this manager on each request.
xunjieli413a68782015-06-16 17:15:4336//
37// ExtensionThrottleManager maintains a map of URL IDs to URL request
38// throttler entries. It creates URL request throttler entries when new URLs
39// are registered, and does garbage collection from time to time in order to
40// clean out outdated entries. URL ID consists of lowercased scheme, host, port
41// and path. All URLs converted to the same ID will share the same entry.
Chris Mumford3f0eda92018-07-23 14:51:1742class ExtensionThrottleManager {
xunjieli413a68782015-06-16 17:15:4343 public:
44 ExtensionThrottleManager();
Chris Mumford3f0eda92018-07-23 14:51:1745 virtual ~ExtensionThrottleManager();
xunjieli413a68782015-06-16 17:15:4346
Chris Mumford3f0eda92018-07-23 14:51:1747 // Creates a throttle which uses this class to prevent extensions from
48 // requesting a URL too often, if such a throttle is needed.
Minggang Wangf6840ecf2019-07-29 05:15:0249 std::unique_ptr<blink::URLLoaderThrottle> MaybeCreateURLLoaderThrottle(
Chris Mumford3f0eda92018-07-23 14:51:1750 const blink::WebURLRequest& request);
51
Matt Menke21850502019-10-09 22:34:2452 // Determine if a request to |request_url| should be rejected.
53 bool ShouldRejectRequest(const GURL& request_url);
Chris Mumford3f0eda92018-07-23 14:51:1754
Matt Menke21850502019-10-09 22:34:2455 // Determine if a redirect from the original |request_url| should be allowed
56 // to be redirected as specified by |redirect_info|.
Chris Mumford3f0eda92018-07-23 14:51:1757 bool ShouldRejectRedirect(const GURL& request_url,
Chris Mumford3f0eda92018-07-23 14:51:1758 const net::RedirectInfo& redirect_info);
59
60 // Must be called when the |response_head| for a request has been received.
61 void WillProcessResponse(const GURL& response_url,
62 const network::ResourceResponseHead& response_head);
63
64 // Set the network status online state as specified in |is_online|.
65 void SetOnline(bool is_online);
66
67 void SetBackoffPolicyForTests(
68 std::unique_ptr<net::BackoffEntry::Policy> policy);
69
70 // Registers a new entry in this service and overrides the existing entry (if
71 // any) for the URL. The service will hold a reference to the entry.
72 // It is only used by unit tests.
Chris Mumfordc54b0c342018-07-23 21:29:4673 void OverrideEntryForTests(const GURL& url,
74 std::unique_ptr<ExtensionThrottleEntry> entry);
Chris Mumford3f0eda92018-07-23 14:51:1775
Chris Mumford3f0eda92018-07-23 14:51:1776 int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
77
78 protected:
79 // Method that allows us to transform a URL into an ID that can be used in our
80 // map. Resulting IDs will be lowercase and consist of the scheme, host, port
81 // and path (without query string, fragment, etc.).
82 // If the URL is invalid, the invalid spec will be returned, without any
83 // transformation.
84 std::string GetIdFromUrl(const GURL& url) const;
xunjieli413a68782015-06-16 17:15:4385
xunjieli413a68782015-06-16 17:15:4386 // Must be called for every request, returns the URL request throttler entry
87 // associated with the URL. The caller must inform this entry of some events.
Chris Mumfordc54b0c342018-07-23 21:29:4688 // Please refer to extension_throttle_entry.h for further information.
89 ExtensionThrottleEntry* RegisterRequestUrl(const GURL& url);
xunjieli413a68782015-06-16 17:15:4390
Chris Mumford3f0eda92018-07-23 14:51:1791 // Method that does the actual work of garbage collecting.
92 void GarbageCollectEntries();
xunjielie484e2d62015-06-19 14:31:2193
Chris Mumford3f0eda92018-07-23 14:51:1794 private:
xunjieli413a68782015-06-16 17:15:4395 // Explicitly erases an entry.
96 // This is useful to remove those entries which have got infinite lifetime and
97 // thus won't be garbage collected.
98 // It is only used by unit tests.
99 void EraseEntryForTests(const GURL& url);
100
xunjieli413a68782015-06-16 17:15:43101 // Whether throttling is enabled or not.
102 void set_enforce_throttling(bool enforce);
103 bool enforce_throttling();
104
xunjieli413a68782015-06-16 17:15:43105 // Method that ensures the map gets cleaned from time to time. The period at
106 // which garbage collecting happens is adjustable with the
107 // kRequestBetweenCollecting constant.
108 void GarbageCollectEntriesIfNecessary();
109
xunjieli413a68782015-06-16 17:15:43110 // Maximum number of entries that we are willing to collect in our map.
111 static const unsigned int kMaximumNumberOfEntries;
112 // Number of requests that will be made between garbage collection.
113 static const unsigned int kRequestsBetweenCollecting;
114
Chris Mumfordc54b0c342018-07-23 21:29:46115 // Map that contains a list of URL ID (composed of the scheme, host, port and
116 // path) and their matching ExtensionThrottleEntry.
117 std::map<std::string, std::unique_ptr<ExtensionThrottleEntry>> url_entries_;
xunjieli413a68782015-06-16 17:15:43118
119 // This keeps track of how many requests have been made. Used with
120 // GarbageCollectEntries.
121 unsigned int requests_since_last_gc_;
122
123 // Valid after construction.
124 GURL::Replacements url_id_replacements_;
125
Chris Mumfordc54b0c342018-07-23 21:29:46126 // This is null when it is not set for tests.
dchengf5d241082016-04-21 03:43:11127 std::unique_ptr<net::BackoffEntry::Policy> backoff_policy_for_tests_;
xunjielie484e2d62015-06-19 14:31:21128
Chris Mumford3f0eda92018-07-23 14:51:17129 // Used to synchronize all public methods.
130 base::Lock lock_;
gab370841f22017-06-01 14:38:26131
xunjieli413a68782015-06-16 17:15:43132 DISALLOW_COPY_AND_ASSIGN(ExtensionThrottleManager);
133};
134
135} // namespace extensions
136
Chris Mumford3f0eda92018-07-23 14:51:17137#endif // EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_