OLD | NEW |
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 Loading... |
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 auto data_pack = base::MakeUnique<DataPack>(SCALE_FACTOR_100P); | 214 std::unique_ptr<DataPack> data_pack(new 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 Loading... |
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 | |
262 #if !defined(OS_ANDROID) | 251 #if !defined(OS_ANDROID) |
263 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 252 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
264 return !GetLocaleFilePath(locale, true).empty(); | 253 return !GetLocaleFilePath(locale, true).empty(); |
265 } | 254 } |
266 #endif // !defined(OS_ANDROID) | 255 #endif // !defined(OS_ANDROID) |
267 | 256 |
268 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path, | 257 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path, |
269 ScaleFactor scale_factor) { | 258 ScaleFactor scale_factor) { |
270 AddDataPackFromPathInternal(path, scale_factor, false); | 259 AddDataPackFromPathInternal(path, scale_factor, false); |
271 } | 260 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } else { | 382 } else { |
394 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE)); | 383 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
395 } | 384 } |
396 // This is necessary to initialize ICU since we won't be calling | 385 // This is necessary to initialize ICU since we won't be calling |
397 // LoadLocaleResources in this case. | 386 // LoadLocaleResources in this case. |
398 l10n_util::GetApplicationLocale(std::string()); | 387 l10n_util::GetApplicationLocale(std::string()); |
399 } | 388 } |
400 | 389 |
401 void ResourceBundle::UnloadLocaleResources() { | 390 void ResourceBundle::UnloadLocaleResources() { |
402 locale_resources_data_.reset(); | 391 locale_resources_data_.reset(); |
403 secondary_locale_resources_data_.reset(); | |
404 } | 392 } |
405 | 393 |
406 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) { | 394 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) { |
407 overridden_pak_path_ = pak_path; | 395 overridden_pak_path_ = pak_path; |
408 } | 396 } |
409 | 397 |
410 void ResourceBundle::OverrideLocaleStringResource( | 398 void ResourceBundle::OverrideLocaleStringResource( |
411 int message_id, | 399 int message_id, |
412 const base::string16& string) { | 400 const base::string16& string) { |
413 overridden_locale_strings_[message_id] = string; | 401 overridden_locale_strings_[message_id] = string; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 return it->second; | 544 return it->second; |
557 | 545 |
558 // If for some reason we were unable to load the resources , return an empty | 546 // If for some reason we were unable to load the resources , return an empty |
559 // string (better than crashing). | 547 // string (better than crashing). |
560 if (!locale_resources_data_.get()) { | 548 if (!locale_resources_data_.get()) { |
561 LOG(WARNING) << "locale resources are not loaded"; | 549 LOG(WARNING) << "locale resources are not loaded"; |
562 return base::string16(); | 550 return base::string16(); |
563 } | 551 } |
564 | 552 |
565 base::StringPiece data; | 553 base::StringPiece data; |
566 ResourceHandle::TextEncodingType encoding = | |
567 locale_resources_data_->GetTextEncodingType(); | |
568 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id), | 554 if (!locale_resources_data_->GetStringPiece(static_cast<uint16_t>(message_id), |
569 &data)) { | 555 &data)) { |
570 if (secondary_locale_resources_data_.get() && | 556 // Fall back on the main data pack (shouldn't be any strings here except in |
571 secondary_locale_resources_data_->GetStringPiece( | 557 // unittests). |
572 static_cast<uint16_t>(message_id), &data)) { | 558 data = GetRawDataResource(message_id); |
573 // Fall back on the secondary locale pak if it exists. | 559 if (data.empty()) { |
574 encoding = secondary_locale_resources_data_->GetTextEncodingType(); | 560 NOTREACHED() << "unable to find resource: " << message_id; |
575 } else { | 561 return base::string16(); |
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 } | |
583 } | 562 } |
584 } | 563 } |
585 | 564 |
586 // Strings should not be loaded from a data pack that contains binary data. | 565 // Strings should not be loaded from a data pack that contains binary data. |
| 566 ResourceHandle::TextEncodingType encoding = |
| 567 locale_resources_data_->GetTextEncodingType(); |
587 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | 568 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) |
588 << "requested localized string from binary pack file"; | 569 << "requested localized string from binary pack file"; |
589 | 570 |
590 // Data pack encodes strings as either UTF8 or UTF16. | 571 // Data pack encodes strings as either UTF8 or UTF16. |
591 base::string16 msg; | 572 base::string16 msg; |
592 if (encoding == ResourceHandle::UTF16) { | 573 if (encoding == ResourceHandle::UTF16) { |
593 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), | 574 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), |
594 data.length() / 2); | 575 data.length() / 2); |
595 } else if (encoding == ResourceHandle::UTF8) { | 576 } else if (encoding == ResourceHandle::UTF8) { |
596 msg = base::UTF8ToUTF16(data); | 577 msg = base::UTF8ToUTF16(data); |
597 } | 578 } |
598 return msg; | 579 return msg; |
599 } | 580 } |
600 | 581 |
601 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes( | 582 base::RefCountedMemory* ResourceBundle::LoadLocalizedResourceBytes( |
602 int resource_id) { | 583 int resource_id) { |
603 { | 584 { |
604 base::AutoLock lock_scope(*locale_resources_data_lock_); | 585 base::AutoLock lock_scope(*locale_resources_data_lock_); |
605 base::StringPiece data; | 586 base::StringPiece data; |
606 | |
607 if (locale_resources_data_.get() && | 587 if (locale_resources_data_.get() && |
608 locale_resources_data_->GetStringPiece( | 588 locale_resources_data_->GetStringPiece( |
609 static_cast<uint16_t>(resource_id), &data) && | 589 static_cast<uint16_t>(resource_id), &data) && |
610 !data.empty()) { | 590 !data.empty()) { |
611 return new base::RefCountedStaticMemory(data.data(), data.length()); | 591 return new base::RefCountedStaticMemory(data.data(), data.length()); |
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 } | 592 } |
620 } | 593 } |
621 // Release lock_scope and fall back to main data pack. | 594 // Release lock_scope and fall back to main data pack. |
622 return LoadDataResourceBytes(resource_id); | 595 return LoadDataResourceBytes(resource_id); |
623 } | 596 } |
624 | 597 |
625 const gfx::FontList& ResourceBundle::GetFontListWithDelta( | 598 const gfx::FontList& ResourceBundle::GetFontListWithDelta( |
626 int size_delta, | 599 int size_delta, |
627 gfx::Font::FontStyle style, | 600 gfx::Font::FontStyle style, |
628 gfx::Font::Weight weight) { | 601 gfx::Font::Weight weight) { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 // static | 923 // static |
951 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 924 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
952 size_t size, | 925 size_t size, |
953 SkBitmap* bitmap, | 926 SkBitmap* bitmap, |
954 bool* fell_back_to_1x) { | 927 bool* fell_back_to_1x) { |
955 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 928 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
956 return gfx::PNGCodec::Decode(buf, size, bitmap); | 929 return gfx::PNGCodec::Decode(buf, size, bitmap); |
957 } | 930 } |
958 | 931 |
959 } // namespace ui | 932 } // namespace ui |
OLD | NEW |