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 "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/big_endian.h" | 13 #include "base/big_endian.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/files/file.h" | 15 #include "base/files/file.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ref_counted_memory.h" | 19 #include "base/memory/ref_counted_memory.h" |
20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
23 #include "base/strings/string_piece.h" | 23 #include "base/strings/string_piece.h" |
24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
26 #include "base/sys_byteorder.h" | |
26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
27 #include "skia/ext/image_operations.h" | 28 #include "skia/ext/image_operations.h" |
28 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
29 #include "third_party/skia/include/core/SkColor.h" | 30 #include "third_party/skia/include/core/SkColor.h" |
31 #include "third_party/zlib/zlib.h" | |
30 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/base/layout.h" | 33 #include "ui/base/layout.h" |
32 #include "ui/base/material_design/material_design_controller.h" | 34 #include "ui/base/material_design/material_design_controller.h" |
33 #include "ui/base/resource/data_pack.h" | 35 #include "ui/base/resource/data_pack.h" |
34 #include "ui/base/ui_base_paths.h" | 36 #include "ui/base/ui_base_paths.h" |
35 #include "ui/base/ui_base_switches.h" | 37 #include "ui/base/ui_base_switches.h" |
36 #include "ui/base/ui_features.h" | 38 #include "ui/base/ui_features.h" |
37 #include "ui/display/display.h" | 39 #include "ui/display/display.h" |
38 #include "ui/display/screen.h" | 40 #include "ui/display/screen.h" |
39 #include "ui/gfx/codec/jpeg_codec.h" | 41 #include "ui/gfx/codec/jpeg_codec.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 #endif // OS_WIN | 107 #endif // OS_WIN |
106 } | 108 } |
107 | 109 |
108 SkBitmap CreateEmptyBitmap() { | 110 SkBitmap CreateEmptyBitmap() { |
109 SkBitmap bitmap; | 111 SkBitmap bitmap; |
110 bitmap.allocN32Pixels(32, 32); | 112 bitmap.allocN32Pixels(32, 32); |
111 bitmap.eraseARGB(255, 255, 255, 0); | 113 bitmap.eraseARGB(255, 255, 255, 0); |
112 return bitmap; | 114 return bitmap; |
113 } | 115 } |
114 | 116 |
117 // Decodes given gzip input via zlib. | |
118 base::RefCountedBytes* DecodeGzipData( | |
119 const unsigned char* input_buffer, | |
120 size_t input_size) { | |
121 z_stream infstream; | |
122 infstream.zalloc = Z_NULL; | |
123 infstream.zfree = Z_NULL; | |
124 infstream.opaque = Z_NULL; | |
125 | |
126 infstream.avail_in = input_size; | |
127 infstream.next_in = const_cast<Bytef*>(input_buffer); | |
128 | |
129 // Size of output comes from footer of gzip file format, found as the last 4 | |
130 // bytes in the compressed file, which are stored little endian. | |
131 infstream.avail_out = base::ByteSwapToLE32( | |
132 *reinterpret_cast<const uint32_t*>(&input_buffer[input_size - 4])); | |
133 | |
134 std::vector<unsigned char> output(infstream.avail_out); | |
135 infstream.next_out = reinterpret_cast<Bytef*>(&output[0]); // output buffer | |
136 | |
137 CHECK(inflateInit2(&infstream, 16) == Z_OK); | |
138 CHECK(inflate(&infstream, Z_FINISH) == Z_STREAM_END); | |
139 CHECK(inflateEnd(&infstream) == Z_OK); | |
140 | |
141 return new base::RefCountedBytes(output); | |
142 } | |
143 | |
115 } // namespace | 144 } // namespace |
116 | 145 |
117 // An ImageSkiaSource that loads bitmaps for the requested scale factor from | 146 // An ImageSkiaSource that loads bitmaps for the requested scale factor from |
118 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the | 147 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the |
119 // requested scale factor does not exist, it will return the 1x bitmap scaled | 148 // requested scale factor does not exist, it will return the 1x bitmap scaled |
120 // by the scale factor. This may lead to broken UI if the correct size of the | 149 // by the scale factor. This may lead to broken UI if the correct size of the |
121 // scaled image is not exactly |scale_factor| * the size of the 1x resource. | 150 // scaled image is not exactly |scale_factor| * the size of the 1x resource. |
122 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images | 151 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images |
123 // are higlighted by blending them with red. | 152 // are higlighted by blending them with red. |
124 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { | 153 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 | 476 |
448 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( | 477 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( |
449 int resource_id, | 478 int resource_id, |
450 ScaleFactor scale_factor) const { | 479 ScaleFactor scale_factor) const { |
451 base::RefCountedMemory* bytes = NULL; | 480 base::RefCountedMemory* bytes = NULL; |
452 if (delegate_) | 481 if (delegate_) |
453 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); | 482 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); |
454 | 483 |
455 if (!bytes) { | 484 if (!bytes) { |
456 base::StringPiece data = | 485 base::StringPiece data = |
457 GetRawDataResourceForScale(resource_id, scale_factor); | 486 DoGetRawDataResourceForScale(resource_id, scale_factor); |
458 if (!data.empty()) { | 487 if (!data.empty()) { |
459 bytes = new base::RefCountedStaticMemory(data.data(), data.length()); | 488 if (data.starts_with(CUSTOM_GZIP_HEADER)) { |
489 // Jump past special identification byte prepended to header | |
490 const unsigned char* gzip_start = | |
491 reinterpret_cast<const unsigned char*>(data.data()) + 1; | |
492 bytes = DecodeGzipData(gzip_start, data.length() - 1); | |
493 } else { | |
494 bytes = new base::RefCountedStaticMemory(data.data(), data.length()); | |
495 } | |
460 } | 496 } |
461 } | 497 } |
462 | 498 |
463 return bytes; | 499 return bytes; |
464 } | 500 } |
465 | 501 |
466 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 502 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
467 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); | 503 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); |
468 } | 504 } |
469 | 505 |
470 base::StringPiece ResourceBundle::GetRawDataResourceForScale( | 506 base::StringPiece ResourceBundle::GetRawDataResourceForScale( |
471 int resource_id, | 507 int resource_id, |
472 ScaleFactor scale_factor) const { | 508 ScaleFactor scale_factor) const { |
509 base::StringPiece data = | |
510 DoGetRawDataResourceForScale(resource_id, scale_factor); | |
511 | |
512 // Do not allow this function to retrieve gzip compressed resources | |
agrieve
2016/05/27 17:40:00
nit: missing trailing .
smaier
2016/05/27 17:48:47
Done.
| |
513 CHECK(!data.starts_with(CUSTOM_GZIP_HEADER)); | |
514 | |
515 return data; | |
516 } | |
517 | |
518 base::StringPiece ResourceBundle::DoGetRawDataResourceForScale( | |
519 int resource_id, | |
520 ScaleFactor scale_factor) const { | |
473 base::StringPiece data; | 521 base::StringPiece data; |
474 if (delegate_ && | 522 if (delegate_ && |
475 delegate_->GetRawDataResource(resource_id, scale_factor, &data)) | 523 delegate_->GetRawDataResource(resource_id, scale_factor, &data)) |
476 return data; | 524 return data; |
477 | 525 |
478 if (scale_factor != ui::SCALE_FACTOR_100P) { | 526 if (scale_factor != ui::SCALE_FACTOR_100P) { |
479 for (size_t i = 0; i < data_packs_.size(); i++) { | 527 for (size_t i = 0; i < data_packs_.size(); i++) { |
480 if (data_packs_[i]->GetScaleFactor() == scale_factor && | 528 if (data_packs_[i]->GetScaleFactor() == scale_factor && |
481 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id), | 529 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id), |
482 &data)) | 530 &data)) |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 // static | 944 // static |
897 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 945 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
898 size_t size, | 946 size_t size, |
899 SkBitmap* bitmap, | 947 SkBitmap* bitmap, |
900 bool* fell_back_to_1x) { | 948 bool* fell_back_to_1x) { |
901 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 949 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
902 return gfx::PNGCodec::Decode(buf, size, bitmap); | 950 return gfx::PNGCodec::Decode(buf, size, bitmap); |
903 } | 951 } |
904 | 952 |
905 } // namespace ui | 953 } // namespace ui |
OLD | NEW |