extension_icon_image.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.
  4. #ifndef EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "base/scoped_observation.h"
  10. #include "extensions/browser/extension_registry.h"
  11. #include "extensions/browser/extension_registry_observer.h"
  12. #include "extensions/common/extension_icon_set.h"
  13. #include "ui/gfx/image/image.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. namespace content {
  16. class BrowserContext;
  17. }
  18. namespace extensions {
  19. class Extension;
  20. }
  21. namespace gfx {
  22. class Image;
  23. }
  24. namespace extensions {
  25. // A class that provides an ImageSkia for UI code to use. It handles extension
  26. // icon resource loading, screen scale factor change etc. UI code that uses
  27. // extension icon should host this class. In painting code, UI code paints with
  28. // the ImageSkia provided by this class. If the required extension icon resource
  29. // is not already present, this class tries to load it and calls its observer
  30. // interface when the image get updated. Until the resource is loaded, the UI
  31. // code will be provided with a blank, transparent image.
  32. // If the requested resource doesn't exist or can't be loaded and a default
  33. // icon was supplied in the constructor, icon image will be updated with the
  34. // default icon's resource.
  35. // The default icon doesn't need to be supplied, but in that case, icon image
  36. // representation will be left blank if the resource loading fails.
  37. // If default icon is supplied, it is assumed that it contains or can
  38. // synchronously create (when |GetRepresentation| is called on it)
  39. // representations for all the scale factors supported by the current platform.
  40. // Note that |IconImage| is not thread safe.
  41. class IconImage : public ExtensionRegistryObserver {
  42. public:
  43. class Observer {
  44. public:
  45. // Invoked when a new image rep for an additional scale factor
  46. // is loaded and added to |image|.
  47. virtual void OnExtensionIconImageChanged(IconImage* image) = 0;
  48. // Called when this object is deleted. Objects should observe this if there
  49. // is a question about the lifetime of the icon image vs observer.
  50. virtual void OnExtensionIconImageDestroyed(IconImage* image) {}
  51. protected:
  52. virtual ~Observer() {}
  53. };
  54. // |context| is required by the underlying implementation to retrieve the
  55. // |ImageLoader| instance associated with the given context. |ImageLoader| is
  56. // used to perform the asynchronous image load work.
  57. // Set |keep_original_size| to true to load the icon at the original size
  58. // without resizing. In this case |resource_size_in_dip| will still be used to
  59. // pick the correct icon representation. This is useful if the client code
  60. // performs its own resizing.
  61. IconImage(content::BrowserContext* context,
  62. const Extension* extension,
  63. const ExtensionIconSet& icon_set,
  64. int resource_size_in_dip,
  65. bool keep_original_size,
  66. const gfx::ImageSkia& default_icon,
  67. Observer* observer);
  68. IconImage(content::BrowserContext* context,
  69. const Extension* extension,
  70. const ExtensionIconSet& icon_set,
  71. int resource_size_in_dip,
  72. const gfx::ImageSkia& default_icon,
  73. Observer* observer);
  74. IconImage(const IconImage&) = delete;
  75. IconImage& operator=(const IconImage&) = delete;
  76. ~IconImage() override;
  77. gfx::Image image() const { return image_; }
  78. const gfx::ImageSkia& image_skia() const { return image_skia_; }
  79. // Returns true if the icon is attached to an existing extension.
  80. bool is_valid() const { return extension_ ? true : false; }
  81. bool did_complete_initial_load() const { return did_complete_initial_load_; }
  82. void AddObserver(Observer* observer);
  83. void RemoveObserver(Observer* observer);
  84. private:
  85. class Source;
  86. // Loads an image representation for the scale factor asynchronously. Result
  87. // is passed to OnImageRepLoaded.
  88. void LoadImageForScaleAsync(float scale);
  89. void OnImageLoaded(float scale, const gfx::Image& image);
  90. void OnImageRepLoaded(const gfx::ImageSkiaRep& rep);
  91. // ExtensionRegistryObserver:
  92. void OnExtensionUnloaded(content::BrowserContext* browser_context,
  93. const Extension* extension,
  94. UnloadedExtensionReason reason) override;
  95. void OnShutdown(ExtensionRegistry* extension_registry) override;
  96. raw_ptr<content::BrowserContext> browser_context_;
  97. scoped_refptr<const Extension> extension_;
  98. ExtensionIconSet icon_set_;
  99. const int resource_size_in_dip_;
  100. // Whether the loaded icon should be kept at the original size.
  101. const bool keep_original_size_;
  102. // Set to true when the icon finishes the very first load (which can be
  103. // asynchronous from creation).
  104. bool did_complete_initial_load_;
  105. base::ObserverList<Observer>::Unchecked observers_;
  106. raw_ptr<Source> source_; // Owned by ImageSkia storage.
  107. gfx::ImageSkia image_skia_;
  108. // The icon with whose representation |image_skia_| should be updated if
  109. // its own representation load fails.
  110. gfx::ImageSkia default_icon_;
  111. // The image wrapper around |image_skia_|.
  112. // Note: this is reset each time a new representation is loaded.
  113. gfx::Image image_;
  114. base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
  115. registry_observation_{this};
  116. base::WeakPtrFactory<IconImage> weak_ptr_factory_{this};
  117. };
  118. } // namespace extensions
  119. #endif // EXTENSIONS_BROWSER_EXTENSION_ICON_IMAGE_H_