| OLD | NEW |
| (Empty) |
| 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/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 // 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 ImageLoaderImageCallback& callback); | |
| 97 | |
| 98 // Same as LoadImageAsync() above except it loads multiple images from the | |
| 99 // same 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 ImageLoaderImageCallback& callback); | |
| 104 | |
| 105 // Same as LoadImagesAsync() above except it loads into an image family. This | |
| 106 // is used to load multiple images of different logical sizes as opposed to | |
| 107 // LoadImagesAsync() which loads different scale factors of the same logical | |
| 108 // image size. | |
| 109 // | |
| 110 // If multiple images of the same logical size are loaded, they will be | |
| 111 // combined into a single ImageSkia in the ImageFamily. | |
| 112 void LoadImageFamilyAsync(const extensions::Extension* extension, | |
| 113 const std::vector<ImageRepresentation>& info_list, | |
| 114 const ImageLoaderImageFamilyCallback& callback); | |
| 115 | |
| 116 private: | |
| 117 void ReplyBack(const ImageLoaderImageCallback& callback, | |
| 118 const std::vector<LoadResult>& load_result); | |
| 119 | |
| 120 void ReplyBackWithImageFamily(const ImageLoaderImageFamilyCallback& callback, | |
| 121 const std::vector<LoadResult>& load_result); | |
| 122 | |
| 123 base::WeakPtrFactory<ImageLoader> weak_ptr_factory_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | |
| 126 }; | |
| 127 | |
| 128 } // namespace extensions | |
| 129 | |
| 130 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_ | |
| OLD | NEW |