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

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: add missing files 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" 13 #include "chrome/browser/profiles/profile_keyed_service.h"
15 #include "chrome/common/extensions/extension_icon_set.h"
16 #include "chrome/common/extensions/extension_resource.h" 14 #include "chrome/common/extensions/extension_resource.h"
17 #include "content/public/browser/notification_observer.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "ui/base/layout.h" 16 #include "ui/base/layout.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/size.h" 17 #include "ui/gfx/size.h"
22 18
23 class SkBitmap; 19 FORWARD_DECLARE_TEST(ImageLoaderTest, LoadImage);
20 FORWARD_DECLARE_TEST(ImageLoaderTest, DeleteExtensionWhileWaitingForCache);
21 FORWARD_DECLARE_TEST(ImageLoaderTest, MultipleImages);
24 22
25 namespace extensions { 23 class Profile;
26 class Extension;
27 }
28 24
29 namespace gfx { 25 namespace gfx {
30 class Image; 26 class Image;
31 } 27 }
32 28
33 // The views need to load their icons asynchronously but might be deleted before 29 namespace extensions {
34 // the images have loaded. This class encapsulates a loader class that stays 30
35 // alive while the request is in progress (manages its own lifetime) and keeps 31 class Extension;
36 // track of whether the view still cares about the icon loading. 32
37 // 33 // This class is responsible for asynchronously loading extension images and
38 // To use this class, have your class derive from ImageLoadingTracker::Observer, 34 // calling a callback when an image is loaded.
39 // and add a member variable ImageLoadingTracker tracker_. Then override 35 // The views need to load their icons asynchronously might be deleted before
40 // Observer::OnImageLoaded and call: 36 // the images have loaded. If you pass your callback using a weak_ptr, this
41 // tracker_.LoadImage(extension, resource, max_size, false); 37 // 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 38 class ImageLoader : public ProfileKeyedService {
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: 39 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 40 // Information about a singe image representation to load from an extension
71 // resource. 41 // resource.
72 struct ImageRepresentation { 42 struct ImageRepresentation {
73 // Enum values to indicate whether to resize loaded bitmap when it is larger 43 // Enum values to indicate whether to resize loaded bitmap when it is larger
74 // than |desired_size| or always resize it. 44 // than |desired_size| or always resize it.
75 enum ResizeCondition { 45 enum ResizeCondition {
76 RESIZE_WHEN_LARGER, 46 RESIZE_WHEN_LARGER,
77 ALWAYS_RESIZE, 47 ALWAYS_RESIZE,
78 }; 48 };
79 49
80 ImageRepresentation(const ExtensionResource& resource, 50 ImageRepresentation(const ExtensionResource& resource,
81 ResizeCondition resize_method, 51 ResizeCondition resize_condition,
82 const gfx::Size& desired_size, 52 const gfx::Size& desired_size,
83 ui::ScaleFactor scale_factor); 53 ui::ScaleFactor scale_factor);
84 ~ImageRepresentation(); 54 ~ImageRepresentation();
85 55
86 // Extension resource to load. 56 // Extension resource to load.
87 ExtensionResource resource; 57 ExtensionResource resource;
88 58
89 ResizeCondition resize_method; 59 ResizeCondition resize_condition;
90 60
91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger 61 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger
92 // than |desired_size| it will be resized to these dimensions. 62 // than |desired_size| it will be resized to these dimensions.
93 gfx::Size desired_size; 63 gfx::Size desired_size;
94 64
95 // |scale_factor| is used to construct the loaded gfx::ImageSkia. 65 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
96 ui::ScaleFactor scale_factor; 66 ui::ScaleFactor scale_factor;
97 }; 67 };
98 68
99 explicit ImageLoadingTracker(Observer* observer); 69 // Returns the instance for the given profile, or NULL if none. This is
100 virtual ~ImageLoadingTracker(); 70 // a convenience wrapper around ImageLoaderFactory::GetForProfile.
71 static ImageLoader* Get(Profile* profile);
101 72
102 // Specify image resource to load. If the loaded image is larger than 73 ImageLoader();
103 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this 74 virtual ~ImageLoader();
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 75
125 // Checks whether image is a component extension resource. Returns false 76 // Checks whether image is a component extension resource. Returns false
126 // if a given |resource| does not have a corresponding image in bundled 77 // if a given |resource| does not have a corresponding image in bundled
127 // resources. Otherwise fills |resource_id|. 78 // resources. Otherwise fills |resource_id|.
128 static bool IsComponentExtensionResource( 79 static bool IsComponentExtensionResource(
129 const extensions::Extension* extension, 80 const extensions::Extension* extension,
130 const FilePath& resource_path, 81 const FilePath& resource_path,
131 int* resource_id); 82 int* resource_id);
132 83
84 // Specify image resource to load. If the loaded image is larger than
85 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
86 // function may call back your callback synchronously (ie before it returns)
87 // if the image was found in the cache.
88 // Note this method loads a raw bitmap from the resource. All sizes given are
89 // assumed to be in pixels.
90 void LoadImageAsync(const extensions::Extension* extension,
91 const ExtensionResource& resource,
92 const gfx::Size& max_size,
93 const base::Callback<void(const gfx::Image&)>& callback);
94
95 // Same as LoadImage() above except it loads multiple images from the same
96 // extension. This is used to load multiple resolutions of the same image
97 // type.
98 void LoadImagesAsync(const extensions::Extension* extension,
99 const std::vector<ImageRepresentation>& info_list,
100 const base::Callback<void(const gfx::Image&)>& callback);
101
133 private: 102 private:
134 // Information for pending resource load operation for one or more image 103 struct LoadResult;
135 // representations.
136 struct PendingLoadInfo {
137 PendingLoadInfo();
138 ~PendingLoadInfo();
139 104
140 const extensions::Extension* extension; 105 void LoadImagesOnBlockingPool(
141 // This is cached separate from |extension| in case the extension is 106 const std::vector<ImageRepresentation>& info_list,
142 // unloaded. 107 const std::vector<SkBitmap>& bitmaps,
143 std::string extension_id; 108 const base::Callback<void(const gfx::Image&)>& callback);
144 CacheParam cache;
145 size_t pending_count;
146 gfx::ImageSkia image_skia;
147 };
148 109
149 // Maps an integer identifying a load request to a PendingLoadInfo. 110 void ReplyBack(
150 typedef std::map<int, PendingLoadInfo> LoadMap; 111 const std::vector<LoadResult>& load_result,
112 const base::Callback<void(const gfx::Image&)>& callback);
151 113
152 class ImageLoader; 114 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest, LoadImage);
153 115 FRIEND_TEST_ALL_PREFIXES(::ImageLoaderTest,
154 // Called on the calling thread when the bitmap finishes loading. 116 DeleteExtensionWhileWaitingForCache);
155 // |bitmap| may be null if the image file failed to decode. 117 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 }; 118 };
185 119
186 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 120 } // namespace extensions
121
122 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698