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

Side by Side Diff: chrome/browser/themes/browser_theme_pack.cc

Issue 2799003002: Unpack theme data from extensions off of UI thread. (Closed)
Patch Set: fix gtk case Created 3 years, 6 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
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 "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
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 scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromExtension( 549 void BrowserThemePack::BuildFromExtension(
550 const Extension* extension) { 550 const extensions::Extension* extension,
551 DCHECK_CURRENTLY_ON(BrowserThread::UI); 551 scoped_refptr<BrowserThemePack> pack) {
552 DCHECK(extension); 552 DCHECK(extension);
553 DCHECK(extension->is_theme()); 553 DCHECK(extension->is_theme());
554 DCHECK(!pack->is_valid());
554 555
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_on_ui_thread_)) 570 if (!pack->LoadRawBitmapsTo(file_paths, &pack->images_))
571 return NULL; 571 return;
572 572
573 pack->CreateImages(&pack->images_on_ui_thread_); 573 pack->CreateImages(&pack->images_);
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_on_ui_thread_; 577 pack->images_on_file_thread_ = pack->images_;
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_on_ui_thread_| to resample the source 585 // Set ThemeImageSource on |images_| 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_on_ui_thread_.begin(); 590 for (ImageCache::iterator it = pack->images_.begin();
591 it != pack->images_on_ui_thread_.end(); ++it) { 591 it != pack->images_.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 return pack; 606 pack->is_valid_ = true;
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
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;
674 return pack; 675 return pack;
675 } 676 }
676 677
677 // static 678 // static
678 bool BrowserThemePack::IsPersistentImageID(int id) { 679 bool BrowserThemePack::IsPersistentImageID(int id) {
679 for (size_t i = 0; i < kPersistingImagesLength; ++i) 680 for (size_t i = 0; i < kPersistingImagesLength; ++i)
680 if (kPersistingImages[i].idr_id == id) 681 if (kPersistingImages[i].idr_id == id)
681 return true; 682 return true;
682 683
683 return false; 684 return false;
684 } 685 }
685 686
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
686 bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const { 694 bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const {
687 // Add resources for each of the property arrays. 695 // Add resources for each of the property arrays.
688 RawDataForWriting resources; 696 RawDataForWriting resources;
689 resources[kHeaderID] = base::StringPiece( 697 resources[kHeaderID] = base::StringPiece(
690 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader)); 698 reinterpret_cast<const char*>(header_), sizeof(BrowserThemePackHeader));
691 resources[kTintsID] = base::StringPiece( 699 resources[kTintsID] = base::StringPiece(
692 reinterpret_cast<const char*>(tints_), 700 reinterpret_cast<const char*>(tints_),
693 sizeof(TintEntry[kTintTableLength])); 701 sizeof(TintEntry[kTintTableLength]));
694 resources[kColorsID] = base::StringPiece( 702 resources[kColorsID] = base::StringPiece(
695 reinterpret_cast<const char*>(colors_), 703 reinterpret_cast<const char*>(colors_),
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 772
765 return false; 773 return false;
766 } 774 }
767 775
768 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) { 776 gfx::Image BrowserThemePack::GetImageNamed(int idr_id) {
769 int prs_id = GetPersistentIDByIDR(idr_id); 777 int prs_id = GetPersistentIDByIDR(idr_id);
770 if (prs_id == -1) 778 if (prs_id == -1)
771 return gfx::Image(); 779 return gfx::Image();
772 780
773 // Check if the image is cached. 781 // Check if the image is cached.
774 ImageCache::const_iterator image_iter = images_on_ui_thread_.find(prs_id); 782 ImageCache::const_iterator image_iter = images_.find(prs_id);
775 if (image_iter != images_on_ui_thread_.end()) 783 if (image_iter != images_.end())
776 return image_iter->second; 784 return image_iter->second;
777 785
778 ThemeImagePngSource::PngMap png_map; 786 ThemeImagePngSource::PngMap png_map;
779 for (size_t i = 0; i < scale_factors_.size(); ++i) { 787 for (size_t i = 0; i < scale_factors_.size(); ++i) {
780 scoped_refptr<base::RefCountedMemory> memory = 788 scoped_refptr<base::RefCountedMemory> memory =
781 GetRawData(idr_id, scale_factors_[i]); 789 GetRawData(idr_id, scale_factors_[i]);
782 if (memory.get()) 790 if (memory.get())
783 png_map[scale_factors_[i]] = memory; 791 png_map[scale_factors_[i]] = memory;
784 } 792 }
785 if (!png_map.empty()) { 793 if (!png_map.empty()) {
786 gfx::ImageSkia image_skia(new ThemeImagePngSource(png_map), 1.0f); 794 gfx::ImageSkia image_skia(new ThemeImagePngSource(png_map), 1.0f);
787 // |image_skia| takes ownership of ThemeImagePngSource. 795 // |image_skia| takes ownership of ThemeImagePngSource.
788 gfx::Image ret = gfx::Image(image_skia); 796 gfx::Image ret = gfx::Image(image_skia);
789 images_on_ui_thread_[prs_id] = ret; 797 images_[prs_id] = ret;
790 return ret; 798 return ret;
791 } 799 }
792 800
793 return gfx::Image(); 801 return gfx::Image();
794 } 802 }
795 803
796 base::RefCountedMemory* BrowserThemePack::GetRawData( 804 base::RefCountedMemory* BrowserThemePack::GetRawData(
797 int idr_id, 805 int idr_id,
798 ui::ScaleFactor scale_factor) const { 806 ui::ScaleFactor scale_factor) const {
799 base::RefCountedMemory* memory = NULL; 807 base::RefCountedMemory* memory = NULL;
(...skipping 23 matching lines...) Expand all
823 for (; *img != -1; ++img) { 831 for (; *img != -1; ++img) {
824 if (*img == prs_id) 832 if (*img == prs_id)
825 return true; 833 return true;
826 } 834 }
827 835
828 return false; 836 return false;
829 } 837 }
830 838
831 // private: 839 // private:
832 840
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
846 void BrowserThemePack::BuildHeader(const Extension* extension) { 841 void BrowserThemePack::BuildHeader(const Extension* extension) {
847 header_ = new BrowserThemePackHeader; 842 header_ = new BrowserThemePackHeader;
848 header_->version = kThemePackVersion; 843 header_->version = kThemePackVersion;
849 844
850 // TODO(erg): Need to make this endian safe on other computers. Prerequisite 845 // TODO(erg): Need to make this endian safe on other computers. Prerequisite
851 // is that ui::DataPack removes this same check. 846 // is that ui::DataPack removes this same check.
852 #if defined(__BYTE_ORDER) 847 #if defined(__BYTE_ORDER)
853 // Linux check 848 // Linux check
854 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, 849 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN,
855 "datapack assumes little endian"); 850 "datapack assumes little endian");
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 false, 1425 false,
1431 &bitmap_data)) { 1426 &bitmap_data)) {
1432 NOTREACHED() << "Unable to encode theme image for prs_id=" 1427 NOTREACHED() << "Unable to encode theme image for prs_id="
1433 << prs_id << " for scale_factor=" << scale_factors_[i]; 1428 << prs_id << " for scale_factor=" << scale_factors_[i];
1434 break; 1429 break;
1435 } 1430 }
1436 image_memory_[scaled_raw_id] = 1431 image_memory_[scaled_raw_id] =
1437 base::RefCountedBytes::TakeVector(&bitmap_data); 1432 base::RefCountedBytes::TakeVector(&bitmap_data);
1438 } 1433 }
1439 } 1434 }
OLDNEW
« no previous file with comments | « chrome/browser/themes/browser_theme_pack.h ('k') | chrome/browser/themes/browser_theme_pack_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698