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

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

Powered by Google App Engine
This is Rietveld 408576698