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