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_api.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::RegisterRequestInterceptor(this); | 23 predictor_(predictor) { |
23 } | 24 } |
24 | 25 |
25 ConnectInterceptor::~ConnectInterceptor() { | 26 ConnectInterceptor::~ConnectInterceptor() { |
26 net::URLRequest::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 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
37 | 37 |
38 bool redirected_host = false; | 38 bool redirected_host = false; |
39 if (request->referrer().empty()) { | 39 if (request->referrer().empty()) { |
40 if (request->url() != request->original_url()) { | 40 if (request->url() != request->original_url()) { |
41 // This request was completed with a redirect. | 41 // This request was completed with a redirect. |
42 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); | 42 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); |
43 if (request_scheme_host != original_scheme_host) { | 43 if (request_scheme_host != original_scheme_host) { |
44 redirected_host = true; | 44 redirected_host = true; |
45 // Don't learn from redirects that take path as an argument, but do | 45 // 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 | 46 // 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 "/" | 47 // "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 "/" | 48 // 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 | 49 // (1 character long) to decide the redirect is "definitive" and has no |
50 // significant path. | 50 // significant path. |
51 // TODO(jar): It may be ok to learn from all redirects, as the adaptive | 51 // 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 | 52 // system will not respond until several identical redirects have taken |
53 // place. Hence a use of a path (that changes) wouldn't really be | 53 // place. Hence a use of a path (that changes) wouldn't really be |
54 // learned from anyway. | 54 // learned from anyway. |
55 if (request->original_url().path().length() <= 1 && | 55 if (request->original_url().path().length() <= 1 && |
56 timed_cache_.WasRecentlySeen(original_scheme_host)) { | 56 timed_cache_.WasRecentlySeen(original_scheme_host)) { |
57 // TODO(jar): These definite redirects could be learned much faster. | 57 // TODO(jar): These definite redirects could be learned much faster. |
58 LearnFromNavigation(original_scheme_host, request_scheme_host); | 58 predictor_->LearnFromNavigation(original_scheme_host, |
| 59 request_scheme_host); |
59 } | 60 } |
60 } | 61 } |
61 } | 62 } |
62 } else { | 63 } else { |
63 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); | 64 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); |
64 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); | 65 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); |
65 // Learn about our referring URL, for use in the future. | 66 // Learn about our referring URL, for use in the future. |
66 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 67 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) |
67 LearnFromNavigation(referring_scheme_host, request_scheme_host); | 68 predictor_->LearnFromNavigation(referring_scheme_host, |
| 69 request_scheme_host); |
68 if (referring_scheme_host == request_scheme_host) { | 70 if (referring_scheme_host == request_scheme_host) { |
69 // 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 |
70 // referring host, so we can bail out here. | 72 // referring host, so we can bail out here. |
71 // We don't update the RecentlySeen() time because any preconnections | 73 // 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) | 74 // 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. | 75 // and wouldn't have waited for this current request navigation. |
74 return NULL; | 76 return NULL; |
75 } | 77 } |
76 } | 78 } |
77 timed_cache_.SetRecentlySeen(request_scheme_host); | 79 timed_cache_.SetRecentlySeen(request_scheme_host); |
78 | 80 |
79 // Subresources for main frames usually get predicted when we detected the | 81 // Subresources for main frames usually get predicted when we detected the |
80 // main frame request - way back in RenderViewHost::Navigate. So only handle | 82 // main frame request - way back in RenderViewHost::Navigate. So only handle |
81 // predictions now for subresources or for redirected hosts. | 83 // predictions now for subresources or for redirected hosts. |
82 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 84 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
83 PredictFrameSubresources(request_scheme_host); | 85 predictor_->PredictFrameSubresources(request_scheme_host); |
84 return NULL; | 86 return NULL; |
85 } | 87 } |
86 | 88 |
87 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( | 89 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( |
88 net::URLRequest* request) { | 90 net::URLRequest* request) const { |
89 return NULL; | 91 return NULL; |
90 } | 92 } |
91 | 93 |
92 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( | 94 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( |
93 net::URLRequest* request, | 95 const GURL& location, |
94 const GURL& location) { | 96 net::URLRequest* request) const { |
95 return NULL; | 97 return NULL; |
96 } | 98 } |
97 | 99 |
98 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 100 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
99 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 101 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
100 max_duration_(max_duration) { | 102 max_duration_(max_duration) { |
101 } | 103 } |
102 | 104 |
103 // Make Clang compilation happy with explicit destructor. | 105 // Make Clang compilation happy with explicit destructor. |
104 ConnectInterceptor::TimedCache::~TimedCache() {} | 106 ConnectInterceptor::TimedCache::~TimedCache() {} |
105 | 107 |
106 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) { | 108 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { |
107 DCHECK_EQ(url.GetWithEmptyPath(), url); | 109 DCHECK_EQ(url.GetWithEmptyPath(), url); |
108 // Evict any overly old entries. | 110 // Evict any overly old entries. |
109 base::TimeTicks now = base::TimeTicks::Now(); | 111 base::TimeTicks now = base::TimeTicks::Now(); |
110 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 112 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
111 while (!mru_cache_.empty()) { | 113 while (!mru_cache_.empty()) { |
112 DCHECK(eldest == mru_cache_.rbegin()); | 114 DCHECK(eldest == mru_cache_.rbegin()); |
113 if (now - eldest->second < max_duration_) | 115 if (now - eldest->second < max_duration_) |
114 break; | 116 break; |
115 eldest = mru_cache_.Erase(eldest); | 117 eldest = mru_cache_.Erase(eldest); |
116 } | 118 } |
117 return mru_cache_.end() != mru_cache_.Peek(url); | 119 return mru_cache_.end() != mru_cache_.Peek(url); |
118 } | 120 } |
119 | 121 |
120 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) { | 122 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { |
121 DCHECK_EQ(url.GetWithEmptyPath(), url); | 123 DCHECK_EQ(url.GetWithEmptyPath(), url); |
122 mru_cache_.Put(url, base::TimeTicks::Now()); | 124 mru_cache_.Put(url, base::TimeTicks::Now()); |
123 } | 125 } |
124 | 126 |
125 } // namespace chrome_browser_net | 127 } // namespace chrome_browser_net |
OLD | NEW |