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