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 #include "chrome/browser/net/connect_interceptor.h" | 5 #include "chrome/browser/net/connect_interceptor.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | |
| 7 #include "chrome/browser/net/predictor.h" | 8 #include "chrome/browser/net/predictor.h" |
| 8 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
| 9 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
| 10 | 11 |
| 11 namespace chrome_browser_net { | 12 namespace chrome_browser_net { |
| 12 | 13 |
| 13 // We don't bother learning to preconnect via a GET if the original URL | 14 // 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 // 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 // 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 // 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 // TODO(jar): We should do a persistent field trial to validate/optimize this. |
| 18 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; | 19 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; |
| 19 | 20 |
| 20 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) | 21 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) |
| 21 : timed_cache_(base::TimeDelta::FromSeconds( | 22 : timed_cache_(base::TimeDelta::FromSeconds( |
| 22 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | 23 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), |
| 24 recent_preconnects_(base::TimeDelta::FromSeconds( | |
| 25 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | |
| 23 predictor_(predictor) { | 26 predictor_(predictor) { |
| 24 DCHECK(predictor); | 27 DCHECK(predictor); |
| 25 } | 28 } |
| 26 | 29 |
| 27 ConnectInterceptor::~ConnectInterceptor() { | 30 ConnectInterceptor::~ConnectInterceptor() { |
| 28 } | 31 } |
| 29 | 32 |
| 30 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) const { | 33 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) const { |
| 31 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 34 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
| 32 if (request_scheme_host == GURL::EmptyGURL()) | 35 if (request_scheme_host == GURL::EmptyGURL()) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 if (referring_scheme_host == request_scheme_host) { | 73 if (referring_scheme_host == request_scheme_host) { |
| 71 // We've already made any/all predictions when we navigated to the | 74 // We've already made any/all predictions when we navigated to the |
| 72 // referring host, so we can bail out here. | 75 // referring host, so we can bail out here. |
| 73 // We don't update the RecentlySeen() time because any preconnections | 76 // 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) | 77 // 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. | 78 // and wouldn't have waited for this current request navigation. |
| 76 return; | 79 return; |
| 77 } | 80 } |
| 78 } | 81 } |
| 79 timed_cache_.SetRecentlySeen(request_scheme_host); | 82 timed_cache_.SetRecentlySeen(request_scheme_host); |
| 83 UMA_HISTOGRAM_BOOLEAN( | |
| 84 "Net.PreconnectedNavigation", | |
| 85 recent_preconnects_.WasRecentlySeen(request_scheme_host)); | |
| 80 | 86 |
| 81 // Subresources for main frames usually get predicted when we detected the | 87 // Subresources for main frames usually get predicted when we detected the |
| 82 // main frame request - way back in RenderViewHost::Navigate. So only handle | 88 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 83 // predictions now for subresources or for redirected hosts. | 89 // predictions now for subresources or for redirected hosts. |
| 84 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 90 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
| 85 predictor_->PredictFrameSubresources(request_scheme_host, | 91 predictor_->PredictFrameSubresources(request_scheme_host, |
| 86 request->first_party_for_cookies()); | 92 request->first_party_for_cookies()); |
| 87 return; | 93 return; |
| 88 } | 94 } |
| 89 | 95 |
| 96 void ConnectInterceptor::WitnessPreconnect(const GURL& url) { | |
| 97 GURL request_scheme_host(Predictor::CanonicalizeUrl(url)); | |
| 98 recent_preconnects_.SetRecentlySeen(request_scheme_host); | |
|
mmenke
2013/05/22 17:58:43
I think this could actually all be done in the pre
kouhei (in TOK)
2013/05/23 07:31:13
I tried moving the logic to Predictor. TimedCache
| |
| 99 } | |
| 100 | |
| 90 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 101 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
| 91 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 102 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
| 92 max_duration_(max_duration) { | 103 max_duration_(max_duration) { |
| 93 } | 104 } |
| 94 | 105 |
| 95 // Make Clang compilation happy with explicit destructor. | 106 // Make Clang compilation happy with explicit destructor. |
| 96 ConnectInterceptor::TimedCache::~TimedCache() {} | 107 ConnectInterceptor::TimedCache::~TimedCache() {} |
| 97 | 108 |
| 98 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { | 109 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { |
| 99 DCHECK_EQ(url.GetWithEmptyPath(), url); | 110 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 100 // Evict any overly old entries. | 111 // Evict any overly old entries. |
| 101 base::TimeTicks now = base::TimeTicks::Now(); | 112 base::TimeTicks now = base::TimeTicks::Now(); |
| 102 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 113 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
| 103 while (!mru_cache_.empty()) { | 114 while (!mru_cache_.empty()) { |
| 104 DCHECK(eldest == mru_cache_.rbegin()); | 115 DCHECK(eldest == mru_cache_.rbegin()); |
| 105 if (now - eldest->second < max_duration_) | 116 if (now - eldest->second < max_duration_) |
| 106 break; | 117 break; |
| 107 eldest = mru_cache_.Erase(eldest); | 118 eldest = mru_cache_.Erase(eldest); |
| 108 } | 119 } |
| 109 return mru_cache_.end() != mru_cache_.Peek(url); | 120 return mru_cache_.end() != mru_cache_.Peek(url); |
| 110 } | 121 } |
| 111 | 122 |
| 112 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { | 123 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
| 113 DCHECK_EQ(url.GetWithEmptyPath(), url); | 124 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 114 mru_cache_.Put(url, base::TimeTicks::Now()); | 125 mru_cache_.Put(url, base::TimeTicks::Now()); |
| 115 } | 126 } |
| 116 | 127 |
| 117 } // namespace chrome_browser_net | 128 } // namespace chrome_browser_net |
| OLD | NEW |