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/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "net/base/load_flags.h" | |
| 16 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/redirect_info.h" | 18 #include "net/url_request/redirect_info.h" |
| 18 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 19 | 20 |
| 20 using content::BrowserThread; | 21 using content::BrowserThread; |
| 21 using content::ResourceRequestInfo; | 22 using content::ResourceRequestInfo; |
| 22 using content::ResourceType; | 23 using content::ResourceType; |
| 23 | 24 |
| 24 namespace prerender { | 25 namespace prerender { |
| 25 | 26 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 base::WeakPtr<PrerenderManager> manager_; | 77 base::WeakPtr<PrerenderManager> manager_; |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( | 80 void PrerenderResourceThrottle::OverridePrerenderContentsForTesting( |
| 80 PrerenderContents* contents) { | 81 PrerenderContents* contents) { |
| 81 g_prerender_contents_for_testing = contents; | 82 g_prerender_contents_for_testing = contents; |
| 82 } | 83 } |
| 83 | 84 |
| 84 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) | 85 PrerenderResourceThrottle::PrerenderResourceThrottle(net::URLRequest* request) |
| 85 : request_(request), | 86 : request_(request), |
| 87 load_flags_(net::LOAD_NORMAL), | |
| 86 prerender_throttle_info_(new PrerenderThrottleInfo()) {} | 88 prerender_throttle_info_(new PrerenderThrottleInfo()) {} |
| 87 | 89 |
| 88 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} | 90 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} |
| 89 | 91 |
| 90 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { | 92 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { |
| 91 const content::ResourceRequestInfo* info = | 93 const content::ResourceRequestInfo* info = |
| 92 content::ResourceRequestInfo::ForRequest(request_); | 94 content::ResourceRequestInfo::ForRequest(request_); |
| 93 *defer = true; | 95 *defer = true; |
| 94 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 95 BrowserThread::UI, FROM_HERE, | 97 BrowserThread::UI, FROM_HERE, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 content::IsResourceTypeFrame(info->GetResourceType()), | 134 content::IsResourceTypeFrame(info->GetResourceType()), |
| 133 IsNoStoreResponse(*request_), redirect_count, | 135 IsNoStoreResponse(*request_), redirect_count, |
| 134 prerender_throttle_info_)); | 136 prerender_throttle_info_)); |
| 135 } | 137 } |
| 136 | 138 |
| 137 const char* PrerenderResourceThrottle::GetNameForLogging() const { | 139 const char* PrerenderResourceThrottle::GetNameForLogging() const { |
| 138 return "PrerenderResourceThrottle"; | 140 return "PrerenderResourceThrottle"; |
| 139 } | 141 } |
| 140 | 142 |
| 141 void PrerenderResourceThrottle::ResumeHandler() { | 143 void PrerenderResourceThrottle::ResumeHandler() { |
| 144 request_->SetLoadFlags(request_->load_flags() | load_flags_); | |
| 142 Resume(); | 145 Resume(); |
| 143 } | 146 } |
| 144 | 147 |
| 145 // static | 148 // static |
| 146 void PrerenderResourceThrottle::WillStartRequestOnUI( | 149 void PrerenderResourceThrottle::WillStartRequestOnUI( |
| 147 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 150 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
| 148 const std::string& method, | 151 const std::string& method, |
| 149 ResourceType resource_type, | 152 ResourceType resource_type, |
| 150 const GURL& url, | 153 const GURL& url, |
| 151 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | 154 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
| 152 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { | 155 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { |
|
clamy
2016/12/23 12:33:32
nit: It'd be nice in the long run to have DCHECK(C
pasko
2016/12/23 13:28:51
With correctness - yes, probably should add these
clamy
2016/12/23 14:21:10
That's why I'm marking this as a nit, it doesn't n
| |
| 153 bool cancel = false; | 156 bool cancel = false; |
| 154 PrerenderContents* prerender_contents = | 157 PrerenderContents* prerender_contents = |
| 155 PrerenderContentsFromGetter(web_contents_getter); | 158 PrerenderContentsFromGetter(web_contents_getter); |
| 156 if (prerender_contents) { | 159 if (prerender_contents) { |
| 157 DCHECK(prerender_throttle_info); | 160 DCHECK(prerender_throttle_info); |
| 158 prerender_throttle_info->Set(prerender_contents->prerender_mode(), | 161 prerender_throttle_info->Set(prerender_contents->prerender_mode(), |
| 159 prerender_contents->origin(), | 162 prerender_contents->origin(), |
| 160 prerender_contents->prerender_manager()); | 163 prerender_contents->prerender_manager()); |
| 164 BrowserThread::PostTask( | |
|
clamy
2016/12/23 12:33:31
Rather than having two different PostTasks, why no
pasko
2016/12/23 13:28:51
I had the same question, but then saw that ResumeH
clamy
2016/12/23 14:21:10
Acknowledged.
| |
| 165 BrowserThread::IO, FROM_HERE, | |
| 166 base::Bind(&PrerenderResourceThrottle::SetPrerenderMode, throttle, | |
| 167 prerender_contents->prerender_mode())); | |
| 161 | 168 |
| 162 // Abort any prerenders that spawn requests that use unsupported HTTP | 169 // Abort any prerenders that spawn requests that use unsupported HTTP |
| 163 // methods or schemes. | 170 // methods or schemes. |
| 164 if (!prerender_contents->IsValidHttpMethod(method)) { | 171 if (!prerender_contents->IsValidHttpMethod(method)) { |
| 165 // If this is a full prerender, cancel the prerender in response to | 172 // If this is a full prerender, cancel the prerender in response to |
| 166 // invalid requests. For prefetches, cancel invalid requests but keep the | 173 // invalid requests. For prefetches, cancel invalid requests but keep the |
| 167 // prefetch going, unless it's the main frame that's invalid. | 174 // prefetch going, unless it's the main frame that's invalid. |
| 168 if (prerender_contents->prerender_mode() == FULL_PRERENDER || | 175 if (prerender_contents->prerender_mode() == FULL_PRERENDER || |
| 169 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) { | 176 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 170 prerender_contents->Destroy(FINAL_STATUS_INVALID_HTTP_METHOD); | 177 prerender_contents->Destroy(FINAL_STATUS_INVALID_HTTP_METHOD); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 } | 271 } |
| 265 | 272 |
| 266 // static | 273 // static |
| 267 PrerenderContents* PrerenderResourceThrottle::PrerenderContentsFromGetter( | 274 PrerenderContents* PrerenderResourceThrottle::PrerenderContentsFromGetter( |
| 268 const ResourceRequestInfo::WebContentsGetter& web_contents_getter) { | 275 const ResourceRequestInfo::WebContentsGetter& web_contents_getter) { |
| 269 if (g_prerender_contents_for_testing) | 276 if (g_prerender_contents_for_testing) |
| 270 return g_prerender_contents_for_testing; | 277 return g_prerender_contents_for_testing; |
| 271 return PrerenderContents::FromWebContents(web_contents_getter.Run()); | 278 return PrerenderContents::FromWebContents(web_contents_getter.Run()); |
| 272 } | 279 } |
| 273 | 280 |
| 281 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { | |
| 282 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; | |
| 283 } | |
| 284 | |
| 274 } // namespace prerender | 285 } // namespace prerender |
| OLD | NEW |