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

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

Issue 2149323003: Change the way that gzipped resources are loaded from resources.pak (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: found more Created 4 years, 5 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
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 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 470
506 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( 471 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale(
507 int resource_id, 472 int resource_id,
508 ScaleFactor scale_factor) const { 473 ScaleFactor scale_factor) const {
509 base::RefCountedMemory* bytes = NULL; 474 base::RefCountedMemory* bytes = NULL;
510 if (delegate_) 475 if (delegate_)
511 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); 476 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
512 477
513 if (!bytes) { 478 if (!bytes) {
514 base::StringPiece data = 479 base::StringPiece data =
515 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 480 GetRawDataResourceForScale(resource_id, scale_factor);
516 if (!data.empty()) { 481 if (!data.empty())
517 if (data.starts_with(CUSTOM_GZIP_HEADER)) { 482 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
518 // Jump past special identification byte prepended to header
519 const unsigned char* gzip_start =
520 reinterpret_cast<const unsigned char*>(data.data()) + 1;
521 bytes = DecodeGzipData(gzip_start, data.length() - 1);
522 } else {
523 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
524 }
525 }
526 } 483 }
527 484
528 return bytes; 485 return bytes;
529 } 486 }
530 487
531 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 488 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
532 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); 489 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE);
533 } 490 }
534 491
535 base::StringPiece ResourceBundle::GetRawDataResourceForScale( 492 base::StringPiece ResourceBundle::GetRawDataResourceForScale(
536 int resource_id, 493 int resource_id,
537 ScaleFactor scale_factor) const { 494 ScaleFactor scale_factor) const {
538 base::StringPiece data = 495 base::StringPiece data;
539 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 496 if (delegate_ &&
497 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
498 return data;
540 499
541 // Do not allow this function to retrieve gzip compressed resources. 500 if (scale_factor != ui::SCALE_FACTOR_100P) {
542 CHECK(!data.starts_with(CUSTOM_GZIP_HEADER)); 501 for (size_t i = 0; i < data_packs_.size(); i++) {
502 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
503 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
504 &data))
505 return data;
506 }
507 }
543 508
544 return data; 509 for (size_t i = 0; i < data_packs_.size(); i++) {
510 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
511 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
512 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
513 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
514 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
515 &data))
516 return data;
517 }
518
519 return base::StringPiece();
545 } 520 }
546 521
547 base::string16 ResourceBundle::GetLocalizedString(int message_id) { 522 base::string16 ResourceBundle::GetLocalizedString(int message_id) {
548 base::string16 string; 523 base::string16 string;
549 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 524 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
550 return string; 525 return string;
551 526
552 // Ensure that ReloadLocaleResources() doesn't drop the resources while 527 // Ensure that ReloadLocaleResources() doesn't drop the resources while
553 // we're using them. 528 // we're using them.
554 base::AutoLock lock_scope(*locale_resources_data_lock_); 529 base::AutoLock lock_scope(*locale_resources_data_lock_);
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 data_handle_100_percent && 869 data_handle_100_percent &&
895 LoadBitmap(*data_handle_100_percent, resource_id, bitmap, 870 LoadBitmap(*data_handle_100_percent, resource_id, bitmap,
896 fell_back_to_1x)) { 871 fell_back_to_1x)) {
897 *fell_back_to_1x = true; 872 *fell_back_to_1x = true;
898 return true; 873 return true;
899 } 874 }
900 875
901 return false; 876 return false;
902 } 877 }
903 878
904 base::StringPiece ResourceBundle::GetRawDataResourceForScaleImpl(
905 int resource_id,
906 ScaleFactor scale_factor) const {
907 base::StringPiece data;
908 if (delegate_ &&
909 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
910 return data;
911
912 if (scale_factor != ui::SCALE_FACTOR_100P) {
913 for (size_t i = 0; i < data_packs_.size(); i++) {
914 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
915 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
916 &data))
917 return data;
918 }
919 }
920
921 for (size_t i = 0; i < data_packs_.size(); i++) {
922 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
923 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
924 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
925 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
926 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
927 &data))
928 return data;
929 }
930
931 return base::StringPiece();
932 }
933
934 gfx::Image& ResourceBundle::GetEmptyImage() { 879 gfx::Image& ResourceBundle::GetEmptyImage() {
935 base::AutoLock lock(*images_and_fonts_lock_); 880 base::AutoLock lock(*images_and_fonts_lock_);
936 881
937 if (empty_image_.IsEmpty()) { 882 if (empty_image_.IsEmpty()) {
938 // The placeholder bitmap is bright red so people notice the problem. 883 // The placeholder bitmap is bright red so people notice the problem.
939 SkBitmap bitmap = CreateEmptyBitmap(); 884 SkBitmap bitmap = CreateEmptyBitmap();
940 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); 885 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap);
941 } 886 }
942 return empty_image_; 887 return empty_image_;
943 } 888 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 // static 925 // static
981 bool ResourceBundle::DecodePNG(const unsigned char* buf, 926 bool ResourceBundle::DecodePNG(const unsigned char* buf,
982 size_t size, 927 size_t size,
983 SkBitmap* bitmap, 928 SkBitmap* bitmap,
984 bool* fell_back_to_1x) { 929 bool* fell_back_to_1x) {
985 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 930 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
986 return gfx::PNGCodec::Decode(buf, size, bitmap); 931 return gfx::PNGCodec::Decode(buf, size, bitmap);
987 } 932 }
988 933
989 } // namespace ui 934 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698