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

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

Powered by Google App Engine
This is Rietveld 408576698