Chromium Code Reviews| 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 // Hooks into URLRequests starting to initiate predictions based on subframe | |
| 46 // requests, and to learn referrer relationships. | |
|
mmenke
2016/05/25 18:16:04
I don't think either of these method-level comment
Charlie Harrison
2016/05/25 18:45:15
Done.
| |
| 47 void PredictorResourceThrottle::WillStartRequest(bool* defer) { | |
| 48 GURL request_scheme_host( | |
| 49 chrome_browser_net::Predictor::CanonicalizeUrl(request_->url())); | |
| 50 if (request_scheme_host.is_empty()) | |
| 51 return; | |
| 52 | |
| 53 // Learn what URLs are likely to be needed during next startup. | |
| 54 // TODO(csharrison): Rename this method, as this code path is used for more | |
| 55 // than just navigation requests. | |
| 56 predictor_->LearnAboutInitialNavigation(request_scheme_host); | |
| 57 | |
| 58 const content::ResourceRequestInfo* info = | |
| 59 content::ResourceRequestInfo::ForRequest(request_); | |
| 60 DCHECK(info); | |
| 61 content::ResourceType resource_type = info->GetResourceType(); | |
| 62 const GURL& referring_scheme_host = | |
| 63 GURL(request_->referrer()).GetWithEmptyPath(); | |
| 64 | |
| 65 // Learn about our referring URL, for use in the future. Only learn | |
| 66 // subresource relationships. | |
| 67 if (!referring_scheme_host.is_empty() && | |
| 68 resource_type != content::RESOURCE_TYPE_MAIN_FRAME && | |
| 69 predictor_->timed_cache()->WasRecentlySeen(referring_scheme_host)) { | |
| 70 predictor_->LearnFromNavigation(referring_scheme_host, request_scheme_host); | |
| 71 } | |
| 72 | |
| 73 // Subresources for main frames are predicted when navigation starts, in | |
| 74 // PredictorTabHelper, so only handle predictions for subframes here. | |
| 75 // TODO(csharrison): When the PreconnectMore experiment is over, move all | |
| 76 // prediction dispatching to the tab helper. | |
|
mmenke
2016/05/25 18:16:04
Having a TODO in the middle of two informational c
Charlie Harrison
2016/05/25 18:45:16
Done.
| |
| 77 // If the referring host is equal to the request host, then the predictor has | |
| 78 // already made any/all predictions when navigating to the referring host. | |
| 79 // Don't update the RecentlySeen() time because the timed cache is already | |
| 80 // populated (with the correct timeout) based on the initial navigation. | |
| 81 if (IsNavigationRequest(request_) && | |
| 82 referring_scheme_host != request_scheme_host) { | |
| 83 predictor_->timed_cache()->SetRecentlySeen(request_scheme_host); | |
| 84 if (resource_type == content::RESOURCE_TYPE_SUB_FRAME) { | |
| 85 predictor_->PredictFrameSubresources(request_scheme_host, | |
| 86 request_->first_party_for_cookies()); | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // Hooks into redirect notifications to learn and initiates predictions based on | |
| 92 // the redirected url. | |
| 93 void PredictorResourceThrottle::WillRedirectRequest( | |
| 94 const net::RedirectInfo& redirect_info, | |
| 95 bool* defer) { | |
| 96 GURL new_scheme_host( | |
| 97 chrome_browser_net::Predictor::CanonicalizeUrl(redirect_info.new_url)); | |
| 98 GURL original_scheme_host(request_->original_url().GetWithEmptyPath()); | |
| 99 if (new_scheme_host == original_scheme_host || new_scheme_host.is_empty()) | |
|
mmenke
2016/05/25 18:16:04
BUG: We're comparing a non-canonicalized URL with
Charlie Harrison
2016/05/25 18:45:16
Added a note.
| |
| 100 return; | |
| 101 // Don't learn or predict subresource redirects. | |
| 102 if (!IsNavigationRequest(request_)) | |
| 103 return; | |
| 104 | |
| 105 // Don't learn from redirects that take path as an argument, but do | |
| 106 // learn from short-hand typing entries, such as "cnn.com" redirects to | |
| 107 // "www.cnn.com". We can't just check for has_path(), as a mere "/" | |
| 108 // will count as a path, so we check that the path is at most a "/" | |
| 109 // (1 character long) to decide the redirect is "definitive" and has no | |
| 110 // significant path. | |
| 111 // TODO(jar): It may be ok to learn from all redirects, as the adaptive | |
| 112 // system will not respond until several identical redirects have taken | |
| 113 // place. Hence a use of a path (that changes) wouldn't really be | |
| 114 // learned from anyway. | |
| 115 if (request_->original_url().path().length() <= 1 && | |
| 116 predictor_->timed_cache()->WasRecentlySeen(original_scheme_host)) { | |
| 117 // TODO(jar): These definite redirects could be learned much faster. | |
| 118 predictor_->LearnFromNavigation(original_scheme_host, new_scheme_host); | |
| 119 } | |
| 120 | |
| 121 predictor_->timed_cache()->SetRecentlySeen(new_scheme_host); | |
| 122 predictor_->PredictFrameSubresources( | |
| 123 new_scheme_host, redirect_info.new_first_party_for_cookies); | |
| 124 } | |
| 125 | |
| 126 const char* PredictorResourceThrottle::GetNameForLogging() const { | |
| 127 return "PredictorResourceThrottle"; | |
| 128 } | |
| OLD | NEW |