Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ | 5 #ifndef CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ |
| 6 #define CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ | 6 #define CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request_job_factory.h" |
|
willchan no longer on Chromium
2011/08/12 21:17:29
This header shouldn't be first, it should be sorte
rpetterson
2011/08/13 00:55:17
Done.
| |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/mru_cache.h" | 12 #include "base/memory/mru_cache.h" |
| 13 #include "base/time.h" | |
| 13 | 14 |
| 14 namespace chrome_browser_net { | 15 namespace chrome_browser_net { |
| 15 | 16 |
| 17 class Predictor; | |
| 18 | |
| 16 //------------------------------------------------------------------------------ | 19 //------------------------------------------------------------------------------ |
| 17 // An interceptor to monitor URLRequests so that we can do speculative DNS | 20 // An interceptor to monitor URLRequests so that we can do speculative DNS |
| 18 // resolution and/or speculative TCP preconnections. | 21 // resolution and/or speculative TCP preconnections. |
| 19 class ConnectInterceptor : public net::URLRequest::Interceptor { | 22 class ConnectInterceptor : public net::URLRequestJobFactory::Interceptor { |
| 20 public: | 23 public: |
| 21 // Construction includes registration as an URL. | 24 // Construction includes registration as an URL. |
| 22 ConnectInterceptor(); | 25 explicit ConnectInterceptor(Predictor* predictor); |
| 23 // Destruction includes unregistering. | 26 // Destruction includes unregistering. |
| 24 virtual ~ConnectInterceptor(); | 27 virtual ~ConnectInterceptor(); |
| 25 | 28 |
| 26 protected: | 29 protected: |
| 27 // Overridden from net::URLRequest::Interceptor: | 30 // Overridden from net::URLRequest::Interceptor: |
| 28 // Learn about referrers, and optionally preconnect based on history. | 31 // Learn about referrers, and optionally preconnect based on history. |
| 29 virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request); | 32 virtual net::URLRequestJob* MaybeIntercept( |
| 30 virtual net::URLRequestJob* MaybeInterceptResponse(net::URLRequest* request); | 33 net::URLRequest* request) const OVERRIDE; |
| 31 virtual net::URLRequestJob* MaybeInterceptRedirect(net::URLRequest* request, | 34 virtual net::URLRequestJob* MaybeInterceptResponse( |
| 32 const GURL& location); | 35 net::URLRequest* request) const OVERRIDE; |
| 36 virtual net::URLRequestJob* MaybeInterceptRedirect( | |
| 37 const GURL& location, net::URLRequest* request) const OVERRIDE; | |
| 33 | 38 |
| 34 private: | 39 private: |
| 35 // Provide access to local class TimedCache for testing. | 40 // Provide access to local class TimedCache for testing. |
| 36 FRIEND_TEST(ConnectInterceptorTest, TimedCacheRecall); | 41 FRIEND_TEST(ConnectInterceptorTest, TimedCacheRecall); |
| 37 FRIEND_TEST(ConnectInterceptorTest, TimedCacheEviction); | 42 FRIEND_TEST(ConnectInterceptorTest, TimedCacheEviction); |
| 38 | 43 |
| 39 // Define a LRU cache that recalls all navigations within the last N seconds. | 44 // Define a LRU cache that recalls all navigations within the last N seconds. |
| 40 // When we learn about subresources to possibly preconnect to, it would be a | 45 // When we learn about subresources to possibly preconnect to, it would be a |
| 41 // waste to preconnect when the original navigation was too long ago. Any | 46 // waste to preconnect when the original navigation was too long ago. Any |
| 42 // connected, but unused TCP/IP connection, will generally be reset by the | 47 // connected, but unused TCP/IP connection, will generally be reset by the |
| 43 // server if it is not used quickly (i.e., GET or POST is sent). | 48 // server if it is not used quickly (i.e., GET or POST is sent). |
| 44 class TimedCache { | 49 class TimedCache { |
| 45 public: | 50 public: |
| 46 explicit TimedCache(const base::TimeDelta& max_duration); | 51 explicit TimedCache(const base::TimeDelta& max_duration); |
| 47 ~TimedCache(); | 52 ~TimedCache(); |
| 48 | 53 |
| 49 // Evicts any entries that have been in the FIFO "too long," and then checks | 54 // Evicts any entries that have been in the FIFO "too long," and then checks |
| 50 // to see if the given url is (still) in the FIFO cache. | 55 // to see if the given url is (still) in the FIFO cache. |
| 51 bool WasRecentlySeen(const GURL& url); | 56 bool WasRecentlySeen(const GURL& url) const; |
| 52 | 57 |
| 53 // Adds the given url to the cache, where it will remain for max_duration_. | 58 // Adds the given url to the cache, where it will remain for max_duration_. |
| 54 void SetRecentlySeen(const GURL& url); | 59 void SetRecentlySeen(const GURL& url) const; |
| 55 | 60 |
| 56 private: | 61 private: |
| 57 // Our cache will be keyed on a URL (actually, just a scheme/host/port). | 62 // Our cache will be keyed on a URL (actually, just a scheme/host/port). |
| 58 // We will always track the time it was last added to the FIFO cache by | 63 // We will always track the time it was last added to the FIFO cache by |
| 59 // remembering a TimeTicks value. | 64 // remembering a TimeTicks value. |
| 60 typedef base::MRUCache<GURL, base::TimeTicks> UrlMruTimedCache; | 65 typedef base::MRUCache<GURL, base::TimeTicks> UrlMruTimedCache; |
| 61 UrlMruTimedCache mru_cache_; | 66 // mru_cache_ has to be mutable in order to be accessed from the overriden |
| 67 // URLRequestJob functions. It is mutable because it tracks the urls and | |
| 68 // caches them. | |
| 69 mutable UrlMruTimedCache mru_cache_; | |
| 62 | 70 |
| 63 // The longest time an entry can persist in the cache, and still be found. | 71 // The longest time an entry can persist in the cache, and still be found. |
| 64 const base::TimeDelta max_duration_; | 72 const base::TimeDelta max_duration_; |
| 65 | 73 |
| 66 DISALLOW_COPY_AND_ASSIGN(TimedCache); | 74 DISALLOW_COPY_AND_ASSIGN(TimedCache); |
| 67 }; | 75 }; |
| 68 TimedCache timed_cache_; | 76 TimedCache timed_cache_; |
| 77 Predictor* predictor_; | |
|
willchan no longer on Chromium
2011/08/12 21:17:29
I'm a bit of a const nazi, can you make this Predi
rpetterson
2011/08/13 00:55:17
Done.
| |
| 69 | 78 |
| 70 DISALLOW_COPY_AND_ASSIGN(ConnectInterceptor); | 79 DISALLOW_COPY_AND_ASSIGN(ConnectInterceptor); |
| 71 }; | 80 }; |
| 72 | 81 |
| 73 } // namespace chrome_browser_net | 82 } // namespace chrome_browser_net |
| 74 | 83 |
| 75 #endif // CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ | 84 #endif // CHROME_BROWSER_NET_CONNECT_INTERCEPTOR_H_ |
| OLD | NEW |