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) { |
| 24 DCHECK(predictor); |
23 } | 25 } |
24 | 26 |
25 ConnectInterceptor::~ConnectInterceptor() { | 27 ConnectInterceptor::~ConnectInterceptor() { |
26 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | |
27 } | 28 } |
28 | 29 |
29 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( | 30 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( |
30 net::URLRequest* request) { | 31 net::URLRequest* request) const { |
31 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 32 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
32 if (request_scheme_host == GURL::EmptyGURL()) | 33 if (request_scheme_host == GURL::EmptyGURL()) |
33 return NULL; | 34 return NULL; |
34 | 35 |
35 // Learn what URLs are likely to be needed during next startup. | 36 // Learn what URLs are likely to be needed during next startup. |
36 LearnAboutInitialNavigation(request_scheme_host); | 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 predictor_->LearnFromNavigation(original_scheme_host, |
| 60 request_scheme_host); |
59 } | 61 } |
60 } | 62 } |
61 } | 63 } |
62 } else { | 64 } else { |
63 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); | 65 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); |
64 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); | 66 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); |
65 // Learn about our referring URL, for use in the future. | 67 // Learn about our referring URL, for use in the future. |
66 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 68 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) |
67 LearnFromNavigation(referring_scheme_host, request_scheme_host); | 69 predictor_->LearnFromNavigation(referring_scheme_host, |
| 70 request_scheme_host); |
68 if (referring_scheme_host == request_scheme_host) { | 71 if (referring_scheme_host == request_scheme_host) { |
69 // We've already made any/all predictions when we navigated to the | 72 // We've already made any/all predictions when we navigated to the |
70 // referring host, so we can bail out here. | 73 // referring host, so we can bail out here. |
71 // We don't update the RecentlySeen() time because any preconnections | 74 // 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) | 75 // 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. | 76 // and wouldn't have waited for this current request navigation. |
74 return NULL; | 77 return NULL; |
75 } | 78 } |
76 } | 79 } |
77 timed_cache_.SetRecentlySeen(request_scheme_host); | 80 timed_cache_.SetRecentlySeen(request_scheme_host); |
78 | 81 |
79 // Subresources for main frames usually get predicted when we detected the | 82 // Subresources for main frames usually get predicted when we detected the |
80 // main frame request - way back in RenderViewHost::Navigate. So only handle | 83 // main frame request - way back in RenderViewHost::Navigate. So only handle |
81 // predictions now for subresources or for redirected hosts. | 84 // predictions now for subresources or for redirected hosts. |
82 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 85 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
83 PredictFrameSubresources(request_scheme_host); | 86 predictor_->PredictFrameSubresources(request_scheme_host); |
84 return NULL; | 87 return NULL; |
85 } | 88 } |
86 | 89 |
87 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( | 90 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( |
88 net::URLRequest* request) { | 91 net::URLRequest* request) const { |
89 return NULL; | 92 return NULL; |
90 } | 93 } |
91 | 94 |
92 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( | 95 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( |
93 net::URLRequest* request, | 96 const GURL& location, |
94 const GURL& location) { | 97 net::URLRequest* request) const { |
95 return NULL; | 98 return NULL; |
96 } | 99 } |
97 | 100 |
98 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 101 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
99 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 102 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
100 max_duration_(max_duration) { | 103 max_duration_(max_duration) { |
101 } | 104 } |
102 | 105 |
103 // Make Clang compilation happy with explicit destructor. | 106 // Make Clang compilation happy with explicit destructor. |
104 ConnectInterceptor::TimedCache::~TimedCache() {} | 107 ConnectInterceptor::TimedCache::~TimedCache() {} |
105 | 108 |
106 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) { | 109 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { |
107 DCHECK_EQ(url.GetWithEmptyPath(), url); | 110 DCHECK_EQ(url.GetWithEmptyPath(), url); |
108 // Evict any overly old entries. | 111 // Evict any overly old entries. |
109 base::TimeTicks now = base::TimeTicks::Now(); | 112 base::TimeTicks now = base::TimeTicks::Now(); |
110 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 113 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
111 while (!mru_cache_.empty()) { | 114 while (!mru_cache_.empty()) { |
112 DCHECK(eldest == mru_cache_.rbegin()); | 115 DCHECK(eldest == mru_cache_.rbegin()); |
113 if (now - eldest->second < max_duration_) | 116 if (now - eldest->second < max_duration_) |
114 break; | 117 break; |
115 eldest = mru_cache_.Erase(eldest); | 118 eldest = mru_cache_.Erase(eldest); |
116 } | 119 } |
117 return mru_cache_.end() != mru_cache_.Peek(url); | 120 return mru_cache_.end() != mru_cache_.Peek(url); |
118 } | 121 } |
119 | 122 |
120 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) { | 123 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
121 DCHECK_EQ(url.GetWithEmptyPath(), url); | 124 DCHECK_EQ(url.GetWithEmptyPath(), url); |
122 mru_cache_.Put(url, base::TimeTicks::Now()); | 125 mru_cache_.Put(url, base::TimeTicks::Now()); |
123 } | 126 } |
124 | 127 |
125 } // namespace chrome_browser_net | 128 } // namespace chrome_browser_net |
OLD | NEW |