Chromium Code Reviews| 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/appcache_service.h" | |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.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; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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()) {} |
| 89 | 90 |
| 90 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} | 91 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} |
| 91 | 92 |
| 92 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { | 93 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 const content::ResourceRequestInfo* info = | 95 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 95 content::ResourceRequestInfo::ForRequest(request_); | |
| 96 *defer = true; | 96 *defer = true; |
| 97 BrowserThread::PostTask( | 97 BrowserThread::PostTask( |
| 98 BrowserThread::UI, FROM_HERE, | 98 BrowserThread::UI, FROM_HERE, |
| 99 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), | 99 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), |
| 100 request_->method(), info->GetResourceType(), request_->url(), | 100 request_->method(), info->GetResourceType(), request_->url(), |
| 101 info->GetWebContentsGetterForRequest(), | 101 info->GetWebContentsGetterForRequest(), |
| 102 prerender_throttle_info_)); | 102 prerender_throttle_info_)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void PrerenderResourceThrottle::WillRedirectRequest( | 105 void PrerenderResourceThrottle::WillRedirectRequest( |
| 106 const net::RedirectInfo& redirect_info, | 106 const net::RedirectInfo& redirect_info, |
| 107 bool* defer) { | 107 bool* defer) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 const content::ResourceRequestInfo* info = | 109 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 110 content::ResourceRequestInfo::ForRequest(request_); | |
| 111 *defer = true; | 110 *defer = true; |
| 112 std::string header; | 111 std::string header; |
| 113 request_->GetResponseHeaderByName(kFollowOnlyWhenPrerenderShown, &header); | 112 request_->GetResponseHeaderByName(kFollowOnlyWhenPrerenderShown, &header); |
| 114 | 113 |
| 115 BrowserThread::PostTask( | 114 BrowserThread::PostTask( |
| 116 BrowserThread::UI, FROM_HERE, | 115 BrowserThread::UI, FROM_HERE, |
| 117 base::Bind(&PrerenderResourceThrottle::WillRedirectRequestOnUI, | 116 base::Bind(&PrerenderResourceThrottle::WillRedirectRequestOnUI, |
| 118 AsWeakPtr(), header, info->GetResourceType(), info->IsAsync(), | 117 AsWeakPtr(), header, info->GetResourceType(), info->IsAsync(), |
| 119 IsNoStoreResponse(*request_), redirect_info.new_url, | 118 IsNoStoreResponse(*request_), redirect_info.new_url, |
| 120 info->GetWebContentsGetterForRequest())); | 119 info->GetWebContentsGetterForRequest())); |
| 121 } | 120 } |
| 122 | 121 |
| 123 void PrerenderResourceThrottle::WillProcessResponse(bool* defer) { | 122 void PrerenderResourceThrottle::WillProcessResponse(bool* defer) { |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 125 const content::ResourceRequestInfo* info = | 124 |
| 126 content::ResourceRequestInfo::ForRequest(request_); | 125 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 127 if (!info) | 126 if (!info) |
| 128 return; | 127 return; |
| 129 | 128 |
| 129 bool has_appcache = | |
| 130 content::AppCacheService::URLRequestHasActiveAppCache(request_); | |
| 131 | |
| 130 DCHECK_GT(request_->url_chain().size(), 0u); | 132 DCHECK_GT(request_->url_chain().size(), 0u); |
| 131 int redirect_count = | 133 int redirect_count = |
| 132 base::saturated_cast<int>(request_->url_chain().size()) - 1; | 134 base::saturated_cast<int>(request_->url_chain().size()) - 1; |
| 133 | 135 |
| 134 BrowserThread::PostTask( | 136 BrowserThread::PostTask( |
| 135 BrowserThread::UI, FROM_HERE, | 137 BrowserThread::UI, FROM_HERE, |
| 136 base::Bind(&PrerenderResourceThrottle::WillProcessResponseOnUI, | 138 base::Bind(&PrerenderResourceThrottle::WillProcessResponseOnUI, |
| 139 AsWeakPtr(), | |
| 137 content::IsResourceTypeFrame(info->GetResourceType()), | 140 content::IsResourceTypeFrame(info->GetResourceType()), |
| 138 IsNoStoreResponse(*request_), redirect_count, | 141 IsNoStoreResponse(*request_), redirect_count, has_appcache, |
| 142 info->GetWebContentsGetterForRequest(), | |
| 139 prerender_throttle_info_)); | 143 prerender_throttle_info_)); |
| 140 } | 144 } |
| 141 | 145 |
| 142 const char* PrerenderResourceThrottle::GetNameForLogging() const { | 146 const char* PrerenderResourceThrottle::GetNameForLogging() const { |
| 143 return "PrerenderResourceThrottle"; | 147 return "PrerenderResourceThrottle"; |
| 144 } | 148 } |
| 145 | 149 |
| 146 void PrerenderResourceThrottle::ResumeHandler() { | 150 void PrerenderResourceThrottle::ResumeHandler() { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 request_->SetLoadFlags(request_->load_flags() | load_flags_); | 152 request_->SetLoadFlags(request_->load_flags() | load_flags_); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 } | 254 } |
| 251 | 255 |
| 252 BrowserThread::PostTask( | 256 BrowserThread::PostTask( |
| 253 BrowserThread::IO, FROM_HERE, | 257 BrowserThread::IO, FROM_HERE, |
| 254 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel | 258 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel |
| 255 : &PrerenderResourceThrottle::ResumeHandler, | 259 : &PrerenderResourceThrottle::ResumeHandler, |
| 256 throttle)); | 260 throttle)); |
| 257 } | 261 } |
| 258 | 262 |
| 259 // static | 263 // static |
| 260 void PrerenderResourceThrottle::WillProcessResponseOnUI( | 264 void PrerenderResourceThrottle::WillProcessResponseOnUI( |
|
michaeln
2017/03/27 23:27:45
Since this call on the UI thread is advisory only,
| |
| 265 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | |
| 261 bool is_main_resource, | 266 bool is_main_resource, |
| 262 bool is_no_store, | 267 bool is_no_store, |
| 263 int redirect_count, | 268 int redirect_count, |
| 269 bool has_appcache, | |
| 270 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
| 264 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { | 271 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { |
| 265 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 266 DCHECK(prerender_throttle_info); | 273 DCHECK(prerender_throttle_info); |
| 267 if (!prerender_throttle_info->manager()) | 274 if (!prerender_throttle_info->manager()) |
| 268 return; | 275 return; |
| 269 | 276 |
| 270 if (prerender_throttle_info->mode() != PREFETCH_ONLY) | 277 if (prerender_throttle_info->mode() != PREFETCH_ONLY) |
| 271 return; | 278 return; |
| 272 | 279 |
| 280 // Check for an AppCache. If there is one, the AppCache fetcher (?) may not | |
| 281 // have been properly initialized for a prefetch-only resource, so the request | |
| 282 // is canceled. If there is no appcache, it is appropriate to go to the | |
| 283 // network: even if the main resource lists a manifest, its AppCache has not | |
| 284 // yet been initialized, and a network fetch would have happened in any case. | |
| 285 if (has_appcache) { | |
| 286 BrowserThread::PostTask( | |
| 287 BrowserThread::IO, FROM_HERE, | |
| 288 base::Bind(&PrerenderResourceThrottle::Cancel, throttle)); | |
| 289 | |
| 290 PrerenderContents* prerender_contents = | |
| 291 PrerenderContentsFromGetter(web_contents_getter); | |
| 292 if (prerender_contents) | |
| 293 prerender_contents->Destroy(FINAL_STATUS_CANCELLED_FOR_APPCACHE); | |
| 294 return; | |
| 295 } | |
| 296 | |
| 273 prerender_throttle_info->manager()->RecordPrefetchResponseReceived( | 297 prerender_throttle_info->manager()->RecordPrefetchResponseReceived( |
| 274 prerender_throttle_info->origin(), is_main_resource, | 298 prerender_throttle_info->origin(), is_main_resource, |
| 275 false /* is_redirect */, is_no_store); | 299 false /* is_redirect */, is_no_store); |
| 276 prerender_throttle_info->manager()->RecordPrefetchRedirectCount( | 300 prerender_throttle_info->manager()->RecordPrefetchRedirectCount( |
| 277 prerender_throttle_info->origin(), is_main_resource, redirect_count); | 301 prerender_throttle_info->origin(), is_main_resource, redirect_count); |
| 278 } | 302 } |
| 279 | 303 |
| 280 // static | 304 // static |
| 281 PrerenderContents* PrerenderResourceThrottle::PrerenderContentsFromGetter( | 305 PrerenderContents* PrerenderResourceThrottle::PrerenderContentsFromGetter( |
| 282 const ResourceRequestInfo::WebContentsGetter& web_contents_getter) { | 306 const ResourceRequestInfo::WebContentsGetter& web_contents_getter) { |
| 283 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 307 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 284 if (g_prerender_contents_for_testing) | 308 if (g_prerender_contents_for_testing) |
| 285 return g_prerender_contents_for_testing; | 309 return g_prerender_contents_for_testing; |
| 286 return PrerenderContents::FromWebContents(web_contents_getter.Run()); | 310 return PrerenderContents::FromWebContents(web_contents_getter.Run()); |
| 287 } | 311 } |
| 288 | 312 |
| 289 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { | 313 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 314 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 291 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; | 315 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; |
| 292 } | 316 } |
| 293 | 317 |
| 294 } // namespace prerender | 318 } // namespace prerender |
| OLD | NEW |