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 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/sequenced_task_runner_helpers.h" | 14 #include "base/sequenced_task_runner_helpers.h" |
| 15 #include "chrome/browser/themes/custom_theme_supplier.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
19 #include "ui/base/layout.h" | 18 #include "ui/base/layout.h" |
20 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
21 #include "ui/gfx/image/image.h" | |
22 | 20 |
23 namespace base { | 21 namespace base { |
24 class DictionaryValue; | 22 class DictionaryValue; |
25 class FilePath; | 23 class FilePath; |
26 class RefCountedMemory; | 24 class RefCountedMemory; |
27 } | 25 } |
28 | 26 |
29 namespace extensions { | 27 namespace extensions { |
30 class Extensions; | 28 class Extensions; |
31 } | 29 } |
32 | 30 |
| 31 namespace gfx { |
| 32 class Image; |
| 33 } |
| 34 |
33 namespace ui { | 35 namespace ui { |
34 class DataPack; | 36 class DataPack; |
35 } | 37 } |
36 | 38 |
37 // An optimized representation of a theme, backed by a mmapped DataPack. | 39 // An optimized representation of a theme, backed by a mmapped DataPack. |
38 // | 40 // |
39 // The idea is to pre-process all images (tinting, compositing, etc) at theme | 41 // The idea is to pre-process all images (tinting, compositing, etc) at theme |
40 // install time, save all the PNG-ified data into an mmappable file so we don't | 42 // install time, save all the PNG-ified data into an mmappable file so we don't |
41 // suffer multiple file system access times, therefore solving two of the | 43 // suffer multiple file system access times, therefore solving two of the |
42 // problems with the previous implementation. | 44 // problems with the previous implementation. |
43 // | 45 // |
44 // A note on const-ness. All public, non-static methods are const. We do this | 46 // A note on const-ness. All public, non-static methods are const. We do this |
45 // because once we've constructed a BrowserThemePack through the | 47 // because once we've constructed a BrowserThemePack through the |
46 // BuildFromExtension() interface, we WriteToDisk() on a thread other than the | 48 // BuildFromExtension() interface, we WriteToDisk() on a thread other than the |
47 // UI thread that consumes a BrowserThemePack. There is no locking; thread | 49 // UI thread that consumes a BrowserThemePack. There is no locking; thread |
48 // safety between the writing thread and the UI thread is ensured by having the | 50 // safety between the writing thread and the UI thread is ensured by having the |
49 // data be immutable. | 51 // data be immutable. |
50 // | 52 // |
51 // BrowserThemePacks are always deleted on the file thread because in the | 53 // BrowserThemePacks are always deleted on the file thread because in the |
52 // common case, they are backed by mmapped data and the unmmapping operation | 54 // common case, they are backed by mmapped data and the unmmapping operation |
53 // will trip our IO on the UI thread detector. | 55 // will trip our IO on the UI thread detector. |
54 class BrowserThemePack : public base::RefCountedThreadSafe< | 56 class BrowserThemePack : public CustomThemeSupplier { |
55 BrowserThemePack, content::BrowserThread::DeleteOnFileThread> { | |
56 public: | 57 public: |
57 // Builds the theme pack from all data from |extension|. This is often done | 58 // Builds the theme pack from all data from |extension|. This is often done |
58 // on a separate thread as it takes so long. This can fail and return NULL in | 59 // on a separate thread as it takes so long. This can fail and return NULL in |
59 // the case where the theme has invalid data. | 60 // the case where the theme has invalid data. |
60 static scoped_refptr<BrowserThemePack> BuildFromExtension( | 61 static scoped_refptr<BrowserThemePack> BuildFromExtension( |
61 const extensions::Extension* extension); | 62 const extensions::Extension* extension); |
62 | 63 |
63 // Builds the theme pack from a previously performed WriteToDisk(). This | 64 // Builds the theme pack from a previously performed WriteToDisk(). This |
64 // operation should be relatively fast, as it should be an mmap() and some | 65 // operation should be relatively fast, as it should be an mmap() and some |
65 // pointer swizzling. Returns NULL on any error attempting to read |path|. | 66 // pointer swizzling. Returns NULL on any error attempting to read |path|. |
66 static scoped_refptr<BrowserThemePack> BuildFromDataPack( | 67 static scoped_refptr<BrowserThemePack> BuildFromDataPack( |
67 const base::FilePath& path, const std::string& expected_id); | 68 const base::FilePath& path, const std::string& expected_id); |
68 | 69 |
| 70 // Returns the set of image IDRs which can be overwritten by a user provided |
| 71 // theme. |
| 72 static void GetThemeableImageIDRs(std::set<int>* result); |
| 73 |
69 // Builds a data pack on disk at |path| for future quick loading by | 74 // Builds a data pack on disk at |path| for future quick loading by |
70 // BuildFromDataPack(). Often (but not always) called from the file thread; | 75 // BuildFromDataPack(). Often (but not always) called from the file thread; |
71 // implementation should be threadsafe because neither thread will write to | 76 // implementation should be threadsafe because neither thread will write to |
72 // |image_memory_| and the worker thread will keep a reference to prevent | 77 // |image_memory_| and the worker thread will keep a reference to prevent |
73 // destruction. | 78 // destruction. |
74 bool WriteToDisk(const base::FilePath& path) const; | 79 bool WriteToDisk(const base::FilePath& path) const; |
75 | 80 |
76 // If this theme specifies data for the corresponding |id|, return true and | 81 // Overridden from CustomThemeSupplier: |
77 // write the corresponding value to the output parameter. These functions | 82 virtual bool GetTint(int id, color_utils::HSL* hsl) const OVERRIDE; |
78 // don't return the default data. These methods should only be called from | 83 virtual bool GetColor(int id, SkColor* color) const OVERRIDE; |
79 // the UI thread. (But this isn't enforced because of unit tests). | 84 virtual bool GetDisplayProperty(int id, int* result) const OVERRIDE; |
80 bool GetTint(int id, color_utils::HSL* hsl) const; | 85 virtual gfx::Image GetImageNamed(int id) OVERRIDE; |
81 bool GetColor(int id, SkColor* color) const; | 86 virtual base::RefCountedMemory* GetRawData( |
82 bool GetDisplayProperty(int id, int* result) const; | 87 int id, ui::ScaleFactor scale_factor) const OVERRIDE; |
83 | 88 virtual bool HasCustomImage(int id) const OVERRIDE; |
84 // Returns the theme pack image for |id|. Returns an empty image if an image | |
85 // is not found. | |
86 gfx::Image GetImageNamed(int id); | |
87 | |
88 // Returns the raw PNG encoded data for IDR_THEME_NTP_*. This method is only | |
89 // supposed to work for the NTP attribution and background resources. | |
90 base::RefCountedMemory* GetRawData(int id, | |
91 ui::ScaleFactor scale_factor) const; | |
92 | |
93 // Returns the set of image idrs which can be overwritten by a user provided | |
94 // theme. | |
95 static void GetThemeableImageIDRs(std::set<int>* result); | |
96 | |
97 // Whether this theme provides an image for |id|. | |
98 bool HasCustomImage(int id) const; | |
99 | 89 |
100 private: | 90 private: |
101 friend struct content::BrowserThread::DeleteOnThread< | |
102 content::BrowserThread::FILE>; | |
103 friend class base::DeleteHelper<BrowserThemePack>; | |
104 friend class BrowserThemePackTest; | 91 friend class BrowserThemePackTest; |
105 | 92 |
106 // Cached images. | 93 // Cached images. |
107 typedef std::map<int, gfx::Image> ImageCache; | 94 typedef std::map<int, gfx::Image> ImageCache; |
108 | 95 |
109 // The raw PNG memory associated with a certain id. | 96 // The raw PNG memory associated with a certain id. |
110 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; | 97 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; |
111 | 98 |
112 // The type passed to ui::DataPack::WritePack. | 99 // The type passed to ui::DataPack::WritePack. |
113 typedef std::map<uint16, base::StringPiece> RawDataForWriting; | 100 typedef std::map<uint16, base::StringPiece> RawDataForWriting; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 // Cache of images created in BuildFromExtension(). Once the theme pack is | 268 // Cache of images created in BuildFromExtension(). Once the theme pack is |
282 // created, this cache should only be accessed on the file thread. There | 269 // created, this cache should only be accessed on the file thread. There |
283 // should be no IDs in |image_memory_| that are in |images_on_file_thread_| | 270 // should be no IDs in |image_memory_| that are in |images_on_file_thread_| |
284 // or vice versa. | 271 // or vice versa. |
285 ImageCache images_on_file_thread_; | 272 ImageCache images_on_file_thread_; |
286 | 273 |
287 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); | 274 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); |
288 }; | 275 }; |
289 | 276 |
290 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ | 277 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ |
OLD | NEW |