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" |
| 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" | |
41 #include "webkit/child/weburlresponse_extradata_impl.h" | 40 #include "webkit/child/weburlresponse_extradata_impl.h" |
42 | 41 |
43 using base::Time; | 42 using base::Time; |
44 using base::TimeTicks; | 43 using base::TimeTicks; |
45 using blink::WebData; | 44 using blink::WebData; |
46 using blink::WebHTTPBody; | 45 using blink::WebHTTPBody; |
47 using blink::WebHTTPHeaderVisitor; | 46 using blink::WebHTTPHeaderVisitor; |
48 using blink::WebHTTPLoadInfo; | 47 using blink::WebHTTPLoadInfo; |
49 using blink::WebReferrerPolicy; | 48 using blink::WebReferrerPolicy; |
50 using blink::WebSecurityPolicy; | 49 using blink::WebSecurityPolicy; |
51 using blink::WebString; | 50 using blink::WebString; |
52 using blink::WebURL; | 51 using blink::WebURL; |
53 using blink::WebURLError; | 52 using blink::WebURLError; |
54 using blink::WebURLLoadTiming; | 53 using blink::WebURLLoadTiming; |
55 using blink::WebURLLoader; | 54 using blink::WebURLLoader; |
56 using blink::WebURLLoaderClient; | 55 using blink::WebURLLoaderClient; |
57 using blink::WebURLRequest; | 56 using blink::WebURLRequest; |
58 using blink::WebURLResponse; | 57 using blink::WebURLResponse; |
59 using webkit_glue::MultipartResponseDelegate; | 58 using webkit_glue::MultipartResponseDelegate; |
60 using webkit_glue::ResourceDevToolsInfo; | 59 using webkit_glue::ResourceDevToolsInfo; |
61 using webkit_glue::ResourceLoaderBridge; | |
62 using webkit_glue::ResourceResponseInfo; | 60 using webkit_glue::ResourceResponseInfo; |
63 using webkit_glue::WebURLResponseExtraDataImpl; | 61 using webkit_glue::WebURLResponseExtraDataImpl; |
64 | 62 |
65 namespace content { | 63 namespace content { |
66 | 64 |
67 // Utilities ------------------------------------------------------------------ | 65 // Utilities ------------------------------------------------------------------ |
68 | 66 |
69 namespace { | 67 namespace { |
70 | 68 |
71 const char kThrottledErrorDescription[] = | 69 const char kThrottledErrorDescription[] = |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 virtual void OnCompletedRequest( | 250 virtual void OnCompletedRequest( |
253 int error_code, | 251 int error_code, |
254 bool was_ignored_by_handler, | 252 bool was_ignored_by_handler, |
255 bool stale_copy_in_cache, | 253 bool stale_copy_in_cache, |
256 const std::string& security_info, | 254 const std::string& security_info, |
257 const base::TimeTicks& completion_time, | 255 const base::TimeTicks& completion_time, |
258 int64 total_transfer_size) OVERRIDE; | 256 int64 total_transfer_size) OVERRIDE; |
259 | 257 |
260 private: | 258 private: |
261 friend class base::RefCounted<Context>; | 259 friend class base::RefCounted<Context>; |
262 virtual ~Context() {} | 260 virtual ~Context(); |
263 | 261 |
264 // We can optimize the handling of data URLs in most cases. | 262 // We can optimize the handling of data URLs in most cases. |
265 bool CanHandleDataURL(const GURL& url) const; | 263 bool CanHandleDataURL(const GURL& url) const; |
266 void HandleDataURL(); | 264 void HandleDataURL(); |
267 | 265 |
268 WebURLLoaderImpl* loader_; | 266 WebURLLoaderImpl* loader_; |
269 WebURLRequest request_; | 267 WebURLRequest request_; |
270 WebURLLoaderClient* client_; | 268 WebURLLoaderClient* client_; |
271 WebReferrerPolicy referrer_policy_; | 269 WebReferrerPolicy referrer_policy_; |
272 scoped_ptr<ResourceLoaderBridge> bridge_; | |
273 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; | 270 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; |
274 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; | 271 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; |
275 scoped_ptr<ResourceLoaderBridge> completed_bridge_; | 272 int request_id_; |
276 }; | 273 }; |
277 | 274 |
278 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) | 275 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) |
279 : loader_(loader), | 276 : loader_(loader), |
280 client_(NULL), | 277 client_(NULL), |
281 referrer_policy_(blink::WebReferrerPolicyDefault) { | 278 referrer_policy_(blink::WebReferrerPolicyDefault), |
| 279 request_id_(-1) { |
282 } | 280 } |
283 | 281 |
284 void WebURLLoaderImpl::Context::Cancel() { | 282 void WebURLLoaderImpl::Context::Cancel() { |
285 // The bridge will still send OnCompletedRequest, which will Release() us, so | 283 if (ChildThread::current()) // NULL in unittest. |
286 // we don't do that here. | 284 ChildThread::current()->resource_dispatcher()->Cancel(request_id_); |
287 if (bridge_) | |
288 bridge_->Cancel(); | |
289 | 285 |
290 // Ensure that we do not notify the multipart delegate anymore as it has | 286 // Ensure that we do not notify the multipart delegate anymore as it has |
291 // its own pointer to the client. | 287 // its own pointer to the client. |
292 if (multipart_delegate_) | 288 if (multipart_delegate_) |
293 multipart_delegate_->Cancel(); | 289 multipart_delegate_->Cancel(); |
294 | 290 |
295 // Do not make any further calls to the client. | 291 // Do not make any further calls to the client. |
296 client_ = NULL; | 292 client_ = NULL; |
297 loader_ = NULL; | 293 loader_ = NULL; |
298 } | 294 } |
299 | 295 |
300 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { | 296 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { |
301 if (bridge_) | 297 ChildThread::current()->resource_dispatcher()->SetDefersLoading(request_id_, |
302 bridge_->SetDefersLoading(value); | 298 value); |
303 } | 299 } |
304 | 300 |
305 void WebURLLoaderImpl::Context::DidChangePriority( | 301 void WebURLLoaderImpl::Context::DidChangePriority( |
306 WebURLRequest::Priority new_priority, int intra_priority_value) { | 302 WebURLRequest::Priority new_priority, |
307 if (bridge_) | 303 int intra_priority_value) { |
308 bridge_->DidChangePriority( | 304 ChildThread::current()->resource_dispatcher()->DidChangePriority( |
309 ConvertWebKitPriorityToNetPriority(new_priority), intra_priority_value); | 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 AddRef(); // Balanced in OnCompletedRequest. |
462 AddRef(); // Balanced in OnCompletedRequest | 460 request_id_ = dispatcher->StartAsync(request_info, request_body.get(), this); |
463 } else { | |
464 bridge_.reset(); | |
465 } | |
466 } | 461 } |
467 | 462 |
468 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { | 463 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { |
469 if (client_) | 464 if (client_) |
470 client_->didSendData(loader_, position, size); | 465 client_->didSendData(loader_, position, size); |
471 } | 466 } |
472 | 467 |
473 bool WebURLLoaderImpl::Context::OnReceivedRedirect( | 468 bool WebURLLoaderImpl::Context::OnReceivedRedirect( |
474 const GURL& new_url, | 469 const GURL& new_url, |
475 const ResourceResponseInfo& info, | 470 const ResourceResponseInfo& info, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 ftp_listing_delegate_->OnReceivedData(data, data_length); | 587 ftp_listing_delegate_->OnReceivedData(data, data_length); |
593 } else if (multipart_delegate_) { | 588 } else if (multipart_delegate_) { |
594 // The multipart delegate will make the appropriate calls to | 589 // The multipart delegate will make the appropriate calls to |
595 // client_->didReceiveData and client_->didReceiveResponse. | 590 // client_->didReceiveData and client_->didReceiveResponse. |
596 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length); | 591 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length); |
597 } else { | 592 } else { |
598 client_->didReceiveData(loader_, data, data_length, encoded_data_length); | 593 client_->didReceiveData(loader_, data, data_length, encoded_data_length); |
599 } | 594 } |
600 } | 595 } |
601 | 596 |
602 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( | 597 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(const char* data, |
603 const char* data, int len) { | 598 int len) { |
604 if (client_) | 599 if (client_) |
605 client_->didReceiveCachedMetadata(loader_, data, len); | 600 client_->didReceiveCachedMetadata(loader_, data, len); |
606 } | 601 } |
607 | 602 |
608 void WebURLLoaderImpl::Context::OnCompletedRequest( | 603 void WebURLLoaderImpl::Context::OnCompletedRequest( |
609 int error_code, | 604 int error_code, |
610 bool was_ignored_by_handler, | 605 bool was_ignored_by_handler, |
611 bool stale_copy_in_cache, | 606 bool stale_copy_in_cache, |
612 const std::string& security_info, | 607 const std::string& security_info, |
613 const base::TimeTicks& completion_time, | 608 const base::TimeTicks& completion_time, |
614 int64 total_transfer_size) { | 609 int64 total_transfer_size) { |
615 if (ftp_listing_delegate_) { | 610 if (ftp_listing_delegate_) { |
616 ftp_listing_delegate_->OnCompletedRequest(); | 611 ftp_listing_delegate_->OnCompletedRequest(); |
617 ftp_listing_delegate_.reset(NULL); | 612 ftp_listing_delegate_.reset(NULL); |
618 } else if (multipart_delegate_) { | 613 } else if (multipart_delegate_) { |
619 multipart_delegate_->OnCompletedRequest(); | 614 multipart_delegate_->OnCompletedRequest(); |
620 multipart_delegate_.reset(NULL); | 615 multipart_delegate_.reset(NULL); |
621 } | 616 } |
622 | 617 |
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_) { | 618 if (client_) { |
629 if (error_code != net::OK) { | 619 if (error_code != net::OK) { |
630 client_->didFail(loader_, CreateError(request_.url(), | 620 client_->didFail(loader_, CreateError(request_.url(), |
631 stale_copy_in_cache, | 621 stale_copy_in_cache, |
632 error_code)); | 622 error_code)); |
633 } else { | 623 } else { |
634 client_->didFinishLoading( | 624 client_->didFinishLoading( |
635 loader_, (completion_time - TimeTicks()).InSecondsF(), | 625 loader_, (completion_time - TimeTicks()).InSecondsF(), |
636 total_transfer_size); | 626 total_transfer_size); |
637 } | 627 } |
638 } | 628 } |
639 | 629 |
640 // We are done with the bridge now, and so we need to release the reference | 630 // 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 | 631 // to ourselves that we took on behalf of the bridge. This may cause our |
642 // destruction. | 632 // destruction. |
643 Release(); | 633 Release(); |
644 } | 634 } |
645 | 635 |
| 636 WebURLLoaderImpl::Context::~Context() { |
| 637 if (request_id_ >= 0) { |
| 638 ChildThread::current()->resource_dispatcher()->RemovePendingRequest( |
| 639 request_id_); |
| 640 } |
| 641 } |
| 642 |
646 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { | 643 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { |
647 DCHECK(url.SchemeIs("data")); | 644 DCHECK(url.SchemeIs("data")); |
648 | 645 |
649 // Optimize for the case where we can handle a data URL locally. We must | 646 // 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 | 647 // skip this for data URLs targetted at frames since those could trigger a |
651 // download. | 648 // download. |
652 // | 649 // |
653 // NOTE: We special case MIME types we can render both for performance | 650 // 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 | 651 // reasons as well as to support unit tests. |
655 // ResourceLoaderBridge implementation. | |
656 | 652 |
657 #if defined(OS_ANDROID) | 653 #if defined(OS_ANDROID) |
658 // For compatibility reasons on Android we need to expose top-level data:// | 654 // For compatibility reasons on Android we need to expose top-level data:// |
659 // to the browser. | 655 // to the browser. |
660 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) | 656 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) |
661 return false; | 657 return false; |
662 #endif | 658 #endif |
663 | 659 |
664 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && | 660 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && |
665 request_.targetType() != WebURLRequest::TargetIsSubframe) | 661 request_.targetType() != WebURLRequest::TargetIsSubframe) |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 void WebURLLoaderImpl::setDefersLoading(bool value) { | 863 void WebURLLoaderImpl::setDefersLoading(bool value) { |
868 context_->SetDefersLoading(value); | 864 context_->SetDefersLoading(value); |
869 } | 865 } |
870 | 866 |
871 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, | 867 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, |
872 int intra_priority_value) { | 868 int intra_priority_value) { |
873 context_->DidChangePriority(new_priority, intra_priority_value); | 869 context_->DidChangePriority(new_priority, intra_priority_value); |
874 } | 870 } |
875 | 871 |
876 } // namespace content | 872 } // namespace content |
OLD | NEW |