| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" | |
| 16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 18 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 19 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 20 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
| 21 #include "net/url_request/url_request_throttler_entry.h" | 20 #include "net/url_request/url_request_throttler_entry.h" |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 | 23 |
| 25 class BoundNetLog; | 24 class BoundNetLog; |
| 26 class NetLog; | 25 class NetLog; |
| 27 | 26 |
| 28 // Class that registers URL request throttler entries for URLs being accessed | 27 // Class that registers URL request throttler entries for URLs being accessed |
| 29 // in order to supervise traffic. URL requests for HTTP contents should | 28 // in order to supervise traffic. URL requests for HTTP contents should |
| 30 // register their URLs in this manager on each request. | 29 // register their URLs in this manager on each request. |
| 31 // | 30 // |
| 32 // URLRequestThrottlerManager maintains a map of URL IDs to URL request | 31 // URLRequestThrottlerManager maintains a map of URL IDs to URL request |
| 33 // throttler entries. It creates URL request throttler entries when new URLs | 32 // throttler entries. It creates URL request throttler entries when new URLs |
| 34 // are registered, and does garbage collection from time to time in order to | 33 // are registered, and does garbage collection from time to time in order to |
| 35 // clean out outdated entries. URL ID consists of lowercased scheme, host, port | 34 // clean out outdated entries. URL ID consists of lowercased scheme, host, port |
| 36 // and path. All URLs converted to the same ID will share the same entry. | 35 // and path. All URLs converted to the same ID will share the same entry. |
| 37 // | |
| 38 // NOTE: All usage of this singleton object must be on the same thread, | |
| 39 // although to allow it to be used as a singleton, construction and destruction | |
| 40 // can occur on a separate thread. | |
| 41 class NET_EXPORT URLRequestThrottlerManager | 36 class NET_EXPORT URLRequestThrottlerManager |
| 42 : NON_EXPORTED_BASE(public base::NonThreadSafe), | 37 : NON_EXPORTED_BASE(public base::NonThreadSafe), |
| 43 public NetworkChangeNotifier::IPAddressObserver, | 38 public NetworkChangeNotifier::IPAddressObserver, |
| 44 public NetworkChangeNotifier::OnlineStateObserver { | 39 public NetworkChangeNotifier::OnlineStateObserver { |
| 45 public: | 40 public: |
| 46 static URLRequestThrottlerManager* GetInstance(); | 41 URLRequestThrottlerManager(); |
| 42 virtual ~URLRequestThrottlerManager(); |
| 47 | 43 |
| 48 // Must be called for every request, returns the URL request throttler entry | 44 // Must be called for every request, returns the URL request throttler entry |
| 49 // associated with the URL. The caller must inform this entry of some events. | 45 // associated with the URL. The caller must inform this entry of some events. |
| 50 // Please refer to url_request_throttler_entry_interface.h for further | 46 // Please refer to url_request_throttler_entry_interface.h for further |
| 51 // informations. | 47 // informations. |
| 52 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( | 48 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( |
| 53 const GURL& url); | 49 const GURL& url); |
| 54 | 50 |
| 55 // Adds the given host to a list of sites for which exponential back-off | 51 // Adds the given host to a list of sites for which exponential back-off |
| 56 // throttling will be disabled. Subdomains are not included, so they | 52 // throttling will be disabled. Subdomains are not included, so they |
| (...skipping 24 matching lines...) Expand all Loading... |
| 81 // Sets the NetLog instance to use. | 77 // Sets the NetLog instance to use. |
| 82 void set_net_log(NetLog* net_log); | 78 void set_net_log(NetLog* net_log); |
| 83 NetLog* net_log() const; | 79 NetLog* net_log() const; |
| 84 | 80 |
| 85 // IPAddressObserver interface. | 81 // IPAddressObserver interface. |
| 86 virtual void OnIPAddressChanged() OVERRIDE; | 82 virtual void OnIPAddressChanged() OVERRIDE; |
| 87 | 83 |
| 88 // OnlineStateObserver interface. | 84 // OnlineStateObserver interface. |
| 89 virtual void OnOnlineStateChanged(bool online) OVERRIDE; | 85 virtual void OnOnlineStateChanged(bool online) OVERRIDE; |
| 90 | 86 |
| 91 protected: | |
| 92 URLRequestThrottlerManager(); | |
| 93 virtual ~URLRequestThrottlerManager(); | |
| 94 | |
| 95 // Method that allows us to transform a URL into an ID that can be used in our | 87 // Method that allows us to transform a URL into an ID that can be used in our |
| 96 // map. Resulting IDs will be lowercase and consist of the scheme, host, port | 88 // map. Resulting IDs will be lowercase and consist of the scheme, host, port |
| 97 // and path (without query string, fragment, etc.). | 89 // and path (without query string, fragment, etc.). |
| 98 // If the URL is invalid, the invalid spec will be returned, without any | 90 // If the URL is invalid, the invalid spec will be returned, without any |
| 99 // transformation. | 91 // transformation. |
| 100 std::string GetIdFromUrl(const GURL& url) const; | 92 std::string GetIdFromUrl(const GURL& url) const; |
| 101 | 93 |
| 102 // Method that ensures the map gets cleaned from time to time. The period at | 94 // Method that ensures the map gets cleaned from time to time. The period at |
| 103 // which garbage collecting happens is adjustable with the | 95 // which garbage collecting happens is adjustable with the |
| 104 // kRequestBetweenCollecting constant. | 96 // kRequestBetweenCollecting constant. |
| 105 void GarbageCollectEntriesIfNecessary(); | 97 void GarbageCollectEntriesIfNecessary(); |
| 106 | 98 |
| 107 // Method that does the actual work of garbage collecting. | 99 // Method that does the actual work of garbage collecting. |
| 108 void GarbageCollectEntries(); | 100 void GarbageCollectEntries(); |
| 109 | 101 |
| 110 // When we switch from online to offline or change IP addresses, we | 102 // When we switch from online to offline or change IP addresses, we |
| 111 // clear all back-off history. This is a precaution in case the change in | 103 // clear all back-off history. This is a precaution in case the change in |
| 112 // online state now lets us communicate without error with servers that | 104 // online state now lets us communicate without error with servers that |
| 113 // we were previously getting 500 or 503 responses from (perhaps the | 105 // we were previously getting 500 or 503 responses from (perhaps the |
| 114 // responses are from a badly-written proxy that should have returned a | 106 // responses are from a badly-written proxy that should have returned a |
| 115 // 502 or 504 because it's upstream connection was down or it had no route | 107 // 502 or 504 because it's upstream connection was down or it had no route |
| 116 // to the server). | 108 // to the server). |
| 117 void OnNetworkChange(); | 109 void OnNetworkChange(); |
| 118 | 110 |
| 119 // Used by tests. | 111 // Used by tests. |
| 120 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } | 112 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } |
| 121 | 113 |
| 122 private: | 114 private: |
| 123 friend struct DefaultSingletonTraits<URLRequestThrottlerManager>; | |
| 124 | |
| 125 // From each URL we generate an ID composed of the scheme, host, port and path | 115 // From each URL we generate an ID composed of the scheme, host, port and path |
| 126 // that allows us to uniquely map an entry to it. | 116 // that allows us to uniquely map an entry to it. |
| 127 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > | 117 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > |
| 128 UrlEntryMap; | 118 UrlEntryMap; |
| 129 | 119 |
| 130 // We maintain a set of hosts that have opted out of exponential | 120 // We maintain a set of hosts that have opted out of exponential |
| 131 // back-off throttling. | 121 // back-off throttling. |
| 132 typedef std::set<std::string> OptOutHosts; | 122 typedef std::set<std::string> OptOutHosts; |
| 133 | 123 |
| 134 // Maximum number of entries that we are willing to collect in our map. | 124 // Maximum number of entries that we are willing to collect in our map. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 161 |
| 172 // Valid once we've registered for network notifications. | 162 // Valid once we've registered for network notifications. |
| 173 base::PlatformThreadId registered_from_thread_; | 163 base::PlatformThreadId registered_from_thread_; |
| 174 | 164 |
| 175 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); | 165 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); |
| 176 }; | 166 }; |
| 177 | 167 |
| 178 } // namespace net | 168 } // namespace net |
| 179 | 169 |
| 180 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 170 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| OLD | NEW |