| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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. | 5 // An implementation of WebURLLoader in terms of ResourceLoaderBridge. |
| 6 | 6 |
| 7 #include "webkit/glue/weburlloader_impl.h" | 7 #include "webkit/glue/weburlloader_impl.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 private: | 115 private: |
| 116 int load_flags_; | 116 int load_flags_; |
| 117 std::string buffer_; | 117 std::string buffer_; |
| 118 bool has_accept_header_; | 118 bool has_accept_header_; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 // Extracts the information from a data: url. | 121 // Extracts the information from a data: url. |
| 122 bool GetInfoFromDataURL(const GURL& url, | 122 bool GetInfoFromDataURL(const GURL& url, |
| 123 ResourceResponseInfo* info, | 123 ResourceResponseInfo* info, |
| 124 std::string* data, | 124 std::string* data, |
| 125 net::URLRequestStatus* status) { | 125 int* error_code) { |
| 126 std::string mime_type; | 126 std::string mime_type; |
| 127 std::string charset; | 127 std::string charset; |
| 128 if (net::DataURL::Parse(url, &mime_type, &charset, data)) { | 128 if (net::DataURL::Parse(url, &mime_type, &charset, data)) { |
| 129 *status = net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0); | 129 *error_code = net::OK; |
| 130 // Assure same time for all time fields of data: URLs. | 130 // Assure same time for all time fields of data: URLs. |
| 131 Time now = Time::Now(); | 131 Time now = Time::Now(); |
| 132 info->load_timing.base_time = now; | 132 info->load_timing.base_time = now; |
| 133 info->load_timing.base_ticks = TimeTicks::Now(); | 133 info->load_timing.base_ticks = TimeTicks::Now(); |
| 134 info->request_time = now; | 134 info->request_time = now; |
| 135 info->response_time = now; | 135 info->response_time = now; |
| 136 info->headers = NULL; | 136 info->headers = NULL; |
| 137 info->mime_type.swap(mime_type); | 137 info->mime_type.swap(mime_type); |
| 138 info->charset.swap(charset); | 138 info->charset.swap(charset); |
| 139 info->security_info.clear(); | 139 info->security_info.clear(); |
| 140 info->content_length = data->length(); | 140 info->content_length = data->length(); |
| 141 info->encoded_data_length = 0; | 141 info->encoded_data_length = 0; |
| 142 | 142 |
| 143 return true; | 143 return true; |
| 144 } | 144 } |
| 145 | 145 |
| 146 *status = net::URLRequestStatus(net::URLRequestStatus::FAILED, | 146 *error_code = net::ERR_INVALID_URL; |
| 147 net::ERR_INVALID_URL); | |
| 148 return false; | 147 return false; |
| 149 } | 148 } |
| 150 | 149 |
| 151 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; | 150 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; |
| 152 | 151 |
| 153 void PopulateURLResponse( | 152 void PopulateURLResponse( |
| 154 const GURL& url, | 153 const GURL& url, |
| 155 const ResourceResponseInfo& info, | 154 const ResourceResponseInfo& info, |
| 156 WebURLResponse* response) { | 155 WebURLResponse* response) { |
| 157 response->setURL(url); | 156 response->setURL(url); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 const GURL& new_url, | 290 const GURL& new_url, |
| 292 const ResourceResponseInfo& info, | 291 const ResourceResponseInfo& info, |
| 293 bool* has_new_first_party_for_cookies, | 292 bool* has_new_first_party_for_cookies, |
| 294 GURL* new_first_party_for_cookies); | 293 GURL* new_first_party_for_cookies); |
| 295 virtual void OnReceivedResponse(const ResourceResponseInfo& info); | 294 virtual void OnReceivedResponse(const ResourceResponseInfo& info); |
| 296 virtual void OnDownloadedData(int len); | 295 virtual void OnDownloadedData(int len); |
| 297 virtual void OnReceivedData(const char* data, | 296 virtual void OnReceivedData(const char* data, |
| 298 int data_length, | 297 int data_length, |
| 299 int encoded_data_length); | 298 int encoded_data_length); |
| 300 virtual void OnReceivedCachedMetadata(const char* data, int len); | 299 virtual void OnReceivedCachedMetadata(const char* data, int len); |
| 301 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 300 virtual void OnCompletedRequest(int error_code, |
| 302 const std::string& security_info, | 301 const std::string& security_info, |
| 303 const base::TimeTicks& completion_time); | 302 const base::TimeTicks& completion_time); |
| 304 | 303 |
| 305 private: | 304 private: |
| 306 friend class base::RefCounted<Context>; | 305 friend class base::RefCounted<Context>; |
| 307 ~Context() {} | 306 ~Context() {} |
| 308 | 307 |
| 309 // We can optimize the handling of data URLs in most cases. | 308 // We can optimize the handling of data URLs in most cases. |
| 310 bool CanHandleDataURL(const GURL& url) const; | 309 bool CanHandleDataURL(const GURL& url) const; |
| 311 void HandleDataURL(); | 310 void HandleDataURL(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 request_ = request; // Save the request. | 355 request_ = request; // Save the request. |
| 357 | 356 |
| 358 GURL url = request.url(); | 357 GURL url = request.url(); |
| 359 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 358 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
| 360 if (sync_load_response) { | 359 if (sync_load_response) { |
| 361 // This is a sync load. Do the work now. | 360 // This is a sync load. Do the work now. |
| 362 sync_load_response->url = url; | 361 sync_load_response->url = url; |
| 363 std::string data; | 362 std::string data; |
| 364 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 363 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
| 365 &sync_load_response->data, | 364 &sync_load_response->data, |
| 366 &sync_load_response->status); | 365 &sync_load_response->error_code); |
| 367 } else { | 366 } else { |
| 368 AddRef(); // Balanced in OnCompletedRequest | 367 AddRef(); // Balanced in OnCompletedRequest |
| 369 MessageLoop::current()->PostTask(FROM_HERE, | 368 MessageLoop::current()->PostTask(FROM_HERE, |
| 370 base::Bind(&Context::HandleDataURL, this)); | 369 base::Bind(&Context::HandleDataURL, this)); |
| 371 } | 370 } |
| 372 return; | 371 return; |
| 373 } | 372 } |
| 374 | 373 |
| 375 GURL referrer_url( | 374 GURL referrer_url( |
| 376 request.httpHeaderField(WebString::fromUTF8("Referer")).utf8()); | 375 request.httpHeaderField(WebString::fromUTF8("Referer")).utf8()); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 } | 615 } |
| 617 } | 616 } |
| 618 | 617 |
| 619 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( | 618 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( |
| 620 const char* data, int len) { | 619 const char* data, int len) { |
| 621 if (client_) | 620 if (client_) |
| 622 client_->didReceiveCachedMetadata(loader_, data, len); | 621 client_->didReceiveCachedMetadata(loader_, data, len); |
| 623 } | 622 } |
| 624 | 623 |
| 625 void WebURLLoaderImpl::Context::OnCompletedRequest( | 624 void WebURLLoaderImpl::Context::OnCompletedRequest( |
| 626 const net::URLRequestStatus& status, | 625 int error_code, |
| 627 const std::string& security_info, | 626 const std::string& security_info, |
| 628 const base::TimeTicks& completion_time) { | 627 const base::TimeTicks& completion_time) { |
| 629 if (ftp_listing_delegate_.get()) { | 628 if (ftp_listing_delegate_.get()) { |
| 630 ftp_listing_delegate_->OnCompletedRequest(); | 629 ftp_listing_delegate_->OnCompletedRequest(); |
| 631 ftp_listing_delegate_.reset(NULL); | 630 ftp_listing_delegate_.reset(NULL); |
| 632 } else if (multipart_delegate_.get()) { | 631 } else if (multipart_delegate_.get()) { |
| 633 multipart_delegate_->OnCompletedRequest(); | 632 multipart_delegate_->OnCompletedRequest(); |
| 634 multipart_delegate_.reset(NULL); | 633 multipart_delegate_.reset(NULL); |
| 635 } | 634 } |
| 636 | 635 |
| 637 // Prevent any further IPC to the browser now that we're complete, but | 636 // Prevent any further IPC to the browser now that we're complete, but |
| 638 // don't delete it to keep any downloaded temp files alive. | 637 // don't delete it to keep any downloaded temp files alive. |
| 639 DCHECK(!completed_bridge_.get()); | 638 DCHECK(!completed_bridge_.get()); |
| 640 completed_bridge_.swap(bridge_); | 639 completed_bridge_.swap(bridge_); |
| 641 | 640 |
| 642 if (client_) { | 641 if (client_) { |
| 643 if (status.status() != net::URLRequestStatus::SUCCESS) { | 642 if (!net::IsSuccess(error_code) || |
| 644 int error_code; | 643 error_code == net::OK_HANDLED_EXTERNALLY) { |
| 645 if (status.status() == net::URLRequestStatus::HANDLED_EXTERNALLY) { | |
| 646 // By marking this request as aborted we insure that we don't navigate | |
| 647 // to an error page. | |
| 648 error_code = net::ERR_ABORTED; | |
| 649 } else { | |
| 650 error_code = status.error(); | |
| 651 } | |
| 652 WebURLError error; | 644 WebURLError error; |
| 653 if (error_code == net::ERR_ABORTED) { | 645 if (error_code == net::ERR_ABORTED) { |
| 654 error.isCancellation = true; | 646 error.isCancellation = true; |
| 655 } else if (error_code == net::ERR_TEMPORARILY_THROTTLED) { | 647 } else if (error_code == net::ERR_TEMPORARILY_THROTTLED) { |
| 656 error.localizedDescription = WebString::fromUTF8( | 648 error.localizedDescription = WebString::fromUTF8( |
| 657 kThrottledErrorDescription); | 649 kThrottledErrorDescription); |
| 658 } | 650 } |
| 659 error.domain = WebString::fromUTF8(net::kErrorDomain); | 651 error.domain = WebString::fromUTF8(net::kErrorDomain); |
| 660 error.reason = error_code; | 652 error.reason = error_code; |
| 661 error.unreachableURL = request_.url(); | 653 error.unreachableURL = request_.url(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 std::string mime_type, unused_charset; | 689 std::string mime_type, unused_charset; |
| 698 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && | 690 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && |
| 699 net::IsSupportedMimeType(mime_type)) | 691 net::IsSupportedMimeType(mime_type)) |
| 700 return true; | 692 return true; |
| 701 | 693 |
| 702 return false; | 694 return false; |
| 703 } | 695 } |
| 704 | 696 |
| 705 void WebURLLoaderImpl::Context::HandleDataURL() { | 697 void WebURLLoaderImpl::Context::HandleDataURL() { |
| 706 ResourceResponseInfo info; | 698 ResourceResponseInfo info; |
| 707 net::URLRequestStatus status; | 699 int error_code; |
| 708 std::string data; | 700 std::string data; |
| 709 | 701 |
| 710 if (GetInfoFromDataURL(request_.url(), &info, &data, &status)) { | 702 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) { |
| 711 OnReceivedResponse(info); | 703 OnReceivedResponse(info); |
| 712 if (!data.empty()) | 704 if (!data.empty()) |
| 713 OnReceivedData(data.data(), data.size(), 0); | 705 OnReceivedData(data.data(), data.size(), 0); |
| 714 } | 706 } |
| 715 | 707 |
| 716 OnCompletedRequest(status, info.security_info, base::TimeTicks::Now()); | 708 OnCompletedRequest(error_code, info.security_info, base::TimeTicks::Now()); |
| 717 } | 709 } |
| 718 | 710 |
| 719 // WebURLLoaderImpl ----------------------------------------------------------- | 711 // WebURLLoaderImpl ----------------------------------------------------------- |
| 720 | 712 |
| 721 WebURLLoaderImpl::WebURLLoaderImpl(WebKitPlatformSupportImpl* platform) | 713 WebURLLoaderImpl::WebURLLoaderImpl(WebKitPlatformSupportImpl* platform) |
| 722 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))), | 714 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))), |
| 723 platform_(platform) { | 715 platform_(platform) { |
| 724 } | 716 } |
| 725 | 717 |
| 726 WebURLLoaderImpl::~WebURLLoaderImpl() { | 718 WebURLLoaderImpl::~WebURLLoaderImpl() { |
| 727 cancel(); | 719 cancel(); |
| 728 } | 720 } |
| 729 | 721 |
| 730 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request, | 722 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request, |
| 731 WebURLResponse& response, | 723 WebURLResponse& response, |
| 732 WebURLError& error, | 724 WebURLError& error, |
| 733 WebData& data) { | 725 WebData& data) { |
| 734 ResourceLoaderBridge::SyncLoadResponse sync_load_response; | 726 ResourceLoaderBridge::SyncLoadResponse sync_load_response; |
| 735 context_->Start(request, &sync_load_response, platform_); | 727 context_->Start(request, &sync_load_response, platform_); |
| 736 | 728 |
| 737 const GURL& final_url = sync_load_response.url; | 729 const GURL& final_url = sync_load_response.url; |
| 738 | 730 |
| 739 // TODO(tc): For file loads, we may want to include a more descriptive | 731 // TODO(tc): For file loads, we may want to include a more descriptive |
| 740 // status code or status text. | 732 // status code or status text. |
| 741 const net::URLRequestStatus::Status& status = | 733 int error_code = sync_load_response.error_code; |
| 742 sync_load_response.status.status(); | 734 if (!net::IsSuccess(error_code)) { |
| 743 if (status != net::URLRequestStatus::SUCCESS && | |
| 744 status != net::URLRequestStatus::HANDLED_EXTERNALLY) { | |
| 745 response.setURL(final_url); | 735 response.setURL(final_url); |
| 746 error.domain = WebString::fromUTF8(net::kErrorDomain); | 736 error.domain = WebString::fromUTF8(net::kErrorDomain); |
| 747 error.reason = sync_load_response.status.error(); | 737 error.reason = error_code; |
| 748 error.unreachableURL = final_url; | 738 error.unreachableURL = final_url; |
| 749 return; | 739 return; |
| 750 } | 740 } |
| 751 | 741 |
| 752 PopulateURLResponse(final_url, sync_load_response, &response); | 742 PopulateURLResponse(final_url, sync_load_response, &response); |
| 753 | 743 |
| 754 data.assign(sync_load_response.data.data(), | 744 data.assign(sync_load_response.data.data(), |
| 755 sync_load_response.data.size()); | 745 sync_load_response.data.size()); |
| 756 } | 746 } |
| 757 | 747 |
| 758 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request, | 748 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request, |
| 759 WebURLLoaderClient* client) { | 749 WebURLLoaderClient* client) { |
| 760 DCHECK(!context_->client()); | 750 DCHECK(!context_->client()); |
| 761 | 751 |
| 762 context_->set_client(client); | 752 context_->set_client(client); |
| 763 context_->Start(request, NULL, platform_); | 753 context_->Start(request, NULL, platform_); |
| 764 } | 754 } |
| 765 | 755 |
| 766 void WebURLLoaderImpl::cancel() { | 756 void WebURLLoaderImpl::cancel() { |
| 767 context_->Cancel(); | 757 context_->Cancel(); |
| 768 } | 758 } |
| 769 | 759 |
| 770 void WebURLLoaderImpl::setDefersLoading(bool value) { | 760 void WebURLLoaderImpl::setDefersLoading(bool value) { |
| 771 context_->SetDefersLoading(value); | 761 context_->SetDefersLoading(value); |
| 772 } | 762 } |
| 773 | 763 |
| 774 } // namespace webkit_glue | 764 } // namespace webkit_glue |
| OLD | NEW |