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