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

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

Issue 2933343002: Deduplicate Monochrome locale .paks (Closed)
Patch Set: Addressing comments Created 3 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
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_android.h » ('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>
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); 204 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale);
205 g_shared_instance_->InitDefaultFontList(); 205 g_shared_instance_->InitDefaultFontList();
206 return result; 206 return result;
207 } 207 }
208 208
209 // static 209 // static
210 void ResourceBundle::InitSharedInstanceWithPakFileRegion( 210 void ResourceBundle::InitSharedInstanceWithPakFileRegion(
211 base::File pak_file, 211 base::File pak_file,
212 const base::MemoryMappedFile::Region& region) { 212 const base::MemoryMappedFile::Region& region) {
213 InitSharedInstance(NULL); 213 InitSharedInstance(NULL);
214 std::unique_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P)); 214 auto data_pack = base::MakeUnique<DataPack>(SCALE_FACTOR_100P);
215 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) { 215 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) {
216 NOTREACHED() << "failed to load pak file"; 216 NOTREACHED() << "failed to load pak file";
217 return; 217 return;
218 } 218 }
219 g_shared_instance_->locale_resources_data_ = std::move(data_pack); 219 g_shared_instance_->locale_resources_data_ = std::move(data_pack);
220 g_shared_instance_->InitDefaultFontList(); 220 g_shared_instance_->InitDefaultFontList();
221 } 221 }
222 222
223 // static 223 // static
224 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) { 224 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) {
(...skipping 16 matching lines...) Expand all
241 return g_shared_instance_ != NULL; 241 return g_shared_instance_ != NULL;
242 } 242 }
243 243
244 // static 244 // static
245 ResourceBundle& ResourceBundle::GetSharedInstance() { 245 ResourceBundle& ResourceBundle::GetSharedInstance() {
246 // Must call InitSharedInstance before this function. 246 // Must call InitSharedInstance before this function.
247 CHECK(g_shared_instance_ != NULL); 247 CHECK(g_shared_instance_ != NULL);
248 return *g_shared_instance_; 248 return *g_shared_instance_;
249 } 249 }
250 250
251 void ResourceBundle::LoadSecondaryLocaleDataWithPakFileRegion(
252 base::File pak_file,
253 const base::MemoryMappedFile::Region& region) {
254 auto data_pack = base::MakeUnique<DataPack>(SCALE_FACTOR_100P);
255 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) {
256 NOTREACHED() << "failed to load secondary pak file";
257 return;
258 }
259 secondary_locale_resources_data_ = std::move(data_pack);
260 }
261
251 #if !defined(OS_ANDROID) 262 #if !defined(OS_ANDROID)
252 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { 263 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) {
253 return !GetLocaleFilePath(locale, true).empty(); 264 return !GetLocaleFilePath(locale, true).empty();
254 } 265 }
255 #endif // !defined(OS_ANDROID) 266 #endif // !defined(OS_ANDROID)
256 267
257 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path, 268 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path,
258 ScaleFactor scale_factor) { 269 ScaleFactor scale_factor) {
259 AddDataPackFromPathInternal(path, scale_factor, false); 270 AddDataPackFromPathInternal(path, scale_factor, false);
260 } 271 }
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 } else { 393 } else {
383 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE)); 394 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE));
384 } 395 }
385 // This is necessary to initialize ICU since we won't be calling 396 // This is necessary to initialize ICU since we won't be calling
386 // LoadLocaleResources in this case. 397 // LoadLocaleResources in this case.
387 l10n_util::GetApplicationLocale(std::string()); 398 l10n_util::GetApplicationLocale(std::string());
388 } 399 }
389 400
390 void ResourceBundle::UnloadLocaleResources() { 401 void ResourceBundle::UnloadLocaleResources() {
391 locale_resources_data_.reset(); 402 locale_resources_data_.reset();
403 secondary_locale_resources_data_.reset();
392 } 404 }
393 405
394 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) { 406 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) {
395 overridden_pak_path_ = pak_path; 407 overridden_pak_path_ = pak_path;
396 } 408 }
397 409
398 void ResourceBundle::OverrideLocaleStringResource( 410 void ResourceBundle::OverrideLocaleStringResource(
399 int message_id, 411 int message_id,
400 const base::string16& string) { 412 const base::string16& string) {
401 overridden_locale_strings_[message_id] = string; 413 overridden_locale_strings_[message_id] = string;
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 return it->second; 556 return it->second;
545 557
546 // If for some reason we were unable to load the resources , return an empty 558 // If for some reason we were unable to load the resources , return an empty
547 // string (better than crashing). 559 // string (better than crashing).
548 if (!locale_resources_data_.get()) { 560 if (!locale_resources_data_.get()) {
549 LOG(WARNING) << "locale resources are not loaded"; 561 LOG(WARNING) << "locale resources are not loaded";
550 return base::string16(); 562 return base::string16();
551 } 563 }
552 564
553 base::StringPiece data; 565 base::StringPiece data;
566 ResourceHandle::TextEncodingType encoding =
567 locale_resources_data_->GetTextEncodingType();
554 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id), 568 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id),
555 &data)) { 569 &data)) {
556 // Fall back on the main data pack (shouldn't be any strings here except in 570 if (secondary_locale_resources_data_.get() &&
557 // unittests). 571 secondary_locale_resources_data_->GetStringPiece(
558 data = GetRawDataResource(message_id); 572 static_cast<uint16_t>(message_id), &data)) {
559 if (data.empty()) { 573 // Fall back on the secondary locale pak if it exists.
560 NOTREACHED() << "unable to find resource: " << message_id; 574 encoding = secondary_locale_resources_data_->GetTextEncodingType();
561 return base::string16(); 575 } else {
576 // Fall back on the main data pack (shouldn't be any strings here except
577 // in unittests).
578 data = GetRawDataResource(message_id);
579 if (data.empty()) {
580 NOTREACHED() << "unable to find resource: " << message_id;
581 return base::string16();
582 }
562 } 583 }
563 } 584 }
564 585
565 // Strings should not be loaded from a data pack that contains binary data. 586 // Strings should not be loaded from a data pack that contains binary data.
566 ResourceHandle::TextEncodingType encoding =
567 locale_resources_data_->GetTextEncodingType();
568 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) 587 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
569 << "requested localized string from binary pack file"; 588 << "requested localized string from binary pack file";
570 589
571 // Data pack encodes strings as either UTF8 or UTF16. 590 // Data pack encodes strings as either UTF8 or UTF16.
572 base::string16 msg; 591 base::string16 msg;
573 if (encoding == ResourceHandle::UTF16) { 592 if (encoding == ResourceHandle::UTF16) {
574 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), 593 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()),
575 data.length() / 2); 594 data.length() / 2);
576 } else if (encoding == ResourceHandle::UTF8) { 595 } else if (encoding == ResourceHandle::UTF8) {
577 msg = base::UTF8ToUTF16(data); 596 msg = base::UTF8ToUTF16(data);
578 } 597 }
579 return msg; 598 return msg;
580 } 599 }
581 600
582 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes( 601 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes(
583 int resource_id) { 602 int resource_id) {
584 { 603 {
585 base::AutoLock lock_scope(*locale_resources_data_lock_); 604 base::AutoLock lock_scope(*locale_resources_data_lock_);
586 base::StringPiece data; 605 base::StringPiece data;
606
587 if (locale_resources_data_.get() && 607 if (locale_resources_data_.get() &&
588 locale_resources_data_->GetStringPiece( 608 locale_resources_data_->GetStringPiece(
589 static_cast<uint16_t>(resource_id), &data) && 609 static_cast<uint16_t>(resource_id), &data) &&
590 !data.empty()) { 610 !data.empty()) {
591 return new base::RefCountedStaticMemory(data.data(), data.length()); 611 return new base::RefCountedStaticMemory(data.data(), data.length());
592 } 612 }
613
614 if (secondary_locale_resources_data_.get() &&
615 secondary_locale_resources_data_->GetStringPiece(
616 static_cast<uint16_t>(resource_id), &data) &&
617 !data.empty()) {
618 return new base::RefCountedStaticMemory(data.data(), data.length());
619 }
593 } 620 }
594 // Release lock_scope and fall back to main data pack. 621 // Release lock_scope and fall back to main data pack.
595 return LoadDataResourceBytes(resource_id); 622 return LoadDataResourceBytes(resource_id);
596 } 623 }
597 624
598 const gfx::FontList& ResourceBundle::GetFontListWithDelta( 625 const gfx::FontList& ResourceBundle::GetFontListWithDelta(
599 int size_delta, 626 int size_delta,
600 gfx::Font::FontStyle style, 627 gfx::Font::FontStyle style,
601 gfx::Font::Weight weight) { 628 gfx::Font::Weight weight) {
602 DCHECK(sequence_checker_.CalledOnValidSequence()); 629 DCHECK(sequence_checker_.CalledOnValidSequence());
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 // static 950 // static
924 bool ResourceBundle::DecodePNG(const unsigned char* buf, 951 bool ResourceBundle::DecodePNG(const unsigned char* buf,
925 size_t size, 952 size_t size,
926 SkBitmap* bitmap, 953 SkBitmap* bitmap,
927 bool* fell_back_to_1x) { 954 bool* fell_back_to_1x) {
928 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 955 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
929 return gfx::PNGCodec::Decode(buf, size, bitmap); 956 return gfx::PNGCodec::Decode(buf, size, bitmap);
930 } 957 }
931 958
932 } // namespace ui 959 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698