OLD | NEW |
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 // If the loaded image is larger than |max_size| it will be resized to those |
| 68 // dimensions. |
| 69 gfx::Size max_size; |
| 70 }; |
| 71 |
63 explicit ImageLoadingTracker(Observer* observer); | 72 explicit ImageLoadingTracker(Observer* observer); |
64 virtual ~ImageLoadingTracker(); | 73 virtual ~ImageLoadingTracker(); |
65 | 74 |
66 // Specify image resource to load. If the loaded image is larger than | 75 // 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 | 76 // |max_size| it will be resized to those dimensions. IMPORTANT NOTE: this |
68 // function may call back your observer synchronously (ie before it returns) | 77 // function may call back your observer synchronously (ie before it returns) |
69 // if the image was found in the cache. | 78 // if the image was found in the cache. |
70 void LoadImage(const Extension* extension, | 79 void LoadImage(const Extension* extension, |
71 const ExtensionResource& resource, | 80 const ExtensionResource& resource, |
72 const gfx::Size& max_size, | 81 const gfx::Size& max_size, |
73 CacheParam cache); | 82 CacheParam cache); |
74 | 83 |
| 84 // Same as LoadImage() above except it loads multiple images from the same |
| 85 // extension. This is used to load multiple resolutions of the same image |
| 86 // type. |
| 87 void LoadImages(const Extension* extension, |
| 88 const std::vector<ImageInfo>& info_list, |
| 89 CacheParam cache); |
| 90 |
75 // Returns the ID used for the next image that is loaded. That is, the return | 91 // 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 | 92 // value from this method corresponds to the int that is passed to |
77 // OnImageLoaded() the next time LoadImage() is invoked. | 93 // OnImageLoaded() the next time LoadImage() is invoked. |
78 int next_id() const { return next_id_; } | 94 int next_id() const { return next_id_; } |
79 | 95 |
80 private: | 96 private: |
81 typedef std::map<int, const Extension*> LoadMap; | 97 // Information for pending image load operation for one or more images. |
| 98 struct PendingLoadInfo { |
| 99 PendingLoadInfo(); |
| 100 ~PendingLoadInfo(); |
| 101 |
| 102 const Extension* extension; |
| 103 // This is cached separate from |extension| in case the extension in |
| 104 // unloaded. |
| 105 std::string extension_id; |
| 106 CacheParam cache; |
| 107 size_t pending_count; |
| 108 std::vector<SkBitmap> bitmaps; |
| 109 }; |
| 110 |
| 111 // Maps an integer identifying a load request to a PendingLoadInfo. |
| 112 typedef std::map<int, PendingLoadInfo> LoadMap; |
82 | 113 |
83 class ImageLoader; | 114 class ImageLoader; |
84 | 115 |
85 // When an image has finished loaded and been resized on the file thread, it | 116 // 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 | 117 // is posted back to this method on the original thread. This method then |
87 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if | 118 // 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 | 119 // it was the last image in the list. The |original_size| should be the size |
89 // of the image before any resizing was done. | 120 // of the image before any resizing was done. |
90 // |image| may be null if the file failed to decode. | 121 // |image| may be null if the file failed to decode. |
91 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, | 122 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, |
92 const gfx::Size& original_size, int id); | 123 const gfx::Size& original_size, int id, bool should_cache); |
93 | 124 |
94 // content::NotificationObserver method. If an extension is uninstalled while | 125 // content::NotificationObserver method. If an extension is uninstalled while |
95 // we're waiting for the image we remove the entry from load_map_. | 126 // we're waiting for the image we remove the entry from load_map_. |
96 virtual void Observe(int type, | 127 virtual void Observe(int type, |
97 const content::NotificationSource& source, | 128 const content::NotificationSource& source, |
98 const content::NotificationDetails& details) OVERRIDE; | 129 const content::NotificationDetails& details) OVERRIDE; |
99 | 130 |
100 // The view that is waiting for the image to load. | 131 // The view that is waiting for the image to load. |
101 Observer* observer_; | 132 Observer* observer_; |
102 | 133 |
103 // ID to use for next image requested. This is an ever increasing integer. | 134 // ID to use for next image requested. This is an ever increasing integer. |
104 int next_id_; | 135 int next_id_; |
105 | 136 |
106 // The object responsible for loading the image on the File thread. | 137 // The object responsible for loading the image on the File thread. |
107 scoped_refptr<ImageLoader> loader_; | 138 scoped_refptr<ImageLoader> loader_; |
108 | 139 |
109 // If LoadImage is told to cache the result an entry is added here. The | 140 // Information for each LoadImage request is cached here. The integer |
110 // integer identifies the id assigned to the request. If the extension is | 141 // identifies the id assigned to the request. |
111 // deleted while fetching the image the entry is removed from the map. | |
112 LoadMap load_map_; | 142 LoadMap load_map_; |
113 | 143 |
114 content::NotificationRegistrar registrar_; | 144 content::NotificationRegistrar registrar_; |
115 | 145 |
116 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 146 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
117 }; | 147 }; |
118 | 148 |
119 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 149 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |