Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1087)

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 1968993002: Compressing .pak resources with new option: "type=GZIPPABLE_BINDATA" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Python formatting Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(const unsigned char* input_buffer,
119 size_t input_size) {
120 z_stream inflateStream;
121 memset(&inflateStream, 0, sizeof(inflateStream));
122 inflateStream.zalloc = Z_NULL;
123 inflateStream.zfree = Z_NULL;
124 inflateStream.opaque = Z_NULL;
125
126 inflateStream.avail_in = input_size;
127 inflateStream.next_in = const_cast<Bytef*>(input_buffer);
128
129 CHECK(input_size >= 4);
130 // Size of output comes from footer of gzip file format, found as the last 4
131 // bytes in the compressed file, which are stored little endian.
132 inflateStream.avail_out = base::ByteSwapToLE32(
133 *reinterpret_cast<const uint32_t*>(&input_buffer[input_size - 4]));
134
135 std::vector<unsigned char> output(inflateStream.avail_out);
136 inflateStream.next_out = reinterpret_cast<Bytef*>(&output[0]);
137
138 CHECK(inflateInit2(&inflateStream, 16) == Z_OK);
139 CHECK(inflate(&inflateStream, Z_FINISH) == Z_STREAM_END);
140 CHECK(inflateEnd(&inflateStream) == Z_OK);
141
142 // Cannot use TakeVector since it puts the RefCounted* into a scoped_refptr,
143 // and callers of this function return a raw pointer (not a scoped_refptr), so
144 // the memory will be deallocated upon exit of the calling function.
145 base::RefCountedBytes* returnVal = new base::RefCountedBytes();
146 returnVal->data().swap(output);
147 return returnVal;
148 }
149
115 } // namespace 150 } // namespace
116 151
117 // An ImageSkiaSource that loads bitmaps for the requested scale factor from 152 // 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 153 // 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 154 // 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 155 // 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. 156 // 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 157 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images
123 // are higlighted by blending them with red. 158 // are higlighted by blending them with red.
124 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { 159 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource {
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 482
448 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( 483 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale(
449 int resource_id, 484 int resource_id,
450 ScaleFactor scale_factor) const { 485 ScaleFactor scale_factor) const {
451 base::RefCountedMemory* bytes = NULL; 486 base::RefCountedMemory* bytes = NULL;
452 if (delegate_) 487 if (delegate_)
453 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); 488 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
454 489
455 if (!bytes) { 490 if (!bytes) {
456 base::StringPiece data = 491 base::StringPiece data =
457 GetRawDataResourceForScale(resource_id, scale_factor); 492 GetRawDataResourceForScaleImpl(resource_id, scale_factor);
458 if (!data.empty()) { 493 if (!data.empty()) {
459 bytes = new base::RefCountedStaticMemory(data.data(), data.length()); 494 if (data.starts_with(CUSTOM_GZIP_HEADER)) {
495 // Jump past special identification byte prepended to header
496 const unsigned char* gzip_start =
497 reinterpret_cast<const unsigned char*>(data.data()) + 1;
498 bytes = DecodeGzipData(gzip_start, data.length() - 1);
499 } else {
500 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
501 }
460 } 502 }
461 } 503 }
462 504
463 return bytes; 505 return bytes;
464 } 506 }
465 507
466 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 508 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
467 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); 509 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE);
468 } 510 }
469 511
470 base::StringPiece ResourceBundle::GetRawDataResourceForScale( 512 base::StringPiece ResourceBundle::GetRawDataResourceForScale(
471 int resource_id, 513 int resource_id,
472 ScaleFactor scale_factor) const { 514 ScaleFactor scale_factor) const {
473 base::StringPiece data; 515 base::StringPiece data =
474 if (delegate_ && 516 GetRawDataResourceForScaleImpl(resource_id, scale_factor);
475 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
476 return data;
477 517
478 if (scale_factor != ui::SCALE_FACTOR_100P) { 518 // Do not allow this function to retrieve gzip compressed resources.
479 for (size_t i = 0; i < data_packs_.size(); i++) { 519 CHECK(!data.starts_with(CUSTOM_GZIP_HEADER));
480 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
481 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
482 &data))
483 return data;
484 }
485 }
486 520
487 for (size_t i = 0; i < data_packs_.size(); i++) { 521 return data;
488 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
489 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
490 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
491 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
492 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
493 &data))
494 return data;
495 }
496
497 return base::StringPiece();
498 } 522 }
499 523
500 base::string16 ResourceBundle::GetLocalizedString(int message_id) { 524 base::string16 ResourceBundle::GetLocalizedString(int message_id) {
501 base::string16 string; 525 base::string16 string;
502 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 526 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
503 return string; 527 return string;
504 528
505 // Ensure that ReloadLocaleResources() doesn't drop the resources while 529 // Ensure that ReloadLocaleResources() doesn't drop the resources while
506 // we're using them. 530 // we're using them.
507 base::AutoLock lock_scope(*locale_resources_data_lock_); 531 base::AutoLock lock_scope(*locale_resources_data_lock_);
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 data_handle_100_percent && 864 data_handle_100_percent &&
841 LoadBitmap(*data_handle_100_percent, resource_id, bitmap, 865 LoadBitmap(*data_handle_100_percent, resource_id, bitmap,
842 fell_back_to_1x)) { 866 fell_back_to_1x)) {
843 *fell_back_to_1x = true; 867 *fell_back_to_1x = true;
844 return true; 868 return true;
845 } 869 }
846 870
847 return false; 871 return false;
848 } 872 }
849 873
874 base::StringPiece ResourceBundle::GetRawDataResourceForScaleImpl(
875 int resource_id,
876 ScaleFactor scale_factor) const {
877 base::StringPiece data;
878 if (delegate_ &&
879 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
880 return data;
881
882 if (scale_factor != ui::SCALE_FACTOR_100P) {
883 for (size_t i = 0; i < data_packs_.size(); i++) {
884 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
885 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
886 &data))
887 return data;
888 }
889 }
890
891 for (size_t i = 0; i < data_packs_.size(); i++) {
892 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
893 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
894 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
895 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
896 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
897 &data))
898 return data;
899 }
900
901 return base::StringPiece();
902 }
903
850 gfx::Image& ResourceBundle::GetEmptyImage() { 904 gfx::Image& ResourceBundle::GetEmptyImage() {
851 base::AutoLock lock(*images_and_fonts_lock_); 905 base::AutoLock lock(*images_and_fonts_lock_);
852 906
853 if (empty_image_.IsEmpty()) { 907 if (empty_image_.IsEmpty()) {
854 // The placeholder bitmap is bright red so people notice the problem. 908 // The placeholder bitmap is bright red so people notice the problem.
855 SkBitmap bitmap = CreateEmptyBitmap(); 909 SkBitmap bitmap = CreateEmptyBitmap();
856 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); 910 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap);
857 } 911 }
858 return empty_image_; 912 return empty_image_;
859 } 913 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 // static 950 // static
897 bool ResourceBundle::DecodePNG(const unsigned char* buf, 951 bool ResourceBundle::DecodePNG(const unsigned char* buf,
898 size_t size, 952 size_t size,
899 SkBitmap* bitmap, 953 SkBitmap* bitmap,
900 bool* fell_back_to_1x) { 954 bool* fell_back_to_1x) {
901 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 955 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
902 return gfx::PNGCodec::Decode(buf, size, bitmap); 956 return gfx::PNGCodec::Decode(buf, size, bitmap);
903 } 957 }
904 958
905 } // namespace ui 959 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698