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

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

Issue 2028393003: [Merge to 2756] Revert "Compress .pak resources with new option: "type=GZIPPABLE_BINDATA"" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2756
Patch Set: 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"
27 #include "build/build_config.h" 26 #include "build/build_config.h"
28 #include "skia/ext/image_operations.h" 27 #include "skia/ext/image_operations.h"
29 #include "third_party/skia/include/core/SkBitmap.h" 28 #include "third_party/skia/include/core/SkBitmap.h"
30 #include "third_party/skia/include/core/SkColor.h" 29 #include "third_party/skia/include/core/SkColor.h"
31 #include "third_party/zlib/zlib.h"
32 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/layout.h" 31 #include "ui/base/layout.h"
34 #include "ui/base/material_design/material_design_controller.h" 32 #include "ui/base/material_design/material_design_controller.h"
35 #include "ui/base/resource/data_pack.h" 33 #include "ui/base/resource/data_pack.h"
36 #include "ui/base/ui_base_paths.h" 34 #include "ui/base/ui_base_paths.h"
37 #include "ui/base/ui_base_switches.h" 35 #include "ui/base/ui_base_switches.h"
38 #include "ui/base/ui_features.h" 36 #include "ui/base/ui_features.h"
39 #include "ui/display/display.h" 37 #include "ui/display/display.h"
40 #include "ui/display/screen.h" 38 #include "ui/display/screen.h"
41 #include "ui/gfx/codec/jpeg_codec.h" 39 #include "ui/gfx/codec/jpeg_codec.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 #endif // OS_WIN 105 #endif // OS_WIN
108 } 106 }
109 107
110 SkBitmap CreateEmptyBitmap() { 108 SkBitmap CreateEmptyBitmap() {
111 SkBitmap bitmap; 109 SkBitmap bitmap;
112 bitmap.allocN32Pixels(32, 32); 110 bitmap.allocN32Pixels(32, 32);
113 bitmap.eraseARGB(255, 255, 255, 0); 111 bitmap.eraseARGB(255, 255, 255, 0);
114 return bitmap; 112 return bitmap;
115 } 113 }
116 114
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
150 } // namespace 115 } // namespace
151 116
152 // An ImageSkiaSource that loads bitmaps for the requested scale factor from 117 // An ImageSkiaSource that loads bitmaps for the requested scale factor from
153 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the 118 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the
154 // requested scale factor does not exist, it will return the 1x bitmap scaled 119 // requested scale factor does not exist, it will return the 1x bitmap scaled
155 // by the scale factor. This may lead to broken UI if the correct size of the 120 // by the scale factor. This may lead to broken UI if the correct size of the
156 // scaled image is not exactly |scale_factor| * the size of the 1x resource. 121 // scaled image is not exactly |scale_factor| * the size of the 1x resource.
157 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images 122 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images
158 // are higlighted by blending them with red. 123 // are higlighted by blending them with red.
159 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { 124 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource {
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 447
483 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( 448 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale(
484 int resource_id, 449 int resource_id,
485 ScaleFactor scale_factor) const { 450 ScaleFactor scale_factor) const {
486 base::RefCountedMemory* bytes = NULL; 451 base::RefCountedMemory* bytes = NULL;
487 if (delegate_) 452 if (delegate_)
488 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); 453 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
489 454
490 if (!bytes) { 455 if (!bytes) {
491 base::StringPiece data = 456 base::StringPiece data =
492 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 457 GetRawDataResourceForScale(resource_id, scale_factor);
493 if (!data.empty()) { 458 if (!data.empty()) {
494 if (data.starts_with(CUSTOM_GZIP_HEADER)) { 459 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
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 }
502 } 460 }
503 } 461 }
504 462
505 return bytes; 463 return bytes;
506 } 464 }
507 465
508 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 466 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
509 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); 467 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE);
510 } 468 }
511 469
512 base::StringPiece ResourceBundle::GetRawDataResourceForScale( 470 base::StringPiece ResourceBundle::GetRawDataResourceForScale(
513 int resource_id, 471 int resource_id,
514 ScaleFactor scale_factor) const { 472 ScaleFactor scale_factor) const {
515 base::StringPiece data = 473 base::StringPiece data;
516 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 474 if (delegate_ &&
475 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
476 return data;
517 477
518 // Do not allow this function to retrieve gzip compressed resources. 478 if (scale_factor != ui::SCALE_FACTOR_100P) {
519 CHECK(!data.starts_with(CUSTOM_GZIP_HEADER)); 479 for (size_t i = 0; i < data_packs_.size(); i++) {
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 }
520 486
521 return data; 487 for (size_t i = 0; i < data_packs_.size(); i++) {
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();
522 } 498 }
523 499
524 base::string16 ResourceBundle::GetLocalizedString(int message_id) { 500 base::string16 ResourceBundle::GetLocalizedString(int message_id) {
525 base::string16 string; 501 base::string16 string;
526 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 502 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
527 return string; 503 return string;
528 504
529 // Ensure that ReloadLocaleResources() doesn't drop the resources while 505 // Ensure that ReloadLocaleResources() doesn't drop the resources while
530 // we're using them. 506 // we're using them.
531 base::AutoLock lock_scope(*locale_resources_data_lock_); 507 base::AutoLock lock_scope(*locale_resources_data_lock_);
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 data_handle_100_percent && 840 data_handle_100_percent &&
865 LoadBitmap(*data_handle_100_percent, resource_id, bitmap, 841 LoadBitmap(*data_handle_100_percent, resource_id, bitmap,
866 fell_back_to_1x)) { 842 fell_back_to_1x)) {
867 *fell_back_to_1x = true; 843 *fell_back_to_1x = true;
868 return true; 844 return true;
869 } 845 }
870 846
871 return false; 847 return false;
872 } 848 }
873 849
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
904 gfx::Image& ResourceBundle::GetEmptyImage() { 850 gfx::Image& ResourceBundle::GetEmptyImage() {
905 base::AutoLock lock(*images_and_fonts_lock_); 851 base::AutoLock lock(*images_and_fonts_lock_);
906 852
907 if (empty_image_.IsEmpty()) { 853 if (empty_image_.IsEmpty()) {
908 // The placeholder bitmap is bright red so people notice the problem. 854 // The placeholder bitmap is bright red so people notice the problem.
909 SkBitmap bitmap = CreateEmptyBitmap(); 855 SkBitmap bitmap = CreateEmptyBitmap();
910 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); 856 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap);
911 } 857 }
912 return empty_image_; 858 return empty_image_;
913 } 859 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 // static 896 // static
951 bool ResourceBundle::DecodePNG(const unsigned char* buf, 897 bool ResourceBundle::DecodePNG(const unsigned char* buf,
952 size_t size, 898 size_t size,
953 SkBitmap* bitmap, 899 SkBitmap* bitmap,
954 bool* fell_back_to_1x) { 900 bool* fell_back_to_1x) {
955 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 901 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
956 return gfx::PNGCodec::Decode(buf, size, bitmap); 902 return gfx::PNGCodec::Decode(buf, size, bitmap);
957 } 903 }
958 904
959 } // namespace ui 905 } // 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