| 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 "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/net/predictor.h" |
| 9 #include "chrome/browser/profiles/profile_io_data.h" |
| 10 #include "content/public/browser/resource_request_info.h" |
| 11 #include "content/public/common/resource_type.h" |
| 12 #include "net/url_request/redirect_info.h" |
| 13 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 bool IsNavigationRequest(net::URLRequest* request) { |
| 19 content::ResourceType resource_type = |
| 20 content::ResourceRequestInfo::ForRequest(request)->GetResourceType(); |
| 21 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
| 22 resource_type == content::RESOURCE_TYPE_SUB_FRAME; |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 PredictorResourceThrottle::PredictorResourceThrottle( |
| 28 net::URLRequest* request, |
| 29 chrome_browser_net::Predictor* predictor) |
| 30 : request_(request), predictor_(predictor) {} |
| 31 |
| 32 PredictorResourceThrottle::~PredictorResourceThrottle() {} |
| 33 |
| 34 // static |
| 35 std::unique_ptr<PredictorResourceThrottle> |
| 36 PredictorResourceThrottle::MaybeCreate(net::URLRequest* request, |
| 37 ProfileIOData* io_data) { |
| 38 if (io_data->GetPredictor()) { |
| 39 return base::WrapUnique( |
| 40 new PredictorResourceThrottle(request, io_data->GetPredictor())); |
| 41 } |
| 42 return nullptr; |
| 43 } |
| 44 |
| 45 void PredictorResourceThrottle::WillStartRequest(bool* defer) { |
| 46 GURL request_scheme_host( |
| 47 chrome_browser_net::Predictor::CanonicalizeUrl(request_->url())); |
| 48 if (request_scheme_host.is_empty()) |
| 49 return; |
| 50 |
| 51 // Learn what URLs are likely to be needed during next startup. |
| 52 // TODO(csharrison): Rename this method, as this code path is used for more |
| 53 // than just navigation requests. |
| 54 predictor_->LearnAboutInitialNavigation(request_scheme_host); |
| 55 |
| 56 const content::ResourceRequestInfo* info = |
| 57 content::ResourceRequestInfo::ForRequest(request_); |
| 58 DCHECK(info); |
| 59 content::ResourceType resource_type = info->GetResourceType(); |
| 60 const GURL& referring_scheme_host = |
| 61 GURL(request_->referrer()).GetWithEmptyPath(); |
| 62 |
| 63 // Learn about our referring URL, for use in the future. Only learn |
| 64 // subresource relationships. |
| 65 if (!referring_scheme_host.is_empty() && |
| 66 resource_type != content::RESOURCE_TYPE_MAIN_FRAME && |
| 67 predictor_->timed_cache()->WasRecentlySeen(referring_scheme_host)) { |
| 68 predictor_->LearnFromNavigation(referring_scheme_host, request_scheme_host); |
| 69 } |
| 70 |
| 71 // Subresources for main frames are predicted when navigation starts, in |
| 72 // PredictorTabHelper, so only handle predictions for subframes here. |
| 73 // If the referring host is equal to the request host, then the predictor has |
| 74 // already made any/all predictions when navigating to the referring host. |
| 75 // Don't update the RecentlySeen() time because the timed cache is already |
| 76 // populated (with the correct timeout) based on the initial navigation. |
| 77 if (IsNavigationRequest(request_) && |
| 78 referring_scheme_host != request_scheme_host) { |
| 79 predictor_->timed_cache()->SetRecentlySeen(request_scheme_host); |
| 80 if (resource_type == content::RESOURCE_TYPE_SUB_FRAME) { |
| 81 predictor_->PredictFrameSubresources(request_scheme_host, |
| 82 request_->first_party_for_cookies()); |
| 83 } |
| 84 } |
| 85 } |
| 86 |
| 87 void PredictorResourceThrottle::WillRedirectRequest( |
| 88 const net::RedirectInfo& redirect_info, |
| 89 bool* defer) { |
| 90 GURL new_scheme_host( |
| 91 chrome_browser_net::Predictor::CanonicalizeUrl(redirect_info.new_url)); |
| 92 GURL original_scheme_host(request_->original_url().GetWithEmptyPath()); |
| 93 // Note: This is comparing a canonicalizd URL with a non-canonicalized URL. |
| 94 // This should be fixed and the idea of canonicalization revisited. |
| 95 if (new_scheme_host == original_scheme_host || new_scheme_host.is_empty()) |
| 96 return; |
| 97 // Don't learn or predict subresource redirects. |
| 98 if (!IsNavigationRequest(request_)) |
| 99 return; |
| 100 |
| 101 // Don't learn from redirects that take path as an argument, but do |
| 102 // learn from short-hand typing entries, such as "cnn.com" redirects to |
| 103 // "www.cnn.com". We can't just check for has_path(), as a mere "/" |
| 104 // will count as a path, so we check that the path is at most a "/" |
| 105 // (1 character long) to decide the redirect is "definitive" and has no |
| 106 // significant path. |
| 107 // TODO(jar): It may be ok to learn from all redirects, as the adaptive |
| 108 // system will not respond until several identical redirects have taken |
| 109 // place. Hence a use of a path (that changes) wouldn't really be |
| 110 // learned from anyway. |
| 111 if (request_->original_url().path().length() <= 1 && |
| 112 predictor_->timed_cache()->WasRecentlySeen(original_scheme_host)) { |
| 113 // TODO(jar): These definite redirects could be learned much faster. |
| 114 predictor_->LearnFromNavigation(original_scheme_host, new_scheme_host); |
| 115 } |
| 116 |
| 117 predictor_->timed_cache()->SetRecentlySeen(new_scheme_host); |
| 118 predictor_->PredictFrameSubresources( |
| 119 new_scheme_host, redirect_info.new_first_party_for_cookies); |
| 120 } |
| 121 |
| 122 const char* PredictorResourceThrottle::GetNameForLogging() const { |
| 123 return "PredictorResourceThrottle"; |
| 124 } |
| OLD | NEW |