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_provider.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" | 20 #include "ui/gfx/image/image.h" |
22 | 21 |
23 namespace base { | 22 namespace base { |
24 class DictionaryValue; | 23 class DictionaryValue; |
25 class FilePath; | 24 class FilePath; |
26 class RefCountedMemory; | 25 class RefCountedMemory; |
27 } | 26 } |
(...skipping 16 matching lines...) Expand all Loading... |
44 // A note on const-ness. All public, non-static methods are const. We do this | 43 // 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 | 44 // because once we've constructed a BrowserThemePack through the |
46 // BuildFromExtension() interface, we WriteToDisk() on a thread other than the | 45 // BuildFromExtension() interface, we WriteToDisk() on a thread other than the |
47 // UI thread that consumes a BrowserThemePack. There is no locking; thread | 46 // 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 | 47 // safety between the writing thread and the UI thread is ensured by having the |
49 // data be immutable. | 48 // data be immutable. |
50 // | 49 // |
51 // BrowserThemePacks are always deleted on the file thread because in the | 50 // BrowserThemePacks are always deleted on the file thread because in the |
52 // common case, they are backed by mmapped data and the unmmapping operation | 51 // common case, they are backed by mmapped data and the unmmapping operation |
53 // will trip our IO on the UI thread detector. | 52 // will trip our IO on the UI thread detector. |
54 class BrowserThemePack : public base::RefCountedThreadSafe< | 53 class BrowserThemePack : public CustomThemeProvider { |
55 BrowserThemePack, content::BrowserThread::DeleteOnFileThread> { | |
56 public: | 54 public: |
57 // Builds the theme pack from all data from |extension|. This is often done | 55 // 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 | 56 // 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. | 57 // the case where the theme has invalid data. |
60 static scoped_refptr<BrowserThemePack> BuildFromExtension( | 58 static scoped_refptr<BrowserThemePack> BuildFromExtension( |
61 const extensions::Extension* extension); | 59 const extensions::Extension* extension); |
62 | 60 |
63 // Builds the theme pack from a previously performed WriteToDisk(). This | 61 // Builds the theme pack from a previously performed WriteToDisk(). This |
64 // operation should be relatively fast, as it should be an mmap() and some | 62 // 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|. | 63 // pointer swizzling. Returns NULL on any error attempting to read |path|. |
66 static scoped_refptr<BrowserThemePack> BuildFromDataPack( | 64 static scoped_refptr<BrowserThemePack> BuildFromDataPack( |
67 const base::FilePath& path, const std::string& expected_id); | 65 const base::FilePath& path, const std::string& expected_id); |
68 | 66 |
| 67 // Returns the set of image idrs which can be overwritten by a user provided |
| 68 // theme. |
| 69 static void GetThemeableImageIDRs(std::set<int>* result); |
| 70 |
69 // Builds a data pack on disk at |path| for future quick loading by | 71 // Builds a data pack on disk at |path| for future quick loading by |
70 // BuildFromDataPack(). Often (but not always) called from the file thread; | 72 // BuildFromDataPack(). Often (but not always) called from the file thread; |
71 // implementation should be threadsafe because neither thread will write to | 73 // implementation should be threadsafe because neither thread will write to |
72 // |image_memory_| and the worker thread will keep a reference to prevent | 74 // |image_memory_| and the worker thread will keep a reference to prevent |
73 // destruction. | 75 // destruction. |
74 bool WriteToDisk(const base::FilePath& path) const; | 76 bool WriteToDisk(const base::FilePath& path) const; |
75 | 77 |
76 // If this theme specifies data for the corresponding |id|, return true and | 78 // Overridden from CustomThemeProvider: |
77 // write the corresponding value to the output parameter. These functions | 79 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 | 80 virtual bool GetColor(int id, SkColor* color) const OVERRIDE; |
79 // the UI thread. (But this isn't enforced because of unit tests). | 81 virtual bool GetDisplayProperty(int id, int* result) const OVERRIDE; |
80 bool GetTint(int id, color_utils::HSL* hsl) const; | 82 virtual gfx::Image GetImageNamed(int id) OVERRIDE; |
81 bool GetColor(int id, SkColor* color) const; | 83 virtual base::RefCountedMemory* GetRawData( |
82 bool GetDisplayProperty(int id, int* result) const; | 84 int id, ui::ScaleFactor scale_factor) const OVERRIDE; |
83 | 85 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 | 86 |
100 private: | 87 private: |
101 friend struct content::BrowserThread::DeleteOnThread< | |
102 content::BrowserThread::FILE>; | |
103 friend class base::DeleteHelper<BrowserThemePack>; | |
104 friend class BrowserThemePackTest; | 88 friend class BrowserThemePackTest; |
105 | 89 |
106 // Cached images. | 90 // Cached images. |
107 typedef std::map<int, gfx::Image> ImageCache; | 91 typedef std::map<int, gfx::Image> ImageCache; |
108 | 92 |
109 // The raw PNG memory associated with a certain id. | 93 // The raw PNG memory associated with a certain id. |
110 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; | 94 typedef std::map<int, scoped_refptr<base::RefCountedMemory> > RawImages; |
111 | 95 |
112 // The type passed to ui::DataPack::WritePack. | 96 // The type passed to ui::DataPack::WritePack. |
113 typedef std::map<uint16, base::StringPiece> RawDataForWriting; | 97 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 | 265 // Cache of images created in BuildFromExtension(). Once the theme pack is |
282 // created, this cache should only be accessed on the file thread. There | 266 // 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_| | 267 // should be no IDs in |image_memory_| that are in |images_on_file_thread_| |
284 // or vice versa. | 268 // or vice versa. |
285 ImageCache images_on_file_thread_; | 269 ImageCache images_on_file_thread_; |
286 | 270 |
287 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); | 271 DISALLOW_COPY_AND_ASSIGN(BrowserThemePack); |
288 }; | 272 }; |
289 | 273 |
290 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ | 274 #endif // CHROME_BROWSER_THEMES_BROWSER_THEME_PACK_H_ |
OLD | NEW |