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

Unified Diff: ui/base/resource/resource_bundle.cc

Issue 2977993002: Reland of Deduplicate Monochrome locale .paks (Closed)
Patch Set: 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/resource/resource_bundle.cc
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc
index 7e66615fa1b4f0490b78f17dfcd0c6c077f27776..372d3302e133c266429ea49ac449055a8ab4de87 100644
--- a/ui/base/resource/resource_bundle.cc
+++ b/ui/base/resource/resource_bundle.cc
@@ -211,9 +211,10 @@ void ResourceBundle::InitSharedInstanceWithPakFileRegion(
base::File pak_file,
const base::MemoryMappedFile::Region& region) {
InitSharedInstance(NULL);
- std::unique_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P));
+ auto data_pack = base::MakeUnique<DataPack>(SCALE_FACTOR_100P);
if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) {
- NOTREACHED() << "failed to load pak file";
+ LOG(WARNING) << "failed to load pak file";
+ NOTREACHED();
return;
}
g_shared_instance_->locale_resources_data_ = std::move(data_pack);
@@ -248,6 +249,18 @@ ResourceBundle& ResourceBundle::GetSharedInstance() {
return *g_shared_instance_;
}
+void ResourceBundle::LoadSecondaryLocaleDataWithPakFileRegion(
+ base::File pak_file,
+ const base::MemoryMappedFile::Region& region) {
+ auto data_pack = base::MakeUnique<DataPack>(SCALE_FACTOR_100P);
+ if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) {
+ LOG(WARNING) << "failed to load secondary pak file";
+ NOTREACHED();
+ return;
+ }
+ secondary_locale_resources_data_ = std::move(data_pack);
+}
+
#if !defined(OS_ANDROID)
bool ResourceBundle::LocaleDataPakExists(const std::string& locale) {
return !GetLocaleFilePath(locale, true).empty();
@@ -389,6 +402,7 @@ void ResourceBundle::LoadTestResources(const base::FilePath& path,
void ResourceBundle::UnloadLocaleResources() {
locale_resources_data_.reset();
+ secondary_locale_resources_data_.reset();
}
void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) {
@@ -551,20 +565,28 @@ base::string16 ResourceBundle::GetLocalizedString(int message_id) {
}
base::StringPiece data;
+ ResourceHandle::TextEncodingType encoding =
+ locale_resources_data_->GetTextEncodingType();
if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id),
&data)) {
- // Fall back on the main data pack (shouldn't be any strings here except in
- // unittests).
- data = GetRawDataResource(message_id);
- if (data.empty()) {
- NOTREACHED() << "unable to find resource: " << message_id;
- return base::string16();
+ if (secondary_locale_resources_data_.get() &&
+ secondary_locale_resources_data_->GetStringPiece(
+ static_cast<uint16_t>(message_id), &data)) {
+ // Fall back on the secondary locale pak if it exists.
+ encoding = secondary_locale_resources_data_->GetTextEncodingType();
+ } else {
+ // Fall back on the main data pack (shouldn't be any strings here except
+ // in unittests).
+ data = GetRawDataResource(message_id);
+ if (data.empty()) {
+ LOG(WARNING) << "unable to find resource: " << message_id;
+ NOTREACHED();
+ return base::string16();
+ }
}
}
// Strings should not be loaded from a data pack that contains binary data.
- ResourceHandle::TextEncodingType encoding =
- locale_resources_data_->GetTextEncodingType();
DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
<< "requested localized string from binary pack file";
@@ -584,12 +606,20 @@ base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes(
{
base::AutoLock lock_scope(*locale_resources_data_lock_);
base::StringPiece data;
+
if (locale_resources_data_.get() &&
locale_resources_data_->GetStringPiece(
static_cast<uint16_t>(resource_id), &data) &&
!data.empty()) {
return new base::RefCountedStaticMemory(data.data(), data.length());
}
+
+ if (secondary_locale_resources_data_.get() &&
+ secondary_locale_resources_data_->GetStringPiece(
+ static_cast<uint16_t>(resource_id), &data) &&
+ !data.empty()) {
+ return new base::RefCountedStaticMemory(data.data(), data.length());
+ }
}
// Release lock_scope and fall back to main data pack.
return LoadDataResourceBytes(resource_id);
« 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