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