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

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: rename Created 4 years, 4 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/resource/data_pack.h" 32 #include "ui/base/resource/data_pack.h"
35 #include "ui/base/ui_base_paths.h" 33 #include "ui/base/ui_base_paths.h"
36 #include "ui/base/ui_base_switches.h" 34 #include "ui/base/ui_base_switches.h"
37 #include "ui/base/ui_features.h" 35 #include "ui/base/ui_features.h"
38 #include "ui/display/display.h" 36 #include "ui/display/display.h"
39 #include "ui/display/screen.h" 37 #include "ui/display/screen.h"
40 #include "ui/gfx/codec/jpeg_codec.h" 38 #include "ui/gfx/codec/jpeg_codec.h"
41 #include "ui/gfx/codec/png_codec.h" 39 #include "ui/gfx/codec/png_codec.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 #endif // OS_WIN 104 #endif // OS_WIN
107 } 105 }
108 106
109 SkBitmap CreateEmptyBitmap() { 107 SkBitmap CreateEmptyBitmap() {
110 SkBitmap bitmap; 108 SkBitmap bitmap;
111 bitmap.allocN32Pixels(32, 32); 109 bitmap.allocN32Pixels(32, 32);
112 bitmap.eraseARGB(255, 255, 255, 0); 110 bitmap.eraseARGB(255, 255, 255, 0);
113 return bitmap; 111 return bitmap;
114 } 112 }
115 113
116 // Decodes given gzip input via zlib.
117 base::RefCountedBytes* DecodeGzipData(const unsigned char* input_buffer,
118 size_t input_size) {
119 z_stream inflateStream;
120 memset(&inflateStream, 0, sizeof(inflateStream));
121 inflateStream.zalloc = Z_NULL;
122 inflateStream.zfree = Z_NULL;
123 inflateStream.opaque = Z_NULL;
124
125 inflateStream.avail_in = input_size;
126 inflateStream.next_in = const_cast<Bytef*>(input_buffer);
127
128 CHECK(input_size >= 4);
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 inflateStream.avail_out = base::ByteSwapToLE32(
132 *reinterpret_cast<const uint32_t*>(&input_buffer[input_size - 4]));
133
134 std::vector<unsigned char> output(inflateStream.avail_out);
135 inflateStream.next_out = reinterpret_cast<Bytef*>(&output[0]);
136
137 CHECK(inflateInit2(&inflateStream, 16) == Z_OK);
138 CHECK(inflate(&inflateStream, Z_FINISH) == Z_STREAM_END);
139 CHECK(inflateEnd(&inflateStream) == Z_OK);
140
141 // Cannot use TakeVector since it puts the RefCounted* into a scoped_refptr,
142 // and callers of this function return a raw pointer (not a scoped_refptr), so
143 // the memory will be deallocated upon exit of the calling function.
144 base::RefCountedBytes* returnVal = new base::RefCountedBytes();
145 returnVal->data().swap(output);
146 return returnVal;
147 }
148
149 } // namespace 114 } // namespace
150 115
151 // An ImageSkiaSource that loads bitmaps for the requested scale factor from 116 // An ImageSkiaSource that loads bitmaps for the requested scale factor from
152 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the 117 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the
153 // requested scale factor does not exist, it will return the 1x bitmap scaled 118 // requested scale factor does not exist, it will return the 1x bitmap scaled
154 // by the scale factor. This may lead to broken UI if the correct size of the 119 // by the scale factor. This may lead to broken UI if the correct size of the
155 // scaled image is not exactly |scale_factor| * the size of the 1x resource. 120 // scaled image is not exactly |scale_factor| * the size of the 1x resource.
156 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images 121 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images
157 // are higlighted by blending them with red. 122 // are higlighted by blending them with red.
158 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { 123 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource {
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 460
496 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( 461 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale(
497 int resource_id, 462 int resource_id,
498 ScaleFactor scale_factor) const { 463 ScaleFactor scale_factor) const {
499 base::RefCountedMemory* bytes = NULL; 464 base::RefCountedMemory* bytes = NULL;
500 if (delegate_) 465 if (delegate_)
501 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); 466 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
502 467
503 if (!bytes) { 468 if (!bytes) {
504 base::StringPiece data = 469 base::StringPiece data =
505 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 470 GetRawDataResourceForScale(resource_id, scale_factor);
506 if (!data.empty()) { 471 if (!data.empty())
507 if (data.starts_with(CUSTOM_GZIP_HEADER)) { 472 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
508 // Jump past special identification byte prepended to header
509 const unsigned char* gzip_start =
510 reinterpret_cast<const unsigned char*>(data.data()) + 1;
511 bytes = DecodeGzipData(gzip_start, data.length() - 1);
512 } else {
513 bytes = new base::RefCountedStaticMemory(data.data(), data.length());
514 }
515 }
516 } 473 }
517 474
518 return bytes; 475 return bytes;
519 } 476 }
520 477
521 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 478 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const {
522 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE); 479 return GetRawDataResourceForScale(resource_id, ui::SCALE_FACTOR_NONE);
523 } 480 }
524 481
525 base::StringPiece ResourceBundle::GetRawDataResourceForScale( 482 base::StringPiece ResourceBundle::GetRawDataResourceForScale(
526 int resource_id, 483 int resource_id,
527 ScaleFactor scale_factor) const { 484 ScaleFactor scale_factor) const {
528 base::StringPiece data = 485 base::StringPiece data;
529 GetRawDataResourceForScaleImpl(resource_id, scale_factor); 486 if (delegate_ &&
487 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
488 return data;
530 489
531 // Do not allow this function to retrieve gzip compressed resources. 490 if (scale_factor != ui::SCALE_FACTOR_100P) {
532 CHECK(!data.starts_with(CUSTOM_GZIP_HEADER)); 491 for (size_t i = 0; i < data_packs_.size(); i++) {
492 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
493 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
494 &data))
495 return data;
496 }
497 }
533 498
534 return data; 499 for (size_t i = 0; i < data_packs_.size(); i++) {
500 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
501 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
502 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
503 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
504 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
505 &data))
506 return data;
507 }
508
509 return base::StringPiece();
535 } 510 }
536 511
537 base::string16 ResourceBundle::GetLocalizedString(int message_id) { 512 base::string16 ResourceBundle::GetLocalizedString(int message_id) {
538 base::string16 string; 513 base::string16 string;
539 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 514 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
540 return string; 515 return string;
541 516
542 // Ensure that ReloadLocaleResources() doesn't drop the resources while 517 // Ensure that ReloadLocaleResources() doesn't drop the resources while
543 // we're using them. 518 // we're using them.
544 base::AutoLock lock_scope(*locale_resources_data_lock_); 519 base::AutoLock lock_scope(*locale_resources_data_lock_);
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 data_handle_100_percent && 832 data_handle_100_percent &&
858 LoadBitmap(*data_handle_100_percent, resource_id, bitmap, 833 LoadBitmap(*data_handle_100_percent, resource_id, bitmap,
859 fell_back_to_1x)) { 834 fell_back_to_1x)) {
860 *fell_back_to_1x = true; 835 *fell_back_to_1x = true;
861 return true; 836 return true;
862 } 837 }
863 838
864 return false; 839 return false;
865 } 840 }
866 841
867 base::StringPiece ResourceBundle::GetRawDataResourceForScaleImpl(
868 int resource_id,
869 ScaleFactor scale_factor) const {
870 base::StringPiece data;
871 if (delegate_ &&
872 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
873 return data;
874
875 if (scale_factor != ui::SCALE_FACTOR_100P) {
876 for (size_t i = 0; i < data_packs_.size(); i++) {
877 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
878 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
879 &data))
880 return data;
881 }
882 }
883
884 for (size_t i = 0; i < data_packs_.size(); i++) {
885 if ((data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P ||
886 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_200P ||
887 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_300P ||
888 data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_NONE) &&
889 data_packs_[i]->GetStringPiece(static_cast<uint16_t>(resource_id),
890 &data))
891 return data;
892 }
893
894 return base::StringPiece();
895 }
896
897 gfx::Image& ResourceBundle::GetEmptyImage() { 842 gfx::Image& ResourceBundle::GetEmptyImage() {
898 base::AutoLock lock(*images_and_fonts_lock_); 843 base::AutoLock lock(*images_and_fonts_lock_);
899 844
900 if (empty_image_.IsEmpty()) { 845 if (empty_image_.IsEmpty()) {
901 // The placeholder bitmap is bright red so people notice the problem. 846 // The placeholder bitmap is bright red so people notice the problem.
902 SkBitmap bitmap = CreateEmptyBitmap(); 847 SkBitmap bitmap = CreateEmptyBitmap();
903 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); 848 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap);
904 } 849 }
905 return empty_image_; 850 return empty_image_;
906 } 851 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 // static 888 // static
944 bool ResourceBundle::DecodePNG(const unsigned char* buf, 889 bool ResourceBundle::DecodePNG(const unsigned char* buf,
945 size_t size, 890 size_t size,
946 SkBitmap* bitmap, 891 SkBitmap* bitmap,
947 bool* fell_back_to_1x) { 892 bool* fell_back_to_1x) {
948 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 893 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
949 return gfx::PNGCodec::Decode(buf, size, bitmap); 894 return gfx::PNGCodec::Decode(buf, size, bitmap);
950 } 895 }
951 896
952 } // namespace ui 897 } // 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