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 #ifndef CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ | 5 #ifndef CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ |
6 #define CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ | 6 #define CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/sequenced_task_runner_helpers.h" | 15 #include "base/sequenced_task_runner_helpers.h" |
15 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "ui/base/layout.h" |
17 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
18 | 20 |
19 class FilePath; | 21 class FilePath; |
20 | 22 |
21 namespace base { | 23 namespace base { |
22 class DictionaryValue; | 24 class DictionaryValue; |
23 class RefCountedMemory; | 25 class RefCountedMemory; |
24 } | 26 } |
25 | 27 |
26 namespace extensions { | 28 namespace extensions { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // that this is separate from HasCustomImage() which returns whether a custom | 88 // that this is separate from HasCustomImage() which returns whether a custom |
87 // image |id| was included in the unprocessed theme and is used as a proxy | 89 // image |id| was included in the unprocessed theme and is used as a proxy |
88 // for making layout decisions in the interface. | 90 // for making layout decisions in the interface. |
89 SkBitmap* GetBitmapNamed(int id) const; | 91 SkBitmap* GetBitmapNamed(int id) const; |
90 | 92 |
91 // Returns an image if we have a custom image for |id|, otherwise NULL. | 93 // Returns an image if we have a custom image for |id|, otherwise NULL. |
92 const gfx::Image* GetImageNamed(int id) const; | 94 const gfx::Image* GetImageNamed(int id) const; |
93 | 95 |
94 // Returns the raw PNG encoded data for IDR_THEME_NTP_*. This method is only | 96 // Returns the raw PNG encoded data for IDR_THEME_NTP_*. This method is only |
95 // supposed to work for the NTP attribution and background resources. | 97 // supposed to work for the NTP attribution and background resources. |
96 base::RefCountedMemory* GetRawData(int id) const; | 98 base::RefCountedMemory* GetRawData(int id, |
| 99 ui::ScaleFactor scale_factor) const; |
97 | 100 |
98 // Whether this theme provides an image for |id|. | 101 // Whether this theme provides an image for |id|. |
99 bool HasCustomImage(int id) const; | 102 bool HasCustomImage(int id) const; |
100 | 103 |
101 private: | 104 private: |
102 friend struct content::BrowserThread::DeleteOnThread< | 105 friend struct content::BrowserThread::DeleteOnThread< |
103 content::BrowserThread::FILE>; | 106 content::BrowserThread::FILE>; |
104 friend class base::DeleteHelper<BrowserThemePack>; | 107 friend class base::DeleteHelper<BrowserThemePack>; |
105 friend class BrowserThemePackTest; | 108 friend class BrowserThemePackTest; |
106 | 109 |
107 // Cached images. We cache all retrieved and generated bitmaps and keep | 110 // Cached images. We cache all retrieved and generated images and keep |
108 // track of the pointers. We own these and will delete them when we're done | 111 // track of the pointers. We own these and will delete them when we're done |
109 // using them. | 112 // using them. |
110 typedef std::map<int, const gfx::Image*> ImageCache; | 113 typedef std::map<int, const gfx::Image*> ImageCache; |
111 | 114 |
112 // The raw PNG memory associated with a certain id. | 115 // The raw PNG memory associated with a certain id. |
113 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; | 116 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; |
114 | 117 |
115 // The type passed to ui::DataPack::WritePack. | 118 // The type passed to ui::DataPack::WritePack. |
116 typedef std::map<uint16, base::StringPiece> RawDataForWriting; | 119 typedef std::map<uint16, base::StringPiece> RawDataForWriting; |
117 | 120 |
(...skipping 25 matching lines...) Expand all Loading... |
143 void BuildDisplayPropertiesFromJSON(base::DictionaryValue* display_value); | 146 void BuildDisplayPropertiesFromJSON(base::DictionaryValue* display_value); |
144 | 147 |
145 // Parses the image names out of an extension. | 148 // Parses the image names out of an extension. |
146 void ParseImageNamesFromJSON(base::DictionaryValue* images_value, | 149 void ParseImageNamesFromJSON(base::DictionaryValue* images_value, |
147 const FilePath& images_path, | 150 const FilePath& images_path, |
148 FilePathMap* file_paths) const; | 151 FilePathMap* file_paths) const; |
149 | 152 |
150 // Creates the data for |source_images_| from |file_paths|. | 153 // Creates the data for |source_images_| from |file_paths|. |
151 void BuildSourceImagesArray(const FilePathMap& file_paths); | 154 void BuildSourceImagesArray(const FilePathMap& file_paths); |
152 | 155 |
153 // Loads the unmodified bitmaps packed in the extension to SkBitmaps. Returns | 156 // Loads the unmodified images packed in the extension to SkBitmaps. Returns |
154 // true if all images loaded. | 157 // true if all images loaded. |
155 bool LoadRawBitmapsTo(const FilePathMap& file_paths, | 158 bool LoadRawBitmapsTo(const FilePathMap& file_paths, |
156 ImageCache* raw_bitmaps); | 159 ImageCache* image_cache); |
| 160 |
| 161 // Populate |images| cache with empty gfx::Images. Image reps are lazily |
| 162 // generated when an image rep is requested via ImageSkia::GetRepresentation. |
| 163 // Source and destination is |images|. |
| 164 void CreateImages(ImageCache* images) const; |
157 | 165 |
158 // Creates tinted and composited frame images. Source and destination is | 166 // Creates tinted and composited frame images. Source and destination is |
159 // |bitmaps|. | 167 // |images|. |
160 void GenerateFrameImages(ImageCache* bitmaps) const; | 168 void CreateFrameImages(ImageCache* images) const; |
161 | 169 |
162 // Generates button images tinted with |button_tint| and places them in | 170 // Creates button images tinted with |button_tint| and places them in |
163 // processed_bitmaps. | 171 // processed_images. |
164 void GenerateTintedButtons(const color_utils::HSL& button_tint, | 172 void CreateTintedButtons(const color_utils::HSL& button_tint, |
165 ImageCache* processed_bitmaps) const; | 173 ImageCache* processed_images) const; |
166 | 174 |
167 // Generates the semi-transparent tab background images, putting the results | 175 // Creates the semi-transparent tab background images, putting the results |
168 // in |bitmaps|. Must be called after GenerateFrameImages(). | 176 // in |images|. Must be called after GenerateFrameImages(). |
169 void GenerateTabBackgroundImages(ImageCache* bitmaps) const; | 177 void CreateTabBackgroundImages(ImageCache* images) const; |
170 | 178 |
171 // Takes all the SkBitmaps in |images|, encodes them as PNGs and places | 179 // Takes all the SkBitmaps in |images|, encodes them as PNGs and places |
172 // them in |reencoded_images|. | 180 // them in |reencoded_images|. |
173 void RepackImages(const ImageCache& images, | 181 void RepackImages(const ImageCache& images, |
174 RawImages* reencoded_images) const; | 182 RawImages* reencoded_images) const; |
175 | 183 |
| 184 // Generates image reps for |scale_factors| for |
| 185 // |prepared_images_on_ui_thread_| and |prepared_images_on_file_thread_|. |
| 186 void GenerateImageReps(const std::vector<ui::ScaleFactor>& scale_factors); |
| 187 |
176 // Takes all images in |source| and puts them in |destination|, freeing any | 188 // Takes all images in |source| and puts them in |destination|, freeing any |
177 // image already in |destination| that |source| would overwrite. | 189 // image already in |destination| that |source| would overwrite. |
178 void MergeImageCaches(const ImageCache& source, | 190 void MergeImageCaches(const ImageCache& source, |
179 ImageCache* destination) const; | 191 ImageCache* destination) const; |
180 | 192 |
| 193 // Copies images from |source| to |destination| such that the lifetimes of |
| 194 // the images in |destination| are not affected by the lifetimes of the |
| 195 // images in |source|. |
| 196 void CopyImagesTo(const ImageCache& source, ImageCache* destination) const; |
| 197 |
181 // Changes the RefCountedMemory based |images| into StringPiece data in |out|. | 198 // Changes the RefCountedMemory based |images| into StringPiece data in |out|. |
182 void AddRawImagesTo(const RawImages& images, RawDataForWriting* out) const; | 199 void AddRawImagesTo(const RawImages& images, RawDataForWriting* out) const; |
183 | 200 |
184 // Retrieves the tint OR the default tint. Unlike the public interface, we | 201 // Retrieves the tint OR the default tint. Unlike the public interface, we |
185 // always need to return a reasonable tint here, instead of partially | 202 // always need to return a reasonable tint here, instead of partially |
186 // querying if the tint exists. | 203 // querying if the tint exists. |
187 color_utils::HSL GetTintInternal(int id) const; | 204 color_utils::HSL GetTintInternal(int id) const; |
188 | 205 |
| 206 // Returns a unique id to use to store the raw bitmap for |prs_id| at |
| 207 // |scale_factor| in memory. |
| 208 int GetRawIDByPersistentID(int prs_id, ui::ScaleFactor scale_factor) const; |
| 209 |
189 // Data pack, if we have one. | 210 // Data pack, if we have one. |
190 scoped_ptr<ui::DataPack> data_pack_; | 211 scoped_ptr<ui::DataPack> data_pack_; |
191 | 212 |
192 // All structs written to disk need to be packed; no alignment tricks here, | 213 // All structs written to disk need to be packed; no alignment tricks here, |
193 // please. | 214 // please. |
194 #pragma pack(push,1) | 215 #pragma pack(push,1) |
195 // Header that is written to disk. | 216 // Header that is written to disk. |
196 struct BrowserThemePackHeader { | 217 struct BrowserThemePackHeader { |
197 // Numeric version to make sure we're compatible in the future. | 218 // Numeric version to make sure we're compatible in the future. |
198 int32 version; | 219 int32 version; |
(...skipping 23 matching lines...) Expand all Loading... |
222 struct DisplayPropertyPair { | 243 struct DisplayPropertyPair { |
223 int32 id; | 244 int32 id; |
224 int32 property; | 245 int32 property; |
225 } *display_properties_; | 246 } *display_properties_; |
226 | 247 |
227 // A list of included source images. A pointer to a -1 terminated array of | 248 // A list of included source images. A pointer to a -1 terminated array of |
228 // our persistent IDs. | 249 // our persistent IDs. |
229 int* source_images_; | 250 int* source_images_; |
230 #pragma pack(pop) | 251 #pragma pack(pop) |
231 | 252 |
| 253 // The scale factors represented by the images in the theme pack. |
| 254 std::vector<ui::ScaleFactor> scale_factors_; |
| 255 |
232 // References to raw PNG data. This map isn't touched when |data_pack_| is | 256 // References to raw PNG data. This map isn't touched when |data_pack_| is |
233 // non-NULL; |image_memory_| is only filled during BuildFromExtension(). Any | 257 // non-NULL; |image_memory_| is only filled during BuildFromExtension(). Any |
234 // image data that needs to be written to the DataPack during WriteToDisk() | 258 // image data that needs to be written to the DataPack during WriteToDisk() |
235 // needs to be in |image_memory_|. | 259 // needs to be in |image_memory_|. |
236 RawImages image_memory_; | 260 RawImages image_memory_; |
237 | 261 |
238 // An immutable cache of images generated in BuildFromExtension(). When this | 262 // Loaded images. These are loaded from |image_memory_|, from |data_pack_|, |
239 // BrowserThemePack is generated from BuildFromDataPack(), this cache is | 263 // and by BuildFromExtension(). These images should only be accessed on the UI |
240 // empty. We separate the images from the images loaded from disk so that | 264 // thread. |
241 // WriteToDisk()'s implementation doesn't need locks. There should be no IDs | 265 mutable ImageCache images_on_ui_thread_; |
242 // in |image_memory_| that are in |prepared_images_| or vice versa. | |
243 ImageCache prepared_images_; | |
244 | 266 |
245 // Loaded images. These are loaded from |image_memory_| or the |data_pack_|. | 267 // Cache of images created in BuildFromExtension(). Once the theme pack is |
246 mutable ImageCache loaded_images_; | 268 // created, this cache should only be accessed on the file thread. There |
| 269 // should be no IDs in |image_memory_| that are in |images_on_file_thread_| |
| 270 // or vice versa. |
| 271 ImageCache images_on_file_thread_; |
247 | 272 |
248 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); | 273 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); |
249 }; | 274 }; |
250 | 275 |
251 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ | 276 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ |
OLD | NEW |