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, float scale_factor) { | 89 void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { |
| 90 // Do not pass an empty |path| value to this method. If the absolute path is | |
| 91 // unknown pass just the pack file name. | |
| 92 DCHECK(!path.empty()); | |
|
Robert Sesek
2012/05/03 19:53:57
Good! Callers were doing this and it was causing l
tony
2012/05/03 20:35:42
Seems like a bug that they're trying to load a pak
| |
| 93 | |
| 94 FilePath pack_path = path; | |
| 95 if (delegate_) | |
| 96 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | |
| 97 | |
| 98 // Don't try to load empty values or values that are not absolute paths. | |
| 99 if (pack_path.empty() || !pack_path.IsAbsolute()) | |
| 100 return; | |
| 101 | |
| 93 scoped_ptr<DataPack> data_pack( | 102 scoped_ptr<DataPack> data_pack( |
| 94 new DataPack(ResourceHandle::kScaleFactor100x)); | 103 new DataPack(ResourceHandle::kScaleFactor100x)); |
| 95 if (data_pack->Load(path)) { | 104 if (data_pack->Load(pack_path)) { |
| 96 data_packs_.push_back(data_pack.release()); | 105 data_packs_.push_back(data_pack.release()); |
| 97 } else { | 106 } else { |
| 98 LOG(ERROR) << "Failed to load " << path.value() | 107 LOG(ERROR) << "Failed to load " << pack_path.value() |
| 99 << "\nSome features may not be available."; | 108 << "\nSome features may not be available."; |
| 100 } | 109 } |
| 101 } | 110 } |
| 102 | 111 |
| 103 #if !defined(OS_MACOSX) | 112 #if !defined(OS_MACOSX) |
| 104 // static | |
| 105 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 113 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| 114 if (app_locale.empty()) | |
| 115 return FilePath(); | |
| 116 | |
| 106 FilePath locale_file_path; | 117 FilePath locale_file_path; |
| 118 | |
| 107 #if defined(OS_ANDROID) | 119 #if defined(OS_ANDROID) |
| 108 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); | 120 PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); |
| 109 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); | 121 locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); |
| 110 #else | 122 #else |
| 111 PathService::Get(ui::DIR_LOCALES, &locale_file_path); | 123 PathService::Get(ui::DIR_LOCALES, &locale_file_path); |
| 112 #endif | 124 #endif |
| 113 if (locale_file_path.empty()) | 125 |
| 114 return locale_file_path; | 126 if (!locale_file_path.empty()) |
| 115 if (app_locale.empty()) | 127 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
| 128 | |
| 129 if (delegate_) { | |
| 130 locale_file_path = | |
| 131 delegate_->GetPathForLocalePack(locale_file_path, app_locale); | |
| 132 } | |
| 133 | |
| 134 // Don't try to load empty values or values that are not absolute paths. | |
| 135 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) | |
| 116 return FilePath(); | 136 return FilePath(); |
| 117 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | 137 |
| 118 if (!file_util::PathExists(locale_file_path)) | 138 if (!file_util::PathExists(locale_file_path)) |
| 119 return FilePath(); | 139 return FilePath(); |
| 140 | |
| 120 return locale_file_path; | 141 return locale_file_path; |
| 121 } | 142 } |
| 122 #endif | 143 #endif |
| 123 | 144 |
| 124 std::string ResourceBundle::LoadLocaleResources( | 145 std::string ResourceBundle::LoadLocaleResources( |
| 125 const std::string& pref_locale) { | 146 const std::string& pref_locale) { |
| 126 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; | 147 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; |
| 127 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 148 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| 128 FilePath locale_file_path = GetOverriddenPakPath(); | 149 FilePath locale_file_path = GetOverriddenPakPath(); |
| 129 if (locale_file_path.empty()) { | 150 if (locale_file_path.empty()) { |
| 130 CommandLine *command_line = CommandLine::ForCurrentProcess(); | 151 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 131 if (command_line->HasSwitch(switches::kLocalePak)) { | 152 if (command_line->HasSwitch(switches::kLocalePak)) { |
| 132 locale_file_path = | 153 locale_file_path = |
| 133 command_line->GetSwitchValuePath(switches::kLocalePak); | 154 command_line->GetSwitchValuePath(switches::kLocalePak); |
| 134 } else { | 155 } else { |
| 135 locale_file_path = GetLocaleFilePath(app_locale); | 156 locale_file_path = GetLocaleFilePath(app_locale); |
| 136 } | 157 } |
| 137 } | 158 } |
| 138 | 159 |
| 139 if (locale_file_path.empty()) { | 160 if (locale_file_path.empty()) { |
| 140 // It's possible that there is no locale.pak. | 161 // It's possible that there is no locale.pak. |
| 141 NOTREACHED(); | |
| 142 return std::string(); | 162 return std::string(); |
| 143 } | 163 } |
| 144 | 164 |
| 145 scoped_ptr<DataPack> data_pack( | 165 scoped_ptr<DataPack> data_pack( |
| 146 new DataPack(ResourceHandle::kScaleFactor100x)); | 166 new DataPack(ResourceHandle::kScaleFactor100x)); |
| 147 if (!data_pack->Load(locale_file_path)) { | 167 if (!data_pack->Load(locale_file_path)) { |
| 148 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 168 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
| 149 logging::GetLastSystemErrorCode(), 16000); | 169 logging::GetLastSystemErrorCode(), 16000); |
| 150 NOTREACHED() << "failed to load locale.pak"; | 170 NOTREACHED() << "failed to load locale.pak"; |
| 151 return std::string(); | 171 return std::string(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 164 | 184 |
| 165 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); | 185 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); |
| 166 if (data_pack->Load(path)) | 186 if (data_pack->Load(path)) |
| 167 locale_resources_data_.reset(data_pack.release()); | 187 locale_resources_data_.reset(data_pack.release()); |
| 168 } | 188 } |
| 169 | 189 |
| 170 void ResourceBundle::UnloadLocaleResources() { | 190 void ResourceBundle::UnloadLocaleResources() { |
| 171 locale_resources_data_.reset(); | 191 locale_resources_data_.reset(); |
| 172 } | 192 } |
| 173 | 193 |
| 174 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 175 // Ensure that ReloadLocaleResources() doesn't drop the resources while | |
| 176 // we're using them. | |
| 177 base::AutoLock lock_scope(*locale_resources_data_lock_); | |
| 178 | |
| 179 // If for some reason we were unable to load the resources , return an empty | |
| 180 // string (better than crashing). | |
| 181 if (!locale_resources_data_.get()) { | |
| 182 LOG(WARNING) << "locale resources are not loaded"; | |
| 183 return string16(); | |
| 184 } | |
| 185 | |
| 186 base::StringPiece data; | |
| 187 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 188 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 189 // unittests). | |
| 190 data = GetRawDataResource(message_id); | |
| 191 if (data.empty()) { | |
| 192 NOTREACHED() << "unable to find resource: " << message_id; | |
| 193 return string16(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 // Strings should not be loaded from a data pack that contains binary data. | |
| 198 ResourceHandle::TextEncodingType encoding = | |
| 199 locale_resources_data_->GetTextEncodingType(); | |
| 200 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | |
| 201 << "requested localized string from binary pack file"; | |
| 202 | |
| 203 // Data pack encodes strings as either UTF8 or UTF16. | |
| 204 string16 msg; | |
| 205 if (encoding == ResourceHandle::UTF16) { | |
| 206 msg = string16(reinterpret_cast<const char16*>(data.data()), | |
| 207 data.length() / 2); | |
| 208 } else if (encoding == ResourceHandle::UTF8) { | |
| 209 msg = UTF8ToUTF16(data); | |
| 210 } | |
| 211 return msg; | |
| 212 } | |
| 213 | |
| 214 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { | 194 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { |
| 215 overridden_pak_path_ = pak_path; | 195 overridden_pak_path_ = pak_path; |
| 216 } | 196 } |
| 217 | 197 |
| 218 const FilePath& ResourceBundle::GetOverriddenPakPath() { | 198 const FilePath& ResourceBundle::GetOverriddenPakPath() { |
| 219 return overridden_pak_path_; | 199 return overridden_pak_path_; |
| 220 } | 200 } |
| 221 | 201 |
| 222 std::string ResourceBundle::ReloadLocaleResources( | 202 std::string ResourceBundle::ReloadLocaleResources( |
| 223 const std::string& pref_locale) { | 203 const std::string& pref_locale) { |
| 224 base::AutoLock lock_scope(*locale_resources_data_lock_); | 204 base::AutoLock lock_scope(*locale_resources_data_lock_); |
| 225 UnloadLocaleResources(); | 205 UnloadLocaleResources(); |
| 226 return LoadLocaleResources(pref_locale); | 206 return LoadLocaleResources(pref_locale); |
| 227 } | 207 } |
| 228 | 208 |
| 229 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | 209 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { |
| 230 const SkBitmap* bitmap = GetImageNamed(resource_id).ToSkBitmap(); | 210 const SkBitmap* bitmap = GetImageNamed(resource_id).ToSkBitmap(); |
| 231 return const_cast<SkBitmap*>(bitmap); | 211 return const_cast<SkBitmap*>(bitmap); |
| 232 } | 212 } |
| 233 | 213 |
| 234 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { | 214 const gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
| 235 // Check to see if the image is already in the cache. | 215 // Check to see if the image is already in the cache. |
| 236 { | 216 { |
| 237 base::AutoLock lock_scope(*images_and_fonts_lock_); | 217 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 238 ImageMap::const_iterator found = images_.find(resource_id); | 218 ImageMap::const_iterator found = images_.find(resource_id); |
| 239 if (found != images_.end()) | 219 if (found != images_.end()) |
| 240 return *found->second; | 220 return found->second; |
| 241 } | 221 } |
| 242 | 222 |
| 243 DCHECK(!data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; | 223 gfx::Image image; |
| 244 ScopedVector<const SkBitmap> bitmaps; | 224 if (delegate_) |
| 245 for (size_t i = 0; i < data_packs_.size(); ++i) { | 225 image = delegate_->GetImageNamed(resource_id); |
| 246 SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); | |
| 247 if (bitmap) | |
| 248 bitmaps.push_back(bitmap); | |
| 249 } | |
| 250 | 226 |
| 251 if (bitmaps.empty()) { | 227 if (image.IsEmpty()) { |
| 252 LOG(WARNING) << "Unable to load image with id " << resource_id; | 228 DCHECK(!delegate_ && !data_packs_.empty()) << |
| 253 NOTREACHED(); // Want to assert in debug mode. | 229 "Missing call to SetResourcesDataDLL?"; |
| 254 // The load failed to retrieve the image; show a debugging red square. | 230 ScopedVector<const SkBitmap> bitmaps; |
| 255 return *GetEmptyImage(); | 231 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 232 SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); | |
| 233 if (bitmap) | |
| 234 bitmaps.push_back(bitmap); | |
| 235 } | |
| 236 | |
| 237 if (bitmaps.empty()) { | |
| 238 LOG(WARNING) << "Unable to load image with id " << resource_id; | |
| 239 NOTREACHED(); // Want to assert in debug mode. | |
| 240 // The load failed to retrieve the image; show a debugging red square. | |
| 241 return GetEmptyImage(); | |
| 242 } | |
| 243 | |
| 244 std::vector<const SkBitmap*> tmp_bitmaps; | |
| 245 bitmaps.release(&tmp_bitmaps); | |
| 246 | |
| 247 // Takes ownership of bitmaps. | |
| 248 image = gfx::Image(tmp_bitmaps); | |
| 256 } | 249 } |
| 257 | 250 |
| 258 // The load was successful, so cache the image. | 251 // The load was successful, so cache the image. |
| 259 base::AutoLock lock_scope(*images_and_fonts_lock_); | 252 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 260 | 253 |
| 261 // Another thread raced the load and has already cached the image. | 254 // Another thread raced the load and has already cached the image. |
| 262 if (images_.count(resource_id)) | 255 if (images_.count(resource_id)) |
| 263 return *images_[resource_id]; | 256 return images_[resource_id]; |
| 264 | 257 |
| 265 std::vector<const SkBitmap*> tmp_bitmaps; | |
| 266 bitmaps.release(&tmp_bitmaps); | |
| 267 // Takes ownership of bitmaps. | |
| 268 gfx::Image* image = new gfx::Image(tmp_bitmaps); | |
| 269 images_[resource_id] = image; | 258 images_[resource_id] = image; |
| 270 return *image; | 259 return images_[resource_id]; |
| 271 } | 260 } |
| 272 | 261 |
| 273 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 262 const gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
| 274 return GetNativeImageNamed(resource_id, RTL_DISABLED); | 263 return GetNativeImageNamed(resource_id, RTL_DISABLED); |
| 275 } | 264 } |
| 276 | 265 |
| 277 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( | 266 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( |
| 278 int resource_id) const { | 267 int resource_id) const { |
| 279 for (size_t i = 0; i < data_packs_.size(); ++i) { | 268 scoped_refptr<base::RefCountedStaticMemory> bytes; |
| 280 base::RefCountedStaticMemory* bytes = | 269 if (delegate_) |
| 281 data_packs_[i]->GetStaticMemory(resource_id); | 270 bytes = delegate_->LoadDataResourceBytes(resource_id); |
| 282 if (bytes) | 271 |
| 283 return bytes; | 272 if (!bytes) { |
| 273 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) | |
| 274 bytes = data_packs_[i]->GetStaticMemory(resource_id); | |
| 284 } | 275 } |
| 285 | 276 |
| 286 return NULL; | 277 return bytes; |
| 287 } | 278 } |
| 288 | 279 |
| 289 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 280 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
| 281 base::StringPiece data; | |
| 282 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) | |
| 283 return data; | |
| 284 | |
| 290 DCHECK(locale_resources_data_.get()); | 285 DCHECK(locale_resources_data_.get()); |
| 291 base::StringPiece data; | |
| 292 if (locale_resources_data_->GetStringPiece(resource_id, &data)) | 286 if (locale_resources_data_->GetStringPiece(resource_id, &data)) |
| 293 return data; | 287 return data; |
| 294 | 288 |
| 295 for (size_t i = 0; i < data_packs_.size(); ++i) { | 289 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 296 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 290 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 297 return data; | 291 return data; |
| 298 } | 292 } |
| 299 | 293 |
| 300 return base::StringPiece(); | 294 return base::StringPiece(); |
| 301 } | 295 } |
| 302 | 296 |
| 297 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 298 string16 string; | |
| 299 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | |
| 300 return string; | |
| 301 | |
| 302 // Ensure that ReloadLocaleResources() doesn't drop the resources while | |
| 303 // we're using them. | |
| 304 base::AutoLock lock_scope(*locale_resources_data_lock_); | |
| 305 | |
| 306 // If for some reason we were unable to load the resources , return an empty | |
| 307 // string (better than crashing). | |
| 308 if (!locale_resources_data_.get()) { | |
| 309 LOG(WARNING) << "locale resources are not loaded"; | |
| 310 return string16(); | |
| 311 } | |
| 312 | |
| 313 base::StringPiece data; | |
| 314 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 315 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 316 // unittests). | |
| 317 data = GetRawDataResource(message_id); | |
| 318 if (data.empty()) { | |
| 319 NOTREACHED() << "unable to find resource: " << message_id; | |
| 320 return string16(); | |
| 321 } | |
| 322 } | |
| 323 | |
| 324 // Strings should not be loaded from a data pack that contains binary data. | |
| 325 ResourceHandle::TextEncodingType encoding = | |
| 326 locale_resources_data_->GetTextEncodingType(); | |
| 327 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | |
| 328 << "requested localized string from binary pack file"; | |
| 329 | |
| 330 // Data pack encodes strings as either UTF8 or UTF16. | |
| 331 string16 msg; | |
| 332 if (encoding == ResourceHandle::UTF16) { | |
| 333 msg = string16(reinterpret_cast<const char16*>(data.data()), | |
| 334 data.length() / 2); | |
| 335 } else if (encoding == ResourceHandle::UTF8) { | |
| 336 msg = UTF8ToUTF16(data); | |
| 337 } | |
| 338 return msg; | |
| 339 } | |
| 340 | |
| 303 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 341 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 304 { | 342 { |
| 305 base::AutoLock lock_scope(*images_and_fonts_lock_); | 343 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 306 LoadFontsIfNecessary(); | 344 LoadFontsIfNecessary(); |
| 307 } | 345 } |
| 308 switch (style) { | 346 switch (style) { |
| 309 case BoldFont: | 347 case BoldFont: |
| 310 return *bold_font_; | 348 return *bold_font_; |
| 311 case SmallFont: | 349 case SmallFont: |
| 312 return *small_font_; | 350 return *small_font_; |
| 313 case MediumFont: | 351 case MediumFont: |
| 314 return *medium_font_; | 352 return *medium_font_; |
| 315 case MediumBoldFont: | 353 case MediumBoldFont: |
| 316 return *medium_bold_font_; | 354 return *medium_bold_font_; |
| 317 case LargeFont: | 355 case LargeFont: |
| 318 return *large_font_; | 356 return *large_font_; |
| 319 case LargeBoldFont: | 357 case LargeBoldFont: |
| 320 return *large_bold_font_; | 358 return *large_bold_font_; |
| 321 default: | 359 default: |
| 322 return *base_font_; | 360 return *base_font_; |
| 323 } | 361 } |
| 324 } | 362 } |
| 325 | 363 |
| 326 void ResourceBundle::ReloadFonts() { | 364 void ResourceBundle::ReloadFonts() { |
| 327 base::AutoLock lock_scope(*images_and_fonts_lock_); | 365 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 328 base_font_.reset(); | 366 base_font_.reset(); |
| 329 LoadFontsIfNecessary(); | 367 LoadFontsIfNecessary(); |
| 330 } | 368 } |
| 331 | 369 |
| 332 ResourceBundle::ResourceBundle() | 370 ResourceBundle::ResourceBundle(Delegate* delegate) |
| 333 : images_and_fonts_lock_(new base::Lock), | 371 : delegate_(delegate), |
| 372 images_and_fonts_lock_(new base::Lock), | |
| 334 locale_resources_data_lock_(new base::Lock) { | 373 locale_resources_data_lock_(new base::Lock) { |
| 335 } | 374 } |
| 336 | 375 |
| 337 ResourceBundle::~ResourceBundle() { | 376 ResourceBundle::~ResourceBundle() { |
| 338 FreeImages(); | 377 FreeImages(); |
| 339 UnloadLocaleResources(); | 378 UnloadLocaleResources(); |
| 340 } | 379 } |
| 341 | 380 |
| 342 void ResourceBundle::FreeImages() { | 381 void ResourceBundle::FreeImages() { |
| 343 STLDeleteContainerPairSecondPointers(images_.begin(), | |
| 344 images_.end()); | |
| 345 images_.clear(); | 382 images_.clear(); |
| 346 } | 383 } |
| 347 | 384 |
| 348 void ResourceBundle::LoadFontsIfNecessary() { | 385 void ResourceBundle::LoadFontsIfNecessary() { |
| 349 images_and_fonts_lock_->AssertAcquired(); | 386 images_and_fonts_lock_->AssertAcquired(); |
| 350 if (!base_font_.get()) { | 387 if (!base_font_.get()) { |
| 351 base_font_.reset(new gfx::Font()); | 388 if (delegate_) { |
| 389 base_font_.reset(delegate_->GetFont(BaseFont).release()); | |
| 390 bold_font_.reset(delegate_->GetFont(BoldFont).release()); | |
| 391 small_font_.reset(delegate_->GetFont(SmallFont).release()); | |
| 392 medium_font_.reset(delegate_->GetFont(MediumFont).release()); | |
| 393 medium_bold_font_.reset(delegate_->GetFont(MediumBoldFont).release()); | |
| 394 large_font_.reset(delegate_->GetFont(LargeFont).release()); | |
| 395 large_bold_font_.reset(delegate_->GetFont(LargeBoldFont).release()); | |
| 396 } | |
| 352 | 397 |
| 353 bold_font_.reset(new gfx::Font()); | 398 if (!base_font_.get()) |
| 354 *bold_font_ = | 399 base_font_.reset(new gfx::Font()); |
| 355 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | |
| 356 | 400 |
| 357 small_font_.reset(new gfx::Font()); | 401 if (!bold_font_.get()) { |
| 358 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | 402 bold_font_.reset(new gfx::Font()); |
| 403 *bold_font_ = | |
| 404 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | |
| 405 } | |
| 359 | 406 |
| 360 medium_font_.reset(new gfx::Font()); | 407 if (!small_font_.get()) { |
| 361 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); | 408 small_font_.reset(new gfx::Font()); |
| 409 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | |
| 410 } | |
| 362 | 411 |
| 363 medium_bold_font_.reset(new gfx::Font()); | 412 if (!medium_font_.get()) { |
| 364 *medium_bold_font_ = | 413 medium_font_.reset(new gfx::Font()); |
| 365 base_font_->DeriveFont(kMediumFontSizeDelta, | 414 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); |
| 366 base_font_->GetStyle() | gfx::Font::BOLD); | 415 } |
| 367 | 416 |
| 368 large_font_.reset(new gfx::Font()); | 417 if (!medium_bold_font_.get()) { |
| 369 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); | 418 medium_bold_font_.reset(new gfx::Font()); |
| 419 *medium_bold_font_ = | |
| 420 base_font_->DeriveFont(kMediumFontSizeDelta, | |
| 421 base_font_->GetStyle() | gfx::Font::BOLD); | |
| 422 } | |
| 370 | 423 |
| 371 large_bold_font_.reset(new gfx::Font()); | 424 if (!large_font_.get()) { |
| 372 *large_bold_font_ = | 425 large_font_.reset(new gfx::Font()); |
| 373 base_font_->DeriveFont(kLargeFontSizeDelta, | 426 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); |
| 374 base_font_->GetStyle() | gfx::Font::BOLD); | 427 } |
| 428 | |
| 429 if (!large_bold_font_.get()) { | |
| 430 large_bold_font_.reset(new gfx::Font()); | |
| 431 *large_bold_font_ = | |
| 432 base_font_->DeriveFont(kLargeFontSizeDelta, | |
| 433 base_font_->GetStyle() | gfx::Font::BOLD); | |
| 434 } | |
| 375 } | 435 } |
| 376 } | 436 } |
| 377 | 437 |
| 378 SkBitmap* ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, | 438 SkBitmap* ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, |
| 379 int resource_id) { | 439 int resource_id) { |
| 380 scoped_refptr<RefCountedMemory> memory( | 440 scoped_refptr<RefCountedMemory> memory( |
| 381 data_handle.GetStaticMemory(resource_id)); | 441 data_handle.GetStaticMemory(resource_id)); |
| 382 if (!memory) | 442 if (!memory) |
| 383 return NULL; | 443 return NULL; |
| 384 | 444 |
| 385 SkBitmap bitmap; | 445 SkBitmap bitmap; |
| 386 if (gfx::PNGCodec::Decode(memory->front(), memory->size(), &bitmap)) | 446 if (gfx::PNGCodec::Decode(memory->front(), memory->size(), &bitmap)) |
| 387 return new SkBitmap(bitmap); | 447 return new SkBitmap(bitmap); |
| 388 | 448 |
| 389 // 99% of our assets are PNGs, however fallback to JPEG. | 449 // 99% of our assets are PNGs, however fallback to JPEG. |
| 390 SkBitmap* allocated_bitmap = | 450 SkBitmap* allocated_bitmap = |
| 391 gfx::JPEGCodec::Decode(memory->front(), memory->size()); | 451 gfx::JPEGCodec::Decode(memory->front(), memory->size()); |
| 392 if (allocated_bitmap) | 452 if (allocated_bitmap) |
| 393 return allocated_bitmap; | 453 return allocated_bitmap; |
| 394 | 454 |
| 395 NOTREACHED() << "Unable to decode theme image resource " << resource_id; | 455 NOTREACHED() << "Unable to decode theme image resource " << resource_id; |
| 396 return NULL; | 456 return NULL; |
| 397 } | 457 } |
| 398 | 458 |
| 399 gfx::Image* ResourceBundle::GetEmptyImage() { | 459 const gfx::Image& ResourceBundle::GetEmptyImage() { |
| 400 base::AutoLock lock(*images_and_fonts_lock_); | 460 base::AutoLock lock(*images_and_fonts_lock_); |
| 401 | 461 |
| 402 static gfx::Image* empty_image = NULL; | 462 static gfx::Image empty_image; |
|
tony
2012/05/03 20:35:42
This should be a pointer, otherwise an exit time d
Marshall
2012/05/04 14:41:03
Taking a closer look I don't think there's an adva
| |
| 403 if (!empty_image) { | 463 if (empty_image.IsEmpty()) { |
| 404 // The placeholder bitmap is bright red so people notice the problem. | 464 // The placeholder bitmap is bright red so people notice the problem. |
| 405 // This bitmap will be leaked, but this code should never be hit. | 465 // This bitmap will be leaked, but this code should never be hit. |
| 406 SkBitmap* bitmap = new SkBitmap(); | 466 SkBitmap* bitmap = new SkBitmap(); |
| 407 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 467 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| 408 bitmap->allocPixels(); | 468 bitmap->allocPixels(); |
| 409 bitmap->eraseARGB(255, 255, 0, 0); | 469 bitmap->eraseARGB(255, 255, 0, 0); |
| 410 empty_image = new gfx::Image(bitmap); | 470 empty_image = gfx::Image(bitmap); |
| 411 } | 471 } |
| 412 return empty_image; | 472 return empty_image; |
| 413 } | 473 } |
| 414 | 474 |
| 415 } // namespace ui | 475 } // namespace ui |
| OLD | NEW |