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 #include "content/browser/webui/web_ui_data_source_impl.h" | 5 #include "content/browser/webui/web_ui_data_source_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/sys_byteorder.h" | |
16 #include "content/grit/content_resources.h" | 17 #include "content/grit/content_resources.h" |
17 #include "content/public/browser/content_browser_client.h" | 18 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
20 #include "third_party/zlib/zlib.h" | |
19 #include "ui/base/template_expressions.h" | 21 #include "ui/base/template_expressions.h" |
20 #include "ui/base/webui/jstemplate_builder.h" | 22 #include "ui/base/webui/jstemplate_builder.h" |
21 #include "ui/base/webui/web_ui_util.h" | 23 #include "ui/base/webui/web_ui_util.h" |
22 | 24 |
25 namespace { | |
26 // Decodes given gzip input via zlib. | |
27 scoped_refptr<base::RefCountedString> DecodeGzipData( | |
28 const unsigned char* input_buffer, | |
29 size_t input_size) { | |
30 z_stream infstream; | |
31 infstream.zalloc = Z_NULL; | |
32 infstream.zfree = Z_NULL; | |
33 infstream.opaque = Z_NULL; | |
34 | |
35 infstream.avail_in = input_size; | |
36 infstream.next_in = const_cast<Bytef*>(input_buffer); | |
37 | |
38 // Size of output comes from footer of gzip file format, found as the last 4 | |
39 // bytes in the compressed file, which are stored little endian. | |
40 infstream.avail_out = base::ByteSwapToLE32( | |
41 *reinterpret_cast<const uint32_t*>(&input_buffer[input_size - 4])); | |
42 | |
43 std::string output; | |
44 output.resize(infstream.avail_out); | |
45 infstream.next_out = reinterpret_cast<Bytef*>(&output[0]); // output buffer | |
46 | |
47 CHECK(inflateInit2(&infstream, 16) == Z_OK); | |
48 CHECK(inflate(&infstream, Z_FINISH) == Z_STREAM_END); | |
49 CHECK(inflateEnd(&infstream) == Z_OK); | |
50 | |
51 return base::RefCountedString::TakeString(&output); | |
52 } | |
53 } // end of anynomous namespace | |
54 | |
23 namespace content { | 55 namespace content { |
24 | 56 |
25 // static | 57 // static |
26 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) { | 58 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) { |
27 return new WebUIDataSourceImpl(source_name); | 59 return new WebUIDataSourceImpl(source_name); |
28 } | 60 } |
29 | 61 |
30 // static | 62 // static |
31 void WebUIDataSource::Add(BrowserContext* browser_context, | 63 void WebUIDataSource::Add(BrowserContext* browser_context, |
32 WebUIDataSource* source) { | 64 WebUIDataSource* source) { |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 std::map<std::string, int>::iterator result; | 264 std::map<std::string, int>::iterator result; |
233 // Remove the query string for named resource lookups. | 265 // Remove the query string for named resource lookups. |
234 std::string file_path = path.substr(0, path.find_first_of('?')); | 266 std::string file_path = path.substr(0, path.find_first_of('?')); |
235 result = path_to_idr_map_.find(file_path); | 267 result = path_to_idr_map_.find(file_path); |
236 if (result != path_to_idr_map_.end()) | 268 if (result != path_to_idr_map_.end()) |
237 resource_id = result->second; | 269 resource_id = result->second; |
238 DCHECK_NE(resource_id, -1); | 270 DCHECK_NE(resource_id, -1); |
239 scoped_refptr<base::RefCountedMemory> response( | 271 scoped_refptr<base::RefCountedMemory> response( |
240 GetContentClient()->GetDataResourceBytes(resource_id)); | 272 GetContentClient()->GetDataResourceBytes(resource_id)); |
241 | 273 |
274 const unsigned char* response_array = response->front(); | |
275 if (response_array[0] == 0x1f && response_array[1] == 0x8b) { | |
flackr
2016/05/13 23:02:16
I'm concerned with detecting gzipped resources by
| |
276 // We are assuming that since we have hit the "magic header" (1f:8b) of a | |
277 // gzipped file, we can now decode it. | |
278 response = DecodeGzipData(response_array, response->size()); | |
279 } | |
280 | |
242 // TODO(dschuyler): improve filtering of which resource to run template | 281 // TODO(dschuyler): improve filtering of which resource to run template |
243 // expansion upon. | 282 // expansion upon. |
244 if (GetMimeType(path) == "text/html") { | 283 if (GetMimeType(path) == "text/html") { |
245 std::string replaced = ui::ReplaceTemplateExpressions( | 284 std::string replaced = ui::ReplaceTemplateExpressions( |
246 base::StringPiece(response->front_as<char>(), response->size()), | 285 base::StringPiece(response->front_as<char>(), response->size()), |
247 replacements_); | 286 replacements_); |
248 response = base::RefCountedString::TakeString(&replaced); | 287 response = base::RefCountedString::TakeString(&replaced); |
249 } | 288 } |
250 | 289 |
251 callback.Run(response.get()); | 290 callback.Run(response.get()); |
252 } | 291 } |
253 | 292 |
254 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 293 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
255 const URLDataSource::GotDataCallback& callback) { | 294 const URLDataSource::GotDataCallback& callback) { |
256 std::string template_data; | 295 std::string template_data; |
257 webui::AppendJsonJS(&localized_strings_, &template_data); | 296 webui::AppendJsonJS(&localized_strings_, &template_data); |
258 callback.Run(base::RefCountedString::TakeString(&template_data)); | 297 callback.Run(base::RefCountedString::TakeString(&template_data)); |
259 } | 298 } |
260 | 299 |
261 } // namespace content | 300 } // namespace content |
OLD | NEW |