OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ | 5 #ifndef EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ |
6 #define EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ | 6 #define EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/observer_list.h" |
13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
15 #include "extensions/common/extension_icon_set.h" | 16 #include "extensions/common/extension_icon_set.h" |
16 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
17 #include "ui/gfx/image/image_skia.h" | 18 #include "ui/gfx/image/image_skia.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 class BrowserContext; | 21 class BrowserContext; |
21 } | 22 } |
22 | 23 |
23 namespace extensions { | 24 namespace extensions { |
24 class Extension; | 25 class Extension; |
25 } | 26 } |
26 | 27 |
27 namespace gfx { | 28 namespace gfx { |
28 class Size; | 29 class Size; |
29 class Image; | 30 class Image; |
30 } | 31 } |
31 | 32 |
32 namespace extensions { | 33 namespace extensions { |
33 | 34 |
34 // A class that provides an ImageSkia for UI code to use. It handles extension | 35 // A class that provides an ImageSkia for UI code to use. It handles extension |
35 // icon resource loading, screen scale factor change etc. UI code that uses | 36 // icon resource loading, screen scale factor change etc. UI code that uses |
36 // extension icon should host this class and be its observer. ExtensionIconImage | 37 // extension icon should host this class. In painting code, UI code paints with |
37 // should be outlived by the observer. In painting code, UI code paints with the | 38 // the ImageSkia provided by this class. If the required extension icon resource |
38 // ImageSkia provided by this class. If required extension icon resource is not | 39 // is not already present, this class tries to load it and calls its observer |
39 // already present, this class tries to load it and calls its observer interface | 40 // interface when the image get updated. Until the resource is loaded, the UI |
40 // when the image get updated. Until the resource is loaded, the UI code will be | 41 // code will be provided with a blank, transparent image. |
41 // provided with blank, transparent image. | |
42 // If the requested resource doesn't exist or can't be loaded and a default | 42 // If the requested resource doesn't exist or can't be loaded and a default |
43 // icon was supplied in the constructor, icon image will be updated with the | 43 // icon was supplied in the constructor, icon image will be updated with the |
44 // default icon's resource. | 44 // default icon's resource. |
45 // The default icon doesn't need to be supplied, but in that case, icon image | 45 // The default icon doesn't need to be supplied, but in that case, icon image |
46 // representation will be left blank if the resource loading fails. | 46 // representation will be left blank if the resource loading fails. |
47 // If default icon is supplied, it is assumed that it contains or can | 47 // If default icon is supplied, it is assumed that it contains or can |
48 // synchronously create (when |GetRepresentation| is called on it) | 48 // synchronously create (when |GetRepresentation| is called on it) |
49 // representations for all the scale factors supported by the current platform. | 49 // representations for all the scale factors supported by the current platform. |
50 // Note that |IconImage| is not thread safe. | 50 // Note that |IconImage| is not thread safe. |
51 class IconImage : public content::NotificationObserver { | 51 class IconImage : public content::NotificationObserver { |
52 public: | 52 public: |
53 class Observer { | 53 class Observer { |
54 public: | 54 public: |
55 // Invoked when a new image rep for an additional scale factor | 55 // Invoked when a new image rep for an additional scale factor |
56 // is loaded and added to |image|. | 56 // is loaded and added to |image|. |
57 virtual void OnExtensionIconImageChanged(IconImage* image) = 0; | 57 virtual void OnExtensionIconImageChanged(IconImage* image) = 0; |
58 | 58 |
| 59 // Called when this object is deleted. Objects should observe this if there |
| 60 // is a question about the lifetime of the icon image vs observer. |
| 61 virtual void OnExtensionIconImageDestroyed(IconImage* image) {} |
| 62 |
59 protected: | 63 protected: |
60 virtual ~Observer() {} | 64 virtual ~Observer() {} |
61 }; | 65 }; |
62 | 66 |
63 // |context| is required by the underlying implementation to retrieve the | 67 // |context| is required by the underlying implementation to retrieve the |
64 // |ImageLoader| instance associated with the given context. |ImageLoader| is | 68 // |ImageLoader| instance associated with the given context. |ImageLoader| is |
65 // used to perform the asynchronous image load work. | 69 // used to perform the asynchronous image load work. |
66 IconImage(content::BrowserContext* context, | 70 IconImage(content::BrowserContext* context, |
67 const Extension* extension, | 71 const Extension* extension, |
68 const ExtensionIconSet& icon_set, | 72 const ExtensionIconSet& icon_set, |
69 int resource_size_in_dip, | 73 int resource_size_in_dip, |
70 const gfx::ImageSkia& default_icon, | 74 const gfx::ImageSkia& default_icon, |
71 Observer* observer); | 75 Observer* observer); |
72 ~IconImage() override; | 76 ~IconImage() override; |
73 | 77 |
74 const gfx::ImageSkia& image_skia() const { return image_skia_; } | 78 const gfx::ImageSkia& image_skia() const { return image_skia_; } |
75 | 79 |
| 80 void AddObserver(Observer* observer); |
| 81 void RemoveObserver(Observer* observer); |
| 82 |
76 private: | 83 private: |
77 class Source; | 84 class Source; |
78 | 85 |
79 // Loads an image representation for the scale factor. | 86 // Loads an image representation for the scale factor. |
80 // If the representation gets loaded synchronously, it is returned by this | 87 // If the representation gets loaded synchronously, it is returned by this |
81 // method. | 88 // method. |
82 // If representation loading is asynchronous, an empty image | 89 // If representation loading is asynchronous, an empty image |
83 // representation is returned. When the representation gets loaded the | 90 // representation is returned. When the representation gets loaded the |
84 // observer's |OnExtensionIconImageLoaded| will be called. | 91 // observers' OnExtensionIconImageLoaded() will be called. |
85 gfx::ImageSkiaRep LoadImageForScaleFactor(ui::ScaleFactor scale_factor); | 92 gfx::ImageSkiaRep LoadImageForScaleFactor(ui::ScaleFactor scale_factor); |
86 | 93 |
87 void OnImageLoaded(float scale_factor, const gfx::Image& image); | 94 void OnImageLoaded(float scale_factor, const gfx::Image& image); |
88 | 95 |
89 // content::NotificationObserver overrides: | 96 // content::NotificationObserver overrides: |
90 void Observe(int type, | 97 void Observe(int type, |
91 const content::NotificationSource& source, | 98 const content::NotificationSource& source, |
92 const content::NotificationDetails& details) override; | 99 const content::NotificationDetails& details) override; |
93 | 100 |
94 content::BrowserContext* browser_context_; | 101 content::BrowserContext* browser_context_; |
95 const Extension* extension_; | 102 const Extension* extension_; |
96 const ExtensionIconSet& icon_set_; | 103 const ExtensionIconSet& icon_set_; |
97 const int resource_size_in_dip_; | 104 const int resource_size_in_dip_; |
98 | 105 |
99 Observer* observer_; | 106 ObserverList<Observer> observers_; |
100 | 107 |
101 Source* source_; // Owned by ImageSkia storage. | 108 Source* source_; // Owned by ImageSkia storage. |
102 gfx::ImageSkia image_skia_; | 109 gfx::ImageSkia image_skia_; |
103 // The icon with whose representation |image_skia_| should be updated if | 110 // The icon with whose representation |image_skia_| should be updated if |
104 // its own representation load fails. | 111 // its own representation load fails. |
105 gfx::ImageSkia default_icon_; | 112 gfx::ImageSkia default_icon_; |
106 | 113 |
107 content::NotificationRegistrar registrar_; | 114 content::NotificationRegistrar registrar_; |
108 | 115 |
109 base::WeakPtrFactory<IconImage> weak_ptr_factory_; | 116 base::WeakPtrFactory<IconImage> weak_ptr_factory_; |
110 | 117 |
111 DISALLOW_COPY_AND_ASSIGN(IconImage); | 118 DISALLOW_COPY_AND_ASSIGN(IconImage); |
112 }; | 119 }; |
113 | 120 |
114 } // namespace extensions | 121 } // namespace extensions |
115 | 122 |
116 #endif // EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ | 123 #endif // EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_ |
OLD | NEW |