Chromium Code Reviews| 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_EXTENSIONS_EXTENSION_ICON_IMAGE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_IMAGE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_IMAGE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_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 "chrome/browser/extensions/image_loading_tracker.h" | 13 #include "chrome/browser/extensions/image_loading_tracker.h" |
| 13 #include "chrome/common/extensions/extension_icon_set.h" | 14 #include "chrome/common/extensions/extension_icon_set.h" |
| 14 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
| 16 #include "ui/gfx/image/image_skia.h" | 17 #include "ui/gfx/image/image_skia.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 class Extension; | 20 class Extension; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace gfx { | 23 namespace gfx { |
| 23 class Size; | 24 class Size; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 // A class that provides an ImageSkia for UI code to use. It handles extension | 29 // A class that provides an ImageSkia for UI code to use. It handles extension |
| 29 // icon resource loading, screen scale factor change etc. UI code that uses | 30 // icon resource loading, screen scale factor change etc. UI code that uses |
| 30 // extension icon should host this class and be its observer. ExtensionIconImage | 31 // extension icon should host this class and be its observer. ExtensionIconImage |
| 31 // should be outlived by the observer. In painting code, UI code paints with the | 32 // should be outlived by the observer. In painting code, UI code paints with the |
| 32 // ImageSkia provided by this class. If required extension icon resource is not | 33 // ImageSkia provided by this class. If required extension icon resource is not |
| 33 // present, this class uses ImageLoadingTracker to load it and call on its | 34 // present, this class uses ImageLoadingTracker to load it and call on its |
| 34 // observer interface when the resource is loaded. | 35 // observer interface when the resource is loaded. Until the resource is loaded, |
| 36 // the UI code will be provided image representations from the default icon | |
| 37 // supplied in the constructor. If no default icon is supplied, UI code must | |
| 38 // make sure null ImageSkiaReps are handled well. | |
|
pkotwicz
2012/08/22 23:17:55
An idea: Can we have a default 'default icon' whic
tbarzic
2012/08/22 23:46:08
Yeah, I had the same idea (just about to upload th
| |
| 35 class IconImage : public ImageLoadingTracker::Observer, | 39 class IconImage : public ImageLoadingTracker::Observer, |
| 36 public content::NotificationObserver { | 40 public content::NotificationObserver { |
| 37 public: | 41 public: |
| 38 class Observer { | 42 class Observer { |
| 39 public: | 43 public: |
| 40 // Invoked when a new image rep for an additional scale factor | 44 // Invoked when a new image rep for an additional scale factor |
| 41 // is loaded and added to |image|. | 45 // is loaded and added to |image|. |
| 42 virtual void OnExtensionIconImageChanged(IconImage* image) = 0; | 46 virtual void OnExtensionIconImageChanged(IconImage* image) = 0; |
| 43 | 47 |
| 44 // Invoked when the icon image couldn't be loaded. | 48 // Invoked when the icon image couldn't be loaded. This will always be |
| 49 // called asynchronously. | |
| 45 virtual void OnIconImageLoadFailed(IconImage* image, | 50 virtual void OnIconImageLoadFailed(IconImage* image, |
| 46 ui::ScaleFactor scale_factor) = 0; | 51 ui::ScaleFactor scale_factor) = 0; |
| 47 | 52 |
| 48 protected: | 53 protected: |
| 49 virtual ~Observer() {} | 54 virtual ~Observer() {} |
| 50 }; | 55 }; |
| 51 | 56 |
| 52 IconImage(const Extension* extension, | 57 IconImage(const Extension* extension, |
| 53 const ExtensionIconSet& icon_set, | 58 const ExtensionIconSet& icon_set, |
| 54 int resource_size_in_dip, | 59 int resource_size_in_dip, |
| 60 const gfx::ImageSkia& default_icon, | |
| 55 Observer* observer); | 61 Observer* observer); |
| 56 virtual ~IconImage(); | 62 virtual ~IconImage(); |
| 57 | 63 |
| 58 const gfx::ImageSkia& image_skia() const { return image_skia_; } | 64 const gfx::ImageSkia& image_skia() const { return image_skia_; } |
| 59 | 65 |
| 60 private: | 66 private: |
| 61 class Source; | 67 class Source; |
| 62 | 68 |
| 63 typedef std::map<int, ui::ScaleFactor> LoadMap; | 69 struct LoadRequest { |
| 70 ui::ScaleFactor scale_factor; | |
| 71 bool is_async; | |
| 72 }; | |
| 73 | |
| 74 typedef std::map<int, LoadRequest> LoadMap; | |
| 64 | 75 |
| 65 // Loads bitmap for additional scale factor. | 76 // Loads bitmap for additional scale factor. |
| 66 void LoadImageForScaleFactor(ui::ScaleFactor scale_factor); | 77 gfx::ImageSkiaRep LoadImageForScaleFactor(ui::ScaleFactor scale_factor); |
| 67 | 78 |
| 68 // ImageLoadingTracker::Observer overrides: | 79 // ImageLoadingTracker::Observer overrides: |
| 69 virtual void OnImageLoaded(const gfx::Image& image, | 80 virtual void OnImageLoaded(const gfx::Image& image, |
| 70 const std::string& extension_id, | 81 const std::string& extension_id, |
| 71 int index) OVERRIDE; | 82 int index) OVERRIDE; |
| 72 | 83 |
| 73 // content::NotificationObserver overrides: | 84 // content::NotificationObserver overrides: |
| 74 virtual void Observe(int type, | 85 virtual void Observe(int type, |
| 75 const content::NotificationSource& source, | 86 const content::NotificationSource& source, |
| 76 const content::NotificationDetails& details) OVERRIDE; | 87 const content::NotificationDetails& details) OVERRIDE; |
| 77 | 88 |
| 89 void NotifyFail(ui::ScaleFactor scale_factor); | |
| 90 | |
| 78 const Extension* extension_; | 91 const Extension* extension_; |
| 79 const ExtensionIconSet& icon_set_; | 92 const ExtensionIconSet& icon_set_; |
| 80 const int resource_size_in_dip_; | 93 const int resource_size_in_dip_; |
| 81 const gfx::Size desired_size_in_dip_; | 94 const gfx::Size desired_size_in_dip_; |
| 82 | 95 |
| 83 Observer* observer_; | 96 Observer* observer_; |
| 84 | 97 |
| 85 Source* source_; // Owned by ImageSkia storage. | 98 Source* source_; // Owned by ImageSkia storage. |
| 86 gfx::ImageSkia image_skia_; | 99 gfx::ImageSkia image_skia_; |
| 87 | 100 |
| 88 ImageLoadingTracker tracker_; | 101 ImageLoadingTracker tracker_; |
| 89 content::NotificationRegistrar registrar_; | 102 content::NotificationRegistrar registrar_; |
| 90 | 103 |
| 104 base::WeakPtrFactory<IconImage> weak_ptr_factory_; | |
| 105 | |
| 91 LoadMap load_map_; | 106 LoadMap load_map_; |
| 92 | 107 |
| 93 DISALLOW_COPY_AND_ASSIGN(IconImage); | 108 DISALLOW_COPY_AND_ASSIGN(IconImage); |
| 94 }; | 109 }; |
| 95 | 110 |
| 96 } // namespace extensions | 111 } // namespace extensions |
| 97 | 112 |
| 98 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_IMAGE_H_ | 113 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_IMAGE_H_ |
| OLD | NEW |