Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // An implementation of WebURLLoader in terms of ResourceLoaderBridge. | |
| 6 | |
| 7 #include "content/child/web_url_loader_impl.h" | 5 #include "content/child/web_url_loader_impl.h" |
| 8 | 6 |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 11 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 14 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 15 #include "content/child/child_thread.h" | 13 #include "content/child/child_thread.h" |
| 16 #include "content/child/ftp_directory_listing_response_delegate.h" | 14 #include "content/child/ftp_directory_listing_response_delegate.h" |
| 17 #include "content/child/request_extra_data.h" | 15 #include "content/child/request_extra_data.h" |
| 18 #include "content/child/request_info.h" | 16 #include "content/child/request_info.h" |
| 17 #include "content/child/resource_dispatcher.h" | |
| 19 #include "content/child/sync_load_response.h" | 18 #include "content/child/sync_load_response.h" |
| 20 #include "content/common/resource_request_body.h" | 19 #include "content/common/resource_request_body.h" |
| 21 #include "content/public/child/request_peer.h" | 20 #include "content/public/child/request_peer.h" |
| 22 #include "net/base/data_url.h" | 21 #include "net/base/data_url.h" |
| 23 #include "net/base/filename_util.h" | 22 #include "net/base/filename_util.h" |
| 24 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 25 #include "net/base/mime_util.h" | 24 #include "net/base/mime_util.h" |
| 26 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 27 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 28 #include "net/http/http_util.h" | 27 #include "net/http/http_util.h" |
| 29 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
| 30 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 29 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
| 31 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" | 30 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" |
| 32 #include "third_party/WebKit/public/platform/WebURL.h" | 31 #include "third_party/WebKit/public/platform/WebURL.h" |
| 33 #include "third_party/WebKit/public/platform/WebURLError.h" | 32 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 34 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" | 33 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" |
| 35 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 34 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 36 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 35 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 37 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 36 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 38 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 37 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
| 39 #include "webkit/child/multipart_response_delegate.h" | 38 #include "webkit/child/multipart_response_delegate.h" |
| 40 #include "webkit/child/resource_loader_bridge.h" | |
| 41 #include "webkit/child/weburlresponse_extradata_impl.h" | 39 #include "webkit/child/weburlresponse_extradata_impl.h" |
| 42 | 40 |
| 43 using base::Time; | 41 using base::Time; |
| 44 using base::TimeTicks; | 42 using base::TimeTicks; |
| 45 using blink::WebData; | 43 using blink::WebData; |
| 46 using blink::WebHTTPBody; | 44 using blink::WebHTTPBody; |
| 47 using blink::WebHTTPHeaderVisitor; | 45 using blink::WebHTTPHeaderVisitor; |
| 48 using blink::WebHTTPLoadInfo; | 46 using blink::WebHTTPLoadInfo; |
| 49 using blink::WebReferrerPolicy; | 47 using blink::WebReferrerPolicy; |
| 50 using blink::WebSecurityPolicy; | 48 using blink::WebSecurityPolicy; |
| 51 using blink::WebString; | 49 using blink::WebString; |
| 52 using blink::WebURL; | 50 using blink::WebURL; |
| 53 using blink::WebURLError; | 51 using blink::WebURLError; |
| 54 using blink::WebURLLoadTiming; | 52 using blink::WebURLLoadTiming; |
| 55 using blink::WebURLLoader; | 53 using blink::WebURLLoader; |
| 56 using blink::WebURLLoaderClient; | 54 using blink::WebURLLoaderClient; |
| 57 using blink::WebURLRequest; | 55 using blink::WebURLRequest; |
| 58 using blink::WebURLResponse; | 56 using blink::WebURLResponse; |
| 59 using webkit_glue::MultipartResponseDelegate; | 57 using webkit_glue::MultipartResponseDelegate; |
| 60 using webkit_glue::ResourceDevToolsInfo; | 58 using webkit_glue::ResourceDevToolsInfo; |
| 61 using webkit_glue::ResourceLoaderBridge; | |
| 62 using webkit_glue::ResourceResponseInfo; | 59 using webkit_glue::ResourceResponseInfo; |
| 63 using webkit_glue::WebURLResponseExtraDataImpl; | 60 using webkit_glue::WebURLResponseExtraDataImpl; |
| 64 | 61 |
| 65 namespace content { | 62 namespace content { |
| 66 | 63 |
| 67 // Utilities ------------------------------------------------------------------ | 64 // Utilities ------------------------------------------------------------------ |
| 68 | 65 |
| 69 namespace { | 66 namespace { |
| 70 | 67 |
| 71 const char kThrottledErrorDescription[] = | 68 const char kThrottledErrorDescription[] = |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 virtual ~Context() {} | 259 virtual ~Context() {} |
| 263 | 260 |
| 264 // We can optimize the handling of data URLs in most cases. | 261 // We can optimize the handling of data URLs in most cases. |
| 265 bool CanHandleDataURL(const GURL& url) const; | 262 bool CanHandleDataURL(const GURL& url) const; |
| 266 void HandleDataURL(); | 263 void HandleDataURL(); |
| 267 | 264 |
| 268 WebURLLoaderImpl* loader_; | 265 WebURLLoaderImpl* loader_; |
| 269 WebURLRequest request_; | 266 WebURLRequest request_; |
| 270 WebURLLoaderClient* client_; | 267 WebURLLoaderClient* client_; |
| 271 WebReferrerPolicy referrer_policy_; | 268 WebReferrerPolicy referrer_policy_; |
| 272 scoped_ptr<ResourceLoaderBridge> bridge_; | |
| 273 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; | 269 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; |
| 274 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; | 270 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; |
| 275 scoped_ptr<ResourceLoaderBridge> completed_bridge_; | 271 int request_id_; |
| 276 }; | 272 }; |
| 277 | 273 |
| 278 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) | 274 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) |
| 279 : loader_(loader), | 275 : loader_(loader), |
| 280 client_(NULL), | 276 client_(NULL), |
| 281 referrer_policy_(blink::WebReferrerPolicyDefault) { | 277 referrer_policy_(blink::WebReferrerPolicyDefault), |
| 278 request_id_(-1) { | |
| 282 } | 279 } |
| 283 | 280 |
| 284 void WebURLLoaderImpl::Context::Cancel() { | 281 void WebURLLoaderImpl::Context::Cancel() { |
| 285 // The bridge will still send OnCompletedRequest, which will Release() us, so | 282 ChildThread::current()->resource_dispatcher()->CancelPendingRequest( |
| 286 // we don't do that here. | 283 request_id_); |
| 287 if (bridge_) | |
| 288 bridge_->Cancel(); | |
| 289 | 284 |
| 290 // Ensure that we do not notify the multipart delegate anymore as it has | 285 // Ensure that we do not notify the multipart delegate anymore as it has |
| 291 // its own pointer to the client. | 286 // its own pointer to the client. |
| 292 if (multipart_delegate_) | 287 if (multipart_delegate_) |
| 293 multipart_delegate_->Cancel(); | 288 multipart_delegate_->Cancel(); |
| 294 | 289 |
| 295 // Do not make any further calls to the client. | 290 // Do not make any further calls to the client. |
| 296 client_ = NULL; | 291 client_ = NULL; |
| 297 loader_ = NULL; | 292 loader_ = NULL; |
| 298 } | 293 } |
| 299 | 294 |
| 300 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { | 295 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { |
| 301 if (bridge_) | 296 ChildThread::current()->resource_dispatcher()->SetDefersLoading(request_id_, |
| 302 bridge_->SetDefersLoading(value); | 297 value); |
| 303 } | 298 } |
| 304 | 299 |
| 305 void WebURLLoaderImpl::Context::DidChangePriority( | 300 void WebURLLoaderImpl::Context::DidChangePriority( |
| 306 WebURLRequest::Priority new_priority, int intra_priority_value) { | 301 WebURLRequest::Priority new_priority, |
| 307 if (bridge_) | 302 int intra_priority_value) { |
| 308 bridge_->DidChangePriority( | 303 ChildThread::current()->resource_dispatcher()->DidChangePriority( |
| 309 ConvertWebKitPriorityToNetPriority(new_priority), intra_priority_value); | 304 0 /*routing_id*/, |
|
tfarina
2014/04/24 00:17:38
John, is this right?
jam
2014/04/24 16:14:08
since routing_id isn't used, it should be removed
tfarina
2014/04/26 04:23:50
Done.
| |
| 305 request_id_, | |
| 306 ConvertWebKitPriorityToNetPriority(new_priority), | |
| 307 intra_priority_value); | |
| 310 } | 308 } |
| 311 | 309 |
| 312 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request, | 310 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request, |
| 313 SyncLoadResponse* sync_load_response) { | 311 SyncLoadResponse* sync_load_response) { |
| 314 DCHECK(!bridge_.get()); | |
| 315 | |
| 316 request_ = request; // Save the request. | 312 request_ = request; // Save the request. |
| 317 | 313 |
| 318 GURL url = request.url(); | 314 GURL url = request.url(); |
| 319 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 315 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
| 320 if (sync_load_response) { | 316 if (sync_load_response) { |
| 321 // This is a sync load. Do the work now. | 317 // This is a sync load. Do the work now. |
| 322 sync_load_response->url = url; | 318 sync_load_response->url = url; |
| 323 std::string data; | 319 std::string data; |
| 324 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 320 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
| 325 &sync_load_response->data, | 321 &sync_load_response->data, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 ResourceType::FromTargetType(request.targetType()); | 389 ResourceType::FromTargetType(request.targetType()); |
| 394 request_info.priority = | 390 request_info.priority = |
| 395 ConvertWebKitPriorityToNetPriority(request.priority()); | 391 ConvertWebKitPriorityToNetPriority(request.priority()); |
| 396 request_info.appcache_host_id = request.appCacheHostID(); | 392 request_info.appcache_host_id = request.appCacheHostID(); |
| 397 request_info.routing_id = request.requestorID(); | 393 request_info.routing_id = request.requestorID(); |
| 398 request_info.download_to_file = request.downloadToFile(); | 394 request_info.download_to_file = request.downloadToFile(); |
| 399 request_info.has_user_gesture = request.hasUserGesture(); | 395 request_info.has_user_gesture = request.hasUserGesture(); |
| 400 request_info.extra_data = request.extraData(); | 396 request_info.extra_data = request.extraData(); |
| 401 referrer_policy_ = request.referrerPolicy(); | 397 referrer_policy_ = request.referrerPolicy(); |
| 402 request_info.referrer_policy = request.referrerPolicy(); | 398 request_info.referrer_policy = request.referrerPolicy(); |
| 403 bridge_.reset(ChildThread::current()->CreateBridge(request_info)); | 399 ResourceDispatcher* dispatcher = |
| 400 ChildThread::current()->resource_dispatcher(); | |
| 401 | |
| 402 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; | |
| 404 | 403 |
| 405 if (!request.httpBody().isNull()) { | 404 if (!request.httpBody().isNull()) { |
| 406 // GET and HEAD requests shouldn't have http bodies. | 405 // GET and HEAD requests shouldn't have http bodies. |
| 407 DCHECK(method != "GET" && method != "HEAD"); | 406 DCHECK(method != "GET" && method != "HEAD"); |
| 408 const WebHTTPBody& httpBody = request.httpBody(); | 407 const WebHTTPBody& httpBody = request.httpBody(); |
| 409 size_t i = 0; | 408 size_t i = 0; |
| 410 WebHTTPBody::Element element; | 409 WebHTTPBody::Element element; |
| 411 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; | |
| 412 while (httpBody.elementAt(i++, element)) { | 410 while (httpBody.elementAt(i++, element)) { |
| 413 switch (element.type) { | 411 switch (element.type) { |
| 414 case WebHTTPBody::Element::TypeData: | 412 case WebHTTPBody::Element::TypeData: |
| 415 if (!element.data.isEmpty()) { | 413 if (!element.data.isEmpty()) { |
| 416 // WebKit sometimes gives up empty data to append. These aren't | 414 // WebKit sometimes gives up empty data to append. These aren't |
| 417 // necessary so we just optimize those out here. | 415 // necessary so we just optimize those out here. |
| 418 request_body->AppendBytes( | 416 request_body->AppendBytes( |
| 419 element.data.data(), static_cast<int>(element.data.size())); | 417 element.data.data(), static_cast<int>(element.data.size())); |
| 420 } | 418 } |
| 421 break; | 419 break; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 443 break; | 441 break; |
| 444 } | 442 } |
| 445 case WebHTTPBody::Element::TypeBlob: | 443 case WebHTTPBody::Element::TypeBlob: |
| 446 request_body->AppendBlob(element.blobUUID.utf8()); | 444 request_body->AppendBlob(element.blobUUID.utf8()); |
| 447 break; | 445 break; |
| 448 default: | 446 default: |
| 449 NOTREACHED(); | 447 NOTREACHED(); |
| 450 } | 448 } |
| 451 } | 449 } |
| 452 request_body->set_identifier(request.httpBody().identifier()); | 450 request_body->set_identifier(request.httpBody().identifier()); |
| 453 bridge_->SetRequestBody(request_body.get()); | 451 |
| 454 } | 452 } |
| 455 | 453 |
| 456 if (sync_load_response) { | 454 if (sync_load_response) { |
| 457 bridge_->SyncLoad(sync_load_response); | 455 dispatcher->StartSync(request_info, request_body.get(), sync_load_response); |
| 458 return; | 456 return; |
| 459 } | 457 } |
| 460 | 458 |
| 461 if (bridge_->Start(this)) { | 459 request_id_ = dispatcher->StartAsync(request_info, request_body.get(), this); |
| 462 AddRef(); // Balanced in OnCompletedRequest | |
| 463 } else { | |
| 464 bridge_.reset(); | |
| 465 } | |
| 466 } | 460 } |
| 467 | 461 |
| 468 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { | 462 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { |
| 469 if (client_) | 463 if (client_) |
| 470 client_->didSendData(loader_, position, size); | 464 client_->didSendData(loader_, position, size); |
| 471 } | 465 } |
| 472 | 466 |
| 473 bool WebURLLoaderImpl::Context::OnReceivedRedirect( | 467 bool WebURLLoaderImpl::Context::OnReceivedRedirect( |
| 474 const GURL& new_url, | 468 const GURL& new_url, |
| 475 const ResourceResponseInfo& info, | 469 const ResourceResponseInfo& info, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 const base::TimeTicks& completion_time, | 607 const base::TimeTicks& completion_time, |
| 614 int64 total_transfer_size) { | 608 int64 total_transfer_size) { |
| 615 if (ftp_listing_delegate_) { | 609 if (ftp_listing_delegate_) { |
| 616 ftp_listing_delegate_->OnCompletedRequest(); | 610 ftp_listing_delegate_->OnCompletedRequest(); |
| 617 ftp_listing_delegate_.reset(NULL); | 611 ftp_listing_delegate_.reset(NULL); |
| 618 } else if (multipart_delegate_) { | 612 } else if (multipart_delegate_) { |
| 619 multipart_delegate_->OnCompletedRequest(); | 613 multipart_delegate_->OnCompletedRequest(); |
| 620 multipart_delegate_.reset(NULL); | 614 multipart_delegate_.reset(NULL); |
| 621 } | 615 } |
| 622 | 616 |
| 623 // Prevent any further IPC to the browser now that we're complete, but | |
| 624 // don't delete it to keep any downloaded temp files alive. | |
| 625 DCHECK(!completed_bridge_.get()); | |
| 626 completed_bridge_.swap(bridge_); | |
| 627 | |
| 628 if (client_) { | 617 if (client_) { |
| 629 if (error_code != net::OK) { | 618 if (error_code != net::OK) { |
| 630 client_->didFail(loader_, CreateError(request_.url(), | 619 client_->didFail(loader_, CreateError(request_.url(), |
| 631 stale_copy_in_cache, | 620 stale_copy_in_cache, |
| 632 error_code)); | 621 error_code)); |
| 633 } else { | 622 } else { |
| 634 client_->didFinishLoading( | 623 client_->didFinishLoading( |
| 635 loader_, (completion_time - TimeTicks()).InSecondsF(), | 624 loader_, (completion_time - TimeTicks()).InSecondsF(), |
| 636 total_transfer_size); | 625 total_transfer_size); |
| 637 } | 626 } |
| 638 } | 627 } |
| 639 | 628 |
| 640 // We are done with the bridge now, and so we need to release the reference | 629 // We are done with the bridge now, and so we need to release the reference |
| 641 // to ourselves that we took on behalf of the bridge. This may cause our | 630 // to ourselves that we took on behalf of the bridge. This may cause our |
| 642 // destruction. | 631 // destruction. |
| 643 Release(); | 632 Release(); |
| 644 } | 633 } |
| 645 | 634 |
| 646 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { | 635 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { |
| 647 DCHECK(url.SchemeIs("data")); | 636 DCHECK(url.SchemeIs("data")); |
| 648 | 637 |
| 649 // Optimize for the case where we can handle a data URL locally. We must | 638 // Optimize for the case where we can handle a data URL locally. We must |
| 650 // skip this for data URLs targetted at frames since those could trigger a | 639 // skip this for data URLs targetted at frames since those could trigger a |
| 651 // download. | 640 // download. |
| 652 // | 641 // |
| 653 // NOTE: We special case MIME types we can render both for performance | 642 // NOTE: We special case MIME types we can render both for performance |
| 654 // reasons as well as to support unit tests, which do not have an underlying | 643 // reasons as well as to support unit tests. |
| 655 // ResourceLoaderBridge implementation. | |
| 656 | 644 |
| 657 #if defined(OS_ANDROID) | 645 #if defined(OS_ANDROID) |
| 658 // For compatibility reasons on Android we need to expose top-level data:// | 646 // For compatibility reasons on Android we need to expose top-level data:// |
| 659 // to the browser. | 647 // to the browser. |
| 660 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) | 648 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) |
| 661 return false; | 649 return false; |
| 662 #endif | 650 #endif |
| 663 | 651 |
| 664 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && | 652 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && |
| 665 request_.targetType() != WebURLRequest::TargetIsSubframe) | 653 request_.targetType() != WebURLRequest::TargetIsSubframe) |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 void WebURLLoaderImpl::setDefersLoading(bool value) { | 855 void WebURLLoaderImpl::setDefersLoading(bool value) { |
| 868 context_->SetDefersLoading(value); | 856 context_->SetDefersLoading(value); |
| 869 } | 857 } |
| 870 | 858 |
| 871 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, | 859 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, |
| 872 int intra_priority_value) { | 860 int intra_priority_value) { |
| 873 context_->DidChangePriority(new_priority, intra_priority_value); | 861 context_->DidChangePriority(new_priority, intra_priority_value); |
| 874 } | 862 } |
| 875 | 863 |
| 876 } // namespace content | 864 } // namespace content |
| OLD | NEW |