| 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. | 5 // An implementation of WebURLLoader in terms of ResourceLoaderBridge. |
| 6 | 6 |
| 7 #include "content/child/web_url_loader_impl.h" | 7 #include "content/child/web_url_loader_impl.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "content/common/resource_request_body.h" | 23 #include "content/common/resource_request_body.h" |
| 24 #include "content/public/child/request_peer.h" | 24 #include "content/public/child/request_peer.h" |
| 25 #include "net/base/data_url.h" | 25 #include "net/base/data_url.h" |
| 26 #include "net/base/filename_util.h" | 26 #include "net/base/filename_util.h" |
| 27 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 28 #include "net/base/mime_util.h" | 28 #include "net/base/mime_util.h" |
| 29 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 30 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 31 #include "net/http/http_util.h" | 31 #include "net/http/http_util.h" |
| 32 #include "net/url_request/url_request.h" | 32 #include "net/url_request/url_request.h" |
| 33 #include "net/url_request/url_request_data_job.h" |
| 33 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" | 34 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" |
| 34 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" | 35 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" |
| 35 #include "third_party/WebKit/public/platform/WebURL.h" | 36 #include "third_party/WebKit/public/platform/WebURL.h" |
| 36 #include "third_party/WebKit/public/platform/WebURLError.h" | 37 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 37 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" | 38 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" |
| 38 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 39 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 39 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 40 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 40 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 41 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 41 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 42 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
| 42 #include "webkit/child/resource_loader_bridge.h" | 43 #include "webkit/child/resource_loader_bridge.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 56 using blink::WebURLLoader; | 57 using blink::WebURLLoader; |
| 57 using blink::WebURLLoaderClient; | 58 using blink::WebURLLoaderClient; |
| 58 using blink::WebURLRequest; | 59 using blink::WebURLRequest; |
| 59 using blink::WebURLResponse; | 60 using blink::WebURLResponse; |
| 60 using webkit_glue::ResourceLoaderBridge; | 61 using webkit_glue::ResourceLoaderBridge; |
| 61 | 62 |
| 62 namespace content { | 63 namespace content { |
| 63 | 64 |
| 64 // Utilities ------------------------------------------------------------------ | 65 // Utilities ------------------------------------------------------------------ |
| 65 | 66 |
| 67 int GetInfoFromDataURL(const GURL& url, |
| 68 ResourceResponseInfo* info, |
| 69 std::string* data) { |
| 70 // Assure same time for all time fields of data: URLs. |
| 71 Time now = Time::Now(); |
| 72 info->load_timing.request_start = TimeTicks::Now(); |
| 73 info->load_timing.request_start_time = now; |
| 74 info->request_time = now; |
| 75 info->response_time = now; |
| 76 |
| 77 std::string mime_type; |
| 78 std::string charset; |
| 79 scoped_refptr<net::HttpResponseHeaders> headers( |
| 80 new net::HttpResponseHeaders(std::string())); |
| 81 int result = net::BuildResponseForDataURL( |
| 82 url, &mime_type, &charset, data, headers.get()); |
| 83 if (result != net::OK) |
| 84 return result; |
| 85 |
| 86 info->headers = headers; |
| 87 info->mime_type.swap(mime_type); |
| 88 info->charset.swap(charset); |
| 89 info->security_info.clear(); |
| 90 info->content_length = data->length(); |
| 91 info->encoded_data_length = 0; |
| 92 |
| 93 return net::OK; |
| 94 } |
| 95 |
| 66 namespace { | 96 namespace { |
| 67 | 97 |
| 68 const char kThrottledErrorDescription[] = | 98 const char kThrottledErrorDescription[] = |
| 69 "Request throttled. Visit http://dev.chromium.org/throttling for more " | 99 "Request throttled. Visit http://dev.chromium.org/throttling for more " |
| 70 "information."; | 100 "information."; |
| 71 | 101 |
| 72 class HeaderFlattener : public WebHTTPHeaderVisitor { | 102 class HeaderFlattener : public WebHTTPHeaderVisitor { |
| 73 public: | 103 public: |
| 74 HeaderFlattener() : has_accept_header_(false) {} | 104 HeaderFlattener() : has_accept_header_(false) {} |
| 75 | 105 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 101 has_accept_header_ = true; | 131 has_accept_header_ = true; |
| 102 } | 132 } |
| 103 return buffer_; | 133 return buffer_; |
| 104 } | 134 } |
| 105 | 135 |
| 106 private: | 136 private: |
| 107 std::string buffer_; | 137 std::string buffer_; |
| 108 bool has_accept_header_; | 138 bool has_accept_header_; |
| 109 }; | 139 }; |
| 110 | 140 |
| 111 // Extracts the information from a data: url. | |
| 112 bool GetInfoFromDataURL(const GURL& url, | |
| 113 ResourceResponseInfo* info, | |
| 114 std::string* data, | |
| 115 int* error_code) { | |
| 116 std::string mime_type; | |
| 117 std::string charset; | |
| 118 if (net::DataURL::Parse(url, &mime_type, &charset, data)) { | |
| 119 *error_code = net::OK; | |
| 120 // Assure same time for all time fields of data: URLs. | |
| 121 Time now = Time::Now(); | |
| 122 info->load_timing.request_start = TimeTicks::Now(); | |
| 123 info->load_timing.request_start_time = now; | |
| 124 info->request_time = now; | |
| 125 info->response_time = now; | |
| 126 info->headers = NULL; | |
| 127 info->mime_type.swap(mime_type); | |
| 128 info->charset.swap(charset); | |
| 129 info->security_info.clear(); | |
| 130 info->content_length = data->length(); | |
| 131 info->encoded_data_length = 0; | |
| 132 | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 *error_code = net::ERR_INVALID_URL; | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; | 141 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; |
| 141 | 142 |
| 142 // Converts timing data from |load_timing| to the format used by WebKit. | 143 // Converts timing data from |load_timing| to the format used by WebKit. |
| 143 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, | 144 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, |
| 144 WebURLLoadTiming* url_timing) { | 145 WebURLLoadTiming* url_timing) { |
| 145 DCHECK(!load_timing.request_start.is_null()); | 146 DCHECK(!load_timing.request_start.is_null()); |
| 146 | 147 |
| 147 const TimeTicks kNullTicks; | 148 const TimeTicks kNullTicks; |
| 148 url_timing->initialize(); | 149 url_timing->initialize(); |
| 149 url_timing->setRequestTime( | 150 url_timing->setRequestTime( |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 SyncLoadResponse* sync_load_response) { | 311 SyncLoadResponse* sync_load_response) { |
| 311 DCHECK(!bridge_.get()); | 312 DCHECK(!bridge_.get()); |
| 312 | 313 |
| 313 request_ = request; // Save the request. | 314 request_ = request; // Save the request. |
| 314 | 315 |
| 315 GURL url = request.url(); | 316 GURL url = request.url(); |
| 316 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 317 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
| 317 if (sync_load_response) { | 318 if (sync_load_response) { |
| 318 // This is a sync load. Do the work now. | 319 // This is a sync load. Do the work now. |
| 319 sync_load_response->url = url; | 320 sync_load_response->url = url; |
| 320 std::string data; | 321 sync_load_response->error_code = |
| 321 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 322 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
| 322 &sync_load_response->data, | 323 &sync_load_response->data); |
| 323 &sync_load_response->error_code); | |
| 324 } else { | 324 } else { |
| 325 base::MessageLoop::current()->PostTask( | 325 base::MessageLoop::current()->PostTask( |
| 326 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); | 326 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); |
| 327 } | 327 } |
| 328 return; | 328 return; |
| 329 } | 329 } |
| 330 | 330 |
| 331 GURL referrer_url( | 331 GURL referrer_url( |
| 332 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); | 332 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); |
| 333 const std::string& method = request.httpMethod().latin1(); | 333 const std::string& method = request.httpMethod().latin1(); |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 std::string mime_type, unused_charset; | 688 std::string mime_type, unused_charset; |
| 689 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && | 689 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && |
| 690 net::IsSupportedMimeType(mime_type)) | 690 net::IsSupportedMimeType(mime_type)) |
| 691 return true; | 691 return true; |
| 692 | 692 |
| 693 return false; | 693 return false; |
| 694 } | 694 } |
| 695 | 695 |
| 696 void WebURLLoaderImpl::Context::HandleDataURL() { | 696 void WebURLLoaderImpl::Context::HandleDataURL() { |
| 697 ResourceResponseInfo info; | 697 ResourceResponseInfo info; |
| 698 int error_code; | |
| 699 std::string data; | 698 std::string data; |
| 700 | 699 |
| 701 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) { | 700 int error_code = GetInfoFromDataURL(request_.url(), &info, &data); |
| 701 |
| 702 if (error_code == net::OK) { |
| 702 OnReceivedResponse(info); | 703 OnReceivedResponse(info); |
| 703 if (!data.empty()) | 704 if (!data.empty()) |
| 704 OnReceivedData(data.data(), data.size(), 0); | 705 OnReceivedData(data.data(), data.size(), 0); |
| 705 } | 706 } |
| 706 | 707 |
| 707 OnCompletedRequest(error_code, false, false, info.security_info, | 708 OnCompletedRequest(error_code, false, false, info.security_info, |
| 708 base::TimeTicks::Now(), 0); | 709 base::TimeTicks::Now(), 0); |
| 709 } | 710 } |
| 710 | 711 |
| 711 // WebURLLoaderImpl ----------------------------------------------------------- | 712 // WebURLLoaderImpl ----------------------------------------------------------- |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 int intra_priority_value) { | 894 int intra_priority_value) { |
| 894 context_->DidChangePriority(new_priority, intra_priority_value); | 895 context_->DidChangePriority(new_priority, intra_priority_value); |
| 895 } | 896 } |
| 896 | 897 |
| 897 bool WebURLLoaderImpl::attachThreadedDataReceiver( | 898 bool WebURLLoaderImpl::attachThreadedDataReceiver( |
| 898 blink::WebThreadedDataReceiver* threaded_data_receiver) { | 899 blink::WebThreadedDataReceiver* threaded_data_receiver) { |
| 899 return context_->AttachThreadedDataReceiver(threaded_data_receiver); | 900 return context_->AttachThreadedDataReceiver(threaded_data_receiver); |
| 900 } | 901 } |
| 901 | 902 |
| 902 } // namespace content | 903 } // namespace content |
| OLD | NEW |