OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/loader/navigation_resource_throttle.h" | 5 #include "content/browser/loader/navigation_resource_throttle.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "content/browser/frame_host/navigation_handle_impl.h" | 14 #include "content/browser/frame_host/navigation_handle_impl.h" |
15 #include "content/browser/frame_host/navigator.h" | 15 #include "content/browser/frame_host/navigator.h" |
16 #include "content/browser/frame_host/render_frame_host_impl.h" | 16 #include "content/browser/frame_host/render_frame_host_impl.h" |
17 #include "content/browser/loader/navigation_resource_handler.h" | 17 #include "content/browser/loader/navigation_resource_handler.h" |
| 18 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 19 #include "content/browser/loader/resource_loader.h" |
18 #include "content/browser/loader/resource_request_info_impl.h" | 20 #include "content/browser/loader/resource_request_info_impl.h" |
19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/navigation_data.h" | 22 #include "content/public/browser/navigation_data.h" |
21 #include "content/public/browser/resource_context.h" | 23 #include "content/public/browser/resource_context.h" |
22 #include "content/public/browser/resource_controller.h" | 24 #include "content/public/browser/resource_controller.h" |
23 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 25 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
24 #include "content/public/browser/resource_request_info.h" | 26 #include "content/public/browser/resource_request_info.h" |
25 #include "content/public/browser/ssl_status.h" | 27 #include "content/public/browser/ssl_status.h" |
26 #include "content/public/common/referrer.h" | 28 #include "content/public/common/referrer.h" |
27 #include "net/url_request/redirect_info.h" | 29 #include "net/url_request/redirect_info.h" |
28 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
29 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_job_factory.h" | 32 #include "net/url_request/url_request_job_factory.h" |
31 #include "ui/base/page_transition_types.h" | 33 #include "ui/base/page_transition_types.h" |
32 | 34 |
33 namespace content { | 35 namespace content { |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 // Used in unit tests to make UI thread checks succeed even if there is no | 39 // Used in unit tests to make UI thread checks succeed even if there is no |
38 // NavigationHandle. | 40 // NavigationHandle. |
39 bool g_ui_checks_always_succeed = false; | 41 bool g_ui_checks_always_succeed = false; |
40 | 42 |
| 43 // Used in unit tests to transfer all navigations. |
| 44 bool g_force_transfer = false; |
| 45 |
41 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> | 46 typedef base::Callback<void(NavigationThrottle::ThrottleCheckResult)> |
42 UIChecksPerformedCallback; | 47 UIChecksPerformedCallback; |
43 | 48 |
44 void SendCheckResultToIOThread(UIChecksPerformedCallback callback, | 49 void SendCheckResultToIOThread(UIChecksPerformedCallback callback, |
45 NavigationThrottle::ThrottleCheckResult result) { | 50 NavigationThrottle::ThrottleCheckResult result) { |
46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
47 DCHECK_NE(result, NavigationThrottle::DEFER); | 52 DCHECK_NE(result, NavigationThrottle::DEFER); |
48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 53 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
49 base::Bind(callback, result)); | 54 base::Bind(callback, result)); |
50 } | 55 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, | 138 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, |
134 headers, base::Bind(&SendCheckResultToIOThread, callback)); | 139 headers, base::Bind(&SendCheckResultToIOThread, callback)); |
135 } | 140 } |
136 | 141 |
137 void WillProcessResponseOnUIThread( | 142 void WillProcessResponseOnUIThread( |
138 UIChecksPerformedCallback callback, | 143 UIChecksPerformedCallback callback, |
139 int render_process_id, | 144 int render_process_id, |
140 int render_frame_host_id, | 145 int render_frame_host_id, |
141 scoped_refptr<net::HttpResponseHeaders> headers, | 146 scoped_refptr<net::HttpResponseHeaders> headers, |
142 const SSLStatus& ssl_status, | 147 const SSLStatus& ssl_status, |
| 148 const GlobalRequestID& request_id, |
| 149 bool should_replace_current_entry, |
| 150 bool is_download, |
| 151 bool is_stream, |
| 152 const base::Closure& transfer_callback, |
143 std::unique_ptr<NavigationData> navigation_data) { | 153 std::unique_ptr<NavigationData> navigation_data) { |
144 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 154 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
145 NavigationHandleImpl* navigation_handle = | 155 NavigationHandleImpl* navigation_handle = |
146 FindNavigationHandle(render_process_id, render_frame_host_id, callback); | 156 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
147 if (!navigation_handle) | 157 if (!navigation_handle) |
148 return; | 158 return; |
149 | 159 |
150 if (navigation_data) | 160 if (navigation_data) |
151 navigation_handle->set_navigation_data(std::move(navigation_data)); | 161 navigation_handle->set_navigation_data(std::move(navigation_data)); |
152 | 162 |
153 RenderFrameHostImpl* render_frame_host = | 163 RenderFrameHostImpl* render_frame_host = |
154 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 164 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
155 DCHECK(render_frame_host); | 165 DCHECK(render_frame_host); |
156 navigation_handle->WillProcessResponse( | 166 navigation_handle->WillProcessResponse( |
157 render_frame_host, headers, ssl_status, | 167 render_frame_host, headers, ssl_status, request_id, |
| 168 should_replace_current_entry, is_download, is_stream, transfer_callback, |
158 base::Bind(&SendCheckResultToIOThread, callback)); | 169 base::Bind(&SendCheckResultToIOThread, callback)); |
159 } | 170 } |
160 | 171 |
161 } // namespace | 172 } // namespace |
162 | 173 |
163 NavigationResourceThrottle::NavigationResourceThrottle( | 174 NavigationResourceThrottle::NavigationResourceThrottle( |
164 net::URLRequest* request, | 175 net::URLRequest* request, |
165 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, | 176 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, |
166 RequestContextType request_context_type) | 177 RequestContextType request_context_type) |
167 : request_(request), | 178 : request_(request), |
168 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate), | 179 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate), |
169 request_context_type_(request_context_type), | 180 request_context_type_(request_context_type), |
| 181 in_cross_site_transition_(false), |
| 182 on_transfer_done_result_(NavigationThrottle::DEFER), |
170 weak_ptr_factory_(this) {} | 183 weak_ptr_factory_(this) {} |
171 | 184 |
172 NavigationResourceThrottle::~NavigationResourceThrottle() {} | 185 NavigationResourceThrottle::~NavigationResourceThrottle() {} |
173 | 186 |
174 void NavigationResourceThrottle::WillStartRequest(bool* defer) { | 187 void NavigationResourceThrottle::WillStartRequest(bool* defer) { |
175 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 188 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
176 const ResourceRequestInfoImpl* info = | 189 const ResourceRequestInfoImpl* info = |
177 ResourceRequestInfoImpl::ForRequest(request_); | 190 ResourceRequestInfoImpl::ForRequest(request_); |
178 if (!info) | 191 if (!info) |
179 return; | 192 return; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 BrowserThread::UI, FROM_HERE, | 251 BrowserThread::UI, FROM_HERE, |
239 base::Bind(&CheckWillRedirectRequestOnUIThread, callback, | 252 base::Bind(&CheckWillRedirectRequestOnUIThread, callback, |
240 render_process_id, render_frame_id, redirect_info.new_url, | 253 render_process_id, render_frame_id, redirect_info.new_url, |
241 redirect_info.new_method, GURL(redirect_info.new_referrer), | 254 redirect_info.new_method, GURL(redirect_info.new_referrer), |
242 new_is_external_protocol, response_headers)); | 255 new_is_external_protocol, response_headers)); |
243 *defer = true; | 256 *defer = true; |
244 } | 257 } |
245 | 258 |
246 void NavigationResourceThrottle::WillProcessResponse(bool* defer) { | 259 void NavigationResourceThrottle::WillProcessResponse(bool* defer) { |
247 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
248 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | 261 const ResourceRequestInfoImpl* info = |
| 262 ResourceRequestInfoImpl::ForRequest(request_); |
249 if (!info) | 263 if (!info) |
250 return; | 264 return; |
251 | 265 |
252 int render_process_id, render_frame_id; | 266 int render_process_id, render_frame_id; |
253 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) | 267 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
254 return; | 268 return; |
255 | 269 |
256 // Send a copy of the response headers to the NavigationHandle on the UI | 270 // Send a copy of the response headers to the NavigationHandle on the UI |
257 // thread. | 271 // thread. |
258 scoped_refptr<net::HttpResponseHeaders> response_headers; | 272 scoped_refptr<net::HttpResponseHeaders> response_headers; |
259 if (request_->response_headers()) { | 273 if (request_->response_headers()) { |
260 response_headers = new net::HttpResponseHeaders( | 274 response_headers = new net::HttpResponseHeaders( |
261 request_->response_headers()->raw_headers()); | 275 request_->response_headers()->raw_headers()); |
262 } | 276 } |
263 | 277 |
264 std::unique_ptr<NavigationData> cloned_data; | 278 std::unique_ptr<NavigationData> cloned_data; |
265 if (resource_dispatcher_host_delegate_) { | 279 if (resource_dispatcher_host_delegate_) { |
266 // Ask the embedder for a NavigationData instance. | 280 // Ask the embedder for a NavigationData instance. |
267 NavigationData* navigation_data = | 281 NavigationData* navigation_data = |
268 resource_dispatcher_host_delegate_->GetNavigationData(request_); | 282 resource_dispatcher_host_delegate_->GetNavigationData(request_); |
269 | 283 |
270 // Clone the embedder's NavigationData before moving it to the UI thread. | 284 // Clone the embedder's NavigationData before moving it to the UI thread. |
271 if (navigation_data) | 285 if (navigation_data) |
272 cloned_data = navigation_data->Clone(); | 286 cloned_data = navigation_data->Clone(); |
273 } | 287 } |
274 | 288 |
275 UIChecksPerformedCallback callback = | 289 UIChecksPerformedCallback callback = |
276 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, | 290 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, |
277 weak_ptr_factory_.GetWeakPtr()); | 291 weak_ptr_factory_.GetWeakPtr()); |
| 292 base::Closure transfer_callback = |
| 293 base::Bind(&NavigationResourceThrottle::InitiateTransfer, |
| 294 weak_ptr_factory_.GetWeakPtr()); |
278 | 295 |
279 SSLStatus ssl_status; | 296 SSLStatus ssl_status; |
280 if (request_->ssl_info().cert.get()) { | 297 if (request_->ssl_info().cert.get()) { |
281 NavigationResourceHandler::GetSSLStatusForRequest( | 298 NavigationResourceHandler::GetSSLStatusForRequest( |
282 request_->url(), request_->ssl_info(), info->GetChildID(), &ssl_status); | 299 request_->url(), request_->ssl_info(), info->GetChildID(), &ssl_status); |
283 } | 300 } |
284 | 301 |
285 BrowserThread::PostTask( | 302 BrowserThread::PostTask( |
286 BrowserThread::UI, FROM_HERE, | 303 BrowserThread::UI, FROM_HERE, |
287 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, | 304 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, |
288 render_frame_id, response_headers, ssl_status, | 305 render_frame_id, response_headers, ssl_status, |
| 306 info->GetGlobalRequestID(), |
| 307 info->should_replace_current_entry(), info->IsDownload(), |
| 308 info->is_stream(), transfer_callback, |
289 base::Passed(&cloned_data))); | 309 base::Passed(&cloned_data))); |
290 *defer = true; | 310 *defer = true; |
291 } | 311 } |
292 | 312 |
293 const char* NavigationResourceThrottle::GetNameForLogging() const { | 313 const char* NavigationResourceThrottle::GetNameForLogging() const { |
294 return "NavigationResourceThrottle"; | 314 return "NavigationResourceThrottle"; |
295 } | 315 } |
296 | 316 |
297 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( | 317 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( |
298 bool ui_checks_always_succeed) { | 318 bool ui_checks_always_succeed) { |
299 g_ui_checks_always_succeed = ui_checks_always_succeed; | 319 g_ui_checks_always_succeed = ui_checks_always_succeed; |
300 } | 320 } |
301 | 321 |
| 322 void NavigationResourceThrottle::set_force_transfer_for_testing( |
| 323 bool force_transfer) { |
| 324 g_force_transfer = force_transfer; |
| 325 } |
| 326 |
302 void NavigationResourceThrottle::OnUIChecksPerformed( | 327 void NavigationResourceThrottle::OnUIChecksPerformed( |
303 NavigationThrottle::ThrottleCheckResult result) { | 328 NavigationThrottle::ThrottleCheckResult result) { |
304 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 329 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 330 DCHECK_NE(NavigationThrottle::DEFER, result); |
| 331 if (in_cross_site_transition_) { |
| 332 on_transfer_done_result_ = result; |
| 333 return; |
| 334 } |
| 335 |
305 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { | 336 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { |
306 controller()->CancelAndIgnore(); | 337 controller()->CancelAndIgnore(); |
307 } else if (result == NavigationThrottle::CANCEL) { | 338 } else if (result == NavigationThrottle::CANCEL) { |
308 controller()->Cancel(); | 339 controller()->Cancel(); |
309 } else if (result == NavigationThrottle::BLOCK_REQUEST) { | 340 } else if (result == NavigationThrottle::BLOCK_REQUEST) { |
310 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); | 341 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); |
311 } else { | 342 } else { |
312 controller()->Resume(); | 343 controller()->Resume(); |
313 } | 344 } |
314 } | 345 } |
315 | 346 |
| 347 void NavigationResourceThrottle::InitiateTransfer() { |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 349 in_cross_site_transition_ = true; |
| 350 ResourceRequestInfoImpl* info = |
| 351 ResourceRequestInfoImpl::ForRequest(request_); |
| 352 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation( |
| 353 info->GetGlobalRequestID(), |
| 354 base::Bind(&NavigationResourceThrottle::OnTransferComplete, |
| 355 weak_ptr_factory_.GetWeakPtr())); |
| 356 } |
| 357 |
| 358 void NavigationResourceThrottle::OnTransferComplete() { |
| 359 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 360 DCHECK(in_cross_site_transition_); |
| 361 in_cross_site_transition_ = false; |
| 362 |
| 363 // If the results of the checks on the UI thread are known, unblock the |
| 364 // navigation. Otherwise, wait until the callback has executed. |
| 365 if (on_transfer_done_result_ != NavigationThrottle::DEFER) { |
| 366 OnUIChecksPerformed(on_transfer_done_result_); |
| 367 on_transfer_done_result_ = NavigationThrottle::DEFER; |
| 368 } |
| 369 } |
| 370 |
316 } // namespace content | 371 } // namespace content |
OLD | NEW |