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 "chrome/browser/themes/browser_theme_pack.h" | 5 #include "chrome/browser/themes/browser_theme_pack.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 if (!data_pack_.get()) { | 539 if (!data_pack_.get()) { |
540 delete header_; | 540 delete header_; |
541 delete [] tints_; | 541 delete [] tints_; |
542 delete [] colors_; | 542 delete [] colors_; |
543 delete [] display_properties_; | 543 delete [] display_properties_; |
544 delete [] source_images_; | 544 delete [] source_images_; |
545 } | 545 } |
546 } | 546 } |
547 | 547 |
548 // static | 548 // static |
549 void BrowserThemePack::BuildFromExtension( | 549 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromExtension( |
550 const extensions::Extension* extension, | 550 const Extension* extension) { |
551 scoped_refptr<BrowserThemePack> pack) { | 551 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
552 DCHECK(extension); | 552 DCHECK(extension); |
553 DCHECK(extension->is_theme()); | 553 DCHECK(extension->is_theme()); |
554 DCHECK(!pack->is_valid()); | |
555 | 554 |
| 555 scoped_refptr<BrowserThemePack> pack(new BrowserThemePack); |
556 pack->BuildHeader(extension); | 556 pack->BuildHeader(extension); |
557 pack->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension)); | 557 pack->BuildTintsFromJSON(extensions::ThemeInfo::GetTints(extension)); |
558 pack->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension)); | 558 pack->BuildColorsFromJSON(extensions::ThemeInfo::GetColors(extension)); |
559 pack->BuildDisplayPropertiesFromJSON( | 559 pack->BuildDisplayPropertiesFromJSON( |
560 extensions::ThemeInfo::GetDisplayProperties(extension)); | 560 extensions::ThemeInfo::GetDisplayProperties(extension)); |
561 | 561 |
562 // Builds the images. (Image building is dependent on tints). | 562 // Builds the images. (Image building is dependent on tints). |
563 FilePathMap file_paths; | 563 FilePathMap file_paths; |
564 pack->ParseImageNamesFromJSON( | 564 pack->ParseImageNamesFromJSON( |
565 extensions::ThemeInfo::GetImages(extension), | 565 extensions::ThemeInfo::GetImages(extension), |
566 extension->path(), | 566 extension->path(), |
567 &file_paths); | 567 &file_paths); |
568 pack->BuildSourceImagesArray(file_paths); | 568 pack->BuildSourceImagesArray(file_paths); |
569 | 569 |
570 if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_)) | 570 if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_on_ui_thread_)) |
571 return; | 571 return NULL; |
572 | 572 |
573 pack->CreateImages(&pack->images_); | 573 pack->CreateImages(&pack->images_on_ui_thread_); |
574 | 574 |
575 // Make sure the |images_on_file_thread_| has bitmaps for supported | 575 // Make sure the |images_on_file_thread_| has bitmaps for supported |
576 // scale factors before passing to FILE thread. | 576 // scale factors before passing to FILE thread. |
577 pack->images_on_file_thread_ = pack->images_; | 577 pack->images_on_file_thread_ = pack->images_on_ui_thread_; |
578 for (ImageCache::iterator it = pack->images_on_file_thread_.begin(); | 578 for (ImageCache::iterator it = pack->images_on_file_thread_.begin(); |
579 it != pack->images_on_file_thread_.end(); ++it) { | 579 it != pack->images_on_file_thread_.end(); ++it) { |
580 gfx::ImageSkia* image_skia = | 580 gfx::ImageSkia* image_skia = |
581 const_cast<gfx::ImageSkia*>(it->second.ToImageSkia()); | 581 const_cast<gfx::ImageSkia*>(it->second.ToImageSkia()); |
582 image_skia->MakeThreadSafe(); | 582 image_skia->MakeThreadSafe(); |
583 } | 583 } |
584 | 584 |
585 // Set ThemeImageSource on |images_| to resample the source | 585 // Set ThemeImageSource on |images_on_ui_thread_| to resample the source |
586 // image if a caller of BrowserThemePack::GetImageNamed() requests an | 586 // image if a caller of BrowserThemePack::GetImageNamed() requests an |
587 // ImageSkiaRep for a scale factor not specified by the theme author. | 587 // ImageSkiaRep for a scale factor not specified by the theme author. |
588 // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve | 588 // Callers of BrowserThemePack::GetImageNamed() to be able to retrieve |
589 // ImageSkiaReps for all supported scale factors. | 589 // ImageSkiaReps for all supported scale factors. |
590 for (ImageCache::iterator it = pack->images_.begin(); | 590 for (ImageCache::iterator it = pack->images_on_ui_thread_.begin(); |
591 it != pack->images_.end(); ++it) { | 591 it != pack->images_on_ui_thread_.end(); ++it) { |
592 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia(); | 592 const gfx::ImageSkia source_image_skia = it->second.AsImageSkia(); |
593 ThemeImageSource* source = new ThemeImageSource(source_image_skia); | 593 ThemeImageSource* source = new ThemeImageSource(source_image_skia); |
594 // image_skia takes ownership of source. | 594 // image_skia takes ownership of source. |
595 gfx::ImageSkia image_skia(source, source_image_skia.size()); | 595 gfx::ImageSkia image_skia(source, source_image_skia.size()); |
596 it->second = gfx::Image(image_skia); | 596 it->second = gfx::Image(image_skia); |
597 } | 597 } |
598 | 598 |
599 // Generate raw images (for new-tab-page attribution and background) for | 599 // Generate raw images (for new-tab-page attribution and background) for |
600 // any missing scale from an available scale image. | 600 // any missing scale from an available scale image. |
601 for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) { | 601 for (size_t i = 0; i < arraysize(kPreloadIDs); ++i) { |
602 pack->GenerateRawImageForAllSupportedScales(kPreloadIDs[i]); | 602 pack->GenerateRawImageForAllSupportedScales(kPreloadIDs[i]); |
603 } | 603 } |
604 | 604 |
605 // The BrowserThemePack is now in a consistent state. | 605 // The BrowserThemePack is now in a consistent state. |
606 pack->is_valid_ = true; | 606 return pack; |
607 } | 607 } |
608 | 608 |
609 // static | 609 // static |
610 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( | 610 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack( |
611 const base::FilePath& path, const std::string& expected_id) { | 611 const base::FilePath& path, const std::string& expected_id) { |
612 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 612 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
613 // Allow IO on UI thread due to deep-seated theme design issues. | 613 // Allow IO on UI thread due to deep-seated theme design issues. |
614 // (see http://crbug.com/80206) | 614 // (see http://crbug.com/80206) |
615 base::ThreadRestrictions::ScopedAllowIO allow_io; | 615 base::ThreadRestrictions::ScopedAllowIO allow_io; |
616 scoped_refptr<BrowserThemePack> pack(new BrowserThemePack); | 616 scoped_refptr<BrowserThemePack> pack(new BrowserThemePack); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 const_cast<char*>(pointer.data())); | 664 const_cast<char*>(pointer.data())); |
665 | 665 |
666 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) | 666 if (!pack->data_pack_->GetStringPiece(kScaleFactorsID, &pointer)) |
667 return NULL; | 667 return NULL; |
668 | 668 |
669 if (!InputScalesValid(pointer, pack->scale_factors_)) { | 669 if (!InputScalesValid(pointer, pack->scale_factors_)) { |
670 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " | 670 DLOG(ERROR) << "BuildFromDataPack failure! The pack scale factors differ " |
671 << "from those supported by platform."; | 671 << "from those supported by platform."; |
672 return NULL; | 672 return NULL; |
673 } | 673 } |
674 pack->is_valid_ = true; | |
675 return pack; | 674 return pack; |
676 } | 675 } |
677 | 676 |
678 // static | 677 // static |
679 bool BrowserThemePack::IsPersistentImageID(int id) { | 678 bool BrowserThemePack::IsPersistentImageID(int id) { |
680 for (size_t i = 0; i < kPersistingImagesLength; ++i) | 679 for (size_t i = 0; i < kPersistingImagesLength; ++i) |
681 if (kPersistingImages[i].idr_id == id) | 680 if (kPersistingImages[i].idr_id == id) |
682 return true; | 681 return true; |
683 | 682 |
684 return false; | 683 return false; |
685 } | 684 } |
686 | 685 |
687 BrowserThemePack::BrowserThemePack() : CustomThemeSupplier(EXTENSION) { | |
688 scale_factors_ = ui::GetSupportedScaleFactors(); | |
689 // On Windows HiDPI SCALE_FACTOR_100P may not be supported by default. | |
690 if (!base::ContainsValue(scale_factors_, ui::SCALE_FACTOR_100P)) | |
691 scale_factors_.push_back(ui::SCALE_FACTOR_100P); | |
692 } | |
693 | |
694 bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const { | 686 bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const { |
695 // Add resources for each of the property arrays. | 687 // Add resources for each of the property arrays. |
696 RawDataForWriting resources; | 688 RawDataForWriting resources; |
697 resources[kHeaderID] = base::StringPiece( | 689 resources[kHeaderID] = base::StringPiece( |
698 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); | 690 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); |
699 resources[kTintsID] = base::StringPiece( | 691 resources[kTintsID] = base::StringPiece( |
700 reinterpret_cast<const char*>(tints_), | 692 reinterpret_cast<const char*>(tints_), |
701 sizeof(TintEntry[kTintTableLength])); | 693 sizeof(TintEntry[kTintTableLength])); |
702 resources[kColorsID] = base::StringPiece( | 694 resources[kColorsID] = base::StringPiece( |
703 reinterpret_cast<const char*>(colors_), | 695 reinterpret_cast<const char*>(colors_), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 | 764 |
773 return false; | 765 return false; |
774 } | 766 } |
775 | 767 |
776 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) { | 768 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) { |
777 int prs_id = GetPersistentIDByIDR(idr_id); | 769 int prs_id = GetPersistentIDByIDR(idr_id); |
778 if (prs_id == -1) | 770 if (prs_id == -1) |
779 return gfx::Image(); | 771 return gfx::Image(); |
780 | 772 |
781 // Check if the image is cached. | 773 // Check if the image is cached. |
782 ImageCache::const_iterator image_iter = images_.find(prs_id); | 774 ImageCache::const_iterator image_iter = images_on_ui_thread_.find(prs_id); |
783 if (image_iter != images_.end()) | 775 if (image_iter != images_on_ui_thread_.end()) |
784 return image_iter->second; | 776 return image_iter->second; |
785 | 777 |
786 ThemeImagePngSource::PngMap png_map; | 778 ThemeImagePngSource::PngMap png_map; |
787 for (size_t i = 0; i < scale_factors_.size(); ++i) { | 779 for (size_t i = 0; i < scale_factors_.size(); ++i) { |
788 scoped_refptr<base::RefCountedMemory> memory = | 780 scoped_refptr<base::RefCountedMemory> memory = |
789 GetRawData(idr_id, scale_factors_[i]); | 781 GetRawData(idr_id, scale_factors_[i]); |
790 if (memory.get()) | 782 if (memory.get()) |
791 png_map[scale_factors_[i]] = memory; | 783 png_map[scale_factors_[i]] = memory; |
792 } | 784 } |
793 if (!png_map.empty()) { | 785 if (!png_map.empty()) { |
794 gfx::ImageSkia image_skia(new ThemeImagePngSource(png_map), 1.0f); | 786 gfx::ImageSkia image_skia(new ThemeImagePngSource(png_map), 1.0f); |
795 // |image_skia| takes ownership of ThemeImagePngSource. | 787 // |image_skia| takes ownership of ThemeImagePngSource. |
796 gfx::Image ret = gfx::Image(image_skia); | 788 gfx::Image ret = gfx::Image(image_skia); |
797 images_[prs_id] = ret; | 789 images_on_ui_thread_[prs_id] = ret; |
798 return ret; | 790 return ret; |
799 } | 791 } |
800 | 792 |
801 return gfx::Image(); | 793 return gfx::Image(); |
802 } | 794 } |
803 | 795 |
804 base::RefCountedMemory* BrowserThemePack::GetRawData( | 796 base::RefCountedMemory* BrowserThemePack::GetRawData( |
805 int idr_id, | 797 int idr_id, |
806 ui::ScaleFactor scale_factor) const { | 798 ui::ScaleFactor scale_factor) const { |
807 base::RefCountedMemory* memory = NULL; | 799 base::RefCountedMemory* memory = NULL; |
(...skipping 23 matching lines...) Expand all Loading... |
831 for (; *img != -1; ++img) { | 823 for (; *img != -1; ++img) { |
832 if (*img == prs_id) | 824 if (*img == prs_id) |
833 return true; | 825 return true; |
834 } | 826 } |
835 | 827 |
836 return false; | 828 return false; |
837 } | 829 } |
838 | 830 |
839 // private: | 831 // private: |
840 | 832 |
| 833 BrowserThemePack::BrowserThemePack() |
| 834 : CustomThemeSupplier(EXTENSION), |
| 835 header_(NULL), |
| 836 tints_(NULL), |
| 837 colors_(NULL), |
| 838 display_properties_(NULL), |
| 839 source_images_(NULL) { |
| 840 scale_factors_ = ui::GetSupportedScaleFactors(); |
| 841 // On Windows HiDPI SCALE_FACTOR_100P may not be supported by default. |
| 842 if (!base::ContainsValue(scale_factors_, ui::SCALE_FACTOR_100P)) |
| 843 scale_factors_.push_back(ui::SCALE_FACTOR_100P); |
| 844 } |
| 845 |
841 void BrowserThemePack::BuildHeader(const Extension* extension) { | 846 void BrowserThemePack::BuildHeader(const Extension* extension) { |
842 header_ = new BrowserThemePackHeader; | 847 header_ = new BrowserThemePackHeader; |
843 header_->version = kThemePackVersion; | 848 header_->version = kThemePackVersion; |
844 | 849 |
845 // TODO(erg): Need to make this endian safe on other computers. Prerequisite | 850 // TODO(erg): Need to make this endian safe on other computers. Prerequisite |
846 // is that ui::DataPack removes this same check. | 851 // is that ui::DataPack removes this same check. |
847 #if defined(__BYTE_ORDER) | 852 #if defined(__BYTE_ORDER) |
848 // Linux check | 853 // Linux check |
849 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, | 854 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, |
850 "datapack assumes little endian"); | 855 "datapack assumes little endian"); |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 false, | 1430 false, |
1426 &bitmap_data)) { | 1431 &bitmap_data)) { |
1427 NOTREACHED() << "Unable to encode theme image for prs_id=" | 1432 NOTREACHED() << "Unable to encode theme image for prs_id=" |
1428 << prs_id << " for scale_factor=" << scale_factors_[i]; | 1433 << prs_id << " for scale_factor=" << scale_factors_[i]; |
1429 break; | 1434 break; |
1430 } | 1435 } |
1431 image_memory_[scaled_raw_id] = | 1436 image_memory_[scaled_raw_id] = |
1432 base::RefCountedBytes::TakeVector(&bitmap_data); | 1437 base::RefCountedBytes::TakeVector(&bitmap_data); |
1433 } | 1438 } |
1434 } | 1439 } |
OLD | NEW |