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