| 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); | 169 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
| 170 g_shared_instance_->InitDefaultFontList(); | 170 g_shared_instance_->InitDefaultFontList(); |
| 171 return result; | 171 return result; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // static | 174 // static |
| 175 void ResourceBundle::InitSharedInstanceWithPakFileRegion( | 175 void ResourceBundle::InitSharedInstanceWithPakFileRegion( |
| 176 base::File pak_file, | 176 base::File pak_file, |
| 177 const base::MemoryMappedFile::Region& region) { | 177 const base::MemoryMappedFile::Region& region) { |
| 178 InitSharedInstance(NULL); | 178 InitSharedInstance(NULL); |
| 179 scoped_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P)); | 179 std::unique_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P)); |
| 180 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) { | 180 if (!data_pack->LoadFromFileRegion(std::move(pak_file), region)) { |
| 181 NOTREACHED() << "failed to load pak file"; | 181 NOTREACHED() << "failed to load pak file"; |
| 182 return; | 182 return; |
| 183 } | 183 } |
| 184 g_shared_instance_->locale_resources_data_.reset(data_pack.release()); | 184 g_shared_instance_->locale_resources_data_.reset(data_pack.release()); |
| 185 g_shared_instance_->InitDefaultFontList(); | 185 g_shared_instance_->InitDefaultFontList(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 // static | 188 // static |
| 189 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) { | 189 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 ScaleFactor scale_factor) { | 245 ScaleFactor scale_factor) { |
| 246 AddDataPackFromFileRegion(std::move(file), | 246 AddDataPackFromFileRegion(std::move(file), |
| 247 base::MemoryMappedFile::Region::kWholeFile, | 247 base::MemoryMappedFile::Region::kWholeFile, |
| 248 scale_factor); | 248 scale_factor); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void ResourceBundle::AddDataPackFromFileRegion( | 251 void ResourceBundle::AddDataPackFromFileRegion( |
| 252 base::File file, | 252 base::File file, |
| 253 const base::MemoryMappedFile::Region& region, | 253 const base::MemoryMappedFile::Region& region, |
| 254 ScaleFactor scale_factor) { | 254 ScaleFactor scale_factor) { |
| 255 scoped_ptr<DataPack> data_pack( | 255 std::unique_ptr<DataPack> data_pack(new DataPack(scale_factor)); |
| 256 new DataPack(scale_factor)); | |
| 257 if (data_pack->LoadFromFileRegion(std::move(file), region)) { | 256 if (data_pack->LoadFromFileRegion(std::move(file), region)) { |
| 258 AddDataPack(data_pack.release()); | 257 AddDataPack(data_pack.release()); |
| 259 } else { | 258 } else { |
| 260 LOG(ERROR) << "Failed to load data pack from file." | 259 LOG(ERROR) << "Failed to load data pack from file." |
| 261 << "\nSome features may not be available."; | 260 << "\nSome features may not be available."; |
| 262 } | 261 } |
| 263 } | 262 } |
| 264 | 263 |
| 265 #if !defined(OS_MACOSX) | 264 #if !defined(OS_MACOSX) |
| 266 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, | 265 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 base::FilePath locale_file_path = GetOverriddenPakPath(); | 300 base::FilePath locale_file_path = GetOverriddenPakPath(); |
| 302 if (locale_file_path.empty()) | 301 if (locale_file_path.empty()) |
| 303 locale_file_path = GetLocaleFilePath(app_locale, true); | 302 locale_file_path = GetLocaleFilePath(app_locale, true); |
| 304 | 303 |
| 305 if (locale_file_path.empty()) { | 304 if (locale_file_path.empty()) { |
| 306 // It's possible that there is no locale.pak. | 305 // It's possible that there is no locale.pak. |
| 307 LOG(WARNING) << "locale_file_path.empty() for locale " << app_locale; | 306 LOG(WARNING) << "locale_file_path.empty() for locale " << app_locale; |
| 308 return std::string(); | 307 return std::string(); |
| 309 } | 308 } |
| 310 | 309 |
| 311 scoped_ptr<DataPack> data_pack( | 310 std::unique_ptr<DataPack> data_pack(new DataPack(SCALE_FACTOR_100P)); |
| 312 new DataPack(SCALE_FACTOR_100P)); | |
| 313 if (!data_pack->LoadFromPath(locale_file_path)) { | 311 if (!data_pack->LoadFromPath(locale_file_path)) { |
| 314 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 312 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
| 315 logging::GetLastSystemErrorCode(), 16000); | 313 logging::GetLastSystemErrorCode(), 16000); |
| 316 LOG(ERROR) << "failed to load locale.pak"; | 314 LOG(ERROR) << "failed to load locale.pak"; |
| 317 NOTREACHED(); | 315 NOTREACHED(); |
| 318 return std::string(); | 316 return std::string(); |
| 319 } | 317 } |
| 320 | 318 |
| 321 locale_resources_data_.reset(data_pack.release()); | 319 locale_resources_data_.reset(data_pack.release()); |
| 322 return app_locale; | 320 return app_locale; |
| 323 } | 321 } |
| 324 #endif // defined(OS_ANDROID) | 322 #endif // defined(OS_ANDROID) |
| 325 | 323 |
| 326 void ResourceBundle::LoadTestResources(const base::FilePath& path, | 324 void ResourceBundle::LoadTestResources(const base::FilePath& path, |
| 327 const base::FilePath& locale_path) { | 325 const base::FilePath& locale_path) { |
| 328 DCHECK(!ui::GetSupportedScaleFactors().empty()); | 326 DCHECK(!ui::GetSupportedScaleFactors().empty()); |
| 329 const ScaleFactor scale_factor(ui::GetSupportedScaleFactors()[0]); | 327 const ScaleFactor scale_factor(ui::GetSupportedScaleFactors()[0]); |
| 330 // Use the given resource pak for both common and localized resources. | 328 // Use the given resource pak for both common and localized resources. |
| 331 scoped_ptr<DataPack> data_pack(new DataPack(scale_factor)); | 329 std::unique_ptr<DataPack> data_pack(new DataPack(scale_factor)); |
| 332 if (!path.empty() && data_pack->LoadFromPath(path)) | 330 if (!path.empty() && data_pack->LoadFromPath(path)) |
| 333 AddDataPack(data_pack.release()); | 331 AddDataPack(data_pack.release()); |
| 334 | 332 |
| 335 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); | 333 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
| 336 if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) { | 334 if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) { |
| 337 locale_resources_data_.reset(data_pack.release()); | 335 locale_resources_data_.reset(data_pack.release()); |
| 338 } else { | 336 } else { |
| 339 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE)); | 337 locale_resources_data_.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
| 340 } | 338 } |
| 341 } | 339 } |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 DCHECK(!path.empty()); | 725 DCHECK(!path.empty()); |
| 728 | 726 |
| 729 base::FilePath pack_path = path; | 727 base::FilePath pack_path = path; |
| 730 if (delegate_) | 728 if (delegate_) |
| 731 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | 729 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); |
| 732 | 730 |
| 733 // Don't try to load empty values or values that are not absolute paths. | 731 // Don't try to load empty values or values that are not absolute paths. |
| 734 if (pack_path.empty() || !pack_path.IsAbsolute()) | 732 if (pack_path.empty() || !pack_path.IsAbsolute()) |
| 735 return; | 733 return; |
| 736 | 734 |
| 737 scoped_ptr<DataPack> data_pack(new DataPack(scale_factor)); | 735 std::unique_ptr<DataPack> data_pack(new DataPack(scale_factor)); |
| 738 data_pack->set_has_only_material_design_assets(has_only_material_assets); | 736 data_pack->set_has_only_material_design_assets(has_only_material_assets); |
| 739 if (data_pack->LoadFromPath(pack_path)) { | 737 if (data_pack->LoadFromPath(pack_path)) { |
| 740 AddDataPack(data_pack.release()); | 738 AddDataPack(data_pack.release()); |
| 741 } else if (!optional) { | 739 } else if (!optional) { |
| 742 LOG(ERROR) << "Failed to load " << pack_path.value() | 740 LOG(ERROR) << "Failed to load " << pack_path.value() |
| 743 << "\nSome features may not be available."; | 741 << "\nSome features may not be available."; |
| 744 } | 742 } |
| 745 } | 743 } |
| 746 | 744 |
| 747 void ResourceBundle::AddDataPack(DataPack* data_pack) { | 745 void ResourceBundle::AddDataPack(DataPack* data_pack) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id))); | 778 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id))); |
| 781 if (!memory.get()) | 779 if (!memory.get()) |
| 782 return false; | 780 return false; |
| 783 | 781 |
| 784 if (DecodePNG(memory->front(), memory->size(), bitmap, fell_back_to_1x)) | 782 if (DecodePNG(memory->front(), memory->size(), bitmap, fell_back_to_1x)) |
| 785 return true; | 783 return true; |
| 786 | 784 |
| 787 #if !defined(OS_IOS) | 785 #if !defined(OS_IOS) |
| 788 // iOS does not compile or use the JPEG codec. On other platforms, | 786 // iOS does not compile or use the JPEG codec. On other platforms, |
| 789 // 99% of our assets are PNGs, however fallback to JPEG. | 787 // 99% of our assets are PNGs, however fallback to JPEG. |
| 790 scoped_ptr<SkBitmap> jpeg_bitmap( | 788 std::unique_ptr<SkBitmap> jpeg_bitmap( |
| 791 gfx::JPEGCodec::Decode(memory->front(), memory->size())); | 789 gfx::JPEGCodec::Decode(memory->front(), memory->size())); |
| 792 if (jpeg_bitmap.get()) { | 790 if (jpeg_bitmap.get()) { |
| 793 bitmap->swap(*jpeg_bitmap.get()); | 791 bitmap->swap(*jpeg_bitmap.get()); |
| 794 *fell_back_to_1x = false; | 792 *fell_back_to_1x = false; |
| 795 return true; | 793 return true; |
| 796 } | 794 } |
| 797 #endif | 795 #endif |
| 798 | 796 |
| 799 NOTREACHED() << "Unable to decode theme image resource " << resource_id; | 797 NOTREACHED() << "Unable to decode theme image resource " << resource_id; |
| 800 return false; | 798 return false; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 // static | 869 // static |
| 872 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 870 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 873 size_t size, | 871 size_t size, |
| 874 SkBitmap* bitmap, | 872 SkBitmap* bitmap, |
| 875 bool* fell_back_to_1x) { | 873 bool* fell_back_to_1x) { |
| 876 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 874 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 877 return gfx::PNGCodec::Decode(buf, size, bitmap); | 875 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 878 } | 876 } |
| 879 | 877 |
| 880 } // namespace ui | 878 } // namespace ui |
| OLD | NEW |