| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/prerender/prerender_resource_throttle.h" | 5 #include "chrome/browser/prerender/prerender_resource_throttle.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/prerender/prerender_final_status.h" | 11 #include "chrome/browser/prerender/prerender_final_status.h" |
| 12 #include "chrome/browser/prerender/prerender_manager.h" | 12 #include "chrome/browser/prerender/prerender_manager.h" |
| 13 #include "chrome/browser/prerender/prerender_util.h" | 13 #include "chrome/browser/prerender/prerender_util.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/resource_dispatcher_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 17 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 18 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 19 #include "net/url_request/redirect_info.h" | 18 #include "net/url_request/redirect_info.h" |
| 20 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 21 | 20 |
| 22 using content::BrowserThread; | 21 using content::BrowserThread; |
| 23 using content::ResourceRequestInfo; | 22 using content::ResourceRequestInfo; |
| 24 using content::ResourceType; | 23 using content::ResourceType; |
| 25 | 24 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 }; | 78 }; |
| 80 | 79 |
| 81 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( | 80 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( |
| 82 PrerenderContents* contents) { | 81 PrerenderContents* contents) { |
| 83 g_prerender_contents_for_testing = contents; | 82 g_prerender_contents_for_testing = contents; |
| 84 } | 83 } |
| 85 | 84 |
| 86 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) | 85 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) |
| 87 : request_(request), | 86 : request_(request), |
| 88 load_flags_(net::LOAD_NORMAL), | 87 load_flags_(net::LOAD_NORMAL), |
| 89 prerender_throttle_info_(new PrerenderThrottleInfo()) { | 88 prerender_throttle_info_(new PrerenderThrottleInfo()) {} |
| 90 // Priorities for prerendering requests are lowered, to avoid competing with | |
| 91 // other page loads, except on Android where this is less likely to be a | |
| 92 // problem. In some cases, this may negatively impact the performance of | |
| 93 // prerendering, see https://crbug.com/652746 for details. | |
| 94 #if !defined(OS_ANDROID) | |
| 95 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs) | |
| 96 // should remain at MAXIMUM_PRIORITY. | |
| 97 if (request_->load_flags() & net::LOAD_IGNORE_LIMITS) { | |
| 98 DCHECK_EQ(request_->priority(), net::MAXIMUM_PRIORITY); | |
| 99 } else if (request_->priority() != net::IDLE) { | |
| 100 original_request_priority_ = request_->priority(); | |
| 101 // In practice, the resource scheduler does not know about the request yet, | |
| 102 // and it falls back to calling request_->SetPriority(), so it would be | |
| 103 // possible to do just that here. It is cleaner and more robust to go | |
| 104 // through the resource dispatcher host though. | |
| 105 if (content::ResourceDispatcherHost::Get()) { | |
| 106 content::ResourceDispatcherHost::Get()->ReprioritizeRequest(request_, | |
| 107 net::IDLE); | |
| 108 } | |
| 109 } | |
| 110 #endif // OS_ANDROID | |
| 111 } | |
| 112 | 89 |
| 113 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} | 90 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} |
| 114 | 91 |
| 115 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { | 92 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 117 const content::ResourceRequestInfo* info = | 94 const content::ResourceRequestInfo* info = |
| 118 content::ResourceRequestInfo::ForRequest(request_); | 95 content::ResourceRequestInfo::ForRequest(request_); |
| 119 *defer = true; | 96 *defer = true; |
| 120 | |
| 121 BrowserThread::PostTask( | 97 BrowserThread::PostTask( |
| 122 BrowserThread::UI, FROM_HERE, | 98 BrowserThread::UI, FROM_HERE, |
| 123 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), | 99 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), |
| 124 request_->method(), info->GetResourceType(), request_->url(), | 100 request_->method(), info->GetResourceType(), request_->url(), |
| 125 info->GetWebContentsGetterForRequest(), | 101 info->GetWebContentsGetterForRequest(), |
| 126 prerender_throttle_info_)); | 102 prerender_throttle_info_)); |
| 127 } | 103 } |
| 128 | 104 |
| 129 void PrerenderResourceThrottle::WillRedirectRequest( | 105 void PrerenderResourceThrottle::WillRedirectRequest( |
| 130 const net::RedirectInfo& redirect_info, | 106 const net::RedirectInfo& redirect_info, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 const char* PrerenderResourceThrottle::GetNameForLogging() const { | 142 const char* PrerenderResourceThrottle::GetNameForLogging() const { |
| 167 return "PrerenderResourceThrottle"; | 143 return "PrerenderResourceThrottle"; |
| 168 } | 144 } |
| 169 | 145 |
| 170 void PrerenderResourceThrottle::ResumeHandler() { | 146 void PrerenderResourceThrottle::ResumeHandler() { |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 172 request_->SetLoadFlags(request_->load_flags() | load_flags_); | 148 request_->SetLoadFlags(request_->load_flags() | load_flags_); |
| 173 Resume(); | 149 Resume(); |
| 174 } | 150 } |
| 175 | 151 |
| 176 void PrerenderResourceThrottle::ResetResourcePriority() { | |
| 177 if (!original_request_priority_) | |
| 178 return; | |
| 179 | |
| 180 if (content::ResourceDispatcherHost::Get()) { | |
| 181 content::ResourceDispatcherHost::Get()->ReprioritizeRequest( | |
| 182 request_, original_request_priority_.value()); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 // static | 152 // static |
| 187 void PrerenderResourceThrottle::WillStartRequestOnUI( | 153 void PrerenderResourceThrottle::WillStartRequestOnUI( |
| 188 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 154 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
| 189 const std::string& method, | 155 const std::string& method, |
| 190 ResourceType resource_type, | 156 ResourceType resource_type, |
| 191 const GURL& url, | 157 const GURL& url, |
| 192 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | 158 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 193 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { | 159 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 160 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 195 bool cancel = false; | 161 bool cancel = false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. | 195 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. |
| 230 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); | 196 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 231 ReportUnsupportedPrerenderScheme(url); | 197 ReportUnsupportedPrerenderScheme(url); |
| 232 } | 198 } |
| 233 cancel = true; | 199 cancel = true; |
| 234 #if defined(OS_ANDROID) | 200 #if defined(OS_ANDROID) |
| 235 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { | 201 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { |
| 236 // Delay icon fetching until the contents are getting swapped in | 202 // Delay icon fetching until the contents are getting swapped in |
| 237 // to conserve network usage in mobile devices. | 203 // to conserve network usage in mobile devices. |
| 238 prerender_contents->AddResourceThrottle(throttle); | 204 prerender_contents->AddResourceThrottle(throttle); |
| 239 | |
| 240 // No need to call AddIdleResource() on Android. | |
| 241 return; | 205 return; |
| 242 #endif | 206 #endif |
| 243 } | 207 } |
| 244 | |
| 245 #if !defined(OS_ANDROID) | |
| 246 if (!cancel) | |
| 247 prerender_contents->AddIdleResource(throttle); | |
| 248 #endif | |
| 249 } | 208 } |
| 250 | 209 |
| 251 BrowserThread::PostTask( | 210 BrowserThread::PostTask( |
| 252 BrowserThread::IO, FROM_HERE, | 211 BrowserThread::IO, FROM_HERE, |
| 253 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel | 212 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel |
| 254 : &PrerenderResourceThrottle::ResumeHandler, | 213 : &PrerenderResourceThrottle::ResumeHandler, |
| 255 throttle)); | 214 throttle)); |
| 256 } | 215 } |
| 257 | 216 |
| 258 // static | 217 // static |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 return g_prerender_contents_for_testing; | 290 return g_prerender_contents_for_testing; |
| 332 return PrerenderContents::FromWebContents(web_contents_getter.Run()); | 291 return PrerenderContents::FromWebContents(web_contents_getter.Run()); |
| 333 } | 292 } |
| 334 | 293 |
| 335 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { | 294 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 295 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 337 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; | 296 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; |
| 338 } | 297 } |
| 339 | 298 |
| 340 } // namespace prerender | 299 } // namespace prerender |
| OLD | NEW |