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