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

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

Issue 9428025: Add support for multiple icon sizes for Mac platform apps (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 9 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 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "chrome/common/extensions/extension_resource.h"
13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_registrar.h"
16 #include "ui/gfx/size.h"
15 17
16 class Extension; 18 class Extension;
17 class ExtensionResource;
18 class SkBitmap; 19 class SkBitmap;
19 20
20 namespace gfx { 21 namespace gfx {
21 class Size; 22 class Image;
22 } 23 }
23 24
24 // The views need to load their icons asynchronously but might be deleted before 25 // The views need to load their icons asynchronously but might be deleted before
25 // the images have loaded. This class encapsulates a loader class that stays 26 // the images have loaded. This class encapsulates a loader class that stays
26 // alive while the request is in progress (manages its own lifetime) and keeps 27 // alive while the request is in progress (manages its own lifetime) and keeps
27 // track of whether the view still cares about the icon loading. 28 // track of whether the view still cares about the icon loading.
28 // 29 //
29 // To use this class, have your class derive from ImageLoadingTracker::Observer, 30 // To use this class, have your class derive from ImageLoadingTracker::Observer,
30 // and add a member variable ImageLoadingTracker tracker_. Then override 31 // and add a member variable ImageLoadingTracker tracker_. Then override
31 // Observer::OnImageLoaded and call: 32 // Observer::OnImageLoaded and call:
32 // tracker_.LoadImage(extension, resource, max_size, false); 33 // tracker_.LoadImage(extension, resource, max_size, false);
33 // ... and wait for OnImageLoaded to be called back on you with a pointer to the 34 // ... and wait for OnImageLoaded to be called back on you with a pointer to the
34 // SkBitmap loaded. 35 // SkBitmap loaded.
35 // NOTE: if the image is available already (or the resource is not valid), the 36 // NOTE: if the image is available already (or the resource is not valid), the
36 // Observer is notified immediately from the call to LoadImage. In other words, 37 // Observer is notified immediately from the call to LoadImage. In other words,
37 // by the time LoadImage returns the observer has been notified. 38 // by the time LoadImage returns the observer has been notified.
38 // 39 //
39 class ImageLoadingTracker : public content::NotificationObserver { 40 class ImageLoadingTracker : public content::NotificationObserver {
40 public: 41 public:
41 enum CacheParam { 42 enum CacheParam {
42 CACHE, 43 CACHE,
43 DONT_CACHE 44 DONT_CACHE
44 }; 45 };
45 46
46 class Observer { 47 class Observer {
47 public: 48 public:
48 // Will be called when the image with the given index has loaded. 49 // Will be called when the image with the given index has loaded.
49 // The |image| is owned by the tracker, so the observer should make a copy 50 // |image| can be empty if a valid image was not found or it failed to
50 // if they need to access it after this call. |image| can be null if a valid 51 // decode. |extension_id| is the ID of the extension the images are loaded
51 // image was not found or it failed to decode. |resource| is the 52 // from. |index| represents the index of the image just loaded (starts at 0
52 // ExtensionResource where the |image| came from and the |index| represents 53 // and increments every time LoadImage is called).
53 // the index of the image just loaded (starts at 0 and increments every 54 virtual void OnImageLoaded(const gfx::Image& image,
54 // time LoadImage is called). 55 const std::string& extension_id,
55 virtual void OnImageLoaded(SkBitmap* image,
56 const ExtensionResource& resource,
57 int index) = 0; 56 int index) = 0;
58 57
59 protected: 58 protected:
60 virtual ~Observer(); 59 virtual ~Observer();
61 }; 60 };
62 61
62 // Information about a single image to load from a extension resource.
63 struct ImageInfo {
64 ImageInfo(const ExtensionResource resource, gfx::Size max_size);
65 ~ImageInfo();
66 ExtensionResource resource;
67 gfx::Size max_size;
Finnur 2012/02/28 10:26:51 I think it is worth calling out that the image wil
sail 2012/02/28 23:25:36 Done.
sail 2012/02/28 23:25:36 Done.
68 };
69
63 explicit ImageLoadingTracker(Observer* observer); 70 explicit ImageLoadingTracker(Observer* observer);
64 virtual ~ImageLoadingTracker(); 71 virtual ~ImageLoadingTracker();
65 72
66 // Specify image resource to load. If the loaded image is larger than 73 // Specify image resource to load. If the loaded image is larger than
67 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this 74 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this
68 // function may call back your observer synchronously (ie before it returns) 75 // function may call back your observer synchronously (ie before it returns)
69 // if the image was found in the cache. 76 // if the image was found in the cache.
70 void LoadImage(const Extension* extension, 77 void LoadImage(const Extension* extension,
71 const ExtensionResource& resource, 78 const ExtensionResource& resource,
72 const gfx::Size& max_size, 79 const gfx::Size& max_size,
73 CacheParam cache); 80 CacheParam cache);
74 81
82 // Same as LoadImage() above except it loads multiple images from the same
Finnur 2012/02/28 10:26:51 nit: It bothers me a bit that we can't agree on wh
sail 2012/02/28 23:25:36 How about renaming this function LoadImage?
Finnur 2012/02/29 00:17:26 The style guide doesn't like functions with same n
83 // extension. This is used to load multiple resolutions of the same image
84 // type.
85 void LoadImages(const Extension* extension,
86 const std::vector<ImageInfo>& info_list,
87 CacheParam cache);
88
75 // Returns the ID used for the next image that is loaded. That is, the return 89 // Returns the ID used for the next image that is loaded. That is, the return
76 // value from this method corresponds to the int that is passed to 90 // value from this method corresponds to the int that is passed to
77 // OnImageLoaded() the next time LoadImage() is invoked. 91 // OnImageLoaded() the next time LoadImage() is invoked.
78 int next_id() const { return next_id_; } 92 int next_id() const { return next_id_; }
79 93
80 private: 94 private:
81 typedef std::map<int, const Extension*> LoadMap; 95 // Information for pending image load operation for one or more images.
96 struct PendingLoadInfo {
97 PendingLoadInfo();
98 ~PendingLoadInfo();
99
100 const Extension* extension;
101 // This is cached separate from |extension| incase the extension in
Finnur 2012/02/28 10:26:51 nit: incase -> in case
sail 2012/02/28 23:25:36 Done.
102 // unloaded.
103 std::string extension_id;
104 CacheParam cache;
105 size_t pending_count;
106 std::vector<SkBitmap> bitmaps;
107 };
108
109 // Maps an integer identifying a load request to a PendingLoadInfo.
110 typedef std::map<int, PendingLoadInfo> LoadMap;
82 111
83 class ImageLoader; 112 class ImageLoader;
84 113
85 // When an image has finished loaded and been resized on the file thread, it 114 // When an image has finished loaded and been resized on the file thread, it
86 // is posted back to this method on the original thread. This method then 115 // is posted back to this method on the original thread. This method then
87 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if 116 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if
88 // it was the last image in the list. The |original_size| should be the size 117 // it was the last image in the list. The |original_size| should be the size
89 // of the image before any resizing was done. 118 // of the image before any resizing was done.
90 // |image| may be null if the file failed to decode. 119 // |image| may be null if the file failed to decode.
91 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, 120 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource,
92 const gfx::Size& original_size, int id); 121 const gfx::Size& original_size, int id);
93 122
94 // content::NotificationObserver method. If an extension is uninstalled while 123 // content::NotificationObserver method. If an extension is uninstalled while
95 // we're waiting for the image we remove the entry from load_map_. 124 // we're waiting for the image we remove the entry from load_map_.
96 virtual void Observe(int type, 125 virtual void Observe(int type,
97 const content::NotificationSource& source, 126 const content::NotificationSource& source,
98 const content::NotificationDetails& details) OVERRIDE; 127 const content::NotificationDetails& details) OVERRIDE;
99 128
100 // The view that is waiting for the image to load. 129 // The view that is waiting for the image to load.
101 Observer* observer_; 130 Observer* observer_;
102 131
103 // ID to use for next image requested. This is an ever increasing integer. 132 // ID to use for next image requested. This is an ever increasing integer.
104 int next_id_; 133 int next_id_;
105 134
106 // The object responsible for loading the image on the File thread. 135 // The object responsible for loading the image on the File thread.
107 scoped_refptr<ImageLoader> loader_; 136 scoped_refptr<ImageLoader> loader_;
108 137
109 // If LoadImage is told to cache the result an entry is added here. The 138 // Information for each LoadImage request is cached here. The integer
110 // integer identifies the id assigned to the request. If the extension is 139 // identifies the id assigned to the request.
Finnur 2012/02/28 10:26:51 nit: The reference to 'The integer' is probably a
111 // deleted while fetching the image the entry is removed from the map.
112 LoadMap load_map_; 140 LoadMap load_map_;
113 141
114 content::NotificationRegistrar registrar_; 142 content::NotificationRegistrar registrar_;
115 143
116 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); 144 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker);
117 }; 145 };
118 146
119 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ 147 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698