Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_BROWSER_THEME_PACK_H_ | |
| 6 #define CHROME_BROWSER_BROWSER_THEME_PACK_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "app/gfx/color_utils.h" | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/scoped_ptr.h" | |
| 14 #include "base/ref_counted.h" | |
| 15 #include "chrome/common/extensions/extension.h" | |
| 16 | |
| 17 class BrowserThemeProviderTest; | |
| 18 namespace base { | |
| 19 class DataPack; | |
| 20 } | |
| 21 class DictionaryValue; | |
| 22 class FilePath; | |
| 23 class RefCountedMemory; | |
| 24 | |
| 25 // An optimized representation of a theme, backed by a mmapped DataPack. | |
| 26 // | |
| 27 // The idea is to pre-process all images (tinting, compositing, etc) at theme | |
| 28 // install time, save all the PNG-ified data into an mmappable file so we don't | |
| 29 // suffer multiple file system access times, therefore solving two of the | |
| 30 // problems with the previous implementation. | |
| 31 // | |
| 32 // A note on const-ness. All public, non-static methods are const. We do this | |
| 33 // because once we've constructed a BrowserThemePack through the | |
| 34 // BuildFromExtension() interface, we WriteToDisk() on a thread other than the | |
| 35 // UI thread that consumes a BrowserThemePack. | |
| 36 class BrowserThemePack : public base::RefCountedThreadSafe<BrowserThemePack> { | |
| 37 public: | |
| 38 ~BrowserThemePack(); | |
| 39 | |
| 40 // Builds the theme pack from all data from |extension|. This is often done | |
| 41 // on a separate thread as it takes so long. Returns true if this is now a | |
| 42 // valid theme pack. | |
| 43 static scoped_refptr<BrowserThemePack> BuildFromExtension( | |
|
tony
2009/12/11 19:14:23
What does it mean to return a scoped_refptr? Shou
Elliot Glaysher
2009/12/11 19:35:38
The same thing as returning any C++ object by valu
| |
| 44 Extension* extension); | |
| 45 | |
| 46 // Builds the theme pack from a previously WriteToDisk(). This operation | |
| 47 // should be relatively fast, as it should be an mmap() and some pointer | |
| 48 // swizzling. | |
| 49 static scoped_refptr<BrowserThemePack> BuildFromDataPack( | |
| 50 FilePath path, const std::string& expected_id); | |
| 51 | |
| 52 // Builds a data pack on disk at |path| for future quick loading by | |
| 53 // BuildFromDataPack(). Often (but not always) called from the file thread; | |
| 54 // implementation should be threadsafe because neither thread will write to | |
| 55 // |image_memory_| and the worker thread will keep a reference to prevent | |
| 56 // destruction. | |
| 57 bool WriteToDisk(FilePath path) const; | |
| 58 | |
| 59 // Returns data from the pack, or the default value if |id| doesn't | |
| 60 // exist. These methods should only be called from the UI thread. (But this | |
| 61 // isn't enforced because of unit tests). | |
| 62 bool GetTint(int id, color_utils::HSL* hsl) const; | |
| 63 bool GetColor(int id, SkColor* color) const; | |
| 64 bool GetDisplayProperty(int id, int* result) const; | |
| 65 SkBitmap* GetBitmapNamed(int id) const; | |
| 66 RefCountedMemory* GetRawData(int id) const; | |
| 67 | |
| 68 // Whether this theme provides an image for |id|. | |
| 69 bool HasCustomImage(int id) const; | |
| 70 | |
| 71 private: | |
| 72 friend class BrowserThemePackTest; | |
| 73 | |
| 74 // Cached images. We cache all retrieved and generated bitmaps and keep | |
| 75 // track of the pointers. We own these and will delete them when we're done | |
| 76 // using them. | |
| 77 typedef std::map<int, SkBitmap*> ImageCache; | |
| 78 | |
| 79 // The raw PNG memory associated with a certain id. | |
| 80 typedef std::map<int, scoped_refptr<RefCountedMemory> > RawImages; | |
| 81 | |
| 82 // Default. Everything is empty. | |
| 83 BrowserThemePack(); | |
| 84 | |
| 85 // Builds a header ready to write to disk. | |
| 86 void BuildHeader(Extension* extension); | |
| 87 | |
| 88 // Transforms the JSON tint values into their final versions in the |tints_| | |
| 89 // array. | |
| 90 void BuildTintsFromJSON(DictionaryValue* tints_value); | |
| 91 | |
| 92 // Transforms the JSON color values into their final versions in the | |
| 93 // |colors_| array and also fills in unspecified colors based on tint values. | |
| 94 void BuildColorsFromJSON(DictionaryValue* color_value); | |
| 95 | |
| 96 // Implementation details of BuildColorsFromJSON(). | |
| 97 void ReadColorsFromJSON(DictionaryValue* colors_value, | |
| 98 std::map<int, SkColor>* temp_colors); | |
| 99 void GenerateMissingColors(std::map<int, SkColor>* temp_colors); | |
| 100 | |
| 101 // Transforms the JSON display properties into |display_properties_|. | |
| 102 void BuildDisplayPropertiesFromJSON(DictionaryValue* display_value); | |
| 103 | |
| 104 // Parses the image names out of an extension. | |
| 105 void ParseImageNamesFromJSON(DictionaryValue* images_value, | |
| 106 FilePath images_path, | |
| 107 std::map<int, FilePath>* file_paths) const; | |
| 108 | |
| 109 // Loads the unmodified bitmaps packed in the extension to SkBitmaps. | |
| 110 void LoadRawBitmapsTo(const std::map<int, FilePath>& file_paths, | |
| 111 ImageCache* raw_bitmaps); | |
| 112 | |
| 113 // Creates tinted and composited frame images. Source and destination is | |
| 114 // |bitmaps|. | |
| 115 void GenerateFrameImages(ImageCache* bitmaps) const; | |
| 116 | |
| 117 // Generates button images tinted with |button_tint| and places them in | |
| 118 // processed_bitmaps. | |
| 119 void GenerateTintedButtons(color_utils::HSL button_tint, | |
| 120 ImageCache* processed_bitmaps) const; | |
| 121 | |
| 122 // Generates the semi-transparent tab background images, putting the results | |
| 123 // in |bitmaps|. Must be called after GenerateFrameImages(). | |
| 124 void GenerateTabBackgroundImages(ImageCache* bitmaps) const; | |
| 125 | |
| 126 // Takes all the SkBitmaps in |image_cache_|, encodes them as PNGs and places | |
| 127 // them in |image_memory_|. | |
| 128 void RepackImageCacheToImageMemory(); | |
| 129 | |
| 130 // Takes all images in |source| and puts them in |destination|, freeing any | |
| 131 // image already in |destination| that |source| would overwrite. | |
| 132 void MergeImageCaches(const ImageCache& source, | |
| 133 ImageCache* destination) const; | |
| 134 | |
| 135 // Retrieves the tint OR the default tint. Unlike the public interface, we | |
| 136 // always need to return a reasonable tint here, instead of partially | |
| 137 // querying if the tint exists. | |
| 138 color_utils::HSL GetTintInternal(int id) const; | |
| 139 | |
| 140 // Data pack, if we have one. | |
| 141 scoped_ptr<base::DataPack> data_pack_; | |
| 142 | |
| 143 // All structs written to disk need to be packed; no alignment tricks here, | |
| 144 // please. | |
| 145 #pragma pack(push,1) | |
| 146 // Header that is written to disk. | |
| 147 struct BrowserThemePackHeader { | |
| 148 // Numeric version to make sure we're compatible in the future. | |
| 149 int32 version; | |
| 150 | |
| 151 // 1 if little_endian. 0 if big_endian. On mismatch, abort load. | |
| 152 int32 little_endian; | |
| 153 | |
| 154 // theme_id without NULL terminator. | |
| 155 uint8 theme_id[16]; | |
| 156 } *header_; | |
| 157 | |
| 158 // The remaining structs represent individual entries in an array. For the | |
| 159 // following three structs, BrowserThemePack will either allocate an array or | |
| 160 // will point directly to mmapped data. | |
| 161 struct TintEntry { | |
| 162 int32 id; | |
| 163 double h; | |
| 164 double s; | |
| 165 double l; | |
| 166 } *tints_; | |
| 167 | |
| 168 struct ColorPair { | |
| 169 int32 id; | |
| 170 SkColor color; | |
| 171 } *colors_; | |
| 172 | |
| 173 struct DisplayPropertyPair { | |
| 174 int32 id; | |
| 175 int32 property; | |
| 176 } *display_properties_; | |
| 177 #pragma pack(pop) | |
| 178 | |
| 179 // References to raw PNG data. This map isn't touched when |data_pack_| is | |
| 180 // non-NULL; |image_memory_| is only filled during BuildFromExtension(). Any | |
| 181 // image data that needs to be written to the DataPack during WriteToDisk() | |
| 182 // needs to be in |image_memory_|. | |
| 183 RawImages image_memory_; | |
| 184 | |
| 185 // Tinted (or otherwise prepared) images for passing out. BrowserThemePack | |
| 186 // owns all these images. This cache isn't touched when we write our data to | |
| 187 // a DataPack. | |
| 188 mutable ImageCache image_cache_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); | |
| 191 }; | |
| 192 | |
| 193 #endif // CHROME_BROWSER_BROWSER_THEME_PACK_H_ | |
| OLD | NEW |