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

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: Convert ptr to bool for win compile. 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"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 g_shared_instance_->LoadCommonResources(); 54 g_shared_instance_->LoadCommonResources();
55 return g_shared_instance_->LoadLocaleResources(pref_locale); 55 return g_shared_instance_->LoadLocaleResources(pref_locale);
56 } 56 }
57 57
58 // static 58 // static
59 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { 59 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) {
60 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; 60 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
61 g_shared_instance_ = new ResourceBundle(NULL); 61 g_shared_instance_ = new ResourceBundle(NULL);
62 62
63 g_shared_instance_->LoadTestResources(path); 63 g_shared_instance_->LoadTestResources(path, path);
64 } 64 }
65 65
66 // static 66 // static
67 void ResourceBundle::CleanupSharedInstance() { 67 void ResourceBundle::CleanupSharedInstance() {
68 if (g_shared_instance_) { 68 if (g_shared_instance_) {
69 delete g_shared_instance_; 69 delete g_shared_instance_;
70 g_shared_instance_ = NULL; 70 g_shared_instance_ = NULL;
71 } 71 }
72 } 72 }
73 73
(...skipping 20 matching lines...) Expand all
94 94
95 FilePath pack_path = path; 95 FilePath pack_path = path;
96 if (delegate_) 96 if (delegate_)
97 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); 97 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor);
98 98
99 // Don't try to load empty values or values that are not absolute paths. 99 // Don't try to load empty values or values that are not absolute paths.
100 if (pack_path.empty() || !pack_path.IsAbsolute()) 100 if (pack_path.empty() || !pack_path.IsAbsolute())
101 return; 101 return;
102 102
103 scoped_ptr<DataPack> data_pack( 103 scoped_ptr<DataPack> data_pack(
104 new DataPack(ResourceHandle::kScaleFactor100x)); 104 new DataPack(scale_factor));
105 if (data_pack->Load(pack_path)) { 105 if (data_pack->Load(pack_path)) {
106 data_packs_.push_back(data_pack.release()); 106 data_packs_.push_back(data_pack.release());
107 } else { 107 } else {
108 LOG(ERROR) << "Failed to load " << pack_path.value() 108 LOG(ERROR) << "Failed to load " << pack_path.value()
109 << "\nSome features may not be available."; 109 << "\nSome features may not be available.";
110 } 110 }
111 } 111 }
112 112
113 #if !defined(OS_MACOSX) 113 #if !defined(OS_MACOSX)
114 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { 114 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", 169 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError",
170 logging::GetLastSystemErrorCode(), 16000); 170 logging::GetLastSystemErrorCode(), 16000);
171 NOTREACHED() << "failed to load locale.pak"; 171 NOTREACHED() << "failed to load locale.pak";
172 return std::string(); 172 return std::string();
173 } 173 }
174 174
175 locale_resources_data_.reset(data_pack.release()); 175 locale_resources_data_.reset(data_pack.release());
176 return app_locale; 176 return app_locale;
177 } 177 }
178 178
179 void ResourceBundle::LoadTestResources(const FilePath& path) { 179 void ResourceBundle::LoadTestResources(const FilePath& path,
180 const FilePath& locale_path) {
180 // Use the given resource pak for both common and localized resources. 181 // Use the given resource pak for both common and localized resources.
181 scoped_ptr<DataPack> data_pack( 182 scoped_ptr<DataPack> data_pack(
182 new DataPack(ResourceHandle::kScaleFactor100x)); 183 new DataPack(ResourceHandle::kScaleFactor100x));
183 if (data_pack->Load(path)) 184 if (!path.empty() && data_pack->Load(path))
184 data_packs_.push_back(data_pack.release()); 185 data_packs_.push_back(data_pack.release());
185 186
186 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); 187 data_pack.reset(new DataPack(ResourceHandle::kScaleFactorNone));
187 if (data_pack->Load(path)) 188 if (!locale_path.empty() && data_pack->Load(locale_path)) {
188 locale_resources_data_.reset(data_pack.release()); 189 locale_resources_data_.reset(data_pack.release());
190 } else {
191 locale_resources_data_.reset(
192 new DataPack(ResourceHandle::kScaleFactorNone));
193 }
189 } 194 }
190 195
191 void ResourceBundle::UnloadLocaleResources() { 196 void ResourceBundle::UnloadLocaleResources() {
192 locale_resources_data_.reset(); 197 locale_resources_data_.reset();
193 } 198 }
194 199
195 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { 200 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) {
196 overridden_pak_path_ = pak_path; 201 overridden_pak_path_ = pak_path;
197 } 202 }
198 203
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 268
264 images_[resource_id] = image; 269 images_[resource_id] = image;
265 return images_[resource_id]; 270 return images_[resource_id];
266 } 271 }
267 272
268 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { 273 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
269 return GetNativeImageNamed(resource_id, RTL_DISABLED); 274 return GetNativeImageNamed(resource_id, RTL_DISABLED);
270 } 275 }
271 276
272 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( 277 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes(
273 int resource_id) const { 278 int resource_id,
279 float scale_factor) const {
274 base::RefCountedStaticMemory* bytes = NULL; 280 base::RefCountedStaticMemory* bytes = NULL;
275 if (delegate_) 281 if (delegate_)
276 bytes = delegate_->LoadDataResourceBytes(resource_id); 282 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor);
277 283
278 if (!bytes) { 284 if (!bytes) {
279 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) 285 base::StringPiece data = GetRawDataResource(resource_id, scale_factor);
280 bytes = data_packs_[i]->GetStaticMemory(resource_id); 286 if (!data.empty()) {
287 bytes = new base::RefCountedStaticMemory(
288 reinterpret_cast<const unsigned char*>(data.data()), data.length());
289 }
281 } 290 }
282 291
283 return bytes; 292 return bytes;
284 } 293 }
285 294
286 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { 295 base::StringPiece ResourceBundle::GetRawDataResource(
296 int resource_id,
297 float scale_factor) const {
287 base::StringPiece data; 298 base::StringPiece data;
288 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) 299 if (delegate_ &&
300 delegate_->GetRawDataResource(resource_id, scale_factor, &data))
289 return data; 301 return data;
290 302
291 DCHECK(locale_resources_data_.get()); 303 DCHECK(locale_resources_data_.get());
292 if (locale_resources_data_->GetStringPiece(resource_id, &data)) 304 if (locale_resources_data_->GetStringPiece(resource_id, &data))
293 return data; 305 return data;
294 306
307 size_t best_match = data_packs_.size();
308 float best_match_scale = 0;
295 for (size_t i = 0; i < data_packs_.size(); ++i) { 309 for (size_t i = 0; i < data_packs_.size(); ++i) {
296 if (data_packs_[i]->GetStringPiece(resource_id, &data)) 310 if (best_match == data_packs_.size() ||
tony 2012/05/11 21:22:03 This seems unnecessarily complex considering the p
flackr 2012/05/11 21:59:07 We should always have a 1x asset. I was mostly wor
flackr 2012/05/15 02:16:33 Done.
297 return data; 311 fabs(best_match_scale - scale_factor) >
312 fabs(data_packs_[i]->GetScaleFactor() - scale_factor)) {
313 if (data_packs_[i]->HasResource(resource_id)) {
314 best_match = i;
315 best_match_scale = data_packs_[i]->GetScaleFactor();
316 }
317 }
318 }
319
320 if (best_match < data_packs_.size() &&
321 data_packs_[best_match]->GetStringPiece(resource_id, &data)) {
322 return data;
298 } 323 }
299 324
300 return base::StringPiece(); 325 return base::StringPiece();
301 } 326 }
302 327
303 string16 ResourceBundle::GetLocalizedString(int message_id) { 328 string16 ResourceBundle::GetLocalizedString(int message_id) {
304 string16 string; 329 string16 string;
305 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) 330 if (delegate_ && delegate_->GetLocalizedString(message_id, &string))
306 return string; 331 return string;
307 332
308 // Ensure that ReloadLocaleResources() doesn't drop the resources while 333 // Ensure that ReloadLocaleResources() doesn't drop the resources while
309 // we're using them. 334 // we're using them.
310 base::AutoLock lock_scope(*locale_resources_data_lock_); 335 base::AutoLock lock_scope(*locale_resources_data_lock_);
311 336
312 // If for some reason we were unable to load the resources , return an empty 337 // If for some reason we were unable to load the resources , return an empty
313 // string (better than crashing). 338 // string (better than crashing).
314 if (!locale_resources_data_.get()) { 339 if (!locale_resources_data_.get()) {
315 LOG(WARNING) << "locale resources are not loaded"; 340 LOG(WARNING) << "locale resources are not loaded";
316 return string16(); 341 return string16();
317 } 342 }
318 343
319 base::StringPiece data; 344 base::StringPiece data;
320 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { 345 if (!locale_resources_data_->GetStringPiece(message_id, &data)) {
321 // Fall back on the main data pack (shouldn't be any strings here except in 346 // Fall back on the main data pack (shouldn't be any strings here except in
322 // unittests). 347 // unittests).
323 data = GetRawDataResource(message_id); 348 data = GetRawDataResource(message_id, ResourceHandle::kScaleFactorNone);
324 if (data.empty()) { 349 if (data.empty()) {
325 NOTREACHED() << "unable to find resource: " << message_id; 350 NOTREACHED() << "unable to find resource: " << message_id;
326 return string16(); 351 return string16();
327 } 352 }
328 } 353 }
329 354
330 // Strings should not be loaded from a data pack that contains binary data. 355 // Strings should not be loaded from a data pack that contains binary data.
331 ResourceHandle::TextEncodingType encoding = 356 ResourceHandle::TextEncodingType encoding =
332 locale_resources_data_->GetTextEncodingType(); 357 locale_resources_data_->GetTextEncodingType();
333 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) 358 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 SkBitmap* bitmap = new SkBitmap(); 495 SkBitmap* bitmap = new SkBitmap();
471 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32); 496 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 32, 32);
472 bitmap->allocPixels(); 497 bitmap->allocPixels();
473 bitmap->eraseARGB(255, 255, 0, 0); 498 bitmap->eraseARGB(255, 255, 0, 0);
474 empty_image_ = gfx::Image(bitmap); 499 empty_image_ = gfx::Image(bitmap);
475 } 500 }
476 return empty_image_; 501 return empty_image_;
477 } 502 }
478 503
479 } // namespace ui 504 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698