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 namespace content { 20 class BrowserContext; 21 } 22 23 namespace gfx { 24 class Image; 25 } 26 27 namespace extensions { 28 29 class Extension; 30 31 typedef base::Callback<void(const gfx::Image&)> ImageLoaderCallback; 32 33 // This class is responsible for asynchronously loading extension images and 34 // calling a callback when an image is loaded. 35 // The views need to load their icons asynchronously might be deleted before 36 // the images have loaded. If you pass your callback using a weak_ptr, this 37 // will make sure the callback won't be called after the view is deleted. 38 class ImageLoader : public BrowserContextKeyedService { 39 public: 40 // Information about a singe image representation to load from an extension 41 // resource. 42 struct ImageRepresentation { 43 // Enum values to indicate whether to resize loaded bitmap when it is larger 44 // than |desired_size| or always resize it. 45 enum ResizeCondition { 46 RESIZE_WHEN_LARGER, 47 ALWAYS_RESIZE, 48 }; 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 // Checks whether image is a component extension resource. Returns false 79 // if a given |resource| does not have a corresponding image in bundled 80 // resources. Otherwise fills |resource_id|. This doesn't check if the 81 // extension the resource is in is actually a component extension. 82 static bool IsComponentExtensionResource( 83 const base::FilePath& extension_path, 84 const base::FilePath& resource_path, 85 int* resource_id); 86 87 // Specify image resource to load. If the loaded image is larger than 88 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this 89 // function may call back your callback synchronously (ie before it returns) 90 // if the image was found in the cache. 91 // Note this method loads a raw bitmap from the resource. All sizes given are 92 // assumed to be in pixels. 93 void LoadImageAsync(const extensions::Extension* extension, 94 const ExtensionResource& resource, 95 const gfx::Size& max_size, 96 const ImageLoaderCallback& callback); 97 98 // Same as LoadImage() above except it loads multiple images from the same 99 // extension. This is used to load multiple resolutions of the same image 100 // type. 101 void LoadImagesAsync(const extensions::Extension* extension, 102 const std::vector<ImageRepresentation>& info_list, 103 const ImageLoaderCallback& callback); 104 105 private: 106 void ReplyBack(const ImageLoaderCallback& callback, 107 const std::vector<LoadResult>& load_result); 108 109 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; 110 111 DISALLOW_COPY_AND_ASSIGN(ImageLoader); 112 }; 113 114 } // namespace extensions 115 116 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ 117