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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 has_accept_header_ = true; | 102 has_accept_header_ = true; |
102 } | 103 } |
103 return buffer_; | 104 return buffer_; |
104 } | 105 } |
105 | 106 |
106 private: | 107 private: |
107 std::string buffer_; | 108 std::string buffer_; |
108 bool has_accept_header_; | 109 bool has_accept_header_; |
109 }; | 110 }; |
110 | 111 |
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; | 112 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; |
141 | 113 |
142 // Converts timing data from |load_timing| to the format used by WebKit. | 114 // Converts timing data from |load_timing| to the format used by WebKit. |
143 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, | 115 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, |
144 WebURLLoadTiming* url_timing) { | 116 WebURLLoadTiming* url_timing) { |
145 DCHECK(!load_timing.request_start.is_null()); | 117 DCHECK(!load_timing.request_start.is_null()); |
146 | 118 |
147 const TimeTicks kNullTicks; | 119 const TimeTicks kNullTicks; |
148 url_timing->initialize(); | 120 url_timing->initialize(); |
149 url_timing->setRequestTime( | 121 url_timing->setRequestTime( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 case WebURLRequest::PriorityVeryLow: | 162 case WebURLRequest::PriorityVeryLow: |
191 return net::IDLE; | 163 return net::IDLE; |
192 | 164 |
193 case WebURLRequest::PriorityUnresolved: | 165 case WebURLRequest::PriorityUnresolved: |
194 default: | 166 default: |
195 NOTREACHED(); | 167 NOTREACHED(); |
196 return net::LOW; | 168 return net::LOW; |
197 } | 169 } |
198 } | 170 } |
199 | 171 |
| 172 // Extracts info from a data scheme URL into |info| and |data|. Returns net::OK |
| 173 // if successful. Returns a net error code otherwise. Exported only for testing. |
| 174 int GetInfoFromDataURL(const GURL& url, |
| 175 ResourceResponseInfo* info, |
| 176 std::string* data) { |
| 177 // Assure same time for all time fields of data: URLs. |
| 178 Time now = Time::Now(); |
| 179 info->load_timing.request_start = TimeTicks::Now(); |
| 180 info->load_timing.request_start_time = now; |
| 181 info->request_time = now; |
| 182 info->response_time = now; |
| 183 |
| 184 std::string mime_type; |
| 185 std::string charset; |
| 186 scoped_refptr<net::HttpResponseHeaders> headers( |
| 187 new net::HttpResponseHeaders(std::string())); |
| 188 int result = net::URLRequestDataJob::BuildResponse( |
| 189 url, &mime_type, &charset, data, headers.get()); |
| 190 if (result != net::OK) |
| 191 return result; |
| 192 |
| 193 info->headers = headers; |
| 194 info->mime_type.swap(mime_type); |
| 195 info->charset.swap(charset); |
| 196 info->security_info.clear(); |
| 197 info->content_length = data->length(); |
| 198 info->encoded_data_length = 0; |
| 199 |
| 200 return net::OK; |
| 201 } |
| 202 |
200 } // namespace | 203 } // namespace |
201 | 204 |
202 // WebURLLoaderImpl::Context -------------------------------------------------- | 205 // WebURLLoaderImpl::Context -------------------------------------------------- |
203 | 206 |
204 // This inner class exists since the WebURLLoader may be deleted while inside a | 207 // This inner class exists since the WebURLLoader may be deleted while inside a |
205 // call to WebURLLoaderClient. Refcounting is to keep the context from being | 208 // call to WebURLLoaderClient. Refcounting is to keep the context from being |
206 // deleted if it may have work to do after calling into the client. | 209 // deleted if it may have work to do after calling into the client. |
207 class WebURLLoaderImpl::Context : public base::RefCounted<Context>, | 210 class WebURLLoaderImpl::Context : public base::RefCounted<Context>, |
208 public RequestPeer { | 211 public RequestPeer { |
209 public: | 212 public: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 SyncLoadResponse* sync_load_response) { | 313 SyncLoadResponse* sync_load_response) { |
311 DCHECK(!bridge_.get()); | 314 DCHECK(!bridge_.get()); |
312 | 315 |
313 request_ = request; // Save the request. | 316 request_ = request; // Save the request. |
314 | 317 |
315 GURL url = request.url(); | 318 GURL url = request.url(); |
316 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 319 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
317 if (sync_load_response) { | 320 if (sync_load_response) { |
318 // This is a sync load. Do the work now. | 321 // This is a sync load. Do the work now. |
319 sync_load_response->url = url; | 322 sync_load_response->url = url; |
320 std::string data; | 323 sync_load_response->error_code = |
321 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 324 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
322 &sync_load_response->data, | 325 &sync_load_response->data); |
323 &sync_load_response->error_code); | |
324 } else { | 326 } else { |
325 base::MessageLoop::current()->PostTask( | 327 base::MessageLoop::current()->PostTask( |
326 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); | 328 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); |
327 } | 329 } |
328 return; | 330 return; |
329 } | 331 } |
330 | 332 |
331 GURL referrer_url( | 333 GURL referrer_url( |
332 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); | 334 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); |
333 const std::string& method = request.httpMethod().latin1(); | 335 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; | 690 std::string mime_type, unused_charset; |
689 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && | 691 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && |
690 net::IsSupportedMimeType(mime_type)) | 692 net::IsSupportedMimeType(mime_type)) |
691 return true; | 693 return true; |
692 | 694 |
693 return false; | 695 return false; |
694 } | 696 } |
695 | 697 |
696 void WebURLLoaderImpl::Context::HandleDataURL() { | 698 void WebURLLoaderImpl::Context::HandleDataURL() { |
697 ResourceResponseInfo info; | 699 ResourceResponseInfo info; |
698 int error_code; | |
699 std::string data; | 700 std::string data; |
700 | 701 |
701 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) { | 702 int error_code = GetInfoFromDataURL(request_.url(), &info, &data); |
| 703 |
| 704 if (error_code == net::OK) { |
702 OnReceivedResponse(info); | 705 OnReceivedResponse(info); |
703 if (!data.empty()) | 706 if (!data.empty()) |
704 OnReceivedData(data.data(), data.size(), 0); | 707 OnReceivedData(data.data(), data.size(), 0); |
705 } | 708 } |
706 | 709 |
707 OnCompletedRequest(error_code, false, false, info.security_info, | 710 OnCompletedRequest(error_code, false, false, info.security_info, |
708 base::TimeTicks::Now(), 0); | 711 base::TimeTicks::Now(), 0); |
709 } | 712 } |
710 | 713 |
711 // WebURLLoaderImpl ----------------------------------------------------------- | 714 // WebURLLoaderImpl ----------------------------------------------------------- |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
893 int intra_priority_value) { | 896 int intra_priority_value) { |
894 context_->DidChangePriority(new_priority, intra_priority_value); | 897 context_->DidChangePriority(new_priority, intra_priority_value); |
895 } | 898 } |
896 | 899 |
897 bool WebURLLoaderImpl::attachThreadedDataReceiver( | 900 bool WebURLLoaderImpl::attachThreadedDataReceiver( |
898 blink::WebThreadedDataReceiver* threaded_data_receiver) { | 901 blink::WebThreadedDataReceiver* threaded_data_receiver) { |
899 return context_->AttachThreadedDataReceiver(threaded_data_receiver); | 902 return context_->AttachThreadedDataReceiver(threaded_data_receiver); |
900 } | 903 } |
901 | 904 |
902 } // namespace content | 905 } // namespace content |
OLD | NEW |