Chromium Code Reviews| 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/bit_cast.h" | |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.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 |
| 23 namespace content { | 25 namespace content { |
| 24 | 26 |
| 25 // static | 27 // static |
| 26 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) { | 28 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) { |
| 27 return new WebUIDataSourceImpl(source_name); | 29 return new WebUIDataSourceImpl(source_name); |
| 28 } | 30 } |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 std::map<std::string, int>::iterator result; | 234 std::map<std::string, int>::iterator result; |
| 233 // Remove the query string for named resource lookups. | 235 // Remove the query string for named resource lookups. |
| 234 std::string file_path = path.substr(0, path.find_first_of('?')); | 236 std::string file_path = path.substr(0, path.find_first_of('?')); |
| 235 result = path_to_idr_map_.find(file_path); | 237 result = path_to_idr_map_.find(file_path); |
| 236 if (result != path_to_idr_map_.end()) | 238 if (result != path_to_idr_map_.end()) |
| 237 resource_id = result->second; | 239 resource_id = result->second; |
| 238 DCHECK_NE(resource_id, -1); | 240 DCHECK_NE(resource_id, -1); |
| 239 scoped_refptr<base::RefCountedMemory> response( | 241 scoped_refptr<base::RefCountedMemory> response( |
| 240 GetContentClient()->GetDataResourceBytes(resource_id)); | 242 GetContentClient()->GetDataResourceBytes(resource_id)); |
| 241 | 243 |
| 244 const unsigned char* response_array = response->front(); | |
| 245 if (response_array[0] == 0x1f && response_array[1] == 0x8b) { | |
|
agrieve
2016/05/11 18:29:52
I think this is a big enough chunk of logic that i
smaier
2016/05/13 15:27:25
Done.
| |
| 246 // We are assuming that since we have hit the "magic header" (1f:8b) of a | |
| 247 // gzipped file, we can now decode it. | |
| 248 z_stream infstream; | |
| 249 infstream.zalloc = Z_NULL; | |
| 250 infstream.zfree = Z_NULL; | |
| 251 infstream.opaque = Z_NULL; | |
| 252 | |
| 253 infstream.avail_in = response->size(); // size of input | |
| 254 infstream.next_in = bit_cast<Bytef*>(response_array); // input buffer | |
|
agrieve
2016/05/11 18:29:52
nit: I don't think bit_cast<> is meant for pointer
smaier
2016/05/13 15:27:25
Done.
| |
| 255 | |
| 256 // Size of output comes from footer of gzip file format, found as the last 4 | |
| 257 // bytes in the compressed file. | |
| 258 infstream.avail_out = *reinterpret_cast<const uint32_t*>( | |
|
agrieve
2016/05/11 18:29:52
This assumes a certain endianness. Probably better
smaier
2016/05/13 15:27:25
Done.
| |
| 259 &response_array[response->size() - 4]); | |
| 260 | |
| 261 std::string output; | |
| 262 output.resize(infstream.avail_out); | |
| 263 infstream.next_out = bit_cast<Bytef*>(&output[0]); // output buffer | |
| 264 | |
| 265 CHECK(inflateInit2(&infstream, 16) == Z_OK); | |
| 266 CHECK(inflate(&infstream, Z_FINISH) == Z_STREAM_END); | |
| 267 CHECK(inflateEnd(&infstream) == Z_OK); | |
| 268 | |
| 269 response = base::RefCountedString::TakeString(&output); | |
| 270 } | |
| 271 | |
| 242 // TODO(dschuyler): improve filtering of which resource to run template | 272 // TODO(dschuyler): improve filtering of which resource to run template |
| 243 // expansion upon. | 273 // expansion upon. |
| 244 if (GetMimeType(path) == "text/html") { | 274 if (GetMimeType(path) == "text/html") { |
| 245 std::string replaced = ui::ReplaceTemplateExpressions( | 275 std::string replaced = ui::ReplaceTemplateExpressions( |
| 246 base::StringPiece(response->front_as<char>(), response->size()), | 276 base::StringPiece(response->front_as<char>(), response->size()), |
| 247 replacements_); | 277 replacements_); |
| 248 response = base::RefCountedString::TakeString(&replaced); | 278 response = base::RefCountedString::TakeString(&replaced); |
| 249 } | 279 } |
| 250 | 280 |
| 251 callback.Run(response.get()); | 281 callback.Run(response.get()); |
| 252 } | 282 } |
| 253 | 283 |
| 254 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 284 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
| 255 const URLDataSource::GotDataCallback& callback) { | 285 const URLDataSource::GotDataCallback& callback) { |
| 256 std::string template_data; | 286 std::string template_data; |
| 257 webui::AppendJsonJS(&localized_strings_, &template_data); | 287 webui::AppendJsonJS(&localized_strings_, &template_data); |
| 258 callback.Run(base::RefCountedString::TakeString(&template_data)); | 288 callback.Run(base::RefCountedString::TakeString(&template_data)); |
| 259 } | 289 } |
| 260 | 290 |
| 261 } // namespace content | 291 } // namespace content |
| OLD | NEW |