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/layout.h" |
22 #include "ui/base/resource/data_pack.h" | 23 #include "ui/base/resource/data_pack.h" |
23 #include "ui/base/ui_base_paths.h" | 24 #include "ui/base/ui_base_paths.h" |
24 #include "ui/base/ui_base_switches.h" | 25 #include "ui/base/ui_base_switches.h" |
25 #include "ui/gfx/codec/jpeg_codec.h" | 26 #include "ui/gfx/codec/jpeg_codec.h" |
26 #include "ui/gfx/codec/png_codec.h" | 27 #include "ui/gfx/codec/png_codec.h" |
27 #include "ui/gfx/image/image_skia.h" | 28 #include "ui/gfx/image/image_skia.h" |
28 #include "ui/gfx/screen.h" | 29 #include "ui/gfx/screen.h" |
29 | 30 |
30 namespace ui { | 31 namespace ui { |
31 | 32 |
(...skipping 22 matching lines...) Expand all Loading... |
54 | 55 |
55 g_shared_instance_->LoadCommonResources(); | 56 g_shared_instance_->LoadCommonResources(); |
56 return g_shared_instance_->LoadLocaleResources(pref_locale); | 57 return g_shared_instance_->LoadLocaleResources(pref_locale); |
57 } | 58 } |
58 | 59 |
59 // static | 60 // static |
60 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { | 61 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { |
61 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 62 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
62 g_shared_instance_ = new ResourceBundle(NULL); | 63 g_shared_instance_ = new ResourceBundle(NULL); |
63 | 64 |
64 g_shared_instance_->LoadTestResources(path); | 65 g_shared_instance_->LoadTestResources(path, path); |
65 } | 66 } |
66 | 67 |
67 // static | 68 // static |
68 void ResourceBundle::CleanupSharedInstance() { | 69 void ResourceBundle::CleanupSharedInstance() { |
69 if (g_shared_instance_) { | 70 if (g_shared_instance_) { |
70 delete g_shared_instance_; | 71 delete g_shared_instance_; |
71 g_shared_instance_ = NULL; | 72 g_shared_instance_ = NULL; |
72 } | 73 } |
73 } | 74 } |
74 | 75 |
75 // static | 76 // static |
76 bool ResourceBundle::HasSharedInstance() { | 77 bool ResourceBundle::HasSharedInstance() { |
77 return g_shared_instance_ != NULL; | 78 return g_shared_instance_ != NULL; |
78 } | 79 } |
79 | 80 |
80 // static | 81 // static |
81 ResourceBundle& ResourceBundle::GetSharedInstance() { | 82 ResourceBundle& ResourceBundle::GetSharedInstance() { |
82 // Must call InitSharedInstance before this function. | 83 // Must call InitSharedInstance before this function. |
83 CHECK(g_shared_instance_ != NULL); | 84 CHECK(g_shared_instance_ != NULL); |
84 return *g_shared_instance_; | 85 return *g_shared_instance_; |
85 } | 86 } |
86 | 87 |
87 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 88 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
88 return !GetLocaleFilePath(locale).empty(); | 89 return !GetLocaleFilePath(locale).empty(); |
89 } | 90 } |
90 | 91 |
91 void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { | 92 void ResourceBundle::AddDataPack(const FilePath& path, |
| 93 ScaleFactor scale_factor) { |
92 // Do not pass an empty |path| value to this method. If the absolute path is | 94 // Do not pass an empty |path| value to this method. If the absolute path is |
93 // unknown pass just the pack file name. | 95 // unknown pass just the pack file name. |
94 DCHECK(!path.empty()); | 96 DCHECK(!path.empty()); |
95 | 97 |
96 FilePath pack_path = path; | 98 FilePath pack_path = path; |
97 if (delegate_) | 99 if (delegate_) |
98 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | 100 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); |
99 | 101 |
100 // Don't try to load empty values or values that are not absolute paths. | 102 // Don't try to load empty values or values that are not absolute paths. |
101 if (pack_path.empty() || !pack_path.IsAbsolute()) | 103 if (pack_path.empty() || !pack_path.IsAbsolute()) |
102 return; | 104 return; |
103 | 105 |
104 scoped_ptr<DataPack> data_pack( | 106 scoped_ptr<DataPack> data_pack( |
105 new DataPack(ResourceHandle::kScaleFactor100x)); | 107 new DataPack(scale_factor)); |
106 if (data_pack->Load(pack_path)) { | 108 if (data_pack->Load(pack_path)) { |
107 data_packs_.push_back(data_pack.release()); | 109 data_packs_.push_back(data_pack.release()); |
108 } else { | 110 } else { |
109 LOG(ERROR) << "Failed to load " << pack_path.value() | 111 LOG(ERROR) << "Failed to load " << pack_path.value() |
110 << "\nSome features may not be available."; | 112 << "\nSome features may not be available."; |
111 } | 113 } |
112 } | 114 } |
113 | 115 |
114 #if !defined(OS_MACOSX) | 116 #if !defined(OS_MACOSX) |
115 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 117 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 locale_file_path = GetLocaleFilePath(app_locale); | 160 locale_file_path = GetLocaleFilePath(app_locale); |
159 } | 161 } |
160 } | 162 } |
161 | 163 |
162 if (locale_file_path.empty()) { | 164 if (locale_file_path.empty()) { |
163 // It's possible that there is no locale.pak. | 165 // It's possible that there is no locale.pak. |
164 return std::string(); | 166 return std::string(); |
165 } | 167 } |
166 | 168 |
167 scoped_ptr<DataPack> data_pack( | 169 scoped_ptr<DataPack> data_pack( |
168 new DataPack(ResourceHandle::kScaleFactor100x)); | 170 new DataPack(SCALE_FACTOR_100P)); |
169 if (!data_pack->Load(locale_file_path)) { | 171 if (!data_pack->Load(locale_file_path)) { |
170 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 172 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
171 logging::GetLastSystemErrorCode(), 16000); | 173 logging::GetLastSystemErrorCode(), 16000); |
172 NOTREACHED() << "failed to load locale.pak"; | 174 NOTREACHED() << "failed to load locale.pak"; |
173 return std::string(); | 175 return std::string(); |
174 } | 176 } |
175 | 177 |
176 locale_resources_data_.reset(data_pack.release()); | 178 locale_resources_data_.reset(data_pack.release()); |
177 return app_locale; | 179 return app_locale; |
178 } | 180 } |
179 | 181 |
180 void ResourceBundle::LoadTestResources(const FilePath& path) { | 182 void ResourceBundle::LoadTestResources(const FilePath& path, |
| 183 const FilePath& locale_path) { |
181 // Use the given resource pak for both common and localized resources. | 184 // Use the given resource pak for both common and localized resources. |
182 scoped_ptr<DataPack> data_pack( | 185 scoped_ptr<DataPack> data_pack( |
183 new DataPack(ResourceHandle::kScaleFactor100x)); | 186 new DataPack(SCALE_FACTOR_100P)); |
184 if (data_pack->Load(path)) | 187 if (!path.empty() && data_pack->Load(path)) |
185 data_packs_.push_back(data_pack.release()); | 188 data_packs_.push_back(data_pack.release()); |
186 | 189 |
187 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); | 190 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
188 if (data_pack->Load(path)) | 191 if (!locale_path.empty() && data_pack->Load(locale_path)) { |
189 locale_resources_data_.reset(data_pack.release()); | 192 locale_resources_data_.reset(data_pack.release()); |
| 193 } else { |
| 194 locale_resources_data_.reset( |
| 195 new DataPack(ui::SCALE_FACTOR_NONE)); |
| 196 } |
190 } | 197 } |
191 | 198 |
192 void ResourceBundle::UnloadLocaleResources() { | 199 void ResourceBundle::UnloadLocaleResources() { |
193 locale_resources_data_.reset(); | 200 locale_resources_data_.reset(); |
194 } | 201 } |
195 | 202 |
196 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { | 203 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { |
197 overridden_pak_path_ = pak_path; | 204 overridden_pak_path_ = pak_path; |
198 } | 205 } |
199 | 206 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 239 |
233 if (image.IsEmpty()) { | 240 if (image.IsEmpty()) { |
234 DCHECK(!delegate_ && !data_packs_.empty()) << | 241 DCHECK(!delegate_ && !data_packs_.empty()) << |
235 "Missing call to SetResourcesDataDLL?"; | 242 "Missing call to SetResourcesDataDLL?"; |
236 gfx::ImageSkia image_skia; | 243 gfx::ImageSkia image_skia; |
237 for (size_t i = 0; i < data_packs_.size(); ++i) { | 244 for (size_t i = 0; i < data_packs_.size(); ++i) { |
238 scoped_ptr<SkBitmap> bitmap(LoadBitmap(*data_packs_[i], resource_id)); | 245 scoped_ptr<SkBitmap> bitmap(LoadBitmap(*data_packs_[i], resource_id)); |
239 if (bitmap.get()) { | 246 if (bitmap.get()) { |
240 if (gfx::Screen::IsDIPEnabled()) | 247 if (gfx::Screen::IsDIPEnabled()) |
241 image_skia.AddBitmapForScale(*bitmap, | 248 image_skia.AddBitmapForScale(*bitmap, |
242 data_packs_[i]->GetScaleFactor()); | 249 ui::GetScaleFactorScale(data_packs_[i]->GetScaleFactor())); |
243 else | 250 else |
244 image_skia.AddBitmapForScale(*bitmap, 1.0f); | 251 image_skia.AddBitmapForScale(*bitmap, 1.0f); |
245 } | 252 } |
246 } | 253 } |
247 | 254 |
248 if (image_skia.empty()) { | 255 if (image_skia.empty()) { |
249 LOG(WARNING) << "Unable to load image with id " << resource_id; | 256 LOG(WARNING) << "Unable to load image with id " << resource_id; |
250 NOTREACHED(); // Want to assert in debug mode. | 257 NOTREACHED(); // Want to assert in debug mode. |
251 // The load failed to retrieve the image; show a debugging red square. | 258 // The load failed to retrieve the image; show a debugging red square. |
252 return GetEmptyImage(); | 259 return GetEmptyImage(); |
(...skipping 11 matching lines...) Expand all Loading... |
264 | 271 |
265 images_[resource_id] = image; | 272 images_[resource_id] = image; |
266 return images_[resource_id]; | 273 return images_[resource_id]; |
267 } | 274 } |
268 | 275 |
269 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 276 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
270 return GetNativeImageNamed(resource_id, RTL_DISABLED); | 277 return GetNativeImageNamed(resource_id, RTL_DISABLED); |
271 } | 278 } |
272 | 279 |
273 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( | 280 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( |
274 int resource_id) const { | 281 int resource_id, |
| 282 ScaleFactor scale_factor) const { |
275 base::RefCountedStaticMemory* bytes = NULL; | 283 base::RefCountedStaticMemory* bytes = NULL; |
276 if (delegate_) | 284 if (delegate_) |
277 bytes = delegate_->LoadDataResourceBytes(resource_id); | 285 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); |
278 | 286 |
279 if (!bytes) { | 287 if (!bytes) { |
280 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) | 288 base::StringPiece data = GetRawDataResource(resource_id, scale_factor); |
281 bytes = data_packs_[i]->GetStaticMemory(resource_id); | 289 if (!data.empty()) { |
| 290 bytes = new base::RefCountedStaticMemory( |
| 291 reinterpret_cast<const unsigned char*>(data.data()), data.length()); |
| 292 } |
282 } | 293 } |
283 | 294 |
284 return bytes; | 295 return bytes; |
285 } | 296 } |
286 | 297 |
287 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 298 base::StringPiece ResourceBundle::GetRawDataResource( |
| 299 int resource_id, |
| 300 ScaleFactor scale_factor) const { |
288 base::StringPiece data; | 301 base::StringPiece data; |
289 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) | 302 if (delegate_ && |
| 303 delegate_->GetRawDataResource(resource_id, scale_factor, &data)) |
290 return data; | 304 return data; |
291 | 305 |
292 DCHECK(locale_resources_data_.get()); | 306 DCHECK(locale_resources_data_.get()); |
293 if (locale_resources_data_->GetStringPiece(resource_id, &data)) | 307 if (locale_resources_data_->GetStringPiece(resource_id, &data)) |
294 return data; | 308 return data; |
295 | 309 |
296 for (size_t i = 0; i < data_packs_.size(); ++i) { | 310 if (scale_factor != ui::SCALE_FACTOR_100P) { |
297 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 311 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 312 if (data_packs_[i]->GetScaleFactor() == scale_factor && |
| 313 data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 314 return data; |
| 315 } |
| 316 } |
| 317 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 318 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && |
| 319 data_packs_[i]->GetStringPiece(resource_id, &data)) |
298 return data; | 320 return data; |
299 } | 321 } |
300 | 322 |
301 return base::StringPiece(); | 323 return base::StringPiece(); |
302 } | 324 } |
303 | 325 |
304 string16 ResourceBundle::GetLocalizedString(int message_id) { | 326 string16 ResourceBundle::GetLocalizedString(int message_id) { |
305 string16 string; | 327 string16 string; |
306 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | 328 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) |
307 return string; | 329 return string; |
308 | 330 |
309 // Ensure that ReloadLocaleResources() doesn't drop the resources while | 331 // Ensure that ReloadLocaleResources() doesn't drop the resources while |
310 // we're using them. | 332 // we're using them. |
311 base::AutoLock lock_scope(*locale_resources_data_lock_); | 333 base::AutoLock lock_scope(*locale_resources_data_lock_); |
312 | 334 |
313 // If for some reason we were unable to load the resources , return an empty | 335 // If for some reason we were unable to load the resources , return an empty |
314 // string (better than crashing). | 336 // string (better than crashing). |
315 if (!locale_resources_data_.get()) { | 337 if (!locale_resources_data_.get()) { |
316 LOG(WARNING) << "locale resources are not loaded"; | 338 LOG(WARNING) << "locale resources are not loaded"; |
317 return string16(); | 339 return string16(); |
318 } | 340 } |
319 | 341 |
320 base::StringPiece data; | 342 base::StringPiece data; |
321 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | 343 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { |
322 // Fall back on the main data pack (shouldn't be any strings here except in | 344 // Fall back on the main data pack (shouldn't be any strings here except in |
323 // unittests). | 345 // unittests). |
324 data = GetRawDataResource(message_id); | 346 data = GetRawDataResource(message_id, ui::SCALE_FACTOR_NONE); |
325 if (data.empty()) { | 347 if (data.empty()) { |
326 NOTREACHED() << "unable to find resource: " << message_id; | 348 NOTREACHED() << "unable to find resource: " << message_id; |
327 return string16(); | 349 return string16(); |
328 } | 350 } |
329 } | 351 } |
330 | 352 |
331 // Strings should not be loaded from a data pack that contains binary data. | 353 // Strings should not be loaded from a data pack that contains binary data. |
332 ResourceHandle::TextEncodingType encoding = | 354 ResourceHandle::TextEncodingType encoding = |
333 locale_resources_data_->GetTextEncodingType(); | 355 locale_resources_data_->GetTextEncodingType(); |
334 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | 356 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 SkBitmap bitmap; | 493 SkBitmap bitmap; |
472 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 494 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
473 bitmap.allocPixels(); | 495 bitmap.allocPixels(); |
474 bitmap.eraseARGB(255, 255, 0, 0); | 496 bitmap.eraseARGB(255, 255, 0, 0); |
475 empty_image_ = gfx::Image(bitmap); | 497 empty_image_ = gfx::Image(bitmap); |
476 } | 498 } |
477 return empty_image_; | 499 return empty_image_; |
478 } | 500 } |
479 | 501 |
480 } // namespace ui | 502 } // namespace ui |
OLD | NEW |