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 |
| 21 // static | |
| 22 scoped_ptr<net::URLRequestJobFactory> | |
| 23 ConnectInterceptor::CreateURLRequestJobFactory( | |
| 24 scoped_ptr<net::URLRequestJobFactory> base_job_factory, | |
| 25 Predictor* predictor) { | |
| 26 scoped_ptr<net::URLRequestJobFactory> top_job_factory( | |
| 27 new net::ProtocolInterceptJobFactory(base_job_factory.Pass(), | |
| 28 new ConnectInterceptor(predictor))); | |
| 29 return top_job_factory.Pass(); | |
| 30 } | |
| 31 | |
| 20 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) | 32 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) |
| 21 : timed_cache_(base::TimeDelta::FromSeconds( | 33 : timed_cache_(base::TimeDelta::FromSeconds( |
| 22 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | 34 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), |
| 23 predictor_(predictor) { | 35 predictor_(predictor) { |
| 24 DCHECK(predictor); | 36 DCHECK(predictor); |
| 25 } | 37 } |
| 26 | 38 |
| 27 ConnectInterceptor::~ConnectInterceptor() { | 39 ConnectInterceptor::~ConnectInterceptor() { |
| 28 } | 40 } |
| 29 | 41 |
| 30 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( | 42 net::URLRequestJob* ConnectInterceptor::MaybeCreateJob( |
|
willchan no longer on Chromium
2012/12/11 19:14:38
No, this ConnectInterceptor is all wrong :) When c
pauljensen
2012/12/11 22:28:14
Right now the ConnectionInterceptor is installed i
mmenke
2012/12/13 16:12:04
I think that's fine for now, though should be done
pauljensen
2012/12/13 17:53:30
Sorry, I'm confused.
What is fine for now? Moving
willchan no longer on Chromium
2012/12/13 17:59:53
So, long-term we need to start refactoring ChromeN
pauljensen
2012/12/13 18:21:54
If I move ConnectionInterceptor into the ChromeNet
| |
| 31 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 43 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 32 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 44 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
| 33 if (request_scheme_host == GURL::EmptyGURL()) | 45 if (request_scheme_host == GURL::EmptyGURL()) |
| 34 return NULL; | 46 return NULL; |
| 35 | 47 |
| 36 // Learn what URLs are likely to be needed during next startup. | 48 // Learn what URLs are likely to be needed during next startup. |
| 37 predictor_->LearnAboutInitialNavigation(request_scheme_host); | 49 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
| 38 | 50 |
| 39 bool redirected_host = false; | 51 bool redirected_host = false; |
| 40 if (request->referrer().empty()) { | 52 if (request->referrer().empty()) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 timed_cache_.SetRecentlySeen(request_scheme_host); | 92 timed_cache_.SetRecentlySeen(request_scheme_host); |
| 81 | 93 |
| 82 // Subresources for main frames usually get predicted when we detected the | 94 // Subresources for main frames usually get predicted when we detected the |
| 83 // main frame request - way back in RenderViewHost::Navigate. So only handle | 95 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 84 // predictions now for subresources or for redirected hosts. | 96 // predictions now for subresources or for redirected hosts. |
| 85 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 97 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
| 86 predictor_->PredictFrameSubresources(request_scheme_host); | 98 predictor_->PredictFrameSubresources(request_scheme_host); |
| 87 return NULL; | 99 return NULL; |
| 88 } | 100 } |
| 89 | 101 |
| 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 } | |
| 101 | |
| 102 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 102 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
| 103 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 103 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
| 104 max_duration_(max_duration) { | 104 max_duration_(max_duration) { |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Make Clang compilation happy with explicit destructor. | 107 // Make Clang compilation happy with explicit destructor. |
| 108 ConnectInterceptor::TimedCache::~TimedCache() {} | 108 ConnectInterceptor::TimedCache::~TimedCache() {} |
| 109 | 109 |
| 110 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { | 110 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { |
| 111 DCHECK_EQ(url.GetWithEmptyPath(), url); | 111 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 112 // Evict any overly old entries. | 112 // Evict any overly old entries. |
| 113 base::TimeTicks now = base::TimeTicks::Now(); | 113 base::TimeTicks now = base::TimeTicks::Now(); |
| 114 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 114 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
| 115 while (!mru_cache_.empty()) { | 115 while (!mru_cache_.empty()) { |
| 116 DCHECK(eldest == mru_cache_.rbegin()); | 116 DCHECK(eldest == mru_cache_.rbegin()); |
| 117 if (now - eldest->second < max_duration_) | 117 if (now - eldest->second < max_duration_) |
| 118 break; | 118 break; |
| 119 eldest = mru_cache_.Erase(eldest); | 119 eldest = mru_cache_.Erase(eldest); |
| 120 } | 120 } |
| 121 return mru_cache_.end() != mru_cache_.Peek(url); | 121 return mru_cache_.end() != mru_cache_.Peek(url); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { | 124 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
| 125 DCHECK_EQ(url.GetWithEmptyPath(), url); | 125 DCHECK_EQ(url.GetWithEmptyPath(), url); |
| 126 mru_cache_.Put(url, base::TimeTicks::Now()); | 126 mru_cache_.Put(url, base::TimeTicks::Now()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 } // namespace chrome_browser_net | 129 } // namespace chrome_browser_net |
| OLD | NEW |