Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 using blink::WebURLRequest; | 58 using blink::WebURLRequest; |
| 59 using blink::WebURLResponse; | 59 using blink::WebURLResponse; |
| 60 using webkit_glue::MultipartResponseDelegate; | 60 using webkit_glue::MultipartResponseDelegate; |
| 61 using webkit_glue::ResourceLoaderBridge; | 61 using webkit_glue::ResourceLoaderBridge; |
| 62 using webkit_glue::WebURLResponseExtraDataImpl; | 62 using webkit_glue::WebURLResponseExtraDataImpl; |
| 63 | 63 |
| 64 namespace content { | 64 namespace content { |
| 65 | 65 |
| 66 // Utilities ------------------------------------------------------------------ | 66 // Utilities ------------------------------------------------------------------ |
| 67 | 67 |
| 68 int GetInfoFromDataURL(const GURL& url, | |
| 69 ResourceResponseInfo* info, | |
| 70 std::string* data) { | |
| 71 std::string mime_type; | |
| 72 std::string charset; | |
| 73 if (!net::DataURL::Parse(url, &mime_type, &charset, data)) | |
| 74 return net::ERR_INVALID_URL; | |
| 75 | |
| 76 // mime_type set by net::DataURL::Parse() is guaranteed to be in | |
| 77 // token "/" token | |
| 78 // form. charset is also guaranteed to be a token. | |
| 79 | |
| 80 DCHECK(!mime_type.empty()); | |
| 81 DCHECK(!charset.empty()); | |
| 82 | |
| 83 // Assure same time for all time fields of data: URLs. | |
| 84 Time now = Time::Now(); | |
| 85 info->load_timing.request_start = TimeTicks::Now(); | |
| 86 info->load_timing.request_start_time = now; | |
| 87 info->request_time = now; | |
| 88 info->response_time = now; | |
| 89 | |
| 90 scoped_refptr<net::HttpResponseHeaders> headers( | |
| 91 new net::HttpResponseHeaders(std::string())); | |
| 92 headers->ReplaceStatusLine("HTTP/1.1 200 OK"); | |
| 93 // charset in Content-Type header is specified explicitly to follow token | |
| 94 // ABNF in httpbis spec. | |
| 95 std::string content_type_header = | |
| 96 "Content-Type: " + mime_type + ";charset=" + charset; | |
| 97 headers->AddHeader(content_type_header); | |
| 98 headers->AddHeader("Access-Control-Allow-Origin: *"); | |
|
robwu
2014/07/29 10:44:44
Could you also add the following line:
header
| |
| 99 info->headers = headers; | |
| 100 | |
| 101 info->mime_type.swap(mime_type); | |
| 102 info->charset.swap(charset); | |
| 103 info->security_info.clear(); | |
| 104 info->content_length = data->length(); | |
| 105 info->encoded_data_length = 0; | |
| 106 | |
| 107 return net::OK; | |
| 108 } | |
| 109 | |
| 68 namespace { | 110 namespace { |
| 69 | 111 |
| 70 const char kThrottledErrorDescription[] = | 112 const char kThrottledErrorDescription[] = |
| 71 "Request throttled. Visit http://dev.chromium.org/throttling for more " | 113 "Request throttled. Visit http://dev.chromium.org/throttling for more " |
| 72 "information."; | 114 "information."; |
| 73 | 115 |
| 74 class HeaderFlattener : public WebHTTPHeaderVisitor { | 116 class HeaderFlattener : public WebHTTPHeaderVisitor { |
| 75 public: | 117 public: |
| 76 explicit HeaderFlattener(int load_flags) | 118 explicit HeaderFlattener(int load_flags) |
| 77 : load_flags_(load_flags), | 119 : load_flags_(load_flags), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 } | 159 } |
| 118 return buffer_; | 160 return buffer_; |
| 119 } | 161 } |
| 120 | 162 |
| 121 private: | 163 private: |
| 122 int load_flags_; | 164 int load_flags_; |
| 123 std::string buffer_; | 165 std::string buffer_; |
| 124 bool has_accept_header_; | 166 bool has_accept_header_; |
| 125 }; | 167 }; |
| 126 | 168 |
| 127 // Extracts the information from a data: url. | |
| 128 bool GetInfoFromDataURL(const GURL& url, | |
| 129 ResourceResponseInfo* info, | |
| 130 std::string* data, | |
| 131 int* error_code) { | |
| 132 std::string mime_type; | |
| 133 std::string charset; | |
| 134 if (net::DataURL::Parse(url, &mime_type, &charset, data)) { | |
| 135 *error_code = net::OK; | |
| 136 // Assure same time for all time fields of data: URLs. | |
| 137 Time now = Time::Now(); | |
| 138 info->load_timing.request_start = TimeTicks::Now(); | |
| 139 info->load_timing.request_start_time = now; | |
| 140 info->request_time = now; | |
| 141 info->response_time = now; | |
| 142 info->headers = NULL; | |
| 143 info->mime_type.swap(mime_type); | |
| 144 info->charset.swap(charset); | |
| 145 info->security_info.clear(); | |
| 146 info->content_length = data->length(); | |
| 147 info->encoded_data_length = 0; | |
| 148 | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 *error_code = net::ERR_INVALID_URL; | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; | 169 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; |
| 157 | 170 |
| 158 // Converts timing data from |load_timing| to the format used by WebKit. | 171 // Converts timing data from |load_timing| to the format used by WebKit. |
| 159 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, | 172 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, |
| 160 WebURLLoadTiming* url_timing) { | 173 WebURLLoadTiming* url_timing) { |
| 161 DCHECK(!load_timing.request_start.is_null()); | 174 DCHECK(!load_timing.request_start.is_null()); |
| 162 | 175 |
| 163 const TimeTicks kNullTicks; | 176 const TimeTicks kNullTicks; |
| 164 url_timing->initialize(); | 177 url_timing->initialize(); |
| 165 url_timing->setRequestTime( | 178 url_timing->setRequestTime( |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 DCHECK(!bridge_.get()); | 327 DCHECK(!bridge_.get()); |
| 315 | 328 |
| 316 request_ = request; // Save the request. | 329 request_ = request; // Save the request. |
| 317 | 330 |
| 318 GURL url = request.url(); | 331 GURL url = request.url(); |
| 319 if (url.SchemeIs("data") && CanHandleDataURL(url)) { | 332 if (url.SchemeIs("data") && CanHandleDataURL(url)) { |
| 320 if (sync_load_response) { | 333 if (sync_load_response) { |
| 321 // This is a sync load. Do the work now. | 334 // This is a sync load. Do the work now. |
| 322 sync_load_response->url = url; | 335 sync_load_response->url = url; |
| 323 std::string data; | 336 std::string data; |
| 324 GetInfoFromDataURL(sync_load_response->url, sync_load_response, | 337 sync_load_response->error_code = |
| 325 &sync_load_response->data, | 338 GetInfoFromDataURL(sync_load_response->url, sync_load_response, |
| 326 &sync_load_response->error_code); | 339 &sync_load_response->data); |
| 327 } else { | 340 } else { |
| 328 AddRef(); // Balanced in OnCompletedRequest | 341 AddRef(); // Balanced in OnCompletedRequest |
| 329 base::MessageLoop::current()->PostTask( | 342 base::MessageLoop::current()->PostTask( |
| 330 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); | 343 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); |
| 331 } | 344 } |
| 332 return; | 345 return; |
| 333 } | 346 } |
| 334 | 347 |
| 335 GURL referrer_url( | 348 GURL referrer_url( |
| 336 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); | 349 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 674 std::string mime_type, unused_charset; | 687 std::string mime_type, unused_charset; |
| 675 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && | 688 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) && |
| 676 net::IsSupportedMimeType(mime_type)) | 689 net::IsSupportedMimeType(mime_type)) |
| 677 return true; | 690 return true; |
| 678 | 691 |
| 679 return false; | 692 return false; |
| 680 } | 693 } |
| 681 | 694 |
| 682 void WebURLLoaderImpl::Context::HandleDataURL() { | 695 void WebURLLoaderImpl::Context::HandleDataURL() { |
| 683 ResourceResponseInfo info; | 696 ResourceResponseInfo info; |
| 684 int error_code; | |
| 685 std::string data; | 697 std::string data; |
| 686 | 698 |
| 687 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) { | 699 int error_code = GetInfoFromDataURL(request_.url(), &info, &data); |
| 700 if (error_code == net::OK) { | |
| 688 OnReceivedResponse(info); | 701 OnReceivedResponse(info); |
| 689 if (!data.empty()) | 702 if (!data.empty()) |
| 690 OnReceivedData(data.data(), data.size(), 0); | 703 OnReceivedData(data.data(), data.size(), 0); |
| 691 } | 704 } |
| 692 | 705 |
| 693 OnCompletedRequest(error_code, false, false, info.security_info, | 706 OnCompletedRequest(error_code, false, false, info.security_info, |
| 694 base::TimeTicks::Now(), 0); | 707 base::TimeTicks::Now(), 0); |
| 695 } | 708 } |
| 696 | 709 |
| 697 // WebURLLoaderImpl ----------------------------------------------------------- | 710 // WebURLLoaderImpl ----------------------------------------------------------- |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 873 void WebURLLoaderImpl::setDefersLoading(bool value) { | 886 void WebURLLoaderImpl::setDefersLoading(bool value) { |
| 874 context_->SetDefersLoading(value); | 887 context_->SetDefersLoading(value); |
| 875 } | 888 } |
| 876 | 889 |
| 877 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, | 890 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, |
| 878 int intra_priority_value) { | 891 int intra_priority_value) { |
| 879 context_->DidChangePriority(new_priority, intra_priority_value); | 892 context_->DidChangePriority(new_priority, intra_priority_value); |
| 880 } | 893 } |
| 881 | 894 |
| 882 } // namespace content | 895 } // namespace content |
| OLD | NEW |