Update V8 to version 4.6.55.
[chromium-blink-merge.git] / net / url_request / url_request_throttler_manager.h
blobdbc48ac006161d9f6c0b9f5a676e03e365755f30
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 NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_
6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/threading/platform_thread.h"
16 #include "net/base/net_export.h"
17 #include "net/base/network_change_notifier.h"
18 #include "net/url_request/url_request_throttler_entry.h"
19 #include "url/gurl.h"
21 namespace net {
23 class BoundNetLog;
24 class NetLog;
26 // Class that registers URL request throttler entries for URLs being accessed
27 // in order to supervise traffic. URL requests for HTTP contents should
28 // register their URLs in this manager on each request.
30 // URLRequestThrottlerManager maintains a map of URL IDs to URL request
31 // throttler entries. It creates URL request throttler entries when new URLs
32 // are registered, and does garbage collection from time to time in order to
33 // clean out outdated entries. URL ID consists of lowercased scheme, host, port
34 // and path. All URLs converted to the same ID will share the same entry.
35 class NET_EXPORT URLRequestThrottlerManager
36 : NON_EXPORTED_BASE(public base::NonThreadSafe),
37 public NetworkChangeNotifier::IPAddressObserver,
38 public NetworkChangeNotifier::ConnectionTypeObserver {
39 public:
40 URLRequestThrottlerManager();
41 ~URLRequestThrottlerManager() override;
43 // Must be called for every request, returns the URL request throttler entry
44 // associated with the URL. The caller must inform this entry of some events.
45 // Please refer to url_request_throttler_entry_interface.h for further
46 // informations.
47 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl(
48 const GURL& url);
50 // Registers a new entry in this service and overrides the existing entry (if
51 // any) for the URL. The service will hold a reference to the entry.
52 // It is only used by unit tests.
53 void OverrideEntryForTests(const GURL& url, URLRequestThrottlerEntry* entry);
55 // Explicitly erases an entry.
56 // This is useful to remove those entries which have got infinite lifetime and
57 // thus won't be garbage collected.
58 // It is only used by unit tests.
59 void EraseEntryForTests(const GURL& url);
61 // Turns threading model verification on or off. Any code that correctly
62 // uses the network stack should preferably call this function to enable
63 // verification of correct adherence to the network stack threading model.
64 void set_enable_thread_checks(bool enable);
65 bool enable_thread_checks() const;
67 // Whether throttling is enabled or not.
68 void set_enforce_throttling(bool enforce);
69 bool enforce_throttling();
71 // Sets the NetLog instance to use.
72 void set_net_log(NetLog* net_log);
73 NetLog* net_log() const;
75 // IPAddressObserver interface.
76 void OnIPAddressChanged() override;
78 // ConnectionTypeObserver interface.
79 void OnConnectionTypeChanged(
80 NetworkChangeNotifier::ConnectionType type) override;
82 // Method that allows us to transform a URL into an ID that can be used in our
83 // map. Resulting IDs will be lowercase and consist of the scheme, host, port
84 // and path (without query string, fragment, etc.).
85 // If the URL is invalid, the invalid spec will be returned, without any
86 // transformation.
87 std::string GetIdFromUrl(const GURL& url) const;
89 // Method that ensures the map gets cleaned from time to time. The period at
90 // which garbage collecting happens is adjustable with the
91 // kRequestBetweenCollecting constant.
92 void GarbageCollectEntriesIfNecessary();
94 // Method that does the actual work of garbage collecting.
95 void GarbageCollectEntries();
97 // When we switch from online to offline or change IP addresses, we
98 // clear all back-off history. This is a precaution in case the change in
99 // online state now lets us communicate without error with servers that
100 // we were previously getting 500 or 503 responses from (perhaps the
101 // responses are from a badly-written proxy that should have returned a
102 // 502 or 504 because it's upstream connection was down or it had no route
103 // to the server).
104 void OnNetworkChange();
106 // Used by tests.
107 int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
109 private:
110 // From each URL we generate an ID composed of the scheme, host, port and path
111 // that allows us to uniquely map an entry to it.
112 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> >
113 UrlEntryMap;
115 // Maximum number of entries that we are willing to collect in our map.
116 static const unsigned int kMaximumNumberOfEntries;
117 // Number of requests that will be made between garbage collection.
118 static const unsigned int kRequestsBetweenCollecting;
120 // Map that contains a list of URL ID and their matching
121 // URLRequestThrottlerEntry.
122 UrlEntryMap url_entries_;
124 // This keeps track of how many requests have been made. Used with
125 // GarbageCollectEntries.
126 unsigned int requests_since_last_gc_;
128 // Valid after construction.
129 GURL::Replacements url_id_replacements_;
131 // Certain tests do not obey the net component's threading policy, so we
132 // keep track of whether we're being used by tests, and turn off certain
133 // checks.
135 // TODO(joi): See if we can fix the offending unit tests and remove this
136 // workaround.
137 bool enable_thread_checks_;
139 // Initially false, switches to true once we have logged because of back-off
140 // being disabled for localhost.
141 bool logged_for_localhost_disabled_;
143 // NetLog to use, if configured.
144 BoundNetLog net_log_;
146 // Valid once we've registered for network notifications.
147 base::PlatformThreadId registered_from_thread_;
149 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager);
152 } // namespace net
154 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_