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

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

Issue 10701087: chromeos: Fix pixelated icons in app list and launcher (part 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Defer actual resource loading until ImageSource gets request Created 8 years, 5 months 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_LOADING_TRACKER_H_
6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string>
10 #include <vector>
9 11
10 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "chrome/common/extensions/extension_icon_set.h"
13 #include "chrome/common/extensions/extension_resource.h" 16 #include "chrome/common/extensions/extension_resource.h"
14 #include "content/public/browser/notification_observer.h" 17 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h" 18 #include "content/public/browser/notification_registrar.h"
19 #include "ui/base/layout.h"
20 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/size.h" 21 #include "ui/gfx/size.h"
17 22
18 class SkBitmap; 23 class SkBitmap;
19 24
20 namespace extensions { 25 namespace extensions {
21 class Extension; 26 class Extension;
22 } 27 }
23 28
24 namespace gfx { 29 namespace gfx {
25 class Image; 30 class Image;
26 } 31 }
27 32
28 // The views need to load their icons asynchronously but might be deleted before 33 // The views need to load their icons asynchronously but might be deleted before
29 // the images have loaded. This class encapsulates a loader class that stays 34 // the images have loaded. This class encapsulates a loader class that stays
30 // alive while the request is in progress (manages its own lifetime) and keeps 35 // alive while the request is in progress (manages its own lifetime) and keeps
31 // track of whether the view still cares about the icon loading. 36 // track of whether the view still cares about the icon loading.
32 // 37 //
33 // To use this class, have your class derive from ImageLoadingTracker::Observer, 38 // To use this class, have your class derive from ImageLoadingTracker::Observer,
34 // and add a member variable ImageLoadingTracker tracker_. Then override 39 // and add a member variable ImageLoadingTracker tracker_. Then override
35 // Observer::OnImageLoaded and call: 40 // Observer::OnImageLoaded and call:
36 // tracker_.LoadImage(extension, resource, max_size, false); 41 // tracker_.LoadImage(extension, resource, max_size, false);
37 // ... and wait for OnImageLoaded to be called back on you with a pointer to the 42 // ... and wait for OnImageLoaded to be called back on you with a pointer to the
38 // SkBitmap loaded. 43 // ImageSkia loaded.
44 //
45 // To support DIP, LoadImageSkia should be used. It returns an ImageSkia that
46 // has ImageLoadingTracker::ImageSource. ImageSource would use this class to
47 // load resource bitmap when it is asked for a representation of a scale factor.
48 // One needs to keep ImageLoadingTracker around as long as the returned
49 // ImageSkia is in use and expects OnImageLoaded to be called with updated
50 // ImageSkia when actual resource bitmap is loaded.
51 //
39 // NOTE: if the image is available already (or the resource is not valid), the 52 // NOTE: if the image is available already (or the resource is not valid), the
40 // Observer is notified immediately from the call to LoadImage. In other words, 53 // Observer is notified immediately from the call to LoadImage. In other words,
41 // by the time LoadImage returns the observer has been notified. 54 // by the time LoadImage returns the observer has been notified.
42 // 55 //
43 class ImageLoadingTracker : public content::NotificationObserver { 56 class ImageLoadingTracker : public content::NotificationObserver {
44 public: 57 public:
45 enum CacheParam { 58 enum CacheParam {
46 CACHE, 59 CACHE,
47 DONT_CACHE 60 DONT_CACHE
48 }; 61 };
49 62
50 class Observer { 63 class Observer {
51 public: 64 public:
52 // Will be called when the image with the given index has loaded. 65 // Will be called when the image with the given index has loaded.
53 // |image| can be empty if a valid image was not found or it failed to 66 // |image| can be empty if a valid image was not found or it failed to
54 // decode. |extension_id| is the ID of the extension the images are loaded 67 // decode. |extension_id| is the ID of the extension the images are loaded
55 // from. |index| represents the index of the image just loaded (starts at 0 68 // from. |index| represents the index of the image just loaded (starts at 0
56 // and increments every time LoadImage is called). 69 // and increments every time LoadImage is called).
57 virtual void OnImageLoaded(const gfx::Image& image, 70 virtual void OnImageLoaded(const gfx::Image& image,
58 const std::string& extension_id, 71 const std::string& extension_id,
59 int index) = 0; 72 int index) = 0;
60 73
61 protected: 74 protected:
62 virtual ~Observer(); 75 virtual ~Observer();
63 }; 76 };
64 77
65 // Information about a single image to load from a extension resource. 78 // Information about a single bitmap/image rep to load from an extension
66 struct ImageInfo { 79 // resource.
67 ImageInfo(const ExtensionResource& resource, gfx::Size max_size); 80 struct ImageRepInfo {
68 ~ImageInfo(); 81 enum ResizeMethod {
82 RESIZE_WHEN_LARGER,
83 ALWAYS_RESIZE,
84 };
85
86 ImageRepInfo(const ExtensionResource& resource,
87 ResizeMethod resize_method,
88 const gfx::Size& desired_size,
89 ui::ScaleFactor scale_factor);
90 ~ImageRepInfo();
91
92 // Extension resource to load.
69 ExtensionResource resource; 93 ExtensionResource resource;
70 // If the loaded image is larger than |max_size| it will be resized to those 94
71 // dimensions. 95 // Whether to resize loaded bitmap when it is larger than |desired_size| or
72 gfx::Size max_size; 96 // always resize it.
oshima 2012/07/18 15:16:28 I think it's better to move this comment to enum a
xiyuan 2012/07/18 17:28:32 Done.
97 ResizeMethod resize_method;
98
99 // When |resize_metho| is ALWAYS_RESIZE or when the loaded image is larger
100 // than |desired_size| it will be resized to those dimensions.
oshima 2012/07/18 15:16:28 remove extra space between those and dimensions.
xiyuan 2012/07/18 17:28:32 Done.
101 gfx::Size desired_size;
102
103 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
104 ui::ScaleFactor scale_factor;
oshima 2012/07/18 15:16:28 can these be const?
xiyuan 2012/07/18 17:28:32 We cannot use default copy constructor if they are
73 }; 105 };
74 106
75 explicit ImageLoadingTracker(Observer* observer); 107 explicit ImageLoadingTracker(Observer* observer);
76 virtual ~ImageLoadingTracker(); 108 virtual ~ImageLoadingTracker();
77 109
78 // Specify image resource to load. If the loaded image is larger than 110 // Specify image resource to load. If the loaded image is larger than
79 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this 111 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
80 // function may call back your observer synchronously (ie before it returns) 112 // function may call back your observer synchronously (ie before it returns)
81 // if the image was found in the cache. 113 // if the image was found in the cache.
114 // Note this method loads a raw bitmap from the resource. All sizes given are
115 // assumed to be in pixels. If you want to load a DIP-aware image, use
116 // |LoadImageSkia| instead.
82 void LoadImage(const extensions::Extension* extension, 117 void LoadImage(const extensions::Extension* extension,
83 const ExtensionResource& resource, 118 const ExtensionResource& resource,
84 const gfx::Size& max_size, 119 const gfx::Size& max_size,
85 CacheParam cache); 120 CacheParam cache);
86 121
122 // Similar to LoadImage above but loads an ImageSkia that supports DIP and
123 // always resize loaded image to |desired_size|. |resource_size| and
124 // |desired_size| are in DIP coordinates.
125 void LoadImageSkia(const extensions::Extension* extension,
126 int resource_size,
127 ExtensionIconSet::MatchType resource_match_type,
128 const gfx::Size& desired_size,
129 CacheParam cache);
130
87 // Same as LoadImage() above except it loads multiple images from the same 131 // Same as LoadImage() above except it loads multiple images from the same
88 // extension. This is used to load multiple resolutions of the same image 132 // extension. This is used to load multiple resolutions of the same image
89 // type. 133 // type.
90 void LoadImages(const extensions::Extension* extension, 134 void LoadImages(const extensions::Extension* extension,
91 const std::vector<ImageInfo>& info_list, 135 const std::vector<ImageRepInfo>& info_list,
92 CacheParam cache); 136 CacheParam cache);
93 137
94 // Returns the ID used for the next image that is loaded. That is, the return 138 // Returns the ID used for the next image that is loaded. That is, the return
95 // value from this method corresponds to the int that is passed to 139 // value from this method corresponds to the int that is passed to
96 // OnImageLoaded() the next time LoadImage() is invoked. 140 // OnImageLoaded() the next time LoadImage() is invoked.
97 int next_id() const { return next_id_; } 141 int next_id() const { return next_id_; }
98 142
99 private: 143 private:
100 // Information for pending image load operation for one or more images. 144 class ImageLoader;
145 class ImageSource;
146
147 // Information for pending resource load operation for one or more
148 // bitmaps/image reps.
101 struct PendingLoadInfo { 149 struct PendingLoadInfo {
102 PendingLoadInfo(); 150 PendingLoadInfo();
103 ~PendingLoadInfo(); 151 ~PendingLoadInfo();
104 152
105 const extensions::Extension* extension; 153 const extensions::Extension* extension;
106 // This is cached separate from |extension| in case the extension in 154 // This is cached separate from |extension| in case the extension is
107 // unloaded. 155 // unloaded.
108 std::string extension_id; 156 std::string extension_id;
157
158 // Size info from a LoadImage call.
159 int resource_size_in_dip;
160 ExtensionIconSet::MatchType resource_match_type;
161 gfx::Size desired_size_in_dip;
162
163 // ImageSource loads image for additional scale factor. This is only set if
164 // above size info is valid.
165 ImageSource* image_source; // Owned by ImageSkiaStorage.
166
109 CacheParam cache; 167 CacheParam cache;
110 size_t pending_count; 168 size_t pending_count;
111 std::vector<SkBitmap> bitmaps; 169
170 gfx::ImageSkia image_skia;
112 }; 171 };
113 172
114 // Maps an integer identifying a load request to a PendingLoadInfo. 173 // Maps an integer identifying a load request to a PendingLoadInfo.
115 typedef std::map<int, PendingLoadInfo> LoadMap; 174 typedef std::map<int, PendingLoadInfo> LoadMap;
116 175
117 class ImageLoader; 176 // Loads bitmap resources in |info_list| for load request identified by |id|.
177 void DoLoadImage(int id, const std::vector<ImageRepInfo>& info_list);
118 178
119 // When an image has finished loaded and been resized on the file thread, it 179 // Loads bitmap for additional scale factor for given load |id|.
180 void LoadImageForScaleFactor(int id, ui::ScaleFactor scale_factor);
181
182 // When a bitmap has finished loading and been resized on the file thread, it
120 // is posted back to this method on the original thread. This method then 183 // is posted back to this method on the original thread. This method then
121 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if 184 // calls the observer's OnImageLoaded if it was the last image in the list.
122 // it was the last image in the list. The |original_size| should be the size 185 // The observer could delete the ImageLoadingTracker if it does not care about
123 // of the image before any resizing was done. 186 // DIP scale changes afterwards. The |original_size| should be the size of the
124 // |image| may be null if the file failed to decode. 187 // bitmap before any resizing was done.
125 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, 188 // |bitmap| may be null if the file failed to decode.
126 const gfx::Size& original_size, int id, bool should_cache); 189 void OnBitmapLoaded(SkBitmap* bitmap,
190 const ImageRepInfo& image_info,
191 const gfx::Size& original_size,
192 int id,
193 bool should_cache);
127 194
128 // Checks whether image is a component extension resource. Returns false 195 // Checks whether image is a component extension resource. Returns false
129 // if a given |resource| does not have a corresponding image in bundled 196 // if a given |resource| does not have a corresponding image in bundled
130 // resources. Otherwise fills |resource_id|. 197 // resources. Otherwise fills |resource_id|.
131 bool IsComponentExtensionResource(const extensions::Extension* extension, 198 bool IsComponentExtensionResource(const extensions::Extension* extension,
132 const ExtensionResource& resource, 199 const ExtensionResource& resource,
133 int& resource_id) const; 200 int& resource_id) const;
134 201
135 // content::NotificationObserver method. If an extension is uninstalled while 202 // content::NotificationObserver method. If an extension is uninstalled while
136 // we're waiting for the image we remove the entry from load_map_. 203 // we're waiting for the image we remove the entry from load_map_.
(...skipping 16 matching lines...) Expand all
153 220
154 content::NotificationRegistrar registrar_; 221 content::NotificationRegistrar registrar_;
155 222
156 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, 223 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest,
157 IsComponentExtensionResource); 224 IsComponentExtensionResource);
158 225
159 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); 226 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker);
160 }; 227 };
161 228
162 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 229 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698