Chromium Code Reviews| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/data_pack.h" | 22 #include "ui/base/resource/data_pack.h" |
| 23 #include "ui/base/ui_base_paths.h" | 23 #include "ui/base/ui_base_paths.h" |
| 24 #include "ui/base/ui_base_switches.h" | 24 #include "ui/base/ui_base_switches.h" |
| 25 #include "ui/gfx/codec/jpeg_codec.h" | 25 #include "ui/gfx/codec/jpeg_codec.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/font.h" | |
| 28 #include "ui/gfx/image/image.h" | |
| 29 | 27 |
| 30 namespace ui { | 28 namespace ui { |
| 31 | 29 |
| 32 namespace { | 30 namespace { |
| 33 | 31 |
| 34 // Font sizes relative to base font. | 32 // Font sizes relative to base font. |
| 35 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) | 33 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) |
| 36 const int kSmallFontSizeDelta = -3; | 34 const int kSmallFontSizeDelta = -3; |
| 37 const int kMediumFontSizeDelta = 2; | 35 const int kMediumFontSizeDelta = 2; |
| 38 const int kLargeFontSizeDelta = 7; | 36 const int kLargeFontSizeDelta = 7; |
| 39 #else | 37 #else |
| 40 const int kSmallFontSizeDelta = -2; | 38 const int kSmallFontSizeDelta = -2; |
| 41 const int kMediumFontSizeDelta = 3; | 39 const int kMediumFontSizeDelta = 3; |
| 42 const int kLargeFontSizeDelta = 8; | 40 const int kLargeFontSizeDelta = 8; |
| 43 #endif | 41 #endif |
| 44 | 42 |
| 45 } // namespace | 43 } // namespace |
| 46 | 44 |
| 47 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; | 45 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; |
| 48 | 46 |
| 49 // static | 47 // static |
| 50 std::string ResourceBundle::InitSharedInstanceWithLocale( | 48 std::string ResourceBundle::InitSharedInstanceWithLocale( |
| 51 const std::string& pref_locale) { | 49 const std::string& pref_locale, Delegate* delegate) { |
| 52 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 50 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| 53 g_shared_instance_ = new ResourceBundle(); | 51 g_shared_instance_ = new ResourceBundle(delegate); |
| 54 | 52 |
| 55 g_shared_instance_->LoadCommonResources(); | 53 g_shared_instance_->LoadCommonResources(); |
| 56 return g_shared_instance_->LoadLocaleResources(pref_locale); | 54 return g_shared_instance_->LoadLocaleResources(pref_locale); |
| 57 } | 55 } |
| 58 | 56 |
| 59 // static | 57 // static |
| 60 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { | 58 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { |
| 61 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 59 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| 62 g_shared_instance_ = new ResourceBundle(); | 60 g_shared_instance_ = new ResourceBundle(NULL); |
| 63 | 61 |
| 64 g_shared_instance_->LoadTestResources(path); | 62 g_shared_instance_->LoadTestResources(path); |
| 65 } | 63 } |
| 66 | 64 |
| 67 // static | 65 // static |
| 68 void ResourceBundle::CleanupSharedInstance() { | 66 void ResourceBundle::CleanupSharedInstance() { |
| 69 if (g_shared_instance_) { | 67 if (g_shared_instance_) { |
| 70 delete g_shared_instance_; | 68 delete g_shared_instance_; |
| 71 g_shared_instance_ = NULL; | 69 g_shared_instance_ = NULL; |
| 72 } | 70 } |
| 73 } | 71 } |
| 74 | 72 |
| 75 // static | 73 // static |
| 76 bool ResourceBundle::HasSharedInstance() { | 74 bool ResourceBundle::HasSharedInstance() { |
| 77 return g_shared_instance_ != NULL; | 75 return g_shared_instance_ != NULL; |
| 78 } | 76 } |
| 79 | 77 |
| 80 // static | 78 // static |
| 81 ResourceBundle& ResourceBundle::GetSharedInstance() { | 79 ResourceBundle& ResourceBundle::GetSharedInstance() { |
| 82 // Must call InitSharedInstance before this function. | 80 // Must call InitSharedInstance before this function. |
| 83 CHECK(g_shared_instance_ != NULL); | 81 CHECK(g_shared_instance_ != NULL); |
| 84 return *g_shared_instance_; | 82 return *g_shared_instance_; |
| 85 } | 83 } |
| 86 | 84 |
| 87 // static | |
| 88 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 85 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
| 89 return !GetLocaleFilePath(locale).empty(); | 86 return !GetLocaleFilePath(locale).empty(); |
| 90 } | 87 } |
| 91 | 88 |
| 92 void ResourceBundle::AddDataPack(const FilePath& path) { | 89 void ResourceBundle::AddDataPack(const FilePath& path) { |
| 93 scoped_ptr<DataPack> data_pack(new DataPack()); | 90 scoped_ptr<DataPack> data_pack(new DataPack()); |
| 94 if (data_pack->Load(path)) { | 91 if (data_pack->Load(path)) { |
| 95 data_packs_.push_back(data_pack.release()); | 92 data_packs_.push_back(data_pack.release()); |
| 96 } else { | 93 } else { |
| 97 LOG(ERROR) << "Failed to load " << path.value() | 94 LOG(ERROR) << "Failed to load " << path.value() |
| 98 << "\nSome features may not be available."; | 95 << "\nSome features may not be available."; |
| 99 } | 96 } |
| 100 } | 97 } |
| 101 | 98 |
| 102 #if !defined(OS_MACOSX) | 99 #if !defined(OS_MACOSX) |
| 103 // static | |
| 104 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 100 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| 101 if (app_locale.empty()) | |
| 102 return FilePath(); | |
| 103 | |
| 105 FilePath locale_file_path; | 104 FilePath locale_file_path; |
| 105 | |
| 106 #if defined(OS_ANDROID) | 106 #if defined(OS_ANDROID) |
| 107 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); | 107 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); |
| 108 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); | 108 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); |
| 109 #else | 109 #else |
| 110 PathService::Get(ui::DIR_LOCALES, &locale_file_path); | 110 PathService::Get(ui::DIR_LOCALES, &locale_file_path); |
| 111 #endif | 111 #endif |
| 112 if (locale_file_path.empty()) | 112 |
| 113 return locale_file_path; | 113 if (!locale_file_path.empty()) |
| 114 if (app_locale.empty()) | 114 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
| 115 | |
| 116 if (delegate_ && | |
| 117 !delegate_->GetPathForLocalePack(app_locale, &locale_file_path)) { | |
| 115 return FilePath(); | 118 return FilePath(); |
| 116 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | 119 } |
| 120 | |
| 117 if (!file_util::PathExists(locale_file_path)) | 121 if (!file_util::PathExists(locale_file_path)) |
| 118 return FilePath(); | 122 return FilePath(); |
| 123 | |
| 119 return locale_file_path; | 124 return locale_file_path; |
| 120 } | 125 } |
| 121 #endif | 126 #endif |
| 122 | 127 |
| 123 std::string ResourceBundle::LoadLocaleResources( | 128 std::string ResourceBundle::LoadLocaleResources( |
| 124 const std::string& pref_locale) { | 129 const std::string& pref_locale) { |
| 125 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; | 130 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; |
| 126 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 131 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| 127 FilePath locale_file_path = GetOverriddenPakPath(); | 132 FilePath locale_file_path = GetOverriddenPakPath(); |
| 128 if (locale_file_path.empty()) { | 133 if (locale_file_path.empty()) { |
| 129 CommandLine *command_line = CommandLine::ForCurrentProcess(); | 134 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 130 if (command_line->HasSwitch(switches::kLocalePak)) { | 135 if (command_line->HasSwitch(switches::kLocalePak)) { |
| 131 locale_file_path = | 136 locale_file_path = |
| 132 command_line->GetSwitchValuePath(switches::kLocalePak); | 137 command_line->GetSwitchValuePath(switches::kLocalePak); |
| 133 } else { | 138 } else { |
| 134 locale_file_path = GetLocaleFilePath(app_locale); | 139 locale_file_path = GetLocaleFilePath(app_locale); |
| 135 } | 140 } |
| 136 } | 141 } |
| 137 | 142 |
| 138 if (locale_file_path.empty()) { | 143 if (locale_file_path.empty()) { |
| 139 // It's possible that there is no locale.pak. | 144 // It's possible that there is no locale.pak. |
| 140 NOTREACHED(); | |
| 141 return std::string(); | 145 return std::string(); |
| 142 } | 146 } |
| 143 | 147 |
| 144 scoped_ptr<DataPack> data_pack(new DataPack()); | 148 scoped_ptr<DataPack> data_pack(new DataPack()); |
| 145 if (!data_pack->Load(locale_file_path)) { | 149 if (!data_pack->Load(locale_file_path)) { |
| 146 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 150 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
| 147 logging::GetLastSystemErrorCode(), 16000); | 151 logging::GetLastSystemErrorCode(), 16000); |
| 148 NOTREACHED() << "failed to load locale.pak"; | 152 NOTREACHED() << "failed to load locale.pak"; |
| 149 return std::string(); | 153 return std::string(); |
| 150 } | 154 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 161 | 165 |
| 162 data_pack.reset(new DataPack()); | 166 data_pack.reset(new DataPack()); |
| 163 if (data_pack->Load(path)) | 167 if (data_pack->Load(path)) |
| 164 locale_resources_data_.reset(data_pack.release()); | 168 locale_resources_data_.reset(data_pack.release()); |
| 165 } | 169 } |
| 166 | 170 |
| 167 void ResourceBundle::UnloadLocaleResources() { | 171 void ResourceBundle::UnloadLocaleResources() { |
| 168 locale_resources_data_.reset(); | 172 locale_resources_data_.reset(); |
| 169 } | 173 } |
| 170 | 174 |
| 171 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 172 // Ensure that ReloadLocaleResources() doesn't drop the resources while | |
| 173 // we're using them. | |
| 174 base::AutoLock lock_scope(*locale_resources_data_lock_); | |
| 175 | |
| 176 // If for some reason we were unable to load the resources , return an empty | |
| 177 // string (better than crashing). | |
| 178 if (!locale_resources_data_.get()) { | |
| 179 LOG(WARNING) << "locale resources are not loaded"; | |
| 180 return string16(); | |
| 181 } | |
| 182 | |
| 183 base::StringPiece data; | |
| 184 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 185 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 186 // unittests). | |
| 187 data = GetRawDataResource(message_id); | |
| 188 if (data.empty()) { | |
| 189 NOTREACHED() << "unable to find resource: " << message_id; | |
| 190 return string16(); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 // Strings should not be loaded from a data pack that contains binary data. | |
| 195 ResourceHandle::TextEncodingType encoding = | |
| 196 locale_resources_data_->GetTextEncodingType(); | |
| 197 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | |
| 198 << "requested localized string from binary pack file"; | |
| 199 | |
| 200 // Data pack encodes strings as either UTF8 or UTF16. | |
| 201 string16 msg; | |
| 202 if (encoding == ResourceHandle::UTF16) { | |
| 203 msg = string16(reinterpret_cast<const char16*>(data.data()), | |
| 204 data.length() / 2); | |
| 205 } else if (encoding == ResourceHandle::UTF8) { | |
| 206 msg = UTF8ToUTF16(data); | |
| 207 } | |
| 208 return msg; | |
| 209 } | |
| 210 | |
| 211 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { | 175 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { |
| 212 overridden_pak_path_ = pak_path; | 176 overridden_pak_path_ = pak_path; |
| 213 } | 177 } |
| 214 | 178 |
| 215 const FilePath& ResourceBundle::GetOverriddenPakPath() { | 179 const FilePath& ResourceBundle::GetOverriddenPakPath() { |
| 216 return overridden_pak_path_; | 180 return overridden_pak_path_; |
| 217 } | 181 } |
| 218 | 182 |
| 219 std::string ResourceBundle::ReloadLocaleResources( | 183 std::string ResourceBundle::ReloadLocaleResources( |
| 220 const std::string& pref_locale) { | 184 const std::string& pref_locale) { |
| 221 base::AutoLock lock_scope(*locale_resources_data_lock_); | 185 base::AutoLock lock_scope(*locale_resources_data_lock_); |
| 222 UnloadLocaleResources(); | 186 UnloadLocaleResources(); |
| 223 return LoadLocaleResources(pref_locale); | 187 return LoadLocaleResources(pref_locale); |
| 224 } | 188 } |
| 225 | 189 |
| 226 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | 190 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { |
| 227 const SkBitmap* bitmap = GetImageNamed(resource_id).ToSkBitmap(); | 191 const SkBitmap* bitmap = GetImageNamed(resource_id).ToSkBitmap(); |
| 228 return const_cast<SkBitmap*>(bitmap); | 192 return const_cast<SkBitmap*>(bitmap); |
| 229 } | 193 } |
| 230 | 194 |
| 231 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { | 195 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
| 232 // Check to see if the image is already in the cache. | 196 // Check to see if the image is already in the cache. |
| 233 { | 197 { |
| 234 base::AutoLock lock_scope(*images_and_fonts_lock_); | 198 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 235 ImageMap::const_iterator found = images_.find(resource_id); | 199 ImageMap::const_iterator found = images_.find(resource_id); |
| 236 if (found != images_.end()) | 200 if (found != images_.end()) |
| 237 return *found->second; | 201 return *found->second; |
| 238 } | 202 } |
| 239 | 203 |
| 240 DCHECK(!data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; | 204 scoped_ptr<gfx::Image> image; |
| 241 ScopedVector<const SkBitmap> bitmaps; | 205 if (delegate_) |
| 242 for (size_t i = 0; i < data_packs_.size(); ++i) { | 206 image.reset(delegate_->GetImageNamed(resource_id).release()); |
| 243 SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); | |
| 244 if (bitmap) | |
| 245 bitmaps.push_back(bitmap); | |
| 246 } | |
| 247 | 207 |
| 248 if (bitmaps.empty()) { | 208 if (!image.get()) { |
| 249 LOG(WARNING) << "Unable to load image with id " << resource_id; | 209 DCHECK(!data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; |
|
tony
2012/05/01 15:56:48
Nit: This could also mean that the delegate blocke
Marshall
2012/05/01 17:29:41
Good question. I think these checks can be useful.
| |
| 250 NOTREACHED(); // Want to assert in debug mode. | 210 ScopedVector<const SkBitmap> bitmaps; |
| 251 // The load failed to retrieve the image; show a debugging red square. | 211 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 252 return *GetEmptyImage(); | 212 SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); |
| 213 if (bitmap) | |
| 214 bitmaps.push_back(bitmap); | |
| 215 } | |
| 216 | |
| 217 if (bitmaps.empty()) { | |
| 218 LOG(WARNING) << "Unable to load image with id " << resource_id; | |
| 219 NOTREACHED(); // Want to assert in debug mode. | |
| 220 // The load failed to retrieve the image; show a debugging red square. | |
| 221 return *GetEmptyImage(); | |
| 222 } | |
| 223 | |
| 224 std::vector<const SkBitmap*> tmp_bitmaps; | |
| 225 bitmaps.release(&tmp_bitmaps); | |
| 226 | |
| 227 // Takes ownership of bitmaps. | |
| 228 image.reset(new gfx::Image(tmp_bitmaps)); | |
| 253 } | 229 } |
| 254 | 230 |
| 255 // The load was successful, so cache the image. | 231 // The load was successful, so cache the image. |
| 256 base::AutoLock lock_scope(*images_and_fonts_lock_); | 232 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 257 | 233 |
| 258 // Another thread raced the load and has already cached the image. | 234 // Another thread raced the load and has already cached the image. |
| 259 if (images_.count(resource_id)) | 235 if (images_.count(resource_id)) { |
| 260 return *images_[resource_id]; | 236 return *images_[resource_id]; |
| 237 } | |
| 261 | 238 |
| 262 std::vector<const SkBitmap*> tmp_bitmaps; | 239 images_[resource_id] = image.release(); |
| 263 bitmaps.release(&tmp_bitmaps); | 240 return *images_[resource_id]; |
| 264 // Takes ownership of bitmaps. | |
| 265 gfx::Image* image = new gfx::Image(tmp_bitmaps); | |
| 266 images_[resource_id] = image; | |
| 267 return *image; | |
| 268 } | 241 } |
| 269 | 242 |
| 270 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 243 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
| 271 return GetNativeImageNamed(resource_id, RTL_DISABLED); | 244 return GetNativeImageNamed(resource_id, RTL_DISABLED); |
| 272 } | 245 } |
| 273 | 246 |
| 274 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( | 247 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( |
| 275 int resource_id) const { | 248 int resource_id) const { |
| 276 for (size_t i = 0; i < data_packs_.size(); ++i) { | 249 scoped_refptr<base::RefCountedStaticMemory> bytes; |
| 277 base::RefCountedStaticMemory* bytes = | 250 if (delegate_) |
| 278 data_packs_[i]->GetStaticMemory(resource_id); | 251 bytes = delegate_->LoadDataResourceBytes(resource_id); |
| 279 if (bytes) | 252 |
| 280 return bytes; | 253 if (!bytes) { |
| 254 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) | |
| 255 bytes = data_packs_[i]->GetStaticMemory(resource_id); | |
| 281 } | 256 } |
| 282 | 257 |
| 283 return NULL; | 258 return bytes; |
| 284 } | 259 } |
| 285 | 260 |
| 286 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 261 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
| 262 base::StringPiece data; | |
| 263 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) | |
| 264 return data; | |
| 265 | |
| 287 DCHECK(locale_resources_data_.get()); | 266 DCHECK(locale_resources_data_.get()); |
| 288 base::StringPiece data; | |
| 289 if (locale_resources_data_->GetStringPiece(resource_id, &data)) | 267 if (locale_resources_data_->GetStringPiece(resource_id, &data)) |
| 290 return data; | 268 return data; |
| 291 | 269 |
| 292 for (size_t i = 0; i < data_packs_.size(); ++i) { | 270 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 293 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 271 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 294 return data; | 272 return data; |
| 295 } | 273 } |
| 296 | 274 |
| 297 return base::StringPiece(); | 275 return base::StringPiece(); |
| 298 } | 276 } |
| 299 | 277 |
| 278 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 279 string16 string; | |
| 280 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | |
| 281 return string; | |
| 282 | |
| 283 // Ensure that ReloadLocaleResources() doesn't drop the resources while | |
| 284 // we're using them. | |
| 285 base::AutoLock lock_scope(*locale_resources_data_lock_); | |
| 286 | |
| 287 // If for some reason we were unable to load the resources , return an empty | |
| 288 // string (better than crashing). | |
| 289 if (!locale_resources_data_.get()) { | |
| 290 LOG(WARNING) << "locale resources are not loaded"; | |
| 291 return string16(); | |
| 292 } | |
| 293 | |
| 294 base::StringPiece data; | |
| 295 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 296 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 297 // unittests). | |
| 298 data = GetRawDataResource(message_id); | |
| 299 if (data.empty()) { | |
| 300 NOTREACHED() << "unable to find resource: " << message_id; | |
| 301 return string16(); | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 // Strings should not be loaded from a data pack that contains binary data. | |
| 306 ResourceHandle::TextEncodingType encoding = | |
| 307 locale_resources_data_->GetTextEncodingType(); | |
| 308 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | |
| 309 << "requested localized string from binary pack file"; | |
| 310 | |
| 311 // Data pack encodes strings as either UTF8 or UTF16. | |
| 312 string16 msg; | |
| 313 if (encoding == ResourceHandle::UTF16) { | |
| 314 msg = string16(reinterpret_cast<const char16*>(data.data()), | |
| 315 data.length() / 2); | |
| 316 } else if (encoding == ResourceHandle::UTF8) { | |
| 317 msg = UTF8ToUTF16(data); | |
| 318 } | |
| 319 return msg; | |
| 320 } | |
| 321 | |
| 300 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 322 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 301 { | 323 { |
| 302 base::AutoLock lock_scope(*images_and_fonts_lock_); | 324 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 303 LoadFontsIfNecessary(); | 325 LoadFontsIfNecessary(); |
| 304 } | 326 } |
| 305 switch (style) { | 327 switch (style) { |
| 306 case BoldFont: | 328 case BoldFont: |
| 307 return *bold_font_; | 329 return *bold_font_; |
| 308 case SmallFont: | 330 case SmallFont: |
| 309 return *small_font_; | 331 return *small_font_; |
| 310 case MediumFont: | 332 case MediumFont: |
| 311 return *medium_font_; | 333 return *medium_font_; |
| 312 case MediumBoldFont: | 334 case MediumBoldFont: |
| 313 return *medium_bold_font_; | 335 return *medium_bold_font_; |
| 314 case LargeFont: | 336 case LargeFont: |
| 315 return *large_font_; | 337 return *large_font_; |
| 316 case LargeBoldFont: | 338 case LargeBoldFont: |
| 317 return *large_bold_font_; | 339 return *large_bold_font_; |
| 318 default: | 340 default: |
| 319 return *base_font_; | 341 return *base_font_; |
| 320 } | 342 } |
| 321 } | 343 } |
| 322 | 344 |
| 323 void ResourceBundle::ReloadFonts() { | 345 void ResourceBundle::ReloadFonts() { |
| 324 base::AutoLock lock_scope(*images_and_fonts_lock_); | 346 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 325 base_font_.reset(); | 347 base_font_.reset(); |
| 326 LoadFontsIfNecessary(); | 348 LoadFontsIfNecessary(); |
| 327 } | 349 } |
| 328 | 350 |
| 329 ResourceBundle::ResourceBundle() | 351 ResourceBundle::ResourceBundle(Delegate* delegate) |
| 330 : images_and_fonts_lock_(new base::Lock), | 352 : delegate_(delegate), |
| 353 images_and_fonts_lock_(new base::Lock), | |
| 331 locale_resources_data_lock_(new base::Lock) { | 354 locale_resources_data_lock_(new base::Lock) { |
| 332 } | 355 } |
| 333 | 356 |
| 334 ResourceBundle::~ResourceBundle() { | 357 ResourceBundle::~ResourceBundle() { |
| 335 FreeImages(); | 358 FreeImages(); |
| 336 UnloadLocaleResources(); | 359 UnloadLocaleResources(); |
| 337 } | 360 } |
| 338 | 361 |
| 339 void ResourceBundle::FreeImages() { | 362 void ResourceBundle::FreeImages() { |
| 340 STLDeleteContainerPairSecondPointers(images_.begin(), | 363 STLDeleteContainerPairSecondPointers(images_.begin(), |
| 341 images_.end()); | 364 images_.end()); |
| 342 images_.clear(); | 365 images_.clear(); |
| 343 } | 366 } |
| 344 | 367 |
| 368 void ResourceBundle::AddCommonDataPack(const std::string& pack_name, | |
| 369 const FilePath& path) { | |
| 370 FilePath pack_path = path; | |
| 371 if (!delegate_ || delegate_->GetPathForResourcePack(pack_name, &pack_path)) | |
| 372 AddDataPack(pack_path); | |
| 373 } | |
| 374 | |
| 345 void ResourceBundle::LoadFontsIfNecessary() { | 375 void ResourceBundle::LoadFontsIfNecessary() { |
| 346 images_and_fonts_lock_->AssertAcquired(); | 376 images_and_fonts_lock_->AssertAcquired(); |
| 347 if (!base_font_.get()) { | 377 if (!base_font_.get()) { |
| 348 base_font_.reset(new gfx::Font()); | 378 if (delegate_) |
| 379 base_font_.reset(delegate_->GetFont(BaseFont).release()); | |
| 380 if (!base_font_.get()) | |
| 381 base_font_.reset(new gfx::Font()); | |
| 349 | 382 |
| 350 bold_font_.reset(new gfx::Font()); | 383 if (delegate_) |
| 351 *bold_font_ = | 384 bold_font_.reset(delegate_->GetFont(BoldFont).release()); |
| 352 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | 385 if (!bold_font_.get()) { |
| 386 bold_font_.reset(new gfx::Font()); | |
| 387 *bold_font_ = | |
| 388 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | |
| 389 } | |
| 353 | 390 |
| 354 small_font_.reset(new gfx::Font()); | 391 if (delegate_) |
| 355 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | 392 small_font_.reset(delegate_->GetFont(SmallFont).release()); |
| 393 if (!small_font_.get()) { | |
| 394 small_font_.reset(new gfx::Font()); | |
| 395 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | |
| 396 } | |
| 356 | 397 |
| 357 medium_font_.reset(new gfx::Font()); | 398 if (delegate_) |
| 358 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); | 399 medium_font_.reset(delegate_->GetFont(MediumFont).release()); |
| 400 if (!medium_font_.get()) { | |
| 401 medium_font_.reset(new gfx::Font()); | |
| 402 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); | |
| 403 } | |
| 359 | 404 |
| 360 medium_bold_font_.reset(new gfx::Font()); | 405 if (delegate_) |
| 361 *medium_bold_font_ = | 406 medium_bold_font_.reset(delegate_->GetFont(MediumBoldFont).release()); |
| 362 base_font_->DeriveFont(kMediumFontSizeDelta, | 407 if (!medium_bold_font_.get()) { |
| 363 base_font_->GetStyle() | gfx::Font::BOLD); | 408 medium_bold_font_.reset(new gfx::Font()); |
| 409 *medium_bold_font_ = | |
| 410 base_font_->DeriveFont(kMediumFontSizeDelta, | |
| 411 base_font_->GetStyle() | gfx::Font::BOLD); | |
| 412 } | |
| 364 | 413 |
| 365 large_font_.reset(new gfx::Font()); | 414 if (delegate_) |
| 366 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); | 415 large_font_.reset(delegate_->GetFont(LargeFont).release()); |
| 416 if (!large_font_.get()) { | |
| 417 large_font_.reset(new gfx::Font()); | |
| 418 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); | |
| 419 } | |
| 367 | 420 |
| 368 large_bold_font_.reset(new gfx::Font()); | 421 if (delegate_) |
| 369 *large_bold_font_ = | 422 large_bold_font_.reset(delegate_->GetFont(LargeBoldFont).release()); |
| 370 base_font_->DeriveFont(kLargeFontSizeDelta, | 423 if (!large_bold_font_.get()) { |
| 371 base_font_->GetStyle() | gfx::Font::BOLD); | 424 large_bold_font_.reset(new gfx::Font()); |
| 425 *large_bold_font_ = | |
| 426 base_font_->DeriveFont(kLargeFontSizeDelta, | |
| 427 base_font_->GetStyle() | gfx::Font::BOLD); | |
| 428 } | |
| 372 } | 429 } |
| 373 } | 430 } |
| 374 | 431 |
| 375 SkBitmap* ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, | 432 SkBitmap* ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, |
| 376 int resource_id) { | 433 int resource_id) { |
| 377 scoped_refptr<RefCountedMemory> memory( | 434 scoped_refptr<RefCountedMemory> memory( |
| 378 data_handle.GetStaticMemory(resource_id)); | 435 data_handle.GetStaticMemory(resource_id)); |
| 379 if (!memory) | 436 if (!memory) |
| 380 return NULL; | 437 return NULL; |
| 381 | 438 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 403 SkBitmap* bitmap = new SkBitmap(); | 460 SkBitmap* bitmap = new SkBitmap(); |
| 404 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 461 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| 405 bitmap->allocPixels(); | 462 bitmap->allocPixels(); |
| 406 bitmap->eraseARGB(255, 255, 0, 0); | 463 bitmap->eraseARGB(255, 255, 0, 0); |
| 407 empty_image = new gfx::Image(bitmap); | 464 empty_image = new gfx::Image(bitmap); |
| 408 } | 465 } |
| 409 return empty_image; | 466 return empty_image; |
| 410 } | 467 } |
| 411 | 468 |
| 412 } // namespace ui | 469 } // namespace ui |
| OLD | NEW |