Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/public/browser/resource_request_info.h" | |
| 9 #include "content/public/common/resource_type.h" | |
| 8 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 9 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 10 | 12 |
| 11 namespace chrome_browser_net { | 13 namespace chrome_browser_net { |
| 12 | 14 |
| 13 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) | 15 ConnectInterceptor::ConnectInterceptor(Predictor* predictor) |
| 14 : timed_cache_(base::TimeDelta::FromSeconds( | 16 : timed_cache_(base::TimeDelta::FromSeconds( |
| 15 Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet)), | 17 Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet)), |
| 16 predictor_(predictor) { | 18 predictor_(predictor) { |
| 17 DCHECK(predictor); | 19 DCHECK(predictor); |
| 18 } | 20 } |
| 19 | 21 |
| 20 ConnectInterceptor::~ConnectInterceptor() { | 22 ConnectInterceptor::~ConnectInterceptor() { |
| 21 } | 23 } |
| 22 | 24 |
| 23 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) { | 25 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) { |
| 24 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); | 26 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url())); |
| 25 if (request_scheme_host == GURL::EmptyGURL()) | 27 if (request_scheme_host == GURL::EmptyGURL()) |
| 26 return; | 28 return; |
| 27 | 29 |
| 30 const content::ResourceRequestInfo* info = | |
| 31 content::ResourceRequestInfo::ForRequest(request); | |
| 32 bool is_main_frame = false; | |
| 33 bool is_sub_frame = false; | |
| 34 // TODO(mmenke): Should the predictor really be red requests without a | |
|
davidben
2015/05/12 00:49:47
red -> fed?
mmenke
2015/05/12 16:11:40
Done... I should take the time to skim over CLs b
| |
| 35 // ResourceRequestInfo? | |
| 36 if (info) { | |
| 37 content::ResourceType resoure_type = info->GetResourceType(); | |
|
davidben
2015/05/12 00:49:47
resource_type
mmenke
2015/05/12 16:11:40
Done.
| |
| 38 is_main_frame = (resoure_type == content::RESOURCE_TYPE_MAIN_FRAME); | |
| 39 is_sub_frame = (resoure_type == content::RESOURCE_TYPE_SUB_FRAME); | |
| 40 } | |
| 41 | |
| 28 // Learn what URLs are likely to be needed during next startup. | 42 // Learn what URLs are likely to be needed during next startup. |
| 29 predictor_->LearnAboutInitialNavigation(request_scheme_host); | 43 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
| 30 | 44 |
| 31 bool redirected_host = false; | 45 bool redirected_host = false; |
| 32 bool is_subresource = !(request->load_flags() & net::LOAD_MAIN_FRAME); | |
| 33 if (request->referrer().empty()) { | 46 if (request->referrer().empty()) { |
| 34 if (request->url() != request->original_url()) { | 47 if (request->url() != request->original_url()) { |
| 35 // This request was completed with a redirect. | 48 // This request was completed with a redirect. |
| 36 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); | 49 GURL original_scheme_host(request->original_url().GetWithEmptyPath()); |
| 37 if (request_scheme_host != original_scheme_host) { | 50 if (request_scheme_host != original_scheme_host) { |
| 38 redirected_host = true; | 51 redirected_host = true; |
| 39 // Don't learn from redirects that take path as an argument, but do | 52 // Don't learn from redirects that take path as an argument, but do |
| 40 // learn from short-hand typing entries, such as "cnn.com" redirects to | 53 // learn from short-hand typing entries, such as "cnn.com" redirects to |
| 41 // "www.cnn.com". We can't just check for has_path(), as a mere "/" | 54 // "www.cnn.com". We can't just check for has_path(), as a mere "/" |
| 42 // will count as a path, so we check that the path is at most a "/" | 55 // will count as a path, so we check that the path is at most a "/" |
| 43 // (1 character long) to decide the redirect is "definitive" and has no | 56 // (1 character long) to decide the redirect is "definitive" and has no |
| 44 // significant path. | 57 // significant path. |
| 45 // TODO(jar): It may be ok to learn from all redirects, as the adaptive | 58 // TODO(jar): It may be ok to learn from all redirects, as the adaptive |
| 46 // system will not respond until several identical redirects have taken | 59 // system will not respond until several identical redirects have taken |
| 47 // place. Hence a use of a path (that changes) wouldn't really be | 60 // place. Hence a use of a path (that changes) wouldn't really be |
| 48 // learned from anyway. | 61 // learned from anyway. |
| 49 if (request->original_url().path().length() <= 1 && | 62 if (request->original_url().path().length() <= 1 && |
| 50 timed_cache_.WasRecentlySeen(original_scheme_host)) { | 63 timed_cache_.WasRecentlySeen(original_scheme_host)) { |
| 51 // TODO(jar): These definite redirects could be learned much faster. | 64 // TODO(jar): These definite redirects could be learned much faster. |
| 52 predictor_->LearnFromNavigation(original_scheme_host, | 65 predictor_->LearnFromNavigation(original_scheme_host, |
| 53 request_scheme_host); | 66 request_scheme_host); |
| 54 } | 67 } |
| 55 } | 68 } |
| 56 } | 69 } |
| 57 } else { | 70 } else { |
| 58 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); | 71 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath(); |
| 59 // Learn about our referring URL, for use in the future. | 72 // Learn about our referring URL, for use in the future. |
| 60 if (is_subresource && timed_cache_.WasRecentlySeen(referring_scheme_host)) | 73 if (!is_main_frame && timed_cache_.WasRecentlySeen(referring_scheme_host)) { |
| 61 predictor_->LearnFromNavigation(referring_scheme_host, | 74 predictor_->LearnFromNavigation(referring_scheme_host, |
| 62 request_scheme_host); | 75 request_scheme_host); |
| 76 } | |
| 63 if (referring_scheme_host == request_scheme_host) { | 77 if (referring_scheme_host == request_scheme_host) { |
| 64 // We've already made any/all predictions when we navigated to the | 78 // We've already made any/all predictions when we navigated to the |
| 65 // referring host, so we can bail out here. | 79 // referring host, so we can bail out here. |
| 66 // We don't update the RecentlySeen() time because any preconnections | 80 // We don't update the RecentlySeen() time because any preconnections |
| 67 // need to be made at the first navigation (i.e., when referer was loaded) | 81 // need to be made at the first navigation (i.e., when referer was loaded) |
| 68 // and wouldn't have waited for this current request navigation. | 82 // and wouldn't have waited for this current request navigation. |
| 69 return; | 83 return; |
| 70 } | 84 } |
| 71 } | 85 } |
| 72 timed_cache_.SetRecentlySeen(request_scheme_host); | 86 timed_cache_.SetRecentlySeen(request_scheme_host); |
| 73 | 87 |
| 74 // Subresources for main frames usually get predicted when we detected the | 88 // Subresources for main frames usually get predicted when we detected the |
| 75 // main frame request - way back in RenderViewHost::Navigate. So only handle | 89 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 76 // predictions now for subresources or for redirected hosts. | 90 // predictions now for subresources or for redirected hosts. |
| 77 if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host) | 91 if (is_sub_frame || redirected_host) { |
| 78 predictor_->PredictFrameSubresources(request_scheme_host, | 92 predictor_->PredictFrameSubresources(request_scheme_host, |
| 79 request->first_party_for_cookies()); | 93 request->first_party_for_cookies()); |
| 94 } | |
| 80 return; | 95 return; |
| 81 } | 96 } |
| 82 | 97 |
| 83 } // namespace chrome_browser_net | 98 } // namespace chrome_browser_net |
| OLD | NEW |