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 "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/protocol_intercept_job_factory.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)), |
| 23 predictor_(predictor) { | 24 predictor_(predictor) { |
| 24 DCHECK(predictor); | 25 DCHECK(predictor); |
| 25 } | 26 } |
| 26 | 27 |
| 27 ConnectInterceptor::~ConnectInterceptor() { | 28 ConnectInterceptor::~ConnectInterceptor() { |
| 28 } | 29 } |
| 29 | 30 |
| 30 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( | 31 void ConnectInterceptor::WitnessURLRequest( |
| 31 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 32 net::URLRequest* request) const { |
|
mmenke
2012/12/18 20:32:15
nit: May be able to fit on one line.
| |
| 32 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 33 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
| 33 if (request_scheme_host == GURL::EmptyGURL()) | 34 if (request_scheme_host == GURL::EmptyGURL()) |
| 34 return NULL; | 35 return; |
| 35 | 36 |
| 36 // Learn what URLs are likely to be needed during next startup. | 37 // Learn what URLs are likely to be needed during next startup. |
| 37 predictor_->LearnAboutInitialNavigation(request_scheme_host); | 38 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
| 38 | 39 |
| 39 bool redirected_host = false; | 40 bool redirected_host = false; |
| 40 if (request->referrer().empty()) { | 41 if (request->referrer().empty()) { |
| 41 if (request->url() != request->original_url()) { | 42 if (request->url() != request->original_url()) { |
| 42 // This request was completed with a redirect. | 43 // This request was completed with a redirect. |
| 43 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); | 44 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); |
| 44 if (request_scheme_host != original_scheme_host) { | 45 if (request_scheme_host != original_scheme_host) { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 67 // Learn about our referring URL, for use in the future. | 68 // Learn about our referring URL, for use in the future. |
| 68 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 69 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) |
| 69 predictor_->LearnFromNavigation(referring_scheme_host, | 70 predictor_->LearnFromNavigation(referring_scheme_host, |
| 70 request_scheme_host); | 71 request_scheme_host); |
| 71 if (referring_scheme_host == request_scheme_host) { | 72 if (referring_scheme_host == request_scheme_host) { |
| 72 // We've already made any/all predictions when we navigated to the | 73 // We've already made any/all predictions when we navigated to the |
| 73 // referring host, so we can bail out here. | 74 // referring host, so we can bail out here. |
| 74 // We don't update the RecentlySeen() time because any preconnections | 75 // We don't update the RecentlySeen() time because any preconnections |
| 75 // need to be made at the first navigation (i.e., when referer was loaded) | 76 // need to be made at the first navigation (i.e., when referer was loaded) |
| 76 // and wouldn't have waited for this current request navigation. | 77 // and wouldn't have waited for this current request navigation. |
| 77 return NULL; | 78 return; |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 timed_cache_.SetRecentlySeen(request_scheme_host); | 81 timed_cache_.SetRecentlySeen(request_scheme_host); |
| 81 | 82 |
| 82 // Subresources for main frames usually get predicted when we detected the | 83 // Subresources for main frames usually get predicted when we detected the |
| 83 // main frame request - way back in RenderViewHost::Navigate. So only handle | 84 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 84 // predictions now for subresources or for redirected hosts. | 85 // predictions now for subresources or for redirected hosts. |
| 85 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 86 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
| 86 predictor_->PredictFrameSubresources(request_scheme_host); | 87 predictor_->PredictFrameSubresources(request_scheme_host); |
| 87 return NULL; | 88 return; |
| 88 } | |
| 89 | |
| 90 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( | |
| 91 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
| 92 return NULL; | |
| 93 } | |
| 94 | |
| 95 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( | |
| 96 const GURL& location, | |
| 97 net::URLRequest* request, | |
| 98 net::NetworkDelegate* network_delegate) const { | |
| 99 return NULL; | |
| 100 } | 89 } |
| 101 | 90 |
| 102 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 91 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
| 103 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 92 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
| 104 max_duration_(max_duration) { | 93 max_duration_(max_duration) { |
| 105 } | 94 } |
| 106 | 95 |
| 107 // Make Clang compilation happy with explicit destructor. | 96 // Make Clang compilation happy with explicit destructor. |
| 108 ConnectInterceptor::TimedCache::~TimedCache() {} | 97 ConnectInterceptor::TimedCache::~TimedCache() {} |
| 109 | 98 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 120 } | 109 } |
| 121 return mru_cache_.end() != mru_cache_.Peek(url); | 110 return mru_cache_.end() != mru_cache_.Peek(url); |
| 122 } | 111 } |
| 123 | 112 |
| 124 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { | 113 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
| 125 DCHECK_EQ(url.GetWithEmptyPath(), url); | 114 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 126 mru_cache_.Put(url, base::TimeTicks::Now()); | 115 mru_cache_.Put(url, base::TimeTicks::Now()); |
| 127 } | 116 } |
| 128 | 117 |
| 129 } // namespace chrome_browser_net | 118 } // namespace chrome_browser_net |
| OLD | NEW |