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_api.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 | 10 |
| 10 namespace chrome_browser_net { | 11 namespace chrome_browser_net { |
| 11 | 12 |
| 12 // 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 |
| 13 // 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 |
| 14 // 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 |
| 15 // we currently estimate this time-till-drop at 10 seconds. | 16 // we currently estimate this time-till-drop at 10 seconds. |
| 16 // 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. |
| 17 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; | 18 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; |
| 18 | 19 |
| 19 ConnectInterceptor::ConnectInterceptor() | 20 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) |
| 20 : timed_cache_(base::TimeDelta::FromSeconds( | 21 : timed_cache_(base::TimeDelta::FromSeconds( |
| 21 kMaxUnusedSocketLifetimeSecondsWithoutAGet)) { | 22 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), |
| 22 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 23 predictor_(predictor) { |
|
willchan no longer on Chromium
2011/08/10 16:10:35
there's a friend list in net/url_request/url_reque
rpetterson
2011/08/12 03:12:36
Done.
| |
| 23 } | 24 } |
| 24 | 25 |
| 25 ConnectInterceptor::~ConnectInterceptor() { | 26 ConnectInterceptor::~ConnectInterceptor() { |
| 26 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | |
| 27 } | 27 } |
| 28 | 28 |
| 29 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( | 29 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( |
| 30 net::URLRequest* request) { | 30 net::URLRequest* request) const { |
| 31 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 31 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
| 32 if (request_scheme_host == GURL::EmptyGURL()) | 32 if (request_scheme_host == GURL::EmptyGURL()) |
| 33 return NULL; | 33 return NULL; |
| 34 | 34 |
| 35 // Learn what URLs are likely to be needed during next startup. | 35 // Learn what URLs are likely to be needed during next startup. |
| 36 LearnAboutInitialNavigation(request_scheme_host); | 36 if (predictor_) |
|
willchan no longer on Chromium
2011/08/10 16:10:35
|predictor_| should never be NULL, right? It was p
rpetterson
2011/08/12 03:12:36
Yes, you're right since we only create the connect
| |
| 37 predictor_->LearnAboutInitialNavigation(request_scheme_host); | |
| 37 | 38 |
| 38 bool redirected_host = false; | 39 bool redirected_host = false; |
| 39 if (request->referrer().empty()) { | 40 if (request->referrer().empty()) { |
| 40 if (request->url() != request->original_url()) { | 41 if (request->url() != request->original_url()) { |
| 41 // This request was completed with a redirect. | 42 // This request was completed with a redirect. |
| 42 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); | 43 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); |
| 43 if (request_scheme_host != original_scheme_host) { | 44 if (request_scheme_host != original_scheme_host) { |
| 44 redirected_host = true; | 45 redirected_host = true; |
| 45 // Don't learn from redirects that take path as an argument, but do | 46 // Don't learn from redirects that take path as an argument, but do |
| 46 // learn from short-hand typing entries, such as "cnn.com" redirects to | 47 // learn from short-hand typing entries, such as "cnn.com" redirects to |
| 47 // "www.cnn.com". We can't just check for has_path(), as a mere "/" | 48 // "www.cnn.com". We can't just check for has_path(), as a mere "/" |
| 48 // will count as a path, so we check that the path is at most a "/" | 49 // will count as a path, so we check that the path is at most a "/" |
| 49 // (1 character long) to decide the redirect is "definitive" and has no | 50 // (1 character long) to decide the redirect is "definitive" and has no |
| 50 // significant path. | 51 // significant path. |
| 51 // TODO(jar): It may be ok to learn from all redirects, as the adaptive | 52 // TODO(jar): It may be ok to learn from all redirects, as the adaptive |
| 52 // system will not respond until several identical redirects have taken | 53 // system will not respond until several identical redirects have taken |
| 53 // place. Hence a use of a path (that changes) wouldn't really be | 54 // place. Hence a use of a path (that changes) wouldn't really be |
| 54 // learned from anyway. | 55 // learned from anyway. |
| 55 if (request->original_url().path().length() <= 1 && | 56 if (request->original_url().path().length() <= 1 && |
| 56 timed_cache_.WasRecentlySeen(original_scheme_host)) { | 57 timed_cache_.WasRecentlySeen(original_scheme_host)) { |
| 57 // TODO(jar): These definite redirects could be learned much faster. | 58 // TODO(jar): These definite redirects could be learned much faster. |
| 58 LearnFromNavigation(original_scheme_host, request_scheme_host); | 59 if (predictor_) |
| 60 predictor_->LearnFromNavigation(original_scheme_host, | |
| 61 request_scheme_host); | |
| 59 } | 62 } |
| 60 } | 63 } |
| 61 } | 64 } |
| 62 } else { | 65 } else { |
| 63 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); | 66 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); |
| 64 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); | 67 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); |
| 65 // Learn about our referring URL, for use in the future. | 68 // Learn about our referring URL, for use in the future. |
| 66 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 69 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) |
| 67 LearnFromNavigation(referring_scheme_host, request_scheme_host); | 70 if (predictor_) |
| 71 predictor_->LearnFromNavigation(referring_scheme_host, | |
| 72 request_scheme_host); | |
| 68 if (referring_scheme_host == request_scheme_host) { | 73 if (referring_scheme_host == request_scheme_host) { |
| 69 // 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 |
| 70 // referring host, so we can bail out here. | 75 // referring host, so we can bail out here. |
| 71 // We don't update the RecentlySeen() time because any preconnections | 76 // We don't update the RecentlySeen() time because any preconnections |
| 72 // 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) |
| 73 // and wouldn't have waited for this current request navigation. | 78 // and wouldn't have waited for this current request navigation. |
| 74 return NULL; | 79 return NULL; |
| 75 } | 80 } |
| 76 } | 81 } |
| 77 timed_cache_.SetRecentlySeen(request_scheme_host); | 82 timed_cache_.SetRecentlySeen(request_scheme_host); |
| 78 | 83 |
| 79 // Subresources for main frames usually get predicted when we detected the | 84 // Subresources for main frames usually get predicted when we detected the |
| 80 // main frame request - way back in RenderViewHost::Navigate. So only handle | 85 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 81 // predictions now for subresources or for redirected hosts. | 86 // predictions now for subresources or for redirected hosts. |
| 82 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 87 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
| 83 PredictFrameSubresources(request_scheme_host); | 88 if (predictor_) |
| 89 predictor_->PredictFrameSubresources(request_scheme_host); | |
| 84 return NULL; | 90 return NULL; |
| 85 } | 91 } |
| 86 | 92 |
| 87 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( | 93 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( |
| 88 net::URLRequest* request) { | 94 net::URLRequest* request) const { |
| 89 return NULL; | 95 return NULL; |
| 90 } | 96 } |
| 91 | 97 |
| 92 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( | 98 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( |
| 93 net::URLRequest* request, | 99 const GURL& location, |
| 94 const GURL& location) { | 100 net::URLRequest* request) const { |
| 95 return NULL; | 101 return NULL; |
| 96 } | 102 } |
| 97 | 103 |
| 98 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 104 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
| 99 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 105 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
| 100 max_duration_(max_duration) { | 106 max_duration_(max_duration) { |
| 101 } | 107 } |
| 102 | 108 |
| 103 // Make Clang compilation happy with explicit destructor. | 109 // Make Clang compilation happy with explicit destructor. |
| 104 ConnectInterceptor::TimedCache::~TimedCache() {} | 110 ConnectInterceptor::TimedCache::~TimedCache() {} |
| 105 | 111 |
| 106 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) { | 112 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { |
| 107 DCHECK_EQ(url.GetWithEmptyPath(), url); | 113 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 108 // Evict any overly old entries. | 114 // Evict any overly old entries. |
| 109 base::TimeTicks now = base::TimeTicks::Now(); | 115 base::TimeTicks now = base::TimeTicks::Now(); |
| 110 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 116 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
| 111 while (!mru_cache_.empty()) { | 117 while (!mru_cache_.empty()) { |
| 112 DCHECK(eldest == mru_cache_.rbegin()); | 118 DCHECK(eldest == mru_cache_.rbegin()); |
| 113 if (now - eldest->second < max_duration_) | 119 if (now - eldest->second < max_duration_) |
| 114 break; | 120 break; |
| 115 eldest = mru_cache_.Erase(eldest); | 121 eldest = mru_cache_.Erase(eldest); |
| 116 } | 122 } |
| 117 return mru_cache_.end() != mru_cache_.Peek(url); | 123 return mru_cache_.end() != mru_cache_.Peek(url); |
| 118 } | 124 } |
| 119 | 125 |
| 120 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) { | 126 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
| 121 DCHECK_EQ(url.GetWithEmptyPath(), url); | 127 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 122 mru_cache_.Put(url, base::TimeTicks::Now()); | 128 mru_cache_.Put(url, base::TimeTicks::Now()); |
| 123 } | 129 } |
| 124 | 130 |
| 125 } // namespace chrome_browser_net | 131 } // namespace chrome_browser_net |
| OLD | NEW |