Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: chrome/browser/extensions/image_loader.h

Issue 11027044: Add a class to replace ImageLoadingTracker with a nicer API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove caching, and change from singleton to ExtenionSystem member Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <set>
10 #include <vector>
11 10
12 #include "base/compiler_specific.h" 11 #include "base/callback_forward.h"
13 #include "base/gtest_prod_util.h" 12 #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" 13 #include "chrome/common/extensions/extension_resource.h"
17 #include "content/public/browser/notification_observer.h" 14 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "ui/base/layout.h" 15 #include "ui/base/layout.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/size.h" 16 #include "ui/gfx/size.h"
22 17
23 class SkBitmap; 18 FORWARD_DECLARE_TEST(ImageLoaderTest, LoadImage);
24 19 FORWARD_DECLARE_TEST(ImageLoaderTest, DeleteExtensionWhileWaitingForCache);
25 namespace extensions { 20 FORWARD_DECLARE_TEST(ImageLoaderTest, MultipleImages);
26 class Extension;
27 }
28 21
29 namespace gfx { 22 namespace gfx {
30 class Image; 23 class Image;
31 } 24 }
32 25
33 // The views need to load their icons asynchronously but might be deleted before 26 namespace extensions {
34 // the images have loaded. This class encapsulates a loader class that stays 27
35 // alive while the request is in progress (manages its own lifetime) and keeps 28 class Extension;
36 // track of whether the view still cares about the icon loading. 29
37 // 30 // This class is responsible for asynchronously loading extension images and
38 // To use this class, have your class derive from ImageLoadingTracker::Observer, 31 // calling a callback when an image is loaded.
39 // and add a member variable ImageLoadingTracker tracker_. Then override 32 // The views need to load their icons asynchronously might be deleted before
40 // Observer::OnImageLoaded and call: 33 // the images have loaded. If you pass your callback using a weak_ptr, this
41 // tracker_.LoadImage(extension, resource, max_size, false); 34 // will make sure the callback won't be called after the view is deleted.
42 // ... and wait for OnImageLoaded to be called back on you with a pointer to the 35 class ImageLoader {
43 // ImageSkia loaded.
44 // NOTE: if the image is available already (or the resource is not valid), the
45 // Observer is notified immediately from the call to LoadImage. In other words,
46 // by the time LoadImage returns the observer has been notified.
47 //
48 class ImageLoadingTracker : public content::NotificationObserver {
49 public: 36 public:
50 enum CacheParam {
51 CACHE,
52 DONT_CACHE
53 };
54
55 class Observer {
56 public:
57 // Will be called when the image with the given index has loaded.
58 // |image| can be empty if a valid image was not found or it failed to
59 // decode. |extension_id| is the ID of the extension the images are loaded
60 // from. |index| represents the index of the image just loaded (starts at 0
61 // and increments every time LoadImage is called).
62 virtual void OnImageLoaded(const gfx::Image& image,
63 const std::string& extension_id,
64 int index) = 0;
65
66 protected:
67 virtual ~Observer();
68 };
69
70 // Information about a singe image representation to load from an extension 37 // Information about a singe image representation to load from an extension
71 // resource. 38 // resource.
72 struct ImageRepresentation { 39 struct ImageRepresentation {
73 // Enum values to indicate whether to resize loaded bitmap when it is larger 40 // Enum values to indicate whether to resize loaded bitmap when it is larger
74 // than |desired_size| or always resize it. 41 // than |desired_size| or always resize it.
75 enum ResizeCondition { 42 enum ResizeCondition {
76 RESIZE_WHEN_LARGER, 43 RESIZE_WHEN_LARGER,
77 ALWAYS_RESIZE, 44 ALWAYS_RESIZE,
78 }; 45 };
79 46
80 ImageRepresentation(const ExtensionResource& resource, 47 ImageRepresentation(const ExtensionResource& resource,
81 ResizeCondition resize_method, 48 ResizeCondition resize_condition,
82 const gfx::Size& desired_size, 49 const gfx::Size& desired_size,
83 ui::ScaleFactor scale_factor); 50 ui::ScaleFactor scale_factor);
84 ~ImageRepresentation(); 51 ~ImageRepresentation();
85 52
86 // Extension resource to load. 53 // Extension resource to load.
87 ExtensionResource resource; 54 ExtensionResource resource;
88 55
89 ResizeCondition resize_method; 56 ResizeCondition resize_condition;
90 57
91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger 58 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger
92 // than |desired_size| it will be resized to these dimensions. 59 // than |desired_size| it will be resized to these dimensions.
93 gfx::Size desired_size; 60 gfx::Size desired_size;
94 61
95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. 62 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
96 ui::ScaleFactor scale_factor; 63 ui::ScaleFactor scale_factor;
97 }; 64 };
98 65
99 explicit ImageLoadingTracker(Observer* observer); 66 ImageLoader();
100 virtual ~ImageLoadingTracker(); 67 virtual ~ImageLoader();
101
102 // Specify image resource to load. If the loaded image is larger than
103 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
104 // function may call back your observer synchronously (ie before it returns)
105 // if the image was found in the cache.
106 // Note this method loads a raw bitmap from the resource. All sizes given are
107 // assumed to be in pixels.
108 void LoadImage(const extensions::Extension* extension,
109 const ExtensionResource& resource,
110 const gfx::Size& max_size,
111 CacheParam cache);
112
113 // Same as LoadImage() above except it loads multiple images from the same
114 // extension. This is used to load multiple resolutions of the same image
115 // type.
116 void LoadImages(const extensions::Extension* extension,
117 const std::vector<ImageRepresentation>& info_list,
118 CacheParam cache);
119
120 // Returns the ID used for the next image that is loaded. That is, the return
121 // value from this method corresponds to the int that is passed to
122 // OnImageLoaded() the next time LoadImage() is invoked.
123 int next_id() const { return next_id_; }
124 68
125 // Checks whether image is a component extension resource. Returns false 69 // Checks whether image is a component extension resource. Returns false
126 // if a given |resource| does not have a corresponding image in bundled 70 // if a given |resource| does not have a corresponding image in bundled
127 // resources. Otherwise fills |resource_id|. 71 // resources. Otherwise fills |resource_id|.
128 static bool IsComponentExtensionResource( 72 static bool IsComponentExtensionResource(
129 const extensions::Extension* extension, 73 const extensions::Extension* extension,
130 const FilePath& resource_path, 74 const FilePath& resource_path,
131 int* resource_id); 75 int* resource_id);
132 76
77 // Specify image resource to load. If the loaded image is larger than
78 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
79 // function may call back your callback synchronously (ie before it returns)
80 // if the image was found in the cache.
81 // Note this method loads a raw bitmap from the resource. All sizes given are
82 // assumed to be in pixels.
83 void LoadImageAsync(const extensions::Extension* extension,
84 const ExtensionResource& resource,
85 const gfx::Size& max_size,
86 const base::Callback<void(const gfx::Image&)>& callback);
87
88 // Same as LoadImage() above except it loads multiple images from the same
89 // extension. This is used to load multiple resolutions of the same image
90 // type.
91 void LoadImagesAsync(const extensions::Extension* extension,
92 const std::vector<ImageRepresentation>& info_list,
93 const base::Callback<void(const gfx::Image&)>& callback);
94
133 private: 95 private:
134 // Information for pending resource load operation for one or more image 96 struct LoadResult;
135 // representations.
136 struct PendingLoadInfo {
137 PendingLoadInfo();
138 ~PendingLoadInfo();
139 97
140 const extensions::Extension* extension; 98 void LoadImagesOnBlockingPool(
141 // This is cached separate from |extension| in case the extension is 99 const std::vector<ImageRepresentation>& info_list,
142 // unloaded. 100 const std::vector<SkBitmap>& bitmaps,
143 std::string extension_id; 101 const base::Callback<void(const gfx::Image&)>& callback);
144 CacheParam cache;
145 size_t pending_count;
146 gfx::ImageSkia image_skia;
147 };
148 102
149 // Maps an integer identifying a load request to a PendingLoadInfo. 103 void ReplyBack(
150 typedef std::map<int, PendingLoadInfo> LoadMap; 104 const std::vector<LoadResult>& load_result,
105 const base::Callback<void(const gfx::Image&)>& callback);
151 106
152 class ImageLoader; 107 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, LoadImage);
153 108 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest,
154 // Called on the calling thread when the bitmap finishes loading. 109 DeleteExtensionWhileWaitingForCache);
155 // |bitmap| may be null if the image file failed to decode. 110 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, MultipleImages);
156 void OnBitmapLoaded(const SkBitmap* bitmap,
157 const ImageRepresentation& image_info,
158 const gfx::Size& original_size,
159 int id,
160 bool should_cache);
161
162 // content::NotificationObserver method. If an extension is uninstalled while
163 // we're waiting for the image we remove the entry from load_map_.
164 virtual void Observe(int type,
165 const content::NotificationSource& source,
166 const content::NotificationDetails& details) OVERRIDE;
167
168 // The view that is waiting for the image to load.
169 Observer* observer_;
170
171 // ID to use for next image requested. This is an ever increasing integer.
172 int next_id_;
173
174 // The object responsible for loading the image on the File thread.
175 scoped_refptr<ImageLoader> loader_;
176
177 // Information for each LoadImage request is cached here. The integer
178 // identifies the id assigned to the request.
179 LoadMap load_map_;
180
181 content::NotificationRegistrar registrar_;
182
183 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker);
184 }; 111 };
185 112
186 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 113 } // namespace extensions
114
115 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698