| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/singleton.h" | 13 #include "base/singleton.h" |
| 14 #include "base/synchronization/lock.h" // ThreadCheckerForRelease | 14 #include "base/threading/thread_checker.h" |
| 15 #include "base/threading/platform_thread.h" // ThreadCheckerForRelease | |
| 16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 17 #include "net/url_request/url_request_throttler_entry.h" | 16 #include "net/url_request/url_request_throttler_entry.h" |
| 18 | 17 |
| 19 namespace net { | 18 namespace net { |
| 20 | 19 |
| 21 // TODO(joi): Delete this temporary copy of base::ThreadChecker (needed to | |
| 22 // enable it in release builds) and go back to simply inheriting from | |
| 23 // NonThreadSafe once crbug.com/71721 has been tracked down. | |
| 24 class ThreadCheckerForRelease { | |
| 25 public: | |
| 26 ThreadCheckerForRelease(); | |
| 27 ~ThreadCheckerForRelease(); | |
| 28 | |
| 29 bool CalledOnValidThread() const; | |
| 30 | |
| 31 // Changes the thread that is checked for in CalledOnValidThread. This may | |
| 32 // be useful when an object may be created on one thread and then used | |
| 33 // exclusively on another thread. | |
| 34 void DetachFromThread(); | |
| 35 | |
| 36 private: | |
| 37 void EnsureThreadIdAssigned() const; | |
| 38 | |
| 39 mutable base::Lock lock_; | |
| 40 // This is mutable so that CalledOnValidThread can set it. | |
| 41 // It's guarded by |lock_|. | |
| 42 mutable base::PlatformThreadId valid_thread_id_; | |
| 43 }; | |
| 44 | |
| 45 // Class that registers URL request throttler entries for URLs being accessed | 20 // Class that registers URL request throttler entries for URLs being accessed |
| 46 // in order to supervise traffic. URL requests for HTTP contents should | 21 // in order to supervise traffic. URL requests for HTTP contents should |
| 47 // register their URLs in this manager on each request. | 22 // register their URLs in this manager on each request. |
| 48 // | 23 // |
| 49 // URLRequestThrottlerManager maintains a map of URL IDs to URL request | 24 // URLRequestThrottlerManager maintains a map of URL IDs to URL request |
| 50 // throttler entries. It creates URL request throttler entries when new URLs | 25 // throttler entries. It creates URL request throttler entries when new URLs |
| 51 // are registered, and does garbage collection from time to time in order to | 26 // are registered, and does garbage collection from time to time in order to |
| 52 // clean out outdated entries. URL ID consists of lowercased scheme, host, port | 27 // clean out outdated entries. URL ID consists of lowercased scheme, host, port |
| 53 // and path. All URLs converted to the same ID will share the same entry. | 28 // and path. All URLs converted to the same ID will share the same entry. |
| 54 // | 29 // |
| 55 // NOTE: All usage of the singleton object of this class should be on the same | 30 // NOTE: All usage of the singleton object of this class should be on the same |
| 56 // thread. | 31 // thread (construction and destruction excluded). |
| 57 // | |
| 58 // TODO(joi): Switch back to NonThreadSafe (and remove checks in release builds) | |
| 59 // once crbug.com/71721 has been tracked down. | |
| 60 class URLRequestThrottlerManager { | 32 class URLRequestThrottlerManager { |
| 61 public: | 33 public: |
| 62 static URLRequestThrottlerManager* GetInstance(); | 34 static URLRequestThrottlerManager* GetInstance(); |
| 63 | 35 |
| 64 // Must be called for every request, returns the URL request throttler entry | 36 // Must be called for every request, returns the URL request throttler entry |
| 65 // associated with the URL. The caller must inform this entry of some events. | 37 // associated with the URL. The caller must inform this entry of some events. |
| 66 // Please refer to url_request_throttler_entry_interface.h for further | 38 // Please refer to url_request_throttler_entry_interface.h for further |
| 67 // informations. | 39 // informations. |
| 68 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( | 40 scoped_refptr<URLRequestThrottlerEntryInterface> RegisterRequestUrl( |
| 69 const GURL& url); | 41 const GURL& url); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 bool enforce_throttling_; | 106 bool enforce_throttling_; |
| 135 | 107 |
| 136 // Certain tests do not obey the net component's threading policy, so we | 108 // Certain tests do not obey the net component's threading policy, so we |
| 137 // keep track of whether we're being used by tests, and turn off certain | 109 // keep track of whether we're being used by tests, and turn off certain |
| 138 // checks. | 110 // checks. |
| 139 // | 111 // |
| 140 // TODO(joi): See if we can fix the offending unit tests and remove this | 112 // TODO(joi): See if we can fix the offending unit tests and remove this |
| 141 // workaround. | 113 // workaround. |
| 142 bool being_tested_; | 114 bool being_tested_; |
| 143 | 115 |
| 144 ThreadCheckerForRelease thread_checker_; | 116 // Check threads even in release builds. |
| 117 // |
| 118 // TODO(joi): Switch back to just inheriting NonThreadSafe once |
| 119 // crbug.com/71721 has been tracked down. |
| 120 base::ThreadCheckerImpl thread_checker_; |
| 145 | 121 |
| 146 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); | 122 DISALLOW_COPY_AND_ASSIGN(URLRequestThrottlerManager); |
| 147 }; | 123 }; |
| 148 | 124 |
| 149 } // namespace net | 125 } // namespace net |
| 150 | 126 |
| 151 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ | 127 #endif // NET_URL_REQUEST_URL_REQUEST_THROTTLER_MANAGER_H_ |
| OLD | NEW |