| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 base::AutoLock lock_scope(*locale_resources_data_lock_); | 395 base::AutoLock lock_scope(*locale_resources_data_lock_); |
| 396 | 396 |
| 397 // Remove all overriden strings, as they will not be valid for the new locale. | 397 // Remove all overriden strings, as they will not be valid for the new locale. |
| 398 overridden_locale_strings_.clear(); | 398 overridden_locale_strings_.clear(); |
| 399 | 399 |
| 400 UnloadLocaleResources(); | 400 UnloadLocaleResources(); |
| 401 return LoadLocaleResources(pref_locale); | 401 return LoadLocaleResources(pref_locale); |
| 402 } | 402 } |
| 403 | 403 |
| 404 gfx::ImageSkia* ResourceBundle::GetImageSkiaNamed(int resource_id) { | 404 gfx::ImageSkia* ResourceBundle::GetImageSkiaNamed(int resource_id) { |
| 405 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 406 |
| 405 const gfx::ImageSkia* image = GetImageNamed(resource_id).ToImageSkia(); | 407 const gfx::ImageSkia* image = GetImageNamed(resource_id).ToImageSkia(); |
| 406 return const_cast<gfx::ImageSkia*>(image); | 408 return const_cast<gfx::ImageSkia*>(image); |
| 407 } | 409 } |
| 408 | 410 |
| 409 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { | 411 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
| 412 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 413 |
| 410 // Check to see if the image is already in the cache. | 414 // Check to see if the image is already in the cache. |
| 411 { | 415 if (images_.count(resource_id)) |
| 412 base::AutoLock lock_scope(*images_and_fonts_lock_); | 416 return images_[resource_id]; |
| 413 if (images_.count(resource_id)) | |
| 414 return images_[resource_id]; | |
| 415 } | |
| 416 | 417 |
| 417 gfx::Image image; | 418 gfx::Image image; |
| 418 if (delegate_) | 419 if (delegate_) |
| 419 image = delegate_->GetImageNamed(resource_id); | 420 image = delegate_->GetImageNamed(resource_id); |
| 420 | 421 |
| 421 if (image.IsEmpty()) { | 422 if (image.IsEmpty()) { |
| 422 DCHECK(!data_packs_.empty()) << | 423 DCHECK(!data_packs_.empty()) << |
| 423 "Missing call to SetResourcesDataDLL?"; | 424 "Missing call to SetResourcesDataDLL?"; |
| 424 | 425 |
| 425 #if defined(OS_CHROMEOS) | 426 #if defined(OS_CHROMEOS) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 443 LOG(WARNING) << "Unable to load image with id " << resource_id; | 444 LOG(WARNING) << "Unable to load image with id " << resource_id; |
| 444 NOTREACHED(); // Want to assert in debug mode. | 445 NOTREACHED(); // Want to assert in debug mode. |
| 445 // The load failed to retrieve the image; show a debugging red square. | 446 // The load failed to retrieve the image; show a debugging red square. |
| 446 return GetEmptyImage(); | 447 return GetEmptyImage(); |
| 447 } | 448 } |
| 448 image_skia.SetReadOnly(); | 449 image_skia.SetReadOnly(); |
| 449 image = gfx::Image(image_skia); | 450 image = gfx::Image(image_skia); |
| 450 } | 451 } |
| 451 | 452 |
| 452 // The load was successful, so cache the image. | 453 // The load was successful, so cache the image. |
| 453 base::AutoLock lock_scope(*images_and_fonts_lock_); | |
| 454 | |
| 455 // Another thread raced the load and has already cached the image. | |
| 456 if (images_.count(resource_id)) | |
| 457 return images_[resource_id]; | |
| 458 | |
| 459 images_[resource_id] = image; | 454 images_[resource_id] = image; |
| 460 return images_[resource_id]; | 455 return images_[resource_id]; |
| 461 } | 456 } |
| 462 | 457 |
| 463 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytes( | 458 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytes( |
| 464 int resource_id) const { | 459 int resource_id) const { |
| 465 return LoadDataResourceBytesForScale(resource_id, ui::SCALE_FACTOR_NONE); | 460 return LoadDataResourceBytesForScale(resource_id, ui::SCALE_FACTOR_NONE); |
| 466 } | 461 } |
| 467 | 462 |
| 468 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( | 463 base::RefCountedMemory* ResourceBundle::LoadDataResourceBytesForScale( |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 } | 574 } |
| 580 } | 575 } |
| 581 // Release lock_scope and fall back to main data pack. | 576 // Release lock_scope and fall back to main data pack. |
| 582 return LoadDataResourceBytes(resource_id); | 577 return LoadDataResourceBytes(resource_id); |
| 583 } | 578 } |
| 584 | 579 |
| 585 const gfx::FontList& ResourceBundle::GetFontListWithDelta( | 580 const gfx::FontList& ResourceBundle::GetFontListWithDelta( |
| 586 int size_delta, | 581 int size_delta, |
| 587 gfx::Font::FontStyle style, | 582 gfx::Font::FontStyle style, |
| 588 gfx::Font::Weight weight) { | 583 gfx::Font::Weight weight) { |
| 589 base::AutoLock lock_scope(*images_and_fonts_lock_); | 584 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 590 | 585 |
| 591 const FontKey styled_key(size_delta, style, weight); | 586 const FontKey styled_key(size_delta, style, weight); |
| 592 | 587 |
| 593 auto found = font_cache_.find(styled_key); | 588 auto found = font_cache_.find(styled_key); |
| 594 if (found != font_cache_.end()) | 589 if (found != font_cache_.end()) |
| 595 return found->second; | 590 return found->second; |
| 596 | 591 |
| 597 const FontKey base_key(0, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL); | 592 const FontKey base_key(0, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL); |
| 598 gfx::FontList& base = font_cache_[base_key]; | 593 gfx::FontList& base = font_cache_[base_key]; |
| 599 if (styled_key == base_key) | 594 if (styled_key == base_key) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 615 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. | 610 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. |
| 616 styled.first->second = sized.first->second.Derive( | 611 styled.first->second = sized.first->second.Derive( |
| 617 0, sized.first->second.GetFontStyle() | style, weight); | 612 0, sized.first->second.GetFontStyle() | style, weight); |
| 618 | 613 |
| 619 return styled.first->second; | 614 return styled.first->second; |
| 620 } | 615 } |
| 621 | 616 |
| 622 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, | 617 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, |
| 623 gfx::Font::FontStyle style, | 618 gfx::Font::FontStyle style, |
| 624 gfx::Font::Weight weight) { | 619 gfx::Font::Weight weight) { |
| 620 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 625 return GetFontListWithDelta(size_delta, style, weight).GetPrimaryFont(); | 621 return GetFontListWithDelta(size_delta, style, weight).GetPrimaryFont(); |
| 626 } | 622 } |
| 627 | 623 |
| 628 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { | 624 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { |
| 625 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 629 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL; | 626 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL; |
| 630 if (legacy_style == BoldFont || legacy_style == MediumBoldFont) | 627 if (legacy_style == BoldFont || legacy_style == MediumBoldFont) |
| 631 font_weight = gfx::Font::Weight::BOLD; | 628 font_weight = gfx::Font::Weight::BOLD; |
| 632 | 629 |
| 633 int size_delta = 0; | 630 int size_delta = 0; |
| 634 switch (legacy_style) { | 631 switch (legacy_style) { |
| 635 case SmallFont: | 632 case SmallFont: |
| 636 size_delta = kSmallFontDelta; | 633 size_delta = kSmallFontDelta; |
| 637 break; | 634 break; |
| 638 case MediumFont: | 635 case MediumFont: |
| 639 case MediumBoldFont: | 636 case MediumBoldFont: |
| 640 size_delta = kMediumFontDelta; | 637 size_delta = kMediumFontDelta; |
| 641 break; | 638 break; |
| 642 case LargeFont: | 639 case LargeFont: |
| 643 size_delta = kLargeFontDelta; | 640 size_delta = kLargeFontDelta; |
| 644 break; | 641 break; |
| 645 case BaseFont: | 642 case BaseFont: |
| 646 case BoldFont: | 643 case BoldFont: |
| 647 break; | 644 break; |
| 648 } | 645 } |
| 649 | 646 |
| 650 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); | 647 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); |
| 651 } | 648 } |
| 652 | 649 |
| 653 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 650 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 651 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 654 return GetFontList(style).GetPrimaryFont(); | 652 return GetFontList(style).GetPrimaryFont(); |
| 655 } | 653 } |
| 656 | 654 |
| 657 void ResourceBundle::ReloadFonts() { | 655 void ResourceBundle::ReloadFonts() { |
| 658 base::AutoLock lock_scope(*images_and_fonts_lock_); | 656 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 659 InitDefaultFontList(); | 657 InitDefaultFontList(); |
| 660 font_cache_.clear(); | 658 font_cache_.clear(); |
| 661 } | 659 } |
| 662 | 660 |
| 663 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { | 661 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { |
| 664 #if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_LINUX) | 662 #if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_LINUX) |
| 665 return max_scale_factor_; | 663 return max_scale_factor_; |
| 666 #else | 664 #else |
| 667 return GetSupportedScaleFactors().back(); | 665 return GetSupportedScaleFactors().back(); |
| 668 #endif | 666 #endif |
| 669 } | 667 } |
| 670 | 668 |
| 671 bool ResourceBundle::IsScaleFactorSupported(ScaleFactor scale_factor) { | 669 bool ResourceBundle::IsScaleFactorSupported(ScaleFactor scale_factor) { |
| 672 const std::vector<ScaleFactor>& supported_scale_factors = | 670 const std::vector<ScaleFactor>& supported_scale_factors = |
| 673 ui::GetSupportedScaleFactors(); | 671 ui::GetSupportedScaleFactors(); |
| 674 return std::find(supported_scale_factors.begin(), | 672 return std::find(supported_scale_factors.begin(), |
| 675 supported_scale_factors.end(), | 673 supported_scale_factors.end(), |
| 676 scale_factor) != supported_scale_factors.end(); | 674 scale_factor) != supported_scale_factors.end(); |
| 677 } | 675 } |
| 678 | 676 |
| 679 ResourceBundle::ResourceBundle(Delegate* delegate) | 677 ResourceBundle::ResourceBundle(Delegate* delegate) |
| 680 : delegate_(delegate), | 678 : delegate_(delegate), |
| 681 images_and_fonts_lock_(new base::Lock), | |
| 682 locale_resources_data_lock_(new base::Lock), | 679 locale_resources_data_lock_(new base::Lock), |
| 683 max_scale_factor_(SCALE_FACTOR_100P) { | 680 max_scale_factor_(SCALE_FACTOR_100P) { |
| 684 } | 681 } |
| 685 | 682 |
| 686 ResourceBundle::~ResourceBundle() { | 683 ResourceBundle::~ResourceBundle() { |
| 687 FreeImages(); | 684 FreeImages(); |
| 688 UnloadLocaleResources(); | 685 UnloadLocaleResources(); |
| 689 } | 686 } |
| 690 | 687 |
| 691 // static | 688 // static |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 *fell_back_to_1x = true; | 852 *fell_back_to_1x = true; |
| 856 return true; | 853 return true; |
| 857 } | 854 } |
| 858 } | 855 } |
| 859 } | 856 } |
| 860 | 857 |
| 861 return false; | 858 return false; |
| 862 } | 859 } |
| 863 | 860 |
| 864 gfx::Image& ResourceBundle::GetEmptyImage() { | 861 gfx::Image& ResourceBundle::GetEmptyImage() { |
| 865 base::AutoLock lock(*images_and_fonts_lock_); | 862 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 866 | 863 |
| 867 if (empty_image_.IsEmpty()) { | 864 if (empty_image_.IsEmpty()) { |
| 868 // The placeholder bitmap is bright red so people notice the problem. | 865 // The placeholder bitmap is bright red so people notice the problem. |
| 869 SkBitmap bitmap = CreateEmptyBitmap(); | 866 SkBitmap bitmap = CreateEmptyBitmap(); |
| 870 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); | 867 empty_image_ = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 871 } | 868 } |
| 872 return empty_image_; | 869 return empty_image_; |
| 873 } | 870 } |
| 874 | 871 |
| 875 // static | 872 // static |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 // static | 907 // static |
| 911 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 908 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 912 size_t size, | 909 size_t size, |
| 913 SkBitmap* bitmap, | 910 SkBitmap* bitmap, |
| 914 bool* fell_back_to_1x) { | 911 bool* fell_back_to_1x) { |
| 915 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 912 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 916 return gfx::PNGCodec::Decode(buf, size, bitmap); | 913 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 917 } | 914 } |
| 918 | 915 |
| 919 } // namespace ui | 916 } // namespace ui |
| OLD | NEW |