| 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_LOADING_TRACKER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "chrome/common/extensions/extension_icon_set.h" | |
| 16 #include "chrome/common/extensions/extension_resource.h" | |
| 17 #include "content/public/browser/notification_observer.h" | |
| 18 #include "content/public/browser/notification_registrar.h" | |
| 19 #include "ui/base/layout.h" | |
| 20 #include "ui/gfx/image/image_skia.h" | |
| 21 #include "ui/gfx/size.h" | |
| 22 | |
| 23 class SkBitmap; | |
| 24 | |
| 25 namespace extensions { | |
| 26 class AppShortcutManager; | |
| 27 class Extension; | |
| 28 class IconImage; | |
| 29 class TabHelper; | |
| 30 } | |
| 31 | |
| 32 namespace gfx { | |
| 33 class Image; | |
| 34 } | |
| 35 | |
| 36 // The views need to load their icons asynchronously but might be deleted before | |
| 37 // the images have loaded. This class encapsulates a loader class that stays | |
| 38 // alive while the request is in progress (manages its own lifetime) and keeps | |
| 39 // track of whether the view still cares about the icon loading. | |
| 40 // | |
| 41 // To use this class, have your class derive from ImageLoadingTracker::Observer, | |
| 42 // and add a member variable ImageLoadingTracker tracker_. Then override | |
| 43 // Observer::OnImageLoaded and call: | |
| 44 // tracker_.LoadImage(extension, resource, max_size, false); | |
| 45 // ... and wait for OnImageLoaded to be called back on you with a pointer to the | |
| 46 // ImageSkia loaded. | |
| 47 // NOTE: if the image is available already (or the resource is not valid), the | |
| 48 // Observer is notified immediately from the call to LoadImage. In other words, | |
| 49 // by the time LoadImage returns the observer has been notified. | |
| 50 // | |
| 51 // NOTE: This class is deprecated! New code should be using | |
| 52 // extensions::ImageLoader or extensions::IconImage instead. | |
| 53 class ImageLoadingTracker : public content::NotificationObserver { | |
| 54 public: | |
| 55 enum CacheParam { | |
| 56 CACHE, | |
| 57 DONT_CACHE | |
| 58 }; | |
| 59 | |
| 60 class Observer { | |
| 61 public: | |
| 62 // Will be called when the image with the given index has loaded. | |
| 63 // |image| can be empty if a valid image was not found or it failed to | |
| 64 // decode. |extension_id| is the ID of the extension the images are loaded | |
| 65 // from. |index| represents the index of the image just loaded (starts at 0 | |
| 66 // and increments every time LoadImage is called). | |
| 67 virtual void OnImageLoaded(const gfx::Image& image, | |
| 68 const std::string& extension_id, | |
| 69 int index) = 0; | |
| 70 | |
| 71 protected: | |
| 72 virtual ~Observer(); | |
| 73 }; | |
| 74 | |
| 75 // Information about a singe image representation to load from an extension | |
| 76 // resource. | |
| 77 struct ImageRepresentation { | |
| 78 // Enum values to indicate whether to resize loaded bitmap when it is larger | |
| 79 // than |desired_size| or always resize it. | |
| 80 enum ResizeCondition { | |
| 81 RESIZE_WHEN_LARGER, | |
| 82 ALWAYS_RESIZE, | |
| 83 }; | |
| 84 | |
| 85 ImageRepresentation(const ExtensionResource& resource, | |
| 86 ResizeCondition resize_method, | |
| 87 const gfx::Size& desired_size, | |
| 88 ui::ScaleFactor scale_factor); | |
| 89 ~ImageRepresentation(); | |
| 90 | |
| 91 // Extension resource to load. | |
| 92 ExtensionResource resource; | |
| 93 | |
| 94 ResizeCondition resize_method; | |
| 95 | |
| 96 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger | |
| 97 // than |desired_size| it will be resized to these dimensions. | |
| 98 gfx::Size desired_size; | |
| 99 | |
| 100 // |scale_factor| is used to construct the loaded gfx::ImageSkia. | |
| 101 ui::ScaleFactor scale_factor; | |
| 102 }; | |
| 103 | |
| 104 virtual ~ImageLoadingTracker(); | |
| 105 | |
| 106 // Specify image resource to load. If the loaded image is larger than | |
| 107 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this | |
| 108 // function may call back your observer synchronously (ie before it returns) | |
| 109 // if the image was found in the cache. | |
| 110 // Note this method loads a raw bitmap from the resource. All sizes given are | |
| 111 // assumed to be in pixels. | |
| 112 void LoadImage(const extensions::Extension* extension, | |
| 113 const ExtensionResource& resource, | |
| 114 const gfx::Size& max_size, | |
| 115 CacheParam cache); | |
| 116 | |
| 117 // Same as LoadImage() above except it loads multiple images from the same | |
| 118 // extension. This is used to load multiple resolutions of the same image | |
| 119 // type. | |
| 120 void LoadImages(const extensions::Extension* extension, | |
| 121 const std::vector<ImageRepresentation>& info_list, | |
| 122 CacheParam cache); | |
| 123 | |
| 124 // Returns the ID used for the next image that is loaded. That is, the return | |
| 125 // value from this method corresponds to the int that is passed to | |
| 126 // OnImageLoaded() the next time LoadImage() is invoked. | |
| 127 int next_id() const { return next_id_; } | |
| 128 | |
| 129 private: | |
| 130 // This class is deprecated. This just lists all currently remaining | |
| 131 // usage of this class, so once this list is empty this class can and | |
| 132 // should be removed. | |
| 133 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, Cache); | |
| 134 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, | |
| 135 DeleteExtensionWhileWaitingForCache); | |
| 136 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, MultipleImages); | |
| 137 | |
| 138 explicit ImageLoadingTracker(Observer* observer); | |
| 139 | |
| 140 // Information for pending resource load operation for one or more image | |
| 141 // representations. | |
| 142 struct PendingLoadInfo { | |
| 143 PendingLoadInfo(); | |
| 144 ~PendingLoadInfo(); | |
| 145 | |
| 146 const extensions::Extension* extension; | |
| 147 // This is cached separate from |extension| in case the extension is | |
| 148 // unloaded. | |
| 149 std::string extension_id; | |
| 150 CacheParam cache; | |
| 151 size_t pending_count; | |
| 152 gfx::ImageSkia image_skia; | |
| 153 }; | |
| 154 | |
| 155 // Maps an integer identifying a load request to a PendingLoadInfo. | |
| 156 typedef std::map<int, PendingLoadInfo> LoadMap; | |
| 157 | |
| 158 class ImageLoader; | |
| 159 | |
| 160 // Called on the calling thread when the bitmap finishes loading. | |
| 161 // |bitmap| may be null if the image file failed to decode. | |
| 162 void OnBitmapLoaded(const SkBitmap* bitmap, | |
| 163 const ImageRepresentation& image_info, | |
| 164 const gfx::Size& original_size, | |
| 165 int id, | |
| 166 bool should_cache); | |
| 167 | |
| 168 // content::NotificationObserver method. If an extension is uninstalled while | |
| 169 // we're waiting for the image we remove the entry from load_map_. | |
| 170 virtual void Observe(int type, | |
| 171 const content::NotificationSource& source, | |
| 172 const content::NotificationDetails& details) OVERRIDE; | |
| 173 | |
| 174 // The view that is waiting for the image to load. | |
| 175 Observer* observer_; | |
| 176 | |
| 177 // ID to use for next image requested. This is an ever increasing integer. | |
| 178 int next_id_; | |
| 179 | |
| 180 // The object responsible for loading the image on the File thread. | |
| 181 scoped_refptr<ImageLoader> loader_; | |
| 182 | |
| 183 // Information for each LoadImage request is cached here. The integer | |
| 184 // identifies the id assigned to the request. | |
| 185 LoadMap load_map_; | |
| 186 | |
| 187 content::NotificationRegistrar registrar_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | |
| 190 }; | |
| 191 | |
| 192 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | |
| OLD | NEW |