| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "app/resource_bundle.h" | |
| 6 | |
| 7 #include "app/data_pack.h" | |
| 8 #include "base/lock.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/stl_util-inl.h" | |
| 11 #include "base/string_piece.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "gfx/codec/png_codec.h" | |
| 14 #include "gfx/font.h" | |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Font sizes relative to base font. | |
| 20 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) | |
| 21 const int kSmallFontSizeDelta = -3; | |
| 22 const int kMediumFontSizeDelta = 2; | |
| 23 const int kLargeFontSizeDelta = 7; | |
| 24 #else | |
| 25 const int kSmallFontSizeDelta = -2; | |
| 26 const int kMediumFontSizeDelta = 3; | |
| 27 const int kLargeFontSizeDelta = 8; | |
| 28 #endif | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; | |
| 33 | |
| 34 /* static */ | |
| 35 // TODO(glen): Finish moving these into theme provider (dialogs still | |
| 36 // depend on these colors). | |
| 37 const SkColor ResourceBundle::frame_color = | |
| 38 SkColorSetRGB(66, 116, 201); | |
| 39 const SkColor ResourceBundle::frame_color_inactive = | |
| 40 SkColorSetRGB(161, 182, 228); | |
| 41 const SkColor ResourceBundle::frame_color_app_panel = | |
| 42 SK_ColorWHITE; | |
| 43 const SkColor ResourceBundle::frame_color_app_panel_inactive = | |
| 44 SK_ColorWHITE; | |
| 45 const SkColor ResourceBundle::frame_color_incognito = | |
| 46 SkColorSetRGB(83, 106, 139); | |
| 47 const SkColor ResourceBundle::frame_color_incognito_inactive = | |
| 48 SkColorSetRGB(126, 139, 156); | |
| 49 const SkColor ResourceBundle::toolbar_color = | |
| 50 SkColorSetRGB(210, 225, 246); | |
| 51 const SkColor ResourceBundle::toolbar_separator_color = | |
| 52 SkColorSetRGB(182, 186, 192); | |
| 53 | |
| 54 /* static */ | |
| 55 std::string ResourceBundle::InitSharedInstance( | |
| 56 const std::string& pref_locale) { | |
| 57 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | |
| 58 g_shared_instance_ = new ResourceBundle(); | |
| 59 | |
| 60 g_shared_instance_->LoadCommonResources(); | |
| 61 return g_shared_instance_->LoadLocaleResources(pref_locale); | |
| 62 } | |
| 63 | |
| 64 /* static */ | |
| 65 std::string ResourceBundle::ReloadSharedInstance( | |
| 66 const std::string& pref_locale) { | |
| 67 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; | |
| 68 | |
| 69 g_shared_instance_->UnloadLocaleResources(); | |
| 70 return g_shared_instance_->LoadLocaleResources(pref_locale); | |
| 71 } | |
| 72 | |
| 73 /* static */ | |
| 74 void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) { | |
| 75 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; | |
| 76 g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path)); | |
| 77 } | |
| 78 | |
| 79 /* static */ | |
| 80 void ResourceBundle::CleanupSharedInstance() { | |
| 81 if (g_shared_instance_) { | |
| 82 delete g_shared_instance_; | |
| 83 g_shared_instance_ = NULL; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 /* static */ | |
| 88 ResourceBundle& ResourceBundle::GetSharedInstance() { | |
| 89 // Must call InitSharedInstance before this function. | |
| 90 CHECK(g_shared_instance_ != NULL); | |
| 91 return *g_shared_instance_; | |
| 92 } | |
| 93 | |
| 94 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | |
| 95 // Check to see if we already have the Skia image in the cache. | |
| 96 { | |
| 97 AutoLock lock_scope(*lock_); | |
| 98 SkImageMap::const_iterator found = skia_images_.find(resource_id); | |
| 99 if (found != skia_images_.end()) | |
| 100 return found->second; | |
| 101 } | |
| 102 | |
| 103 scoped_ptr<SkBitmap> bitmap; | |
| 104 | |
| 105 bitmap.reset(LoadBitmap(resources_data_, resource_id)); | |
| 106 | |
| 107 if (bitmap.get()) { | |
| 108 // We loaded successfully. Cache the Skia version of the bitmap. | |
| 109 AutoLock lock_scope(*lock_); | |
| 110 | |
| 111 // Another thread raced us, and has already cached the skia image. | |
| 112 if (skia_images_.count(resource_id)) | |
| 113 return skia_images_[resource_id]; | |
| 114 | |
| 115 skia_images_[resource_id] = bitmap.get(); | |
| 116 return bitmap.release(); | |
| 117 } | |
| 118 | |
| 119 // We failed to retrieve the bitmap, show a debugging red square. | |
| 120 { | |
| 121 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; | |
| 122 NOTREACHED(); // Want to assert in debug mode. | |
| 123 | |
| 124 AutoLock lock_scope(*lock_); // Guard empty_bitmap initialization. | |
| 125 | |
| 126 static SkBitmap* empty_bitmap = NULL; | |
| 127 if (!empty_bitmap) { | |
| 128 // The placeholder bitmap is bright red so people notice the problem. | |
| 129 // This bitmap will be leaked, but this code should never be hit. | |
| 130 empty_bitmap = new SkBitmap(); | |
| 131 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | |
| 132 empty_bitmap->allocPixels(); | |
| 133 empty_bitmap->eraseARGB(255, 255, 0, 0); | |
| 134 } | |
| 135 return empty_bitmap; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( | |
| 140 int resource_id) const { | |
| 141 RefCountedStaticMemory* bytes = | |
| 142 LoadResourceBytes(resources_data_, resource_id); | |
| 143 | |
| 144 // Check all our additional data packs for the resources if it wasn't loaded | |
| 145 // from our main source. | |
| 146 for (std::vector<LoadedDataPack*>::const_iterator it = data_packs_.begin(); | |
| 147 !bytes && it != data_packs_.end(); ++it) { | |
| 148 bytes = (*it)->GetStaticMemory(resource_id); | |
| 149 } | |
| 150 | |
| 151 return bytes; | |
| 152 } | |
| 153 | |
| 154 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | |
| 155 LoadFontsIfNecessary(); | |
| 156 switch (style) { | |
| 157 case BoldFont: | |
| 158 return *bold_font_; | |
| 159 case SmallFont: | |
| 160 return *small_font_; | |
| 161 case MediumFont: | |
| 162 return *medium_font_; | |
| 163 case MediumBoldFont: | |
| 164 return *medium_bold_font_; | |
| 165 case LargeFont: | |
| 166 return *large_font_; | |
| 167 default: | |
| 168 return *base_font_; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 gfx::NativeImage ResourceBundle::GetNativeImageNamed(int resource_id) { | |
| 173 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 174 #if defined(OS_MACOSX) | |
| 175 return rb.GetNSImageNamed(resource_id); | |
| 176 #elif defined(USE_X11) && !defined(TOOLKIT_VIEWS) | |
| 177 return rb.GetPixbufNamed(resource_id); | |
| 178 #else | |
| 179 return rb.GetBitmapNamed(resource_id); | |
| 180 #endif | |
| 181 } | |
| 182 | |
| 183 ResourceBundle::ResourceBundle() | |
| 184 : lock_(new Lock), | |
| 185 resources_data_(NULL), | |
| 186 locale_resources_data_(NULL) { | |
| 187 } | |
| 188 | |
| 189 void ResourceBundle::FreeImages() { | |
| 190 STLDeleteContainerPairSecondPointers(skia_images_.begin(), | |
| 191 skia_images_.end()); | |
| 192 skia_images_.clear(); | |
| 193 } | |
| 194 | |
| 195 void ResourceBundle::LoadFontsIfNecessary() { | |
| 196 AutoLock lock_scope(*lock_); | |
| 197 if (!base_font_.get()) { | |
| 198 base_font_.reset(new gfx::Font()); | |
| 199 | |
| 200 bold_font_.reset(new gfx::Font()); | |
| 201 *bold_font_ = | |
| 202 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | |
| 203 | |
| 204 small_font_.reset(new gfx::Font()); | |
| 205 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | |
| 206 | |
| 207 medium_font_.reset(new gfx::Font()); | |
| 208 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); | |
| 209 | |
| 210 medium_bold_font_.reset(new gfx::Font()); | |
| 211 *medium_bold_font_ = | |
| 212 base_font_->DeriveFont(kMediumFontSizeDelta, | |
| 213 base_font_->GetStyle() | gfx::Font::BOLD); | |
| 214 | |
| 215 large_font_.reset(new gfx::Font()); | |
| 216 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 /* static */ | |
| 221 SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { | |
| 222 scoped_refptr<RefCountedMemory> memory( | |
| 223 LoadResourceBytes(data_handle, resource_id)); | |
| 224 if (!memory) | |
| 225 return NULL; | |
| 226 | |
| 227 SkBitmap bitmap; | |
| 228 if (!gfx::PNGCodec::Decode(memory->front(), memory->size(), &bitmap)) { | |
| 229 NOTREACHED() << "Unable to decode theme image resource " << resource_id; | |
| 230 return NULL; | |
| 231 } | |
| 232 | |
| 233 return new SkBitmap(bitmap); | |
| 234 } | |
| 235 | |
| 236 | |
| 237 // LoadedDataPack ------------------------------------------------------------- | |
| 238 | |
| 239 ResourceBundle::LoadedDataPack::LoadedDataPack(const FilePath& path) | |
| 240 : path_(path) { | |
| 241 // Always preload the data packs so we can maintain constness. | |
| 242 Load(); | |
| 243 } | |
| 244 | |
| 245 ResourceBundle::LoadedDataPack::~LoadedDataPack() { | |
| 246 } | |
| 247 | |
| 248 void ResourceBundle::LoadedDataPack::Load() { | |
| 249 DCHECK(!data_pack_.get()); | |
| 250 data_pack_.reset(new app::DataPack); | |
| 251 bool success = data_pack_->Load(path_); | |
| 252 LOG_IF(ERROR, !success) << "Failed to load " << path_.value() | |
| 253 << "\nYou will not be able to use the Bookmarks Manager or " | |
| 254 << "about:net-internals."; | |
| 255 } | |
| 256 | |
| 257 bool ResourceBundle::LoadedDataPack::GetStringPiece( | |
| 258 int resource_id, base::StringPiece* data) const { | |
| 259 return data_pack_->GetStringPiece(static_cast<uint32>(resource_id), data); | |
| 260 } | |
| 261 | |
| 262 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( | |
| 263 int resource_id) const { | |
| 264 return data_pack_->GetStaticMemory(resource_id); | |
| 265 } | |
| OLD | NEW |