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

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
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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 std::unique_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P));
sadrul 2017/07/04 17:01:15 auto data_pack = base::MakeUnique...
F 2017/07/10 18:56:34 Done.
255 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) {
256 NOTREACHED() << "failed to load secondary pak file";
257 return;
258 }
259 g_shared_instance_->secondary_locale_resources_data_ = std::move(data_pack);
sadrul 2017/07/04 17:01:15 Why do you need to use |g_shared_instance| here?
F 2017/07/10 18:56:34 Oops. Thanks! Done
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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 return it->second; 555 return it->second;
544 556
545 // If for some reason we were unable to load the resources , return an empty 557 // If for some reason we were unable to load the resources , return an empty
546 // string (better than crashing). 558 // string (better than crashing).
547 if (!locale_resources_data_.get()) { 559 if (!locale_resources_data_.get()) {
548 LOG(WARNING) << "locale resources are not loaded"; 560 LOG(WARNING) << "locale resources are not loaded";
549 return base::string16(); 561 return base::string16();
550 } 562 }
551 563
552 base::StringPiece data; 564 base::StringPiece data;
565 ResourceHandle::TextEncodingType encoding =
566 locale_resources_data_->GetTextEncodingType();
553 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id), 567 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id),
554 &data)) { 568 &data)) {
555 // Fall back on the main data pack (shouldn't be any strings here except in 569 if (secondary_locale_resources_data_.get() &&
556 // unittests). 570 secondary_locale_resources_data_->GetStringPiece(
557 data = GetRawDataResource(message_id); 571 static_cast<uint16_t>(message_id), &data)) {
558 if (data.empty()) { 572 // Fall back on the secondary locale pak if it exists.
559 NOTREACHED() << "unable to find resource: " << message_id; 573 encoding = secondary_locale_resources_data_->GetTextEncodingType();
560 return base::string16(); 574 } else {
575 // Fall back on the main data pack (shouldn't be any strings here except
576 // in unittests).
577 data = GetRawDataResource(message_id);
578 if (data.empty()) {
579 NOTREACHED() << "unable to find resource: " << message_id;
580 return base::string16();
581 }
sadrul 2017/07/04 17:01:14 This looks backwards. Shouldn't you look up in the
F 2017/07/10 18:56:34 Before the change, Chrome would look for string re
561 } 582 }
562 } 583 }
563 584
564 // Strings should not be loaded from a data pack that contains binary data. 585 // Strings should not be loaded from a data pack that contains binary data.
565 ResourceHandle::TextEncodingType encoding =
566 locale_resources_data_->GetTextEncodingType();
567 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) 586 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
568 << "requested localized string from binary pack file"; 587 << "requested localized string from binary pack file";
569 588
570 // Data pack encodes strings as either UTF8 or UTF16. 589 // Data pack encodes strings as either UTF8 or UTF16.
571 base::string16 msg; 590 base::string16 msg;
572 if (encoding == ResourceHandle::UTF16) { 591 if (encoding == ResourceHandle::UTF16) {
573 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), 592 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()),
574 data.length() / 2); 593 data.length() / 2);
575 } else if (encoding == ResourceHandle::UTF8) { 594 } else if (encoding == ResourceHandle::UTF8) {
576 msg = base::UTF8ToUTF16(data); 595 msg = base::UTF8ToUTF16(data);
577 } 596 }
578 return msg; 597 return msg;
579 } 598 }
580 599
581 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes( 600 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes(
582 int resource_id) { 601 int resource_id) {
583 { 602 {
584 base::AutoLock lock_scope(*locale_resources_data_lock_); 603 base::AutoLock lock_scope(*locale_resources_data_lock_);
585 base::StringPiece data; 604 base::StringPiece data;
586 if (locale_resources_data_.get() && 605 if (locale_resources_data_.get() &&
587 locale_resources_data_->GetStringPiece( 606 locale_resources_data_->GetStringPiece(
588 static_cast<uint16_t>(resource_id), &data) && 607 static_cast<uint16_t>(resource_id), &data) &&
589 !data.empty()) { 608 !data.empty()) {
590 return new base::RefCountedStaticMemory(data.data(), data.length()); 609 return new base::RefCountedStaticMemory(data.data(), data.length());
610 } else if (secondary_locale_resources_data_.get() &&
sadrul 2017/07/04 17:01:14 Don't need else.
F 2017/07/10 18:56:34 Done.
611 secondary_locale_resources_data_->GetStringPiece(
612 static_cast<uint16_t>(resource_id), &data) &&
613 !data.empty()) {
614 return new base::RefCountedStaticMemory(data.data(), data.length());
591 } 615 }
592 } 616 }
593 // Release lock_scope and fall back to main data pack. 617 // Release lock_scope and fall back to main data pack.
594 return LoadDataResourceBytes(resource_id); 618 return LoadDataResourceBytes(resource_id);
595 } 619 }
596 620
597 const gfx::FontList& ResourceBundle::GetFontListWithDelta( 621 const gfx::FontList& ResourceBundle::GetFontListWithDelta(
598 int size_delta, 622 int size_delta,
599 gfx::Font::FontStyle style, 623 gfx::Font::FontStyle style,
600 gfx::Font::Weight weight) { 624 gfx::Font::Weight weight) {
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 // static 946 // static
923 bool ResourceBundle::DecodePNG(const unsigned char* buf, 947 bool ResourceBundle::DecodePNG(const unsigned char* buf,
924 size_t size, 948 size_t size,
925 SkBitmap* bitmap, 949 SkBitmap* bitmap,
926 bool* fell_back_to_1x) { 950 bool* fell_back_to_1x) {
927 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 951 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
928 return gfx::PNGCodec::Decode(buf, size, bitmap); 952 return gfx::PNGCodec::Decode(buf, size, bitmap);
929 } 953 }
930 954
931 } // namespace ui 955 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698