• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #pragma once
8 
9 #include <map>
10 #include <set>
11 #include <string>
12 
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "googleurl/src/gurl.h"
18 #include "net/url_request/url_request_throttler_entry.h"
19 
20 namespace net {
21 
22 // Class that registers URL request throttler entries for URLs being accessed
23 // in order to supervise traffic. URL requests for HTTP contents should
24 // register their URLs in this manager on each request.
25 //
26 // URLRequestThrottlerManager maintains a map of URL IDs to URL request
27 // throttler entries. It creates URL request throttler entries when new URLs
28 // are registered, and does garbage collection from time to time in order to
29 // clean out outdated entries. URL ID consists of lowercased scheme, host, port
30 // and path. All URLs converted to the same ID will share the same entry.
31 //
32 // NOTE: All usage of this singleton object must be on the same thread,
33 // although to allow it to be used as a singleton, construction and destruction
34 // can occur on a separate thread.
35 class URLRequestThrottlerManager : public base::NonThreadSafe {
36  public:
37   static URLRequestThrottlerManager* GetInstance();
38 
39   // Must be called for every request, returns the URL request throttler entry
40   // associated with the URL. The caller must inform this entry of some events.
41   // Please refer to url_request_throttler_entry_interface.h for further
42   // informations.
43   scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl(
44       const GURL& url);
45 
46   // Adds the given host to a list of sites for which exponential back-off
47   // throttling will be disabled.  Subdomains are not included, so they
48   // must be added separately.
49   void AddToOptOutList(const std::string& host);
50 
51   // Registers a new entry in this service and overrides the existing entry (if
52   // any) for the URL. The service will hold a reference to the entry.
53   // It is only used by unit tests.
54   void OverrideEntryForTests(const GURL& url, URLRequestThrottlerEntry* entry);
55 
56   // Explicitly erases an entry.
57   // This is useful to remove those entries which have got infinite lifetime and
58   // thus won't be garbage collected.
59   // It is only used by unit tests.
60   void EraseEntryForTests(const GURL& url);
61 
62   // Turns threading model verification on or off.  Any code that correctly
63   // uses the network stack should preferably call this function to enable
64   // verification of correct adherence to the network stack threading model.
65   void set_enable_thread_checks(bool enable);
66   bool enable_thread_checks() const;
67 
68   // Whether throttling is enabled or not.
69   void set_enforce_throttling(bool enforce);
70   bool enforce_throttling();
71 
72  protected:
73   URLRequestThrottlerManager();
74   ~URLRequestThrottlerManager();
75 
76   // Method that allows us to transform a URL into an ID that can be used in our
77   // map. Resulting IDs will be lowercase and consist of the scheme, host, port
78   // and path (without query string, fragment, etc.).
79   // If the URL is invalid, the invalid spec will be returned, without any
80   // transformation.
81   std::string GetIdFromUrl(const GURL& url) const;
82 
83   // Method that ensures the map gets cleaned from time to time. The period at
84   // which garbage collecting happens is adjustable with the
85   // kRequestBetweenCollecting constant.
86   void GarbageCollectEntriesIfNecessary();
87 
88   // Method that does the actual work of garbage collecting.
89   void GarbageCollectEntries();
90 
91   // Used by tests.
GetNumberOfEntriesForTests()92   int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
93 
94  private:
95   friend struct DefaultSingletonTraits<URLRequestThrottlerManager>;
96 
97   // From each URL we generate an ID composed of the scheme, host, port and path
98   // that allows us to uniquely map an entry to it.
99   typedef std::map<std::string, scoped_refptr<URLRequestThrottlerEntry> >
100       UrlEntryMap;
101 
102   // We maintain a set of hosts that have opted out of exponential
103   // back-off throttling.
104   typedef std::set<std::string> OptOutHosts;
105 
106   // Maximum number of entries that we are willing to collect in our map.
107   static const unsigned int kMaximumNumberOfEntries;
108   // Number of requests that will be made between garbage collection.
109   static const unsigned int kRequestsBetweenCollecting;
110 
111   // Map that contains a list of URL ID and their matching
112   // URLRequestThrottlerEntry.
113   UrlEntryMap url_entries_;
114 
115   // Set of hosts that have opted out.
116   OptOutHosts opt_out_hosts_;
117 
118   // This keeps track of how many requests have been made. Used with
119   // GarbageCollectEntries.
120   unsigned int requests_since_last_gc_;
121 
122   // Valid after construction.
123   GURL::Replacements url_id_replacements_;
124 
125   // Whether we would like to reject outgoing HTTP requests during the back-off
126   // period.
127   bool enforce_throttling_;
128 
129   // Certain tests do not obey the net component's threading policy, so we
130   // keep track of whether we're being used by tests, and turn off certain
131   // checks.
132   //
133   // TODO(joi): See if we can fix the offending unit tests and remove this
134   // workaround.
135   bool enable_thread_checks_;
136 
137   DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager);
138 };
139 
140 }  // namespace net
141 
142 #endif  // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_
143