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