OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/net/connect_interceptor.h" | 5 #include "chrome/browser/net/connect_interceptor.h" |
6 | 6 |
7 #include "chrome/browser/net/predictor.h" | 7 #include "chrome/browser/net/predictor.h" |
8 #include "net/base/load_flags.h" | 8 #include "net/base/load_flags.h" |
9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request.h" |
10 | 10 |
11 namespace chrome_browser_net { | 11 namespace chrome_browser_net { |
12 | 12 |
13 // We don't bother learning to preconnect via a GET if the original URL | |
14 // navigation was so long ago, that a preconnection would have been dropped | |
15 // anyway. We believe most servers will drop the connection in 10 seconds, so | |
16 // we currently estimate this time-till-drop at 10 seconds. | |
17 // TODO(jar): We should do a persistent field trial to validate/optimize this. | |
18 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; | |
19 | |
20 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) | 13 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) |
21 : timed_cache_(base::TimeDelta::FromSeconds( | 14 : timed_cache_(base::TimeDelta::FromSeconds( |
22 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | 15 Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet)), |
23 predictor_(predictor) { | 16 predictor_(predictor) { |
24 DCHECK(predictor); | 17 DCHECK(predictor); |
25 } | 18 } |
26 | 19 |
27 ConnectInterceptor::~ConnectInterceptor() { | 20 ConnectInterceptor::~ConnectInterceptor() { |
28 } | 21 } |
29 | 22 |
30 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) { | 23 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) { |
31 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 24 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
32 if (request_scheme_host == GURL::EmptyGURL()) | 25 if (request_scheme_host == GURL::EmptyGURL()) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // We've already made any/all predictions when we navigated to the | 64 // We've already made any/all predictions when we navigated to the |
72 // referring host, so we can bail out here. | 65 // referring host, so we can bail out here. |
73 // We don't update the RecentlySeen() time because any preconnections | 66 // We don't update the RecentlySeen() time because any preconnections |
74 // need to be made at the first navigation (i.e., when referer was loaded) | 67 // need to be made at the first navigation (i.e., when referer was loaded) |
75 // and wouldn't have waited for this current request navigation. | 68 // and wouldn't have waited for this current request navigation. |
76 return; | 69 return; |
77 } | 70 } |
78 } | 71 } |
79 timed_cache_.SetRecentlySeen(request_scheme_host); | 72 timed_cache_.SetRecentlySeen(request_scheme_host); |
80 | 73 |
| 74 predictor_->RecordPreconnectNavigationStats(request_scheme_host); |
| 75 |
81 // Subresources for main frames usually get predicted when we detected the | 76 // Subresources for main frames usually get predicted when we detected the |
82 // main frame request - way back in RenderViewHost::Navigate. So only handle | 77 // main frame request - way back in RenderViewHost::Navigate. So only handle |
83 // predictions now for subresources or for redirected hosts. | 78 // predictions now for subresources or for redirected hosts. |
84 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 79 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
85 predictor_->PredictFrameSubresources(request_scheme_host, | 80 predictor_->PredictFrameSubresources(request_scheme_host, |
86 request->first_party_for_cookies()); | 81 request->first_party_for_cookies()); |
87 return; | 82 return; |
88 } | 83 } |
89 | 84 |
90 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | |
91 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | |
92 max_duration_(max_duration) { | |
93 } | |
94 | |
95 // Make Clang compilation happy with explicit destructor. | |
96 ConnectInterceptor::TimedCache::~TimedCache() {} | |
97 | |
98 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) { | |
99 DCHECK_EQ(url.GetWithEmptyPath(), url); | |
100 // Evict any overly old entries. | |
101 base::TimeTicks now = base::TimeTicks::Now(); | |
102 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | |
103 while (!mru_cache_.empty()) { | |
104 DCHECK(eldest == mru_cache_.rbegin()); | |
105 if (now - eldest->second < max_duration_) | |
106 break; | |
107 eldest = mru_cache_.Erase(eldest); | |
108 } | |
109 return mru_cache_.end() != mru_cache_.Peek(url); | |
110 } | |
111 | |
112 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) { | |
113 DCHECK_EQ(url.GetWithEmptyPath(), url); | |
114 mru_cache_.Put(url, base::TimeTicks::Now()); | |
115 } | |
116 | |
117 } // namespace chrome_browser_net | 85 } // namespace chrome_browser_net |
OLD | NEW |