| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/renderer_host/predictor_resource_throttle.h" |
| 6 |
| 7 #include "chrome/browser/net/predictor.h" |
| 8 #include "content/public/browser/resource_request_info.h" |
| 9 #include "net/url_request/redirect_info.h" |
| 10 #include "net/url_request/url_request.h" |
| 11 #include "url/gurl.h" |
| 12 |
| 13 PredictorResourceThrottle::PredictorResourceThrottle( |
| 14 net::URLRequest* request, |
| 15 chrome_browser_net::Predictor* predictor) |
| 16 : request_(request), predictor_(predictor) {} |
| 17 |
| 18 PredictorResourceThrottle::~PredictorResourceThrottle() {} |
| 19 |
| 20 // Hooks into URLRequests starting to initiate subframe predictions, and to |
| 21 // learn referrer relationships. |
| 22 void PredictorResourceThrottle::WillStartRequest(bool* defer) { |
| 23 GURL request_scheme_host( |
| 24 chrome_browser_net::Predictor::CanonicalizeUrl(request_->url())); |
| 25 if (request_scheme_host.is_empty()) |
| 26 return; |
| 27 |
| 28 // Learn what URLs are likely to be needed during next startup. |
| 29 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
| 30 |
| 31 const content::ResourceRequestInfo* info = |
| 32 content::ResourceRequestInfo::ForRequest(request_); |
| 33 DCHECK(info); |
| 34 content::ResourceType resource_type = info->GetResourceType(); |
| 35 const GURL& referring_scheme_host = |
| 36 GURL(request_->referrer()).GetWithEmptyPath(); |
| 37 |
| 38 // Learn about our referring URL, for use in the future. Only learn |
| 39 // subresource relationships. |
| 40 if (!referring_scheme_host.is_empty() && |
| 41 resource_type != content::RESOURCE_TYPE_MAIN_FRAME && |
| 42 predictor_->timed_cache()->WasRecentlySeen(referring_scheme_host)) { |
| 43 predictor_->LearnFromNavigation(referring_scheme_host, request_scheme_host); |
| 44 } |
| 45 |
| 46 // We've already made any/all predictions when we navigated to the referring |
| 47 // host, so we can bail out here. We don't update the RecentlySeen() time |
| 48 // because the timed cache is already populated (with the correct timeout) |
| 49 // based on the initial navigation. |
| 50 if (referring_scheme_host == request_scheme_host) |
| 51 return; |
| 52 |
| 53 predictor_->timed_cache()->SetRecentlySeen(request_scheme_host); |
| 54 |
| 55 // Subresources for main frames usually get predicted when we detected the |
| 56 // main frame request - way back in RenderViewHost::Navigate. So only handle |
| 57 // predsctions now for subframes. |
| 58 predictor_->timed_cache()->SetRecentlySeen(request_scheme_host); |
| 59 if (resource_type == content::RESOURCE_TYPE_SUB_FRAME) { |
| 60 predictor_->PredictFrameSubresources(request_scheme_host, |
| 61 request_->first_party_for_cookies()); |
| 62 } |
| 63 } |
| 64 |
| 65 // Hook into redirect notifications to learn and initiate predictions based on |
| 66 // the redirected url. |
| 67 void PredictorResourceThrottle::WillRedirectRequest( |
| 68 const net::RedirectInfo& redirect_info, |
| 69 bool* defer) { |
| 70 GURL new_scheme_host( |
| 71 chrome_browser_net::Predictor::CanonicalizeUrl(redirect_info.new_url)); |
| 72 GURL original_scheme_host(request_->original_url().GetWithEmptyPath()); |
| 73 if (new_scheme_host == original_scheme_host || new_scheme_host.is_empty()) |
| 74 return; |
| 75 // Don't learn from redirects that take path as an argument, but do |
| 76 // learn from short-hand typing entries, such as "cnn.com" redirects to |
| 77 // "www.cnn.com". We can't just check for has_path(), as a mere "/" |
| 78 // will count as a path, so we check that the path is at most a "/" |
| 79 // (1 character long) to decide the redirect is "definitive" and has no |
| 80 // significant path. |
| 81 // TODO(jar): It may be ok to learn from all redirects, as the adaptive |
| 82 // system will not respond until several identical redirects have taken |
| 83 // place. Hence a use of a path (that changes) wouldn't really be |
| 84 // learned from anyway. |
| 85 if (request_->original_url().path().length() <= 1 && |
| 86 predictor_->timed_cache()->WasRecentlySeen(original_scheme_host)) { |
| 87 // TODO(jar): These definite redirects could be learned much faster. |
| 88 predictor_->LearnFromNavigation(original_scheme_host, new_scheme_host); |
| 89 } |
| 90 predictor_->timed_cache()->SetRecentlySeen(new_scheme_host); |
| 91 predictor_->PredictFrameSubresources( |
| 92 new_scheme_host, redirect_info.new_first_party_for_cookies); |
| 93 } |
| 94 |
| 95 const char* PredictorResourceThrottle::GetNameForLogging() const { |
| 96 return "PredictorResourceThrottle"; |
| 97 } |
| OLD | NEW |