Update V8 to version 4.7.16.
[chromium-blink-merge.git] / extensions / browser / extension_throttle_manager.h
blob00b677f784fdd490020c4893f87e55c7f8855fbb
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.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_THROTTLE_MANAGER_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_THROTTLE_MANAGER_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/threading/platform_thread.h"
16 #include "extensions/browser/extension_throttle_entry.h"
17 #include "net/base/backoff_entry.h"
18 #include "net/base/net_export.h"
19 #include "net/base/network_change_notifier.h"
20 #include "url/gurl.h"
22 namespace content {
23 class ResourceThrottle;
26 namespace net {
27 class BoundNetLog;
28 class NetLog;
31 namespace extensions {
33 // Class that registers URL request throttler entries for URLs being accessed
34 // in order to supervise traffic. URL requests for HTTP contents should
35 // register their URLs in this manager on each request.
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.
42 class ExtensionThrottleManager
43 : NON_EXPORTED_BASE(public base::NonThreadSafe),
44 public net::NetworkChangeNotifier::IPAddressObserver,
45 public net::NetworkChangeNotifier::ConnectionTypeObserver {
46 public:
47 ExtensionThrottleManager();
48 ~ExtensionThrottleManager() override;
50 // Creates a content::ResourceThrottle for |request| to prevent extensions
51 // from requesting a URL too often, if such a throttle is needed.
52 scoped_ptr<content::ResourceThrottle> MaybeCreateThrottle(
53 const net::URLRequest* request);
55 // TODO(xunjieli): Remove this method and replace with
56 // ShouldRejectRequest(request) and UpdateWithResponse(request, status_code),
57 // which will also allow ExtensionThrottleEntry to no longer be reference
58 // counted, and ExtensionThrottleEntryInterface to be removed.
60 // Must be called for every request, returns the URL request throttler entry
61 // associated with the URL. The caller must inform this entry of some events.
62 // Please refer to extension_throttle_entry_interface.h for further
63 // informations.
64 scoped_refptr<ExtensionThrottleEntryInterface> RegisterRequestUrl(
65 const GURL& url);
67 void SetBackoffPolicyForTests(scoped_ptr<net::BackoffEntry::Policy> policy);
69 // Registers a new entry in this service and overrides the existing entry (if
70 // any) for the URL. The service will hold a reference to the entry.
71 // It is only used by unit tests.
72 void OverrideEntryForTests(const GURL& url, ExtensionThrottleEntry* entry);
74 // Explicitly erases an entry.
75 // This is useful to remove those entries which have got infinite lifetime and
76 // thus won't be garbage collected.
77 // It is only used by unit tests.
78 void EraseEntryForTests(const GURL& url);
80 // Sets whether to ignore net::LOAD_MAYBE_USER_GESTURE of the request for
81 // testing. Otherwise, requests will not be throttled when they may have been
82 // throttled in response to a recent user gesture, though they're still
83 // counted for the purpose of throttling other requests.
84 void SetIgnoreUserGestureLoadFlagForTests(
85 bool ignore_user_gesture_load_flag_for_tests);
87 // Turns threading model verification on or off. Any code that correctly
88 // uses the network stack should preferably call this function to enable
89 // verification of correct adherence to the network stack threading model.
90 void set_enable_thread_checks(bool enable);
91 bool enable_thread_checks() const;
93 // Whether throttling is enabled or not.
94 void set_enforce_throttling(bool enforce);
95 bool enforce_throttling();
97 // Sets the net::NetLog instance to use.
98 void set_net_log(net::NetLog* net_log);
99 net::NetLog* net_log() const;
101 // IPAddressObserver interface.
102 void OnIPAddressChanged() override;
104 // ConnectionTypeObserver interface.
105 void OnConnectionTypeChanged(
106 net::NetworkChangeNotifier::ConnectionType type) override;
108 // Method that allows us to transform a URL into an ID that can be used in our
109 // map. Resulting IDs will be lowercase and consist of the scheme, host, port
110 // and path (without query string, fragment, etc.).
111 // If the URL is invalid, the invalid spec will be returned, without any
112 // transformation.
113 std::string GetIdFromUrl(const GURL& url) const;
115 // Method that ensures the map gets cleaned from time to time. The period at
116 // which garbage collecting happens is adjustable with the
117 // kRequestBetweenCollecting constant.
118 void GarbageCollectEntriesIfNecessary();
120 // Method that does the actual work of garbage collecting.
121 void GarbageCollectEntries();
123 // When we switch from online to offline or change IP addresses, we
124 // clear all back-off history. This is a precaution in case the change in
125 // online state now lets us communicate without error with servers that
126 // we were previously getting 500 or 503 responses from (perhaps the
127 // responses are from a badly-written proxy that should have returned a
128 // 502 or 504 because it's upstream connection was down or it had no route
129 // to the server).
130 void OnNetworkChange();
132 // Used by tests.
133 int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
135 private:
136 // From each URL we generate an ID composed of the scheme, host, port and path
137 // that allows us to uniquely map an entry to it.
138 typedef std::map<std::string, scoped_refptr<ExtensionThrottleEntry>>
139 UrlEntryMap;
141 // Maximum number of entries that we are willing to collect in our map.
142 static const unsigned int kMaximumNumberOfEntries;
143 // Number of requests that will be made between garbage collection.
144 static const unsigned int kRequestsBetweenCollecting;
146 // Map that contains a list of URL ID and their matching
147 // ExtensionThrottleEntry.
148 UrlEntryMap url_entries_;
150 // This keeps track of how many requests have been made. Used with
151 // GarbageCollectEntries.
152 unsigned int requests_since_last_gc_;
154 // Valid after construction.
155 GURL::Replacements url_id_replacements_;
157 // Certain tests do not obey the net component's threading policy, so we
158 // keep track of whether we're being used by tests, and turn off certain
159 // checks.
161 // TODO(joi): See if we can fix the offending unit tests and remove this
162 // workaround.
163 bool enable_thread_checks_;
165 // Initially false, switches to true once we have logged because of back-off
166 // being disabled for localhost.
167 bool logged_for_localhost_disabled_;
169 // net::NetLog to use, if configured.
170 net::BoundNetLog net_log_;
172 // Valid once we've registered for network notifications.
173 base::PlatformThreadId registered_from_thread_;
175 bool ignore_user_gesture_load_flag_for_tests_;
177 // This is NULL when it is not set for tests.
178 scoped_ptr<net::BackoffEntry::Policy> backoff_policy_for_tests_;
180 DISALLOW_COPY_AND_ASSIGN(ExtensionThrottleManager);
183 } // namespace extensions
185 #endif // EXTENSIONS_BROWSER_EXTENSION_THROTTLE_MANAGER_H_