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_IMAGE_LOADER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
      7 
      8 #include <set>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/gtest_prod_util.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     14 #include "extensions/common/extension_resource.h"
     15 #include "third_party/skia/include/core/SkBitmap.h"
     16 #include "ui/base/layout.h"
     17 #include "ui/gfx/size.h"
     18 
     19 class Profile;
     20 
     21 namespace gfx {
     22 class Image;
     23 }
     24 
     25 namespace extensions {
     26 
     27 class Extension;
     28 
     29 // This class is responsible for asynchronously loading extension images and
     30 // calling a callback when an image is loaded.
     31 // The views need to load their icons asynchronously might be deleted before
     32 // the images have loaded. If you pass your callback using a weak_ptr, this
     33 // will make sure the callback won't be called after the view is deleted.
     34 class ImageLoader : public BrowserContextKeyedService {
     35  public:
     36   // Information about a singe image representation to load from an extension
     37   // resource.
     38   struct ImageRepresentation {
     39     // Enum values to indicate whether to resize loaded bitmap when it is larger
     40     // than |desired_size| or always resize it.
     41     enum ResizeCondition {
     42       RESIZE_WHEN_LARGER,
     43       ALWAYS_RESIZE,
     44     };
     45 
     46     ImageRepresentation(const ExtensionResource& resource,
     47                         ResizeCondition resize_condition,
     48                         const gfx::Size& desired_size,
     49                         ui::ScaleFactor scale_factor);
     50     ~ImageRepresentation();
     51 
     52     // Extension resource to load.
     53     ExtensionResource resource;
     54 
     55     ResizeCondition resize_condition;
     56 
     57     // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger
     58     // than |desired_size| it will be resized to these dimensions.
     59     gfx::Size desired_size;
     60 
     61     // |scale_factor| is used to construct the loaded gfx::ImageSkia.
     62     ui::ScaleFactor scale_factor;
     63   };
     64 
     65   struct LoadResult;
     66 
     67   // Returns the instance for the given profile, or NULL if none. This is
     68   // a convenience wrapper around ImageLoaderFactory::GetForProfile.
     69   static ImageLoader* Get(Profile* profile);
     70 
     71   ImageLoader();
     72   virtual ~ImageLoader();
     73 
     74   // Checks whether image is a component extension resource. Returns false
     75   // if a given |resource| does not have a corresponding image in bundled
     76   // resources. Otherwise fills |resource_id|. This doesn't check if the
     77   // extension the resource is in is actually a component extension.
     78   static bool IsComponentExtensionResource(
     79       const base::FilePath& extension_path,
     80       const base::FilePath& resource_path,
     81       int* resource_id);
     82 
     83   // Specify image resource to load. If the loaded image is larger than
     84   // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
     85   // function may call back your callback synchronously (ie before it returns)
     86   // if the image was found in the cache.
     87   // Note this method loads a raw bitmap from the resource. All sizes given are
     88   // assumed to be in pixels.
     89   void LoadImageAsync(const extensions::Extension* extension,
     90                       const ExtensionResource& resource,
     91                       const gfx::Size& max_size,
     92                       const base::Callback<void(const gfx::Image&)>& callback);
     93 
     94   // Same as LoadImage() above except it loads multiple images from the same
     95   // extension. This is used to load multiple resolutions of the same image
     96   // type.
     97   void LoadImagesAsync(const extensions::Extension* extension,
     98                        const std::vector<ImageRepresentation>& info_list,
     99                        const base::Callback<void(const gfx::Image&)>& callback);
    100 
    101  private:
    102   base::WeakPtrFactory<ImageLoader> weak_ptr_factory_;
    103 
    104   static void LoadImagesOnBlockingPool(
    105       const std::vector<ImageRepresentation>& info_list,
    106       const std::vector<SkBitmap>& bitmaps,
    107       std::vector<LoadResult>* load_result);
    108 
    109   void ReplyBack(
    110       const std::vector<LoadResult>* load_result,
    111       const base::Callback<void(const gfx::Image&)>& callback);
    112 };
    113 
    114 }  // namespace extensions
    115 
    116 #endif  // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
    117