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), |
89 original_request_priority_(net::IDLE), | |
88 prerender_throttle_info_(new PrerenderThrottleInfo()) {} | 90 prerender_throttle_info_(new PrerenderThrottleInfo()) {} |
89 | 91 |
90 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} | 92 PrerenderResourceThrottle::~PrerenderResourceThrottle() {} |
91 | 93 |
92 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { | 94 void PrerenderResourceThrottle::WillStartRequest(bool* defer) { |
93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 95 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
94 const content::ResourceRequestInfo* info = | 96 const content::ResourceRequestInfo* info = |
95 content::ResourceRequestInfo::ForRequest(request_); | 97 content::ResourceRequestInfo::ForRequest(request_); |
96 *defer = true; | 98 *defer = true; |
99 | |
100 // Priorities for prerendering requests are lowered, to avoid competing with | |
101 // other page loads, except on Android where this is less likely to be a | |
102 // problem. In some cases, this may negatively impact the performance of | |
103 // prerendering, see https://crbug.com/652746 for details. | |
104 #if !defined(OS_ANDROID) | |
105 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs) | |
106 // should remain at MAXIMUM_PRIORITY. | |
107 if (request_->load_flags() & net::LOAD_IGNORE_LIMITS) { | |
108 DCHECK_EQ(request_->priority(), net::MAXIMUM_PRIORITY); | |
109 // original_request_priority_ can be kept to IDLE, so that the priority will | |
110 // never be touched again. | |
111 } else if (request_->priority() != net::IDLE) { | |
112 original_request_priority_ = request_->priority(); | |
113 content::ResourceDispatcherHost::Get()->ReprioritizeRequest(request_, | |
114 net::IDLE); | |
115 } | |
116 #endif // OS_ANDROID | |
117 | |
97 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
98 BrowserThread::UI, FROM_HERE, | 119 BrowserThread::UI, FROM_HERE, |
99 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), | 120 base::Bind(&PrerenderResourceThrottle::WillStartRequestOnUI, AsWeakPtr(), |
100 request_->method(), info->GetResourceType(), request_->url(), | 121 request_->method(), info->GetResourceType(), request_->url(), |
101 info->GetWebContentsGetterForRequest(), | 122 info->GetWebContentsGetterForRequest(), |
102 prerender_throttle_info_)); | 123 prerender_throttle_info_)); |
103 } | 124 } |
104 | 125 |
105 void PrerenderResourceThrottle::WillRedirectRequest( | 126 void PrerenderResourceThrottle::WillRedirectRequest( |
106 const net::RedirectInfo& redirect_info, | 127 const net::RedirectInfo& redirect_info, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 const char* PrerenderResourceThrottle::GetNameForLogging() const { | 163 const char* PrerenderResourceThrottle::GetNameForLogging() const { |
143 return "PrerenderResourceThrottle"; | 164 return "PrerenderResourceThrottle"; |
144 } | 165 } |
145 | 166 |
146 void PrerenderResourceThrottle::ResumeHandler() { | 167 void PrerenderResourceThrottle::ResumeHandler() { |
147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 168 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
148 request_->SetLoadFlags(request_->load_flags() | load_flags_); | 169 request_->SetLoadFlags(request_->load_flags() | load_flags_); |
149 Resume(); | 170 Resume(); |
150 } | 171 } |
151 | 172 |
173 void PrerenderResourceThrottle::ResetResourcePriority() { | |
174 if (original_request_priority_ == net::IDLE) { | |
175 // IDLE means that the original request was either IDLE or must not be | |
176 // changed. | |
Charlie Harrison
2017/04/12 12:37:26
You could use a base::Optional to make this distin
droger
2017/04/12 13:20:54
Done.
| |
177 return; | |
178 } | |
179 | |
180 content::ResourceDispatcherHost::Get()->ReprioritizeRequest( | |
181 request_, original_request_priority_); | |
182 } | |
183 | |
152 // static | 184 // static |
153 void PrerenderResourceThrottle::WillStartRequestOnUI( | 185 void PrerenderResourceThrottle::WillStartRequestOnUI( |
154 const base::WeakPtr<PrerenderResourceThrottle>& throttle, | 186 const base::WeakPtr<PrerenderResourceThrottle>& throttle, |
155 const std::string& method, | 187 const std::string& method, |
156 ResourceType resource_type, | 188 ResourceType resource_type, |
157 const GURL& url, | 189 const GURL& url, |
158 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | 190 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, |
159 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { | 191 scoped_refptr<PrerenderThrottleInfo> prerender_throttle_info) { |
160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 192 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
161 bool cancel = false; | 193 bool cancel = false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 // scheme for the main resource is checked in WillRedirectRequestOnUI() | 226 // scheme for the main resource is checked in WillRedirectRequestOnUI() |
195 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. | 227 // and PrerenderContents::CheckURL(). See http://crbug.com/673771. |
196 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); | 228 prerender_contents->Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
197 ReportUnsupportedPrerenderScheme(url); | 229 ReportUnsupportedPrerenderScheme(url); |
198 } | 230 } |
199 cancel = true; | 231 cancel = true; |
200 #if defined(OS_ANDROID) | 232 #if defined(OS_ANDROID) |
201 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { | 233 } else if (resource_type == content::RESOURCE_TYPE_FAVICON) { |
202 // Delay icon fetching until the contents are getting swapped in | 234 // Delay icon fetching until the contents are getting swapped in |
203 // to conserve network usage in mobile devices. | 235 // to conserve network usage in mobile devices. |
204 prerender_contents->AddResourceThrottle(throttle); | 236 prerender_contents->AddResourceThrottle(throttle); |
Charlie Harrison
2017/04/12 12:37:26
nit: newline below
droger
2017/04/12 13:20:54
Done.
| |
237 // No need to add to call AddIdleResource() on Android. | |
205 return; | 238 return; |
206 #endif | 239 #endif |
207 } | 240 } |
241 | |
242 #if !defined(OS_ANDROID) | |
243 if (!cancel) | |
244 prerender_contents->AddIdleResource(throttle); | |
245 #endif | |
208 } | 246 } |
209 | 247 |
210 BrowserThread::PostTask( | 248 BrowserThread::PostTask( |
211 BrowserThread::IO, FROM_HERE, | 249 BrowserThread::IO, FROM_HERE, |
212 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel | 250 base::Bind(cancel ? &PrerenderResourceThrottle::Cancel |
213 : &PrerenderResourceThrottle::ResumeHandler, | 251 : &PrerenderResourceThrottle::ResumeHandler, |
214 throttle)); | 252 throttle)); |
215 } | 253 } |
216 | 254 |
217 // static | 255 // static |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 return g_prerender_contents_for_testing; | 328 return g_prerender_contents_for_testing; |
291 return PrerenderContents::FromWebContents(web_contents_getter.Run()); | 329 return PrerenderContents::FromWebContents(web_contents_getter.Run()); |
292 } | 330 } |
293 | 331 |
294 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { | 332 void PrerenderResourceThrottle::SetPrerenderMode(PrerenderMode mode) { |
295 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 333 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
296 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; | 334 load_flags_ = (mode == PREFETCH_ONLY) ? net::LOAD_PREFETCH : net::LOAD_NORMAL; |
297 } | 335 } |
298 | 336 |
299 } // namespace prerender | 337 } // namespace prerender |
OLD | NEW |