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