Fix memory leak in Sync FakeServerEntity
[chromium-blink-merge.git] / extensions / browser / image_loader.h
bloba0504e23d5905daafa507c4d84da6785404eded0
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.
5 #ifndef EXTENSIONS_BROWSER_IMAGE_LOADER_H_
6 #define EXTENSIONS_BROWSER_IMAGE_LOADER_H_
8 #include <set>
10 #include "base/callback_forward.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/keyed_service/core/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"
19 namespace content {
20 class BrowserContext;
23 namespace gfx {
24 class Image;
25 class ImageFamily;
28 namespace extensions {
30 class Extension;
32 typedef base::Callback<void(const gfx::Image&)> ImageLoaderImageCallback;
33 typedef base::Callback<void(const gfx::ImageFamily&)>
34 ImageLoaderImageFamilyCallback;
36 // This class is responsible for asynchronously loading extension images and
37 // calling a callback when an image is loaded.
38 // The views need to load their icons asynchronously might be deleted before
39 // the images have loaded. If you pass your callback using a weak_ptr, this
40 // will make sure the callback won't be called after the view is deleted.
41 class ImageLoader : public KeyedService {
42 public:
43 // Information about a singe image representation to load from an extension
44 // resource.
45 struct ImageRepresentation {
46 // Enum values to indicate whether to resize loaded bitmap when it is larger
47 // than |desired_size| or always resize it.
48 enum ResizeCondition { RESIZE_WHEN_LARGER, ALWAYS_RESIZE, NEVER_RESIZE };
50 ImageRepresentation(const ExtensionResource& resource,
51 ResizeCondition resize_condition,
52 const gfx::Size& desired_size,
53 ui::ScaleFactor scale_factor);
54 ~ImageRepresentation();
56 // Extension resource to load.
57 ExtensionResource resource;
59 ResizeCondition resize_condition;
61 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger
62 // than |desired_size| it will be resized to these dimensions.
63 gfx::Size desired_size;
65 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
66 ui::ScaleFactor scale_factor;
69 struct LoadResult;
71 // Returns the instance for the given |context| or NULL if none. This is
72 // a convenience wrapper around ImageLoaderFactory::GetForBrowserContext.
73 static ImageLoader* Get(content::BrowserContext* context);
75 ImageLoader();
76 virtual ~ImageLoader();
78 // Specify image resource to load. If the loaded image is larger than
79 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
80 // function may call back your callback synchronously (ie before it returns)
81 // if the image was found in the cache.
82 // Note this method loads a raw bitmap from the resource. All sizes given are
83 // assumed to be in pixels.
84 void LoadImageAsync(const extensions::Extension* extension,
85 const ExtensionResource& resource,
86 const gfx::Size& max_size,
87 const ImageLoaderImageCallback& callback);
89 // Same as LoadImageAsync() above except it loads multiple images from the
90 // same extension. This is used to load multiple resolutions of the same image
91 // type.
92 void LoadImagesAsync(const extensions::Extension* extension,
93 const std::vector<ImageRepresentation>& info_list,
94 const ImageLoaderImageCallback& callback);
96 // Same as LoadImagesAsync() above except it loads into an image family. This
97 // is used to load multiple images of different logical sizes as opposed to
98 // LoadImagesAsync() which loads different scale factors of the same logical
99 // image size.
101 // If multiple images of the same logical size are loaded, they will be
102 // combined into a single ImageSkia in the ImageFamily.
103 void LoadImageFamilyAsync(const extensions::Extension* extension,
104 const std::vector<ImageRepresentation>& info_list,
105 const ImageLoaderImageFamilyCallback& callback);
107 private:
108 void ReplyBack(const ImageLoaderImageCallback& callback,
109 const std::vector<LoadResult>& load_result);
111 void ReplyBackWithImageFamily(const ImageLoaderImageFamilyCallback& callback,
112 const std::vector<LoadResult>& load_result);
114 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_;
116 DISALLOW_COPY_AND_ASSIGN(ImageLoader);
119 } // namespace extensions
121 #endif // EXTENSIONS_BROWSER_IMAGE_LOADER_H_