| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/singleton.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/url_request/url_request_throttler_entry.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 // Class that registers URL request throttler entries for URLs being accessed in |
| 20 // order to supervise traffic. URL requests for HTTP contents should register |
| 21 // their URLs in this manager on each request. |
| 22 // URLRequestThrottlerManager maintains a map of URL IDs to URL request |
| 23 // throttler entries. It creates URL request throttler entries when new URLs are |
| 24 // registered, and does garbage collection from time to time in order to clean |
| 25 // out outdated entries. URL ID consists of lowercased scheme, host, port and |
| 26 // path. All URLs converted to the same ID will share the same entry. |
| 27 // |
| 28 // NOTE: All usage of the singleton object of this class should be on the same |
| 29 // thread. |
| 30 class URLRequestThrottlerManager { |
| 31 public: |
| 32 static URLRequestThrottlerManager* GetInstance(); |
| 33 |
| 34 // Must be called for every request, returns the URL request throttler entry |
| 35 // associated with the URL. The caller must inform this entry of some events. |
| 36 // Please refer to url_request_throttler_entry_interface.h for further |
| 37 // informations. |
| 38 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( |
| 39 const GURL& url); |
| 40 |
| 41 // Registers a new entry in this service and overrides the existing entry (if |
| 42 // any) for the URL. The service will hold a reference to the entry. |
| 43 // It is only used by unit tests. |
| 44 void OverrideEntryForTests(const GURL& url, URLRequestThrottlerEntry* entry); |
| 45 |
| 46 // Explicitly erases an entry. |
| 47 // This is useful to remove those entries which have got infinite lifetime and |
| 48 // thus won't be garbage collected. |
| 49 // It is only used by unit tests. |
| 50 void EraseEntryForTests(const GURL& url); |
| 51 |
| 52 protected: |
| 53 URLRequestThrottlerManager(); |
| 54 ~URLRequestThrottlerManager(); |
| 55 |
| 56 // Method that allows us to transform a URL into an ID that can be used in our |
| 57 // map. Resulting IDs will be lowercase and consist of the scheme, host, port |
| 58 // and path (without query string, fragment, etc.). |
| 59 // If the URL is invalid, the invalid spec will be returned, without any |
| 60 // transformation. |
| 61 std::string GetIdFromUrl(const GURL& url) const; |
| 62 |
| 63 // Method that ensures the map gets cleaned from time to time. The period at |
| 64 // which garbage collecting happens is adjustable with the |
| 65 // kRequestBetweenCollecting constant. |
| 66 void GarbageCollectEntriesIfNecessary(); |
| 67 // Method that does the actual work of garbage collecting. |
| 68 void GarbageCollectEntries(); |
| 69 |
| 70 // Used by tests. |
| 71 int GetNumberOfEntriesForTests() const { return url_entries_.size(); } |
| 72 |
| 73 private: |
| 74 friend struct DefaultSingletonTraits<URLRequestThrottlerManager>; |
| 75 |
| 76 // From each URL we generate an ID composed of the scheme, host, port and path |
| 77 // that allows us to uniquely map an entry to it. |
| 78 typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> > |
| 79 UrlEntryMap; |
| 80 |
| 81 // Maximum number of entries that we are willing to collect in our map. |
| 82 static const unsigned int kMaximumNumberOfEntries; |
| 83 // Number of requests that will be made between garbage collection. |
| 84 static const unsigned int kRequestsBetweenCollecting; |
| 85 |
| 86 // Map that contains a list of URL ID and their matching |
| 87 // URLRequestThrottlerEntry. |
| 88 UrlEntryMap url_entries_; |
| 89 |
| 90 // This keeps track of how many requests have been made. Used with |
| 91 // GarbageCollectEntries. |
| 92 unsigned int requests_since_last_gc_; |
| 93 |
| 94 mutable scoped_ptr<GURL::Replacements> url_id_replacements_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); |
| 97 }; |
| 98 |
| 99 } // namespace net |
| 100 |
| 101 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| OLD | NEW |