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