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