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_api.h" |
8 #include "net/base/load_flags.h" | 8 #include "net/base/load_flags.h" |
9 #include "net/url_request/url_request.h" | |
10 | 9 |
11 namespace chrome_browser_net { | 10 namespace chrome_browser_net { |
12 | 11 |
13 // We don't bother learning to preconnect via a GET if the original URL | 12 // 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 | 13 // 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 | 14 // anyway. We believe most servers will drop the connection in 10 seconds, so |
16 // we currently estimate this time-till-drop at 10 seconds. | 15 // we currently estimate this time-till-drop at 10 seconds. |
17 // TODO(jar): We should do a persistent field trial to validate/optimize this. | 16 // TODO(jar): We should do a persistent field trial to validate/optimize this. |
18 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; | 17 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet = 10; |
19 | 18 |
20 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) | 19 ConnectInterceptor::ConnectInterceptor() |
21 : timed_cache_(base::TimeDelta::FromSeconds( | 20 : timed_cache_(base::TimeDelta::FromSeconds( |
22 kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | 21 kMaxUnusedSocketLifetimeSecondsWithoutAGet)) { |
23 predictor_(predictor) { | 22 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); |
24 DCHECK(predictor); | |
25 } | 23 } |
26 | 24 |
27 ConnectInterceptor::~ConnectInterceptor() { | 25 ConnectInterceptor::~ConnectInterceptor() { |
| 26 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
28 } | 27 } |
29 | 28 |
30 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( | 29 net::URLRequestJob* ConnectInterceptor::MaybeIntercept( |
31 net::URLRequest* request) const { | 30 net::URLRequest* request) { |
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 NULL; |
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 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) { |
45 redirected_host = true; | 44 redirected_host = true; |
46 // 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 |
47 // 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 |
48 // "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 "/" |
49 // 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 "/" |
50 // (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 |
51 // significant path. | 50 // significant path. |
52 // 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 |
53 // system will not respond until several identical redirects have taken | 52 // system will not respond until several identical redirects have taken |
54 // 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 |
55 // learned from anyway. | 54 // learned from anyway. |
56 if (request->original_url().path().length() <= 1 && | 55 if (request->original_url().path().length() <= 1 && |
57 timed_cache_.WasRecentlySeen(original_scheme_host)) { | 56 timed_cache_.WasRecentlySeen(original_scheme_host)) { |
58 // TODO(jar): These definite redirects could be learned much faster. | 57 // TODO(jar): These definite redirects could be learned much faster. |
59 predictor_->LearnFromNavigation(original_scheme_host, | 58 LearnFromNavigation(original_scheme_host, request_scheme_host); |
60 request_scheme_host); | |
61 } | 59 } |
62 } | 60 } |
63 } | 61 } |
64 } else { | 62 } else { |
65 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); | 63 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); |
66 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); | 64 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); |
67 // Learn about our referring URL, for use in the future. | 65 // Learn about our referring URL, for use in the future. |
68 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 66 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) |
69 predictor_->LearnFromNavigation(referring_scheme_host, | 67 LearnFromNavigation(referring_scheme_host, request_scheme_host); |
70 request_scheme_host); | |
71 if (referring_scheme_host == request_scheme_host) { | 68 if (referring_scheme_host == request_scheme_host) { |
72 // We've already made any/all predictions when we navigated to the | 69 // We've already made any/all predictions when we navigated to the |
73 // referring host, so we can bail out here. | 70 // referring host, so we can bail out here. |
74 // We don't update the RecentlySeen() time because any preconnections | 71 // 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) | 72 // 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. | 73 // and wouldn't have waited for this current request navigation. |
77 return NULL; | 74 return NULL; |
78 } | 75 } |
79 } | 76 } |
80 timed_cache_.SetRecentlySeen(request_scheme_host); | 77 timed_cache_.SetRecentlySeen(request_scheme_host); |
81 | 78 |
82 // Subresources for main frames usually get predicted when we detected the | 79 // Subresources for main frames usually get predicted when we detected the |
83 // main frame request - way back in RenderViewHost::Navigate. So only handle | 80 // main frame request - way back in RenderViewHost::Navigate. So only handle |
84 // predictions now for subresources or for redirected hosts. | 81 // predictions now for subresources or for redirected hosts. |
85 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 82 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) |
86 predictor_->PredictFrameSubresources(request_scheme_host); | 83 PredictFrameSubresources(request_scheme_host); |
87 return NULL; | 84 return NULL; |
88 } | 85 } |
89 | 86 |
90 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( | 87 net::URLRequestJob* ConnectInterceptor::MaybeInterceptResponse( |
91 net::URLRequest* request) const { | 88 net::URLRequest* request) { |
92 return NULL; | 89 return NULL; |
93 } | 90 } |
94 | 91 |
95 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( | 92 net::URLRequestJob* ConnectInterceptor::MaybeInterceptRedirect( |
96 const GURL& location, | 93 net::URLRequest* request, |
97 net::URLRequest* request) const { | 94 const GURL& location) { |
98 return NULL; | 95 return NULL; |
99 } | 96 } |
100 | 97 |
101 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) | 98 ConnectInterceptor::TimedCache::TimedCache(const base::TimeDelta& max_duration) |
102 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), | 99 : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT), |
103 max_duration_(max_duration) { | 100 max_duration_(max_duration) { |
104 } | 101 } |
105 | 102 |
106 // Make Clang compilation happy with explicit destructor. | 103 // Make Clang compilation happy with explicit destructor. |
107 ConnectInterceptor::TimedCache::~TimedCache() {} | 104 ConnectInterceptor::TimedCache::~TimedCache() {} |
108 | 105 |
109 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) const { | 106 bool ConnectInterceptor::TimedCache::WasRecentlySeen(const GURL& url) { |
110 DCHECK_EQ(url.GetWithEmptyPath(), url); | 107 DCHECK_EQ(url.GetWithEmptyPath(), url); |
111 // Evict any overly old entries. | 108 // Evict any overly old entries. |
112 base::TimeTicks now = base::TimeTicks::Now(); | 109 base::TimeTicks now = base::TimeTicks::Now(); |
113 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); | 110 UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin(); |
114 while (!mru_cache_.empty()) { | 111 while (!mru_cache_.empty()) { |
115 DCHECK(eldest == mru_cache_.rbegin()); | 112 DCHECK(eldest == mru_cache_.rbegin()); |
116 if (now - eldest->second < max_duration_) | 113 if (now - eldest->second < max_duration_) |
117 break; | 114 break; |
118 eldest = mru_cache_.Erase(eldest); | 115 eldest = mru_cache_.Erase(eldest); |
119 } | 116 } |
120 return mru_cache_.end() != mru_cache_.Peek(url); | 117 return mru_cache_.end() != mru_cache_.Peek(url); |
121 } | 118 } |
122 | 119 |
123 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) const { | 120 void ConnectInterceptor::TimedCache::SetRecentlySeen(const GURL& url) { |
124 DCHECK_EQ(url.GetWithEmptyPath(), url); | 121 DCHECK_EQ(url.GetWithEmptyPath(), url); |
125 mru_cache_.Put(url, base::TimeTicks::Now()); | 122 mru_cache_.Put(url, base::TimeTicks::Now()); |
126 } | 123 } |
127 | 124 |
128 } // namespace chrome_browser_net | 125 } // namespace chrome_browser_net |
OLD | NEW |