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. | |
| 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 predictor_->LearnAboutInitialNavigation(request_scheme_host); | |
|
mmenke
2016/05/24 20:27:34
Not for this CL, but this is being called for non-
mmenke
2016/05/24 20:27:34
Also, worth noting there's a change in behavior he
mmenke
2016/05/24 21:39:31
Worth noting that SDCH dictionary requests do not
Charlie Harrison
2016/05/24 21:43:22
Added a TODO.
Charlie Harrison
2016/05/24 21:43:22
Okay thanks for the explanation. It seems like thi
mmenke
2016/05/25 14:57:48
Most internal requests won't have referrers, the q
Charlie Harrison
2016/05/25 15:01:08
Gotcha. "LearnAboutInitialNavigation" doesn't care
mmenke
2016/05/25 15:21:20
The rest of this code does, however...
If there a
mmenke
2016/05/25 15:24:41
And Charles pointed out to me that this is not an
| |
| 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 // We've already made any/all predictions when we navigated to the referring | |
| 72 // host, so we can bail out here. We don't update the RecentlySeen() time | |
| 73 // because the timed cache is already populated (with the correct timeout) | |
| 74 // based on the initial navigation. | |
|
mmenke
2016/05/24 20:27:34
This comment assumes this is a navigation request,
Charlie Harrison
2016/05/24 21:43:22
Done.
| |
| 75 if (referring_scheme_host == request_scheme_host) | |
| 76 return; | |
| 77 | |
| 78 // Subresources for main frames are predicted when navigation starts, in | |
| 79 // PredictorTabHelper, so only handle predictions for subframes here. | |
| 80 // TODO(csharrison): When the PreconnectMore experiment is over, move all | |
| 81 // prediction dispatching to the tab helper. | |
| 82 if (IsNavigationRequest(request_)) { | |
| 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/24 20:27:34
Hrm...I kinda wonder if it's possible for "new_sch
Charlie Harrison
2016/05/24 21:43:22
Acknowledged.
| |
| 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 |