OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "chrome/common/resource_bundle.h" | 5 #include "chrome/common/resource_bundle.h" |
6 | 6 |
7 #include "base/gfx/png_decoder.h" | 7 #include "base/gfx/png_decoder.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "net/base/file_stream.h" |
| 11 #include "net/base/net_errors.h" |
10 #include "chrome/common/gfx/chrome_font.h" | 12 #include "chrome/common/gfx/chrome_font.h" |
11 #include "SkBitmap.h" | 13 #include "SkBitmap.h" |
12 | 14 |
13 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; | 15 ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; |
14 | 16 |
15 /* static */ | 17 /* static */ |
16 void ResourceBundle::InitSharedInstance(const std::wstring& pref_locale) { | 18 void ResourceBundle::InitSharedInstance(const std::wstring& pref_locale) { |
17 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 19 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
18 g_shared_instance_ = new ResourceBundle(); | 20 g_shared_instance_ = new ResourceBundle(); |
19 | 21 |
(...skipping 16 matching lines...) Expand all Loading... |
36 } | 38 } |
37 | 39 |
38 ResourceBundle::ResourceBundle() | 40 ResourceBundle::ResourceBundle() |
39 : resources_data_(NULL), | 41 : resources_data_(NULL), |
40 locale_resources_data_(NULL), | 42 locale_resources_data_(NULL), |
41 theme_data_(NULL) { | 43 theme_data_(NULL) { |
42 } | 44 } |
43 | 45 |
44 void ResourceBundle::FreeImages() { | 46 void ResourceBundle::FreeImages() { |
45 for (SkImageMap::iterator i = skia_images_.begin(); | 47 for (SkImageMap::iterator i = skia_images_.begin(); |
46 » i != skia_images_.end(); i++) { | 48 i != skia_images_.end(); i++) { |
47 delete i->second; | 49 delete i->second; |
48 } | 50 } |
49 skia_images_.clear(); | 51 skia_images_.clear(); |
50 } | 52 } |
51 | 53 |
| 54 void ResourceBundle::SetThemeExtension(const Extension& e) { |
| 55 theme_extension_.reset(new Extension(e)); |
| 56 } |
| 57 |
52 /* static */ | 58 /* static */ |
53 SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { | 59 SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { |
54 std::vector<unsigned char> raw_data, png_data; | 60 std::vector<unsigned char> raw_data, png_data; |
55 bool success = LoadResourceBytes(data_handle, resource_id, &raw_data); | 61 bool success = false; |
| 62 // First check to see if we have a registered theme extension and whether |
| 63 // it can handle this resource. |
| 64 // TODO(erikkay): It would be nice to use something less brittle than |
| 65 // resource_id here. |
| 66 if (g_shared_instance_->theme_extension_.get()) { |
| 67 FilePath path = |
| 68 g_shared_instance_->theme_extension_->GetThemeResourcePath(resource_id); |
| 69 if (!path.empty()) { |
| 70 net::FileStream file; |
| 71 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; |
| 72 if (file.Open(path, flags) == net::OK) { |
| 73 int64 avail = file.Available(); |
| 74 if (avail > 0 && avail < INT_MAX) { |
| 75 size_t size = static_cast<size_t>(avail); |
| 76 raw_data.resize(size); |
| 77 char* data = reinterpret_cast<char*>(&(raw_data.front())); |
| 78 if (file.ReadUntilComplete(data, size) == avail) { |
| 79 success= true; |
| 80 } else { |
| 81 raw_data.resize(0); |
| 82 } |
| 83 } |
| 84 } |
| 85 } |
| 86 } |
| 87 if (!success) |
| 88 success = LoadResourceBytes(data_handle, resource_id, &raw_data); |
56 if (!success) | 89 if (!success) |
57 return NULL; | 90 return NULL; |
58 | 91 |
59 // Decode the PNG. | 92 // Decode the PNG. |
60 int image_width; | 93 int image_width; |
61 int image_height; | 94 int image_height; |
62 if (!PNGDecoder::Decode(&raw_data.front(), raw_data.size(), PNGDecoder::FORMAT
_BGRA, | 95 if (!PNGDecoder::Decode(&raw_data.front(), raw_data.size(), |
| 96 PNGDecoder::FORMAT_BGRA, |
63 &png_data, &image_width, &image_height)) { | 97 &png_data, &image_width, &image_height)) { |
64 NOTREACHED() << "Unable to decode image resource " << resource_id; | 98 NOTREACHED() << "Unable to decode image resource " << resource_id; |
65 return NULL; | 99 return NULL; |
66 } | 100 } |
67 | 101 |
68 return PNGDecoder::CreateSkBitmapFromBGRAFormat(png_data, | 102 return PNGDecoder::CreateSkBitmapFromBGRAFormat(png_data, |
69 image_width, | 103 image_width, |
70 image_height); | 104 image_height); |
71 } | 105 } |
72 | 106 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; | 153 LOG(WARNING) << "Unable to load bitmap with id " << resource_id; |
120 NOTREACHED(); // Want to assert in debug mode. | 154 NOTREACHED(); // Want to assert in debug mode. |
121 | 155 |
122 AutoLock lock_scope(lock_); // Guard empty_bitmap initialization. | 156 AutoLock lock_scope(lock_); // Guard empty_bitmap initialization. |
123 | 157 |
124 static SkBitmap* empty_bitmap = NULL; | 158 static SkBitmap* empty_bitmap = NULL; |
125 if (!empty_bitmap) { | 159 if (!empty_bitmap) { |
126 // The placeholder bitmap is bright red so people notice the problem. | 160 // The placeholder bitmap is bright red so people notice the problem. |
127 // This bitmap will be leaked, but this code should never be hit. | 161 // This bitmap will be leaked, but this code should never be hit. |
128 empty_bitmap = new SkBitmap(); | 162 empty_bitmap = new SkBitmap(); |
129 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32);» | 163 empty_bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
130 empty_bitmap->allocPixels();» | 164 empty_bitmap->allocPixels(); |
131 empty_bitmap->eraseARGB(255, 255, 0, 0); | 165 empty_bitmap->eraseARGB(255, 255, 0, 0); |
132 } | 166 } |
133 return empty_bitmap; | 167 return empty_bitmap; |
134 } | 168 } |
135 } | 169 } |
136 | 170 |
137 void ResourceBundle::LoadFontsIfNecessary() { | 171 void ResourceBundle::LoadFontsIfNecessary() { |
138 AutoLock lock_scope(lock_); | 172 AutoLock lock_scope(lock_); |
139 if (!base_font_.get()) { | 173 if (!base_font_.get()) { |
140 base_font_.reset(new ChromeFont()); | 174 base_font_.reset(new ChromeFont()); |
(...skipping 27 matching lines...) Expand all Loading... |
168 case MediumBoldFont: | 202 case MediumBoldFont: |
169 return *medium_bold_font_; | 203 return *medium_bold_font_; |
170 case LargeFont: | 204 case LargeFont: |
171 return *large_font_; | 205 return *large_font_; |
172 case WebFont: | 206 case WebFont: |
173 return *web_font_; | 207 return *web_font_; |
174 default: | 208 default: |
175 return *base_font_; | 209 return *base_font_; |
176 } | 210 } |
177 } | 211 } |
OLD | NEW |