Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 10387010: Select theme resources from ResourceBundle at requested scale factor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with master. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 29
29 namespace ui { 30 namespace ui {
30 31
31 namespace { 32 namespace {
(...skipping 21 matching lines...) Expand all
53 54
54 g_shared_instance_->LoadCommonResources(); 55 g_shared_instance_->LoadCommonResources();
55 return g_shared_instance_->LoadLocaleResources(pref_locale); 56 return g_shared_instance_->LoadLocaleResources(pref_locale);
56 } 57 }
57 58
58 // static 59 // static
59 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { 60 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) {
60 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; 61 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
61 g_shared_instance_ = new ResourceBundle(NULL); 62 g_shared_instance_ = new ResourceBundle(NULL);
62 63
63 g_shared_instance_->LoadTestResources(path); 64 g_shared_instance_->LoadTestResources(path, path);
64 } 65 }
65 66
66 // static 67 // static
67 void ResourceBundle::CleanupSharedInstance() { 68 void ResourceBundle::CleanupSharedInstance() {
68 if (g_shared_instance_) { 69 if (g_shared_instance_) {
69 delete g_shared_instance_; 70 delete g_shared_instance_;
70 g_shared_instance_ = NULL; 71 g_shared_instance_ = NULL;
71 } 72 }
72 } 73 }
73 74
74 // static 75 // static
75 bool ResourceBundle::HasSharedInstance() { 76 bool ResourceBundle::HasSharedInstance() {
76 return g_shared_instance_ != NULL; 77 return g_shared_instance_ != NULL;
77 } 78 }
78 79
79 // static 80 // static
80 ResourceBundle& ResourceBundle::GetSharedInstance() { 81 ResourceBundle& ResourceBundle::GetSharedInstance() {
81 // Must call InitSharedInstance before this function. 82 // Must call InitSharedInstance before this function.
82 CHECK(g_shared_instance_ != NULL); 83 CHECK(g_shared_instance_ != NULL);
83 return *g_shared_instance_; 84 return *g_shared_instance_;
84 } 85 }
85 86
86 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { 87 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) {
87 return !GetLocaleFilePath(locale).empty(); 88 return !GetLocaleFilePath(locale).empty();
88 } 89 }
89 90
90 void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { 91 void ResourceBundle::AddDataPack(const FilePath& path,
92 ScaleFactor scale_factor) {
91 // Do not pass an empty |path| value to this method. If the absolute path is 93 // Do not pass an empty |path| value to this method. If the absolute path is
92 // unknown pass just the pack file name. 94 // unknown pass just the pack file name.
93 DCHECK(!path.empty()); 95 DCHECK(!path.empty());
94 96
95 FilePath pack_path = path; 97 FilePath pack_path = path;
96 if (delegate_) 98 if (delegate_)
97 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); 99 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor);
98 100
99 // Don't try to load empty values or values that are not absolute paths. 101 // Don't try to load empty values or values that are not absolute paths.
100 if (pack_path.empty() || !pack_path.IsAbsolute()) 102 if (pack_path.empty() || !pack_path.IsAbsolute())
101 return; 103 return;
102 104
103 scoped_ptr<DataPack> data_pack( 105 scoped_ptr<DataPack> data_pack(
104 new DataPack(ResourceHandle::kScaleFactor100x)); 106 new DataPack(scale_factor));
105 if (data_pack->Load(pack_path)) { 107 if (data_pack->Load(pack_path)) {
106 data_packs_.push_back(data_pack.release()); 108 data_packs_.push_back(data_pack.release());
107 } else { 109 } else {
108 LOG(ERROR) << "Failed to load " << pack_path.value() 110 LOG(ERROR) << "Failed to load " << pack_path.value()
109 << "\nSome features may not be available."; 111 << "\nSome features may not be available.";
110 } 112 }
111 } 113 }
112 114
113 #if !defined(OS_MACOSX) 115 #if !defined(OS_MACOSX)
114 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { 116 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 locale_file_path = GetLocaleFilePath(app_locale); 159 locale_file_path = GetLocaleFilePath(app_locale);
158 } 160 }
159 } 161 }
160 162
161 if (locale_file_path.empty()) { 163 if (locale_file_path.empty()) {
162 // It's possible that there is no locale.pak. 164 // It's possible that there is no locale.pak.
163 return std::string(); 165 return std::string();
164 } 166 }
165 167
166 scoped_ptr<DataPack> data_pack( 168 scoped_ptr<DataPack> data_pack(
167 new DataPack(ResourceHandle::kScaleFactor100x)); 169 new DataPack(SCALE_FACTOR_100P));
168 if (!data_pack->Load(locale_file_path)) { 170 if (!data_pack->Load(locale_file_path)) {
169 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", 171 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError",
170 logging::GetLastSystemErrorCode(), 16000); 172 logging::GetLastSystemErrorCode(), 16000);
171 NOTREACHED() << "failed to load locale.pak"; 173 NOTREACHED() << "failed to load locale.pak";
172 return std::string(); 174 return std::string();
173 } 175 }
174 176
175 locale_resources_data_.reset(data_pack.release()); 177 locale_resources_data_.reset(data_pack.release());
176 return app_locale; 178 return app_locale;
177 } 179 }
178 180
179 void ResourceBundle::LoadTestResources(const FilePath& path) { 181 void ResourceBundle::LoadTestResources(const FilePath& path,
182 const FilePath& locale_path) {
180 // Use the given resource pak for both common and localized resources. 183 // Use the given resource pak for both common and localized resources.
181 scoped_ptr<DataPack> data_pack( 184 scoped_ptr<DataPack> data_pack(
182 new DataPack(ResourceHandle::kScaleFactor100x)); 185 new DataPack(SCALE_FACTOR_100P));
183 if (data_pack->Load(path)) 186 if (!path.empty() && data_pack->Load(path))
184 data_packs_.push_back(data_pack.release()); 187 data_packs_.push_back(data_pack.release());
185 188
186 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); 189 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE));
187 if (data_pack->Load(path)) 190 if (!locale_path.empty() && data_pack->Load(locale_path)) {
188 locale_resources_data_.reset(data_pack.release()); 191 locale_resources_data_.reset(data_pack.release());
192 } else {
193 locale_resources_data_.reset(
194 new DataPack(ui::SCALE_FACTOR_NONE));
195 }
189 } 196 }
190 197
191 void ResourceBundle::UnloadLocaleResources() { 198 void ResourceBundle::UnloadLocaleResources() {
192 locale_resources_data_.reset(); 199 locale_resources_data_.reset();
193 } 200 }
194 201
195 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { 202 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) {
196 overridden_pak_path_ = pak_path; 203 overridden_pak_path_ = pak_path;
197 } 204 }
198 205
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 270
264 images_[resource_id] = image; 271 images_[resource_id] = image;
265 return images_[resource_id]; 272 return images_[resource_id];
266 } 273 }
267 274
268 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { 275 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
269 return GetNativeImageNamed(resource_id, RTL_DISABLED); 276 return GetNativeImageNamed(resource_id, RTL_DISABLED);
270 } 277 }
271 278
272 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( 279 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes(
273 int resource_id) const { 280 int resource_id,
281 ScaleFactor scale_factor) const {
274 base::RefCountedStaticMemory* bytes = NULL; 282 base::RefCountedStaticMemory* bytes = NULL;
275 if (delegate_) 283 if (delegate_)
276 bytes = delegate_->LoadDataResourceBytes(resource_id); 284 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
277 285
278 if (!bytes) { 286 if (!bytes) {
279 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) 287 base::StringPiece data = GetRawDataResource(resource_id, scale_factor);
280 bytes = data_packs_[i]->GetStaticMemory(resource_id); 288 if (!data.empty()) {
289 bytes = new base::RefCountedStaticMemory(
290 reinterpret_cast<const unsigned char*>(data.data()), data.length());
291 }
281 } 292 }
282 293
283 return bytes; 294 return bytes;
284 } 295 }
285 296
286 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 297 base::StringPiece ResourceBundle::GetRawDataResource(
298 int resource_id,
299 ScaleFactor scale_factor) const {
287 base::StringPiece data; 300 base::StringPiece data;
288 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) 301 if (delegate_ &&
302 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
289 return data; 303 return data;
290 304
291 DCHECK(locale_resources_data_.get()); 305 DCHECK(locale_resources_data_.get());
292 if (locale_resources_data_->GetStringPiece(resource_id, &data)) 306 if (locale_resources_data_->GetStringPiece(resource_id, &data))
293 return data; 307 return data;
294 308
295 for (size_t i = 0; i < data_packs_.size(); ++i) { 309 if (scale_factor != ui::SCALE_FACTOR_100P) {
296 if (data_packs_[i]->GetStringPiece(resource_id, &data)) 310 for (size_t i = 0; i < data_packs_.size(); i++) {
311 if (data_packs_[i]->GetScaleFactor() == scale_factor &&
312 data_packs_[i]->GetStringPiece(resource_id, &data))
313 return data;
314 }
315 }
316 for (size_t i = 0; i < data_packs_.size(); i++) {
317 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P &&
318 data_packs_[i]->GetStringPiece(resource_id, &data))
297 return data; 319 return data;
298 } 320 }
299 321
300 return base::StringPiece(); 322 return base::StringPiece();
301 } 323 }
302 324
303 string16 ResourceBundle::GetLocalizedString(int message_id) { 325 string16 ResourceBundle::GetLocalizedString(int message_id) {
304 string16 string; 326 string16 string;
305 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 327 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
306 return string; 328 return string;
307 329
308 // Ensure that ReloadLocaleResources() doesn't drop the resources while 330 // Ensure that ReloadLocaleResources() doesn't drop the resources while
309 // we're using them. 331 // we're using them.
310 base::AutoLock lock_scope(*locale_resources_data_lock_); 332 base::AutoLock lock_scope(*locale_resources_data_lock_);
311 333
312 // If for some reason we were unable to load the resources , return an empty 334 // If for some reason we were unable to load the resources , return an empty
313 // string (better than crashing). 335 // string (better than crashing).
314 if (!locale_resources_data_.get()) { 336 if (!locale_resources_data_.get()) {
315 LOG(WARNING) << "locale resources are not loaded"; 337 LOG(WARNING) << "locale resources are not loaded";
316 return string16(); 338 return string16();
317 } 339 }
318 340
319 base::StringPiece data; 341 base::StringPiece data;
320 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { 342 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
321 // Fall back on the main data pack (shouldn't be any strings here except in 343 // Fall back on the main data pack (shouldn't be any strings here except in
322 // unittests). 344 // unittests).
323 data = GetRawDataResource(message_id); 345 data = GetRawDataResource(message_id, ui::SCALE_FACTOR_NONE);
324 if (data.empty()) { 346 if (data.empty()) {
325 NOTREACHED() << "unable to find resource: " << message_id; 347 NOTREACHED() << "unable to find resource: " << message_id;
326 return string16(); 348 return string16();
327 } 349 }
328 } 350 }
329 351
330 // Strings should not be loaded from a data pack that contains binary data. 352 // Strings should not be loaded from a data pack that contains binary data.
331 ResourceHandle::TextEncodingType encoding = 353 ResourceHandle::TextEncodingType encoding =
332 locale_resources_data_->GetTextEncodingType(); 354 locale_resources_data_->GetTextEncodingType();
333 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) 355 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 SkBitmap bitmap; 492 SkBitmap bitmap;
471 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 493 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
472 bitmap.allocPixels(); 494 bitmap.allocPixels();
473 bitmap.eraseARGB(255, 255, 0, 0); 495 bitmap.eraseARGB(255, 255, 0, 0);
474 empty_image_ = gfx::Image(bitmap); 496 empty_image_ = gfx::Image(bitmap);
475 } 497 }
476 return empty_image_; 498 return empty_image_;
477 } 499 }
478 500
479 } // namespace ui 501 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698