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