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. | |
42 static BrowserThemePack* BuildFromExtension(Extension* extension); | |
43 | |
44 // Builds the theme pack from a previously WriteToDisk(). This operation | |
Miranda Callahan
2009/12/14 20:47:51
"from a previous WriteToDisk() operation", or "fro
| |
45 // should be relatively fast, as it should be an mmap() and some pointer | |
46 // swizzling. Returns NULL on any error attempting to read |path|. | |
47 static scoped_refptr<BrowserThemePack> BuildFromDataPack( | |
48 FilePath path, const std::string& expected_id); | |
49 | |
50 // Builds a data pack on disk at |path| for future quick loading by | |
51 // BuildFromDataPack(). Often (but not always) called from the file thread; | |
52 // implementation should be threadsafe because neither thread will write to | |
53 // |image_memory_| and the worker thread will keep a reference to prevent | |
54 // destruction. | |
55 bool WriteToDisk(FilePath path) const; | |
56 | |
57 // Returns data from the pack, or the default value if |id| doesn't | |
58 // exist. These methods should only be called from the UI thread. (But this | |
59 // isn't enforced because of unit tests). | |
60 bool GetTint(int id, color_utils::HSL* hsl) const; | |
61 bool GetColor(int id, SkColor* color) const; | |
62 bool GetDisplayProperty(int id, int* result) const; | |
63 SkBitmap* GetBitmapNamed(int id) const; | |
64 RefCountedMemory* GetRawData(int id) const; | |
65 | |
66 // Whether this theme provides an image for |id|. | |
67 bool HasCustomImage(int id) const; | |
68 | |
69 private: | |
70 friend class BrowserThemePackTest; | |
71 | |
72 // Cached images. We cache all retrieved and generated bitmaps and keep | |
73 // track of the pointers. We own these and will delete them when we're done | |
74 // using them. | |
75 typedef std::map<int, SkBitmap*> ImageCache; | |
76 | |
77 // The raw PNG memory associated with a certain id. | |
78 typedef std::map<int, scoped_refptr<RefCountedMemory> > RawImages; | |
79 | |
80 // Default. Everything is empty. | |
81 BrowserThemePack(); | |
82 | |
83 // Builds a header ready to write to disk. | |
84 void BuildHeader(Extension* extension); | |
85 | |
86 // Transforms the JSON tint values into their final versions in the |tints_| | |
87 // array. | |
88 void BuildTintsFromJSON(DictionaryValue* tints_value); | |
89 | |
90 // Transforms the JSON color values into their final versions in the | |
91 // |colors_| array and also fills in unspecified colors based on tint values. | |
92 void BuildColorsFromJSON(DictionaryValue* color_value); | |
93 | |
94 // Implementation details of BuildColorsFromJSON(). | |
95 void ReadColorsFromJSON(DictionaryValue* colors_value, | |
96 std::map<int, SkColor>* temp_colors); | |
97 void GenerateMissingColors(std::map<int, SkColor>* temp_colors); | |
98 | |
99 // Transforms the JSON display properties into |display_properties_|. | |
100 void BuildDisplayPropertiesFromJSON(DictionaryValue* display_value); | |
101 | |
102 // Parses the image names out of an extension. | |
103 void ParseImageNamesFromJSON(DictionaryValue* images_value, | |
104 FilePath images_path, | |
105 std::map<int, FilePath>* file_paths) const; | |
106 | |
107 // Loads the unmodified bitmaps packed in the extension to SkBitmaps. | |
108 void LoadRawBitmapsTo(const std::map<int, FilePath>& file_paths, | |
109 ImageCache* raw_bitmaps); | |
110 | |
111 // Creates tinted and composited frame images. Source and destination is | |
112 // |bitmaps|. | |
113 void GenerateFrameImages(ImageCache* bitmaps) const; | |
114 | |
115 // Generates button images tinted with |button_tint| and places them in | |
116 // processed_bitmaps. | |
117 void GenerateTintedButtons(color_utils::HSL button_tint, | |
118 ImageCache* processed_bitmaps) const; | |
119 | |
120 // Generates the semi-transparent tab background images, putting the results | |
121 // in |bitmaps|. Must be called after GenerateFrameImages(). | |
122 void GenerateTabBackgroundImages(ImageCache* bitmaps) const; | |
123 | |
124 // Takes all the SkBitmaps in |image_cache_|, encodes them as PNGs and places | |
125 // them in |image_memory_|. | |
126 void RepackImageCacheToImageMemory(); | |
127 | |
128 // Takes all images in |source| and puts them in |destination|, freeing any | |
129 // image already in |destination| that |source| would overwrite. | |
130 void MergeImageCaches(const ImageCache& source, | |
131 ImageCache* destination) const; | |
132 | |
133 // Retrieves the tint OR the default tint. Unlike the public interface, we | |
134 // always need to return a reasonable tint here, instead of partially | |
135 // querying if the tint exists. | |
136 color_utils::HSL GetTintInternal(int id) const; | |
137 | |
138 // Data pack, if we have one. | |
139 scoped_ptr<base::DataPack> data_pack_; | |
140 | |
141 // All structs written to disk need to be packed; no alignment tricks here, | |
142 // please. | |
143 #pragma pack(push,1) | |
144 // Header that is written to disk. | |
145 struct BrowserThemePackHeader { | |
146 // Numeric version to make sure we're compatible in the future. | |
147 int32 version; | |
148 | |
149 // 1 if little_endian. 0 if big_endian. On mismatch, abort load. | |
150 int32 little_endian; | |
151 | |
152 // theme_id without NULL terminator. | |
153 uint8 theme_id[16]; | |
154 } *header_; | |
155 | |
156 // The remaining structs represent individual entries in an array. For the | |
157 // following three structs, BrowserThemePack will either allocate an array or | |
158 // will point directly to mmapped data. | |
159 struct TintEntry { | |
160 int32 id; | |
161 double h; | |
162 double s; | |
163 double l; | |
164 } *tints_; | |
165 | |
166 struct ColorPair { | |
167 int32 id; | |
168 SkColor color; | |
169 } *colors_; | |
170 | |
171 struct DisplayPropertyPair { | |
172 int32 id; | |
173 int32 property; | |
174 } *display_properties_; | |
175 #pragma pack(pop) | |
176 | |
177 // References to raw PNG data. This map isn't touched when |data_pack_| is | |
178 // non-NULL; |image_memory_| is only filled during BuildFromExtension(). Any | |
179 // image data that needs to be written to the DataPack during WriteToDisk() | |
180 // needs to be in |image_memory_|. | |
181 RawImages image_memory_; | |
182 | |
183 // Tinted (or otherwise prepared) images for passing out. BrowserThemePack | |
184 // owns all these images. This cache isn't touched when we write our data to | |
185 // a DataPack. | |
186 mutable ImageCache image_cache_; | |
187 | |
188 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); | |
189 }; | |
190 | |
191 #endif // CHROME_BROWSER_BROWSER_THEME_PACK_H_ | |
OLD | NEW |