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