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

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

Issue 10825012: chromeos: Fix pixelated icons in app list and launcher (part 2) (by xiyuan) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 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.
39 // NOTE: if the image is available already (or the resource is not valid), the 44 // 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, 45 // Observer is notified immediately from the call to LoadImage. In other words,
41 // by the time LoadImage returns the observer has been notified. 46 // by the time LoadImage returns the observer has been notified.
42 // 47 //
43 class ImageLoadingTracker : public content::NotificationObserver { 48 class ImageLoadingTracker : public content::NotificationObserver {
44 public: 49 public:
45 enum CacheParam { 50 enum CacheParam {
46 CACHE, 51 CACHE,
47 DONT_CACHE 52 DONT_CACHE
48 }; 53 };
49 54
50 class Observer { 55 class Observer {
51 public: 56 public:
52 // Will be called when the image with the given index has loaded. 57 // 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 58 // |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 59 // 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 60 // from. |index| represents the index of the image just loaded (starts at 0
56 // and increments every time LoadImage is called). 61 // and increments every time LoadImage is called).
57 virtual void OnImageLoaded(const gfx::Image& image, 62 virtual void OnImageLoaded(const gfx::Image& image,
58 const std::string& extension_id, 63 const std::string& extension_id,
59 int index) = 0; 64 int index) = 0;
60 65
61 protected: 66 protected:
62 virtual ~Observer(); 67 virtual ~Observer();
63 }; 68 };
64 69
65 // Information about a single image to load from a extension resource. 70 // Information about a singe image representation to load from an extension
66 struct ImageInfo { 71 // resource.
67 ImageInfo(const ExtensionResource& resource, gfx::Size max_size); 72 struct ImageRepresentation {
68 ~ImageInfo(); 73 // Enum values to indicate whether to resize loaded bitmap when it is larger
74 // than |desired_size| or always resize it.
75 enum ResizeCondition {
76 RESIZE_WHEN_LARGER,
77 ALWAYS_RESIZE,
78 };
79
80 ImageRepresentation(const ExtensionResource& resource,
81 ResizeCondition resize_method,
82 const gfx::Size& desired_size,
83 ui::ScaleFactor scale_factor);
84 ~ImageRepresentation();
85
86 // Extension resource to load.
69 ExtensionResource resource; 87 ExtensionResource resource;
70 // If the loaded image is larger than |max_size| it will be resized to those 88
71 // dimensions. 89 ResizeCondition resize_method;
72 gfx::Size max_size; 90
91 // When |resize_method| is ALWAYS_RESIZE or when the loaded image is larger
92 // than |desired_size| it will be resized to these dimensions.
93 gfx::Size desired_size;
94
95 // |scale_factor| is used to construct the loaded gfx::ImageSkia.
96 ui::ScaleFactor scale_factor;
73 }; 97 };
74 98
75 explicit ImageLoadingTracker(Observer* observer); 99 explicit ImageLoadingTracker(Observer* observer);
76 virtual ~ImageLoadingTracker(); 100 virtual ~ImageLoadingTracker();
77 101
78 // Specify image resource to load. If the loaded image is larger than 102 // 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 103 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
80 // function may call back your observer synchronously (ie before it returns) 104 // function may call back your observer synchronously (ie before it returns)
81 // if the image was found in the cache. 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.
82 void LoadImage(const extensions::Extension* extension, 108 void LoadImage(const extensions::Extension* extension,
83 const ExtensionResource& resource, 109 const ExtensionResource& resource,
84 const gfx::Size& max_size, 110 const gfx::Size& max_size,
85 CacheParam cache); 111 CacheParam cache);
86 112
87 // Same as LoadImage() above except it loads multiple images from the same 113 // 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 114 // extension. This is used to load multiple resolutions of the same image
89 // type. 115 // type.
90 void LoadImages(const extensions::Extension* extension, 116 void LoadImages(const extensions::Extension* extension,
91 const std::vector<ImageInfo>& info_list, 117 const std::vector<ImageRepresentation>& info_list,
92 CacheParam cache); 118 CacheParam cache);
93 119
94 // Returns the ID used for the next image that is loaded. That is, the return 120 // 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 121 // value from this method corresponds to the int that is passed to
96 // OnImageLoaded() the next time LoadImage() is invoked. 122 // OnImageLoaded() the next time LoadImage() is invoked.
97 int next_id() const { return next_id_; } 123 int next_id() const { return next_id_; }
98 124
99 private: 125 private:
100 // Information for pending image load operation for one or more images. 126 // Information for pending resource load operation for one or more image
127 // representations.
101 struct PendingLoadInfo { 128 struct PendingLoadInfo {
102 PendingLoadInfo(); 129 PendingLoadInfo();
103 ~PendingLoadInfo(); 130 ~PendingLoadInfo();
104 131
105 const extensions::Extension* extension; 132 const extensions::Extension* extension;
106 // This is cached separate from |extension| in case the extension in 133 // This is cached separate from |extension| in case the extension is
107 // unloaded. 134 // unloaded.
108 std::string extension_id; 135 std::string extension_id;
109 CacheParam cache; 136 CacheParam cache;
110 size_t pending_count; 137 size_t pending_count;
111 std::vector<SkBitmap> bitmaps; 138 gfx::ImageSkia image_skia;
112 }; 139 };
113 140
114 // Maps an integer identifying a load request to a PendingLoadInfo. 141 // Maps an integer identifying a load request to a PendingLoadInfo.
115 typedef std::map<int, PendingLoadInfo> LoadMap; 142 typedef std::map<int, PendingLoadInfo> LoadMap;
116 143
117 class ImageLoader; 144 class ImageLoader;
118 145
119 // When an image has finished loaded and been resized on the file thread, it 146 // Called on the calling thread when the bitmap finishes loading.
120 // is posted back to this method on the original thread. This method then 147 // |bitmap| may be null if the image file failed to decode.
121 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if 148 void OnBitmapLoaded(const SkBitmap* bitmap,
122 // it was the last image in the list. The |original_size| should be the size 149 const ImageRepresentation& image_info,
123 // of the image before any resizing was done. 150 const gfx::Size& original_size,
124 // |image| may be null if the file failed to decode. 151 int id,
125 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, 152 bool should_cache);
126 const gfx::Size& original_size, int id, bool should_cache);
127 153
128 // Checks whether image is a component extension resource. Returns false 154 // Checks whether image is a component extension resource. Returns false
129 // if a given |resource| does not have a corresponding image in bundled 155 // if a given |resource| does not have a corresponding image in bundled
130 // resources. Otherwise fills |resource_id|. 156 // resources. Otherwise fills |resource_id|.
131 bool IsComponentExtensionResource(const extensions::Extension* extension, 157 bool IsComponentExtensionResource(const extensions::Extension* extension,
132 const ExtensionResource& resource, 158 const ExtensionResource& resource,
133 int& resource_id) const; 159 int& resource_id) const;
134 160
135 // content::NotificationObserver method. If an extension is uninstalled while 161 // content::NotificationObserver method. If an extension is uninstalled while
136 // we're waiting for the image we remove the entry from load_map_. 162 // we're waiting for the image we remove the entry from load_map_.
(...skipping 16 matching lines...) Expand all
153 179
154 content::NotificationRegistrar registrar_; 180 content::NotificationRegistrar registrar_;
155 181
156 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest, 182 FRIEND_TEST_ALL_PREFIXES(ImageLoadingTrackerTest,
157 IsComponentExtensionResource); 183 IsComponentExtensionResource);
158 184
159 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); 185 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker);
160 }; 186 };
161 187
162 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 188 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_icon_image_unittest.cc ('k') | chrome/browser/extensions/image_loading_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698