| 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" |
| 15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 16 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 18 #include "net/url_request/redirect_info.h" | 19 #include "net/url_request/redirect_info.h" |
| 19 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 20 | 21 |
| 21 using content::BrowserThread; | 22 using content::BrowserThread; |
| 22 using content::ResourceRequestInfo; | 23 using content::ResourceRequestInfo; |
| 23 using content::ResourceType; | 24 using content::ResourceType; |
| 24 | 25 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( | 81 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( |
| 81 PrerenderContents* contents) { | 82 PrerenderContents* contents) { |
| 82 g_prerender_contents_for_testing = contents; | 83 g_prerender_contents_for_testing = contents; |
| 83 } | 84 } |
| 84 | 85 |
| 85 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) | 86 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) |
| 86 : request_(request), | 87 : request_(request), |
| 87 load_flags_(net::LOAD_NORMAL), | 88 load_flags_(net::LOAD_NORMAL), |
| 88 prerender_throttle_info_(new PrerenderThrottleInfo()) {} | 89 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 } |
| 89 | 112 |
| 90 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} | 113 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} |
| 91 | 114 |
| 92 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { | 115 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 const content::ResourceRequestInfo* info = | 117 const content::ResourceRequestInfo* info = |
| 95 content::ResourceRequestInfo::ForRequest(request_); | 118 content::ResourceRequestInfo::ForRequest(request_); |
| 96 *defer = true; | 119 *defer = true; |
| 120 |
| 97 BrowserThread::PostTask( | 121 BrowserThread::PostTask( |
| 98 BrowserThread::UI, FROM_HERE, | 122 BrowserThread::UI, FROM_HERE, |
| 99 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), | 123 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), |
| 100 request_->method(), info->GetResourceType(), request_->url(), | 124 request_->method(), info->GetResourceType(), request_->url(), |
| 101 info->GetWebContentsGetterForRequest(), | 125 info->GetWebContentsGetterForRequest(), |
| 102 prerender_throttle_info_)); | 126 prerender_throttle_info_)); |
| 103 } | 127 } |
| 104 | 128 |
| 105 void PrerenderResourceThrottle::WillRedirectRequest( | 129 void PrerenderResourceThrottle::WillRedirectRequest( |
| 106 const net::RedirectInfo& redirect_info, | 130 const net::RedirectInfo& redirect_info, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 const char* PrerenderResourceThrottle::GetNameForLogging() const { | 166 const char* PrerenderResourceThrottle::GetNameForLogging() const { |
| 143 return "PrerenderResourceThrottle"; | 167 return "PrerenderResourceThrottle"; |
| 144 } | 168 } |
| 145 | 169 |
| 146 void PrerenderResourceThrottle::ResumeHandler() { | 170 void PrerenderResourceThrottle::ResumeHandler() { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 171 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 request_->SetLoadFlags(request_->load_flags() | load_flags_); | 172 request_->SetLoadFlags(request_->load_flags() | load_flags_); |
| 149 Resume(); | 173 Resume(); |
| 150 } | 174 } |
| 151 | 175 |
| 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 |
| 152 // static | 186 // static |
| 153 void PrerenderResourceThrottle::WillStartRequestOnUI( | 187 void PrerenderResourceThrottle::WillStartRequestOnUI( |
| 154 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 188 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
| 155 const std::string& method, | 189 const std::string& method, |
| 156 ResourceType resource_type, | 190 ResourceType resource_type, |
| 157 const GURL& url, | 191 const GURL& url, |
| 158 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | 192 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 159 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { | 193 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { |
| 160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 194 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 161 bool cancel = false; | 195 bool cancel = false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. | 229 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. |
| 196 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); | 230 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 197 ReportUnsupportedPrerenderScheme(url); | 231 ReportUnsupportedPrerenderScheme(url); |
| 198 } | 232 } |
| 199 cancel = true; | 233 cancel = true; |
| 200 #if defined(OS_ANDROID) | 234 #if defined(OS_ANDROID) |
| 201 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { | 235 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { |
| 202 // Delay icon fetching until the contents are getting swapped in | 236 // Delay icon fetching until the contents are getting swapped in |
| 203 // to conserve network usage in mobile devices. | 237 // to conserve network usage in mobile devices. |
| 204 prerender_contents->AddResourceThrottle(throttle); | 238 prerender_contents->AddResourceThrottle(throttle); |
| 239 |
| 240 // No need to call AddIdleResource() on Android. |
| 205 return; | 241 return; |
| 206 #endif | 242 #endif |
| 207 } | 243 } |
| 244 |
| 245 #if !defined(OS_ANDROID) |
| 246 if (!cancel) |
| 247 prerender_contents->AddIdleResource(throttle); |
| 248 #endif |
| 208 } | 249 } |
| 209 | 250 |
| 210 BrowserThread::PostTask( | 251 BrowserThread::PostTask( |
| 211 BrowserThread::IO, FROM_HERE, | 252 BrowserThread::IO, FROM_HERE, |
| 212 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel | 253 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel |
| 213 : &PrerenderResourceThrottle::ResumeHandler, | 254 : &PrerenderResourceThrottle::ResumeHandler, |
| 214 throttle)); | 255 throttle)); |
| 215 } | 256 } |
| 216 | 257 |
| 217 // static | 258 // static |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return g_prerender_contents_for_testing; | 331 return g_prerender_contents_for_testing; |
| 291 return PrerenderContents::FromWebContents(web_contents_getter.Run()); | 332 return PrerenderContents::FromWebContents(web_contents_getter.Run()); |
| 292 } | 333 } |
| 293 | 334 |
| 294 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { | 335 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { |
| 295 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 336 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 296 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; | 337 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; |
| 297 } | 338 } |
| 298 | 339 |
| 299 } // namespace prerender | 340 } // namespace prerender |
| OLD | NEW |