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