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 5 #ifndef EXTENSIONS_BROWSER_IMAGE_LOADER_H_ 6 #define EXTENSIONS_BROWSER_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/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" 18 19 namespace content { 20 class BrowserContext; 21 } 22 23 namespace gfx { 24 class Image; 25 class ImageFamily; 26 } 27 28 namespace extensions { 29 30 class Extension; 31 32 typedef base::Callback<void(const gfx::Image&)> ImageLoaderImageCallback; 33 typedef base::Callback<void(const gfx::ImageFamily&)> 34 ImageLoaderImageFamilyCallback; 35 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 }; 49 50 ImageRepresentation(const ExtensionResource& resource, 51 ResizeCondition resize_condition, 52 const gfx::Size& desired_size, 53 ui::ScaleFactor scale_factor); 54 ~ImageRepresentation(); 55 56 // Extension resource to load. 57 ExtensionResource resource; 58 59 ResizeCondition resize_condition; 60 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; 64 65 // |scale_factor| is used to construct the loaded gfx::ImageSkia. 66 ui::ScaleFactor scale_factor; 67 }; 68 69 struct LoadResult; 70 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); 74 75 ImageLoader(); 76 virtual ~ImageLoader(); 77 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); 88 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); 95 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. 100 // 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); 106 107 private: 108 void ReplyBack(const ImageLoaderImageCallback& callback, 109 const std::vector<LoadResult>& load_result); 110 111 void ReplyBackWithImageFamily(const ImageLoaderImageFamilyCallback& callback, 112 const std::vector<LoadResult>& load_result); 113 114 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; 115 116 DISALLOW_COPY_AND_ASSIGN(ImageLoader); 117 }; 118 119 } // namespace extensions 120 121 #endif // EXTENSIONS_BROWSER_IMAGE_LOADER_H_ 122