| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, | 137 new_validated_url, new_method, new_referrer_url, new_is_external_protocol, |
| 133 headers, base::Bind(&SendCheckResultToIOThread, callback)); | 138 headers, base::Bind(&SendCheckResultToIOThread, callback)); |
| 134 } | 139 } |
| 135 | 140 |
| 136 void WillProcessResponseOnUIThread( | 141 void WillProcessResponseOnUIThread( |
| 137 UIChecksPerformedCallback callback, | 142 UIChecksPerformedCallback callback, |
| 138 int render_process_id, | 143 int render_process_id, |
| 139 int render_frame_host_id, | 144 int render_frame_host_id, |
| 140 scoped_refptr<net::HttpResponseHeaders> headers, | 145 scoped_refptr<net::HttpResponseHeaders> headers, |
| 141 const SSLStatus& ssl_status, | 146 const SSLStatus& ssl_status, |
| 147 const GlobalRequestID& request_id, |
| 148 bool should_replace_current_entry, |
| 149 bool is_download, |
| 150 bool is_stream, |
| 151 const base::Closure& transfer_callback, |
| 142 std::unique_ptr<NavigationData> navigation_data) { | 152 std::unique_ptr<NavigationData> navigation_data) { |
| 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 154 |
| 155 if (g_force_transfer) { |
| 156 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, transfer_callback); |
| 157 } |
| 158 |
| 144 NavigationHandleImpl* navigation_handle = | 159 NavigationHandleImpl* navigation_handle = |
| 145 FindNavigationHandle(render_process_id, render_frame_host_id, callback); | 160 FindNavigationHandle(render_process_id, render_frame_host_id, callback); |
| 146 if (!navigation_handle) | 161 if (!navigation_handle) |
| 147 return; | 162 return; |
| 148 | 163 |
| 149 if (navigation_data) | 164 if (navigation_data) |
| 150 navigation_handle->set_navigation_data(std::move(navigation_data)); | 165 navigation_handle->set_navigation_data(std::move(navigation_data)); |
| 151 | 166 |
| 152 RenderFrameHostImpl* render_frame_host = | 167 RenderFrameHostImpl* render_frame_host = |
| 153 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); | 168 RenderFrameHostImpl::FromID(render_process_id, render_frame_host_id); |
| 154 DCHECK(render_frame_host); | 169 DCHECK(render_frame_host); |
| 155 navigation_handle->WillProcessResponse( | 170 navigation_handle->WillProcessResponse( |
| 156 render_frame_host, headers, ssl_status, | 171 render_frame_host, headers, ssl_status, request_id, |
| 172 should_replace_current_entry, is_download, is_stream, transfer_callback, |
| 157 base::Bind(&SendCheckResultToIOThread, callback)); | 173 base::Bind(&SendCheckResultToIOThread, callback)); |
| 158 } | 174 } |
| 159 | 175 |
| 160 } // namespace | 176 } // namespace |
| 161 | 177 |
| 162 NavigationResourceThrottle::NavigationResourceThrottle( | 178 NavigationResourceThrottle::NavigationResourceThrottle( |
| 163 net::URLRequest* request, | 179 net::URLRequest* request, |
| 164 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, | 180 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, |
| 165 RequestContextType request_context_type) | 181 RequestContextType request_context_type) |
| 166 : request_(request), | 182 : request_(request), |
| 167 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate), | 183 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate), |
| 168 request_context_type_(request_context_type), | 184 request_context_type_(request_context_type), |
| 185 in_cross_site_transition_(false), |
| 186 on_transfer_done_result_(NavigationThrottle::DEFER), |
| 169 weak_ptr_factory_(this) {} | 187 weak_ptr_factory_(this) {} |
| 170 | 188 |
| 171 NavigationResourceThrottle::~NavigationResourceThrottle() {} | 189 NavigationResourceThrottle::~NavigationResourceThrottle() {} |
| 172 | 190 |
| 173 void NavigationResourceThrottle::WillStartRequest(bool* defer) { | 191 void NavigationResourceThrottle::WillStartRequest(bool* defer) { |
| 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 192 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 175 const ResourceRequestInfoImpl* info = | 193 const ResourceRequestInfoImpl* info = |
| 176 ResourceRequestInfoImpl::ForRequest(request_); | 194 ResourceRequestInfoImpl::ForRequest(request_); |
| 177 if (!info) | 195 if (!info) |
| 178 return; | 196 return; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 BrowserThread::UI, FROM_HERE, | 255 BrowserThread::UI, FROM_HERE, |
| 238 base::Bind(&CheckWillRedirectRequestOnUIThread, callback, | 256 base::Bind(&CheckWillRedirectRequestOnUIThread, callback, |
| 239 render_process_id, render_frame_id, redirect_info.new_url, | 257 render_process_id, render_frame_id, redirect_info.new_url, |
| 240 redirect_info.new_method, GURL(redirect_info.new_referrer), | 258 redirect_info.new_method, GURL(redirect_info.new_referrer), |
| 241 new_is_external_protocol, response_headers)); | 259 new_is_external_protocol, response_headers)); |
| 242 *defer = true; | 260 *defer = true; |
| 243 } | 261 } |
| 244 | 262 |
| 245 void NavigationResourceThrottle::WillProcessResponse(bool* defer) { | 263 void NavigationResourceThrottle::WillProcessResponse(bool* defer) { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 264 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 247 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | 265 const ResourceRequestInfoImpl* info = |
| 266 ResourceRequestInfoImpl::ForRequest(request_); |
| 248 if (!info) | 267 if (!info) |
| 249 return; | 268 return; |
| 250 | 269 |
| 251 int render_process_id, render_frame_id; | 270 int render_process_id, render_frame_id; |
| 252 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) | 271 if (!info->GetAssociatedRenderFrame(&render_process_id, &render_frame_id)) |
| 253 return; | 272 return; |
| 254 | 273 |
| 255 // Send a copy of the response headers to the NavigationHandle on the UI | 274 // Send a copy of the response headers to the NavigationHandle on the UI |
| 256 // thread. | 275 // thread. |
| 257 scoped_refptr<net::HttpResponseHeaders> response_headers; | 276 scoped_refptr<net::HttpResponseHeaders> response_headers; |
| 258 if (request_->response_headers()) { | 277 if (request_->response_headers()) { |
| 259 response_headers = new net::HttpResponseHeaders( | 278 response_headers = new net::HttpResponseHeaders( |
| 260 request_->response_headers()->raw_headers()); | 279 request_->response_headers()->raw_headers()); |
| 261 } | 280 } |
| 262 | 281 |
| 263 std::unique_ptr<NavigationData> cloned_data; | 282 std::unique_ptr<NavigationData> cloned_data; |
| 264 if (resource_dispatcher_host_delegate_) { | 283 if (resource_dispatcher_host_delegate_) { |
| 265 // Ask the embedder for a NavigationData instance. | 284 // Ask the embedder for a NavigationData instance. |
| 266 NavigationData* navigation_data = | 285 NavigationData* navigation_data = |
| 267 resource_dispatcher_host_delegate_->GetNavigationData(request_); | 286 resource_dispatcher_host_delegate_->GetNavigationData(request_); |
| 268 | 287 |
| 269 // Clone the embedder's NavigationData before moving it to the UI thread. | 288 // Clone the embedder's NavigationData before moving it to the UI thread. |
| 270 if (navigation_data) | 289 if (navigation_data) |
| 271 cloned_data = navigation_data->Clone(); | 290 cloned_data = navigation_data->Clone(); |
| 272 } | 291 } |
| 273 | 292 |
| 274 UIChecksPerformedCallback callback = | 293 UIChecksPerformedCallback callback = |
| 275 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, | 294 base::Bind(&NavigationResourceThrottle::OnUIChecksPerformed, |
| 276 weak_ptr_factory_.GetWeakPtr()); | 295 weak_ptr_factory_.GetWeakPtr()); |
| 296 base::Closure transfer_callback = |
| 297 base::Bind(&NavigationResourceThrottle::InitiateTransfer, |
| 298 weak_ptr_factory_.GetWeakPtr()); |
| 277 | 299 |
| 278 SSLStatus ssl_status; | 300 SSLStatus ssl_status; |
| 279 if (request_->ssl_info().cert.get()) { | 301 if (request_->ssl_info().cert.get()) { |
| 280 NavigationResourceHandler::GetSSLStatusForRequest( | 302 NavigationResourceHandler::GetSSLStatusForRequest( |
| 281 request_->url(), request_->ssl_info(), info->GetChildID(), &ssl_status); | 303 request_->url(), request_->ssl_info(), info->GetChildID(), &ssl_status); |
| 282 } | 304 } |
| 283 | 305 |
| 284 BrowserThread::PostTask( | 306 BrowserThread::PostTask( |
| 285 BrowserThread::UI, FROM_HERE, | 307 BrowserThread::UI, FROM_HERE, |
| 286 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, | 308 base::Bind(&WillProcessResponseOnUIThread, callback, render_process_id, |
| 287 render_frame_id, response_headers, ssl_status, | 309 render_frame_id, response_headers, ssl_status, |
| 310 info->GetGlobalRequestID(), |
| 311 info->should_replace_current_entry(), info->IsDownload(), |
| 312 info->is_stream(), transfer_callback, |
| 288 base::Passed(&cloned_data))); | 313 base::Passed(&cloned_data))); |
| 289 *defer = true; | 314 *defer = true; |
| 290 } | 315 } |
| 291 | 316 |
| 292 const char* NavigationResourceThrottle::GetNameForLogging() const { | 317 const char* NavigationResourceThrottle::GetNameForLogging() const { |
| 293 return "NavigationResourceThrottle"; | 318 return "NavigationResourceThrottle"; |
| 294 } | 319 } |
| 295 | 320 |
| 296 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( | 321 void NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing( |
| 297 bool ui_checks_always_succeed) { | 322 bool ui_checks_always_succeed) { |
| 298 g_ui_checks_always_succeed = ui_checks_always_succeed; | 323 g_ui_checks_always_succeed = ui_checks_always_succeed; |
| 299 } | 324 } |
| 300 | 325 |
| 326 void NavigationResourceThrottle::set_force_transfer_for_testing( |
| 327 bool force_transfer) { |
| 328 g_force_transfer = force_transfer; |
| 329 } |
| 330 |
| 301 void NavigationResourceThrottle::OnUIChecksPerformed( | 331 void NavigationResourceThrottle::OnUIChecksPerformed( |
| 302 NavigationThrottle::ThrottleCheckResult result) { | 332 NavigationThrottle::ThrottleCheckResult result) { |
| 303 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 333 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 334 DCHECK_NE(NavigationThrottle::DEFER, result); |
| 335 if (in_cross_site_transition_) { |
| 336 on_transfer_done_result_ = result; |
| 337 return; |
| 338 } |
| 339 |
| 304 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { | 340 if (result == NavigationThrottle::CANCEL_AND_IGNORE) { |
| 305 controller()->CancelAndIgnore(); | 341 controller()->CancelAndIgnore(); |
| 306 } else if (result == NavigationThrottle::CANCEL) { | 342 } else if (result == NavigationThrottle::CANCEL) { |
| 307 controller()->Cancel(); | 343 controller()->Cancel(); |
| 308 } else if (result == NavigationThrottle::BLOCK_REQUEST) { | 344 } else if (result == NavigationThrottle::BLOCK_REQUEST) { |
| 309 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); | 345 controller()->CancelWithError(net::ERR_BLOCKED_BY_CLIENT); |
| 310 } else { | 346 } else { |
| 311 controller()->Resume(); | 347 controller()->Resume(); |
| 312 } | 348 } |
| 313 } | 349 } |
| 314 | 350 |
| 351 void NavigationResourceThrottle::InitiateTransfer() { |
| 352 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 353 in_cross_site_transition_ = true; |
| 354 ResourceRequestInfoImpl* info = |
| 355 ResourceRequestInfoImpl::ForRequest(request_); |
| 356 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation( |
| 357 info->GetGlobalRequestID(), |
| 358 base::Bind(&NavigationResourceThrottle::OnTransferComplete, |
| 359 weak_ptr_factory_.GetWeakPtr())); |
| 360 } |
| 361 |
| 362 void NavigationResourceThrottle::OnTransferComplete() { |
| 363 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 364 DCHECK(in_cross_site_transition_); |
| 365 in_cross_site_transition_ = false; |
| 366 |
| 367 // If the results of the checks on the UI thread are known, unblock the |
| 368 // navigation. Otherwise, wait until the callback has executed. |
| 369 if (on_transfer_done_result_ != NavigationThrottle::DEFER) { |
| 370 OnUIChecksPerformed(on_transfer_done_result_); |
| 371 on_transfer_done_result_ = NavigationThrottle::DEFER; |
| 372 } |
| 373 } |
| 374 |
| 315 } // namespace content | 375 } // namespace content |
| OLD | NEW |