| 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/load_flags.h" | 22 #include "net/base/load_flags.h" |
| 24 #include "net/base/mime_util.h" | 23 #include "net/base/mime_util.h" |
| 25 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 26 #include "net/base/net_util.h" | 25 #include "net/base/net_util.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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 NOTREACHED(); | 209 NOTREACHED(); |
| 213 return net::LOW; | 210 return net::LOW; |
| 214 } | 211 } |
| 215 } | 212 } |
| 216 | 213 |
| 217 } // namespace | 214 } // namespace |
| 218 | 215 |
| 219 // WebURLLoaderImpl::Context -------------------------------------------------- | 216 // WebURLLoaderImpl::Context -------------------------------------------------- |
| 220 | 217 |
| 221 // This inner class exists since the WebURLLoader may be deleted while inside a | 218 // This inner class exists since the WebURLLoader may be deleted while inside a |
| 222 // call to WebURLLoaderClient. The bridge requires its Peer to stay alive | 219 // call to WebURLLoaderClient. |
| 223 // until it receives OnCompletedRequest. | |
| 224 class WebURLLoaderImpl::Context : public base::RefCounted<Context>, | 220 class WebURLLoaderImpl::Context : public base::RefCounted<Context>, |
| 225 public RequestPeer { | 221 public RequestPeer { |
| 226 public: | 222 public: |
| 227 explicit Context(WebURLLoaderImpl* loader); | 223 explicit Context(WebURLLoaderImpl* loader); |
| 228 | 224 |
| 229 WebURLLoaderClient* client() const { return client_; } | 225 WebURLLoaderClient* client() const { return client_; } |
| 230 void set_client(WebURLLoaderClient* client) { client_ = client; } | 226 void set_client(WebURLLoaderClient* client) { client_ = client; } |
| 231 | 227 |
| 232 void Cancel(); | 228 void Cancel(); |
| 233 void SetDefersLoading(bool value); | 229 void SetDefersLoading(bool value); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 261 virtual ~Context() {} | 257 virtual ~Context() {} |
| 262 | 258 |
| 263 // We can optimize the handling of data URLs in most cases. | 259 // We can optimize the handling of data URLs in most cases. |
| 264 bool CanHandleDataURL(const GURL& url) const; | 260 bool CanHandleDataURL(const GURL& url) const; |
| 265 void HandleDataURL(); | 261 void HandleDataURL(); |
| 266 | 262 |
| 267 WebURLLoaderImpl* loader_; | 263 WebURLLoaderImpl* loader_; |
| 268 WebURLRequest request_; | 264 WebURLRequest request_; |
| 269 WebURLLoaderClient* client_; | 265 WebURLLoaderClient* client_; |
| 270 WebReferrerPolicy referrer_policy_; | 266 WebReferrerPolicy referrer_policy_; |
| 271 scoped_ptr<ResourceLoaderBridge> bridge_; | 267 ResourceDispatcher* dispatcher_; |
| 272 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; | 268 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; |
| 273 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; | 269 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; |
| 274 scoped_ptr<ResourceLoaderBridge> completed_bridge_; | |
| 275 }; | 270 }; |
| 276 | 271 |
| 277 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) | 272 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) |
| 278 : loader_(loader), | 273 : loader_(loader), |
| 279 client_(NULL), | 274 client_(NULL), |
| 280 referrer_policy_(blink::WebReferrerPolicyDefault) { | 275 referrer_policy_(blink::WebReferrerPolicyDefault), |
| 276 dispatcher_(NULL) { |
| 281 } | 277 } |
| 282 | 278 |
| 283 void WebURLLoaderImpl::Context::Cancel() { | 279 void WebURLLoaderImpl::Context::Cancel() { |
| 284 // The bridge will still send OnCompletedRequest, which will Release() us, so | 280 // The dispatcher will still send OnCompletedRequest, which will Release() us, |
| 285 // we don't do that here. | 281 // so we don't do that here. |
| 286 if (bridge_) | 282 if (dispatcher_) |
| 287 bridge_->Cancel(); | 283 dispatcher_->Cancel(); |
| 288 | 284 |
| 289 // 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 |
| 290 // its own pointer to the client. | 286 // its own pointer to the client. |
| 291 if (multipart_delegate_) | 287 if (multipart_delegate_) |
| 292 multipart_delegate_->Cancel(); | 288 multipart_delegate_->Cancel(); |
| 293 | 289 |
| 294 // Do not make any further calls to the client. | 290 // Do not make any further calls to the client. |
| 295 client_ = NULL; | 291 client_ = NULL; |
| 296 loader_ = NULL; | 292 loader_ = NULL; |
| 297 } | 293 } |
| 298 | 294 |
| 299 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { | 295 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { |
| 300 if (bridge_) | 296 if (dispatcher_) |
| 301 bridge_->SetDefersLoading(value); | 297 dispatcher_->SetDefersLoading(value); |
| 302 } | 298 } |
| 303 | 299 |
| 304 void WebURLLoaderImpl::Context::DidChangePriority( | 300 void WebURLLoaderImpl::Context::DidChangePriority( |
| 305 WebURLRequest::Priority new_priority) { | 301 WebURLRequest::Priority new_priority) { |
| 306 if (bridge_) | 302 if (dispatcher_) |
| 307 bridge_->DidChangePriority( | 303 dispatcher_->DidChangePriority( |
| 308 ConvertWebKitPriorityToNetPriority(new_priority)); | 304 ConvertWebKitPriorityToNetPriority(new_priority)); |
| 309 } | 305 } |
| 310 | 306 |
| 311 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request, | 307 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request, |
| 312 SyncLoadResponse* sync_load_response) { | 308 SyncLoadResponse* sync_load_response) { |
| 313 DCHECK(!bridge_.get()); | 309 DCHECK(!dispatcher_); |
| 314 | 310 |
| 315 request_ = request; // Save the request. | 311 request_ = request; // Save the request. |
| 316 | 312 |
| 317 GURL url = request.url(); | 313 GURL url = request.url(); |
| 318 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 314 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
| 319 if (sync_load_response) { | 315 if (sync_load_response) { |
| 320 // This is a sync load. Do the work now. | 316 // This is a sync load. Do the work now. |
| 321 sync_load_response->url = url; | 317 sync_load_response->url = url; |
| 322 std::string data; | 318 std::string data; |
| 323 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 319 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 ResourceType::FromTargetType(request.targetType()); | 388 ResourceType::FromTargetType(request.targetType()); |
| 393 request_info.priority = | 389 request_info.priority = |
| 394 ConvertWebKitPriorityToNetPriority(request.priority()); | 390 ConvertWebKitPriorityToNetPriority(request.priority()); |
| 395 request_info.appcache_host_id = request.appCacheHostID(); | 391 request_info.appcache_host_id = request.appCacheHostID(); |
| 396 request_info.routing_id = request.requestorID(); | 392 request_info.routing_id = request.requestorID(); |
| 397 request_info.download_to_file = request.downloadToFile(); | 393 request_info.download_to_file = request.downloadToFile(); |
| 398 request_info.has_user_gesture = request.hasUserGesture(); | 394 request_info.has_user_gesture = request.hasUserGesture(); |
| 399 request_info.extra_data = request.extraData(); | 395 request_info.extra_data = request.extraData(); |
| 400 referrer_policy_ = request.referrerPolicy(); | 396 referrer_policy_ = request.referrerPolicy(); |
| 401 request_info.referrer_policy = request.referrerPolicy(); | 397 request_info.referrer_policy = request.referrerPolicy(); |
| 402 bridge_.reset(ChildThread::current()->CreateBridge(request_info)); | 398 dispatcher_ = ChildThread::current()->resource_dispatcher(); |
| 399 dispatcher_->CreateBridge(request_info); |
| 403 | 400 |
| 404 if (!request.httpBody().isNull()) { | 401 if (!request.httpBody().isNull()) { |
| 405 // GET and HEAD requests shouldn't have http bodies. | 402 // GET and HEAD requests shouldn't have http bodies. |
| 406 DCHECK(method != "GET" && method != "HEAD"); | 403 DCHECK(method != "GET" && method != "HEAD"); |
| 407 const WebHTTPBody& httpBody = request.httpBody(); | 404 const WebHTTPBody& httpBody = request.httpBody(); |
| 408 size_t i = 0; | 405 size_t i = 0; |
| 409 WebHTTPBody::Element element; | 406 WebHTTPBody::Element element; |
| 410 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; | 407 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody; |
| 411 while (httpBody.elementAt(i++, element)) { | 408 while (httpBody.elementAt(i++, element)) { |
| 412 switch (element.type) { | 409 switch (element.type) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 442 break; | 439 break; |
| 443 } | 440 } |
| 444 case WebHTTPBody::Element::TypeBlob: | 441 case WebHTTPBody::Element::TypeBlob: |
| 445 request_body->AppendBlob(element.blobUUID.utf8()); | 442 request_body->AppendBlob(element.blobUUID.utf8()); |
| 446 break; | 443 break; |
| 447 default: | 444 default: |
| 448 NOTREACHED(); | 445 NOTREACHED(); |
| 449 } | 446 } |
| 450 } | 447 } |
| 451 request_body->set_identifier(request.httpBody().identifier()); | 448 request_body->set_identifier(request.httpBody().identifier()); |
| 452 bridge_->SetRequestBody(request_body.get()); | 449 dispatcher_->SetRequestBody(request_body.get()); |
| 453 } | 450 } |
| 454 | 451 |
| 455 if (sync_load_response) { | 452 if (sync_load_response) { |
| 456 bridge_->SyncLoad(sync_load_response); | 453 dispatcher_->SyncLoad(sync_load_response); |
| 457 return; | 454 return; |
| 458 } | 455 } |
| 459 | 456 |
| 460 if (bridge_->Start(this)) { | 457 if (dispatcher_->Start(this)) { |
| 461 AddRef(); // Balanced in OnCompletedRequest | 458 AddRef(); // Balanced in OnCompletedRequest |
| 462 } else { | |
| 463 bridge_.reset(); | |
| 464 } | 459 } |
| 465 } | 460 } |
| 466 | 461 |
| 467 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { | 462 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { |
| 468 if (client_) | 463 if (client_) |
| 469 client_->didSendData(loader_, position, size); | 464 client_->didSendData(loader_, position, size); |
| 470 } | 465 } |
| 471 | 466 |
| 472 bool WebURLLoaderImpl::Context::OnReceivedRedirect( | 467 bool WebURLLoaderImpl::Context::OnReceivedRedirect( |
| 473 const GURL& new_url, | 468 const GURL& new_url, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 const base::TimeTicks& completion_time, | 607 const base::TimeTicks& completion_time, |
| 613 int64 total_transfer_size) { | 608 int64 total_transfer_size) { |
| 614 if (ftp_listing_delegate_) { | 609 if (ftp_listing_delegate_) { |
| 615 ftp_listing_delegate_->OnCompletedRequest(); | 610 ftp_listing_delegate_->OnCompletedRequest(); |
| 616 ftp_listing_delegate_.reset(NULL); | 611 ftp_listing_delegate_.reset(NULL); |
| 617 } else if (multipart_delegate_) { | 612 } else if (multipart_delegate_) { |
| 618 multipart_delegate_->OnCompletedRequest(); | 613 multipart_delegate_->OnCompletedRequest(); |
| 619 multipart_delegate_.reset(NULL); | 614 multipart_delegate_.reset(NULL); |
| 620 } | 615 } |
| 621 | 616 |
| 622 // Prevent any further IPC to the browser now that we're complete, but | |
| 623 // don't delete it to keep any downloaded temp files alive. | |
| 624 DCHECK(!completed_bridge_.get()); | |
| 625 completed_bridge_.swap(bridge_); | |
| 626 | |
| 627 if (client_) { | 617 if (client_) { |
| 628 if (error_code != net::OK) { | 618 if (error_code != net::OK) { |
| 629 client_->didFail(loader_, CreateError(request_.url(), | 619 client_->didFail(loader_, CreateError(request_.url(), |
| 630 stale_copy_in_cache, | 620 stale_copy_in_cache, |
| 631 error_code)); | 621 error_code)); |
| 632 } else { | 622 } else { |
| 633 client_->didFinishLoading( | 623 client_->didFinishLoading( |
| 634 loader_, (completion_time - TimeTicks()).InSecondsF(), | 624 loader_, (completion_time - TimeTicks()).InSecondsF(), |
| 635 total_transfer_size); | 625 total_transfer_size); |
| 636 } | 626 } |
| 637 } | 627 } |
| 638 | 628 |
| 639 // We are done with the bridge now, and so we need to release the reference | |
| 640 // to ourselves that we took on behalf of the bridge. This may cause our | |
| 641 // destruction. | |
| 642 Release(); | 629 Release(); |
| 643 } | 630 } |
| 644 | 631 |
| 645 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { | 632 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { |
| 646 DCHECK(url.SchemeIs("data")); | 633 DCHECK(url.SchemeIs("data")); |
| 647 | 634 |
| 648 // Optimize for the case where we can handle a data URL locally. We must | 635 // Optimize for the case where we can handle a data URL locally. We must |
| 649 // skip this for data URLs targetted at frames since those could trigger a | 636 // skip this for data URLs targetted at frames since those could trigger a |
| 650 // download. | 637 // download. |
| 651 // | 638 // |
| 652 // NOTE: We special case MIME types we can render both for performance | 639 // NOTE: We special case MIME types we can render both for performance |
| 653 // reasons as well as to support unit tests, which do not have an underlying | 640 // reasons as well as to support unit tests. |
| 654 // ResourceLoaderBridge implementation. | |
| 655 | 641 |
| 656 #if defined(OS_ANDROID) | 642 #if defined(OS_ANDROID) |
| 657 // For compatibility reasons on Android we need to expose top-level data:// | 643 // For compatibility reasons on Android we need to expose top-level data:// |
| 658 // to the browser. | 644 // to the browser. |
| 659 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) | 645 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) |
| 660 return false; | 646 return false; |
| 661 #endif | 647 #endif |
| 662 | 648 |
| 663 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && | 649 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && |
| 664 request_.targetType() != WebURLRequest::TargetIsSubframe) | 650 request_.targetType() != WebURLRequest::TargetIsSubframe) |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 | 851 |
| 866 void WebURLLoaderImpl::setDefersLoading(bool value) { | 852 void WebURLLoaderImpl::setDefersLoading(bool value) { |
| 867 context_->SetDefersLoading(value); | 853 context_->SetDefersLoading(value); |
| 868 } | 854 } |
| 869 | 855 |
| 870 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority) { | 856 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority) { |
| 871 context_->DidChangePriority(new_priority); | 857 context_->DidChangePriority(new_priority); |
| 872 } | 858 } |
| 873 | 859 |
| 874 } // namespace content | 860 } // namespace content |
| OLD | NEW |