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: |
(...skipping 13 matching lines...) Expand all Loading... | |
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 // The |image| is owned by the tracker, so the observer should make a copy |
50 // if they need to access it after this call. |image| can be null if a valid | 51 // if they need to access it after this call. |image| can be null if a valid |
51 // image was not found or it failed to decode. |resource| is the | 52 // image was not found or it failed to decode. |resource| is the |
52 // ExtensionResource where the |image| came from and the |index| represents | 53 // ExtensionResource where the |image| came from and the |index| represents |
53 // the index of the image just loaded (starts at 0 and increments every | 54 // the index of the image just loaded (starts at 0 and increments every |
54 // time LoadImage is called). | 55 // time LoadImage is called). |
55 virtual void OnImageLoaded(SkBitmap* image, | 56 virtual void OnImageLoaded(const gfx::Image* image, |
56 const ExtensionResource& resource, | 57 const std::string& extension_id, |
57 int index) = 0; | 58 int index) = 0; |
58 | 59 |
59 protected: | 60 protected: |
60 virtual ~Observer(); | 61 virtual ~Observer(); |
61 }; | 62 }; |
62 | 63 |
64 struct ImageInfo { | |
65 ImageInfo(const ExtensionResource resource, gfx::Size max_size); | |
Finnur
2012/02/23 12:05:42
nit: Personally, I would define the dtor in the .c
sail
2012/02/27 23:58:37
Done.
| |
66 ExtensionResource resource; | |
67 gfx::Size max_size; | |
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 | |
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 struct PendingLoadInfo { |
Finnur
2012/02/23 12:05:42
nit: Document this struct (and other struct above)
sail
2012/02/27 23:58:37
Done.
| |
96 PendingLoadInfo(); | |
97 ~PendingLoadInfo(); | |
98 | |
99 const Extension* extension; | |
100 std::string extension_id; | |
Finnur
2012/02/23 12:05:42
Why do you need this if you have the pointer to th
sail
2012/02/27 23:58:37
This is needed incase the extension is unload. Add
| |
101 CacheParam cache; | |
102 size_t pending_count; | |
103 std::vector<SkBitmap> bitmaps; | |
104 }; | |
105 | |
106 typedef std::map<int, PendingLoadInfo> LoadMap; | |
Finnur
2012/02/23 12:05:42
Not your fault, but can you document this mapping?
sail
2012/02/27 23:58:37
Done.
| |
82 | 107 |
83 class ImageLoader; | 108 class ImageLoader; |
84 | 109 |
85 // When an image has finished loaded and been resized on the file thread, it | 110 // 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 | 111 // is posted back to this method on the original thread. This method then |
87 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if | 112 // 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 | 113 // it was the last image in the list. The |original_size| should be the size |
89 // of the image before any resizing was done. | 114 // of the image before any resizing was done. |
90 // |image| may be null if the file failed to decode. | 115 // |image| may be null if the file failed to decode. |
91 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, | 116 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource, |
(...skipping 18 matching lines...) Expand all Loading... | |
110 // integer identifies the id assigned to the request. If the extension is | 135 // integer identifies the id assigned to the request. If the extension is |
111 // deleted while fetching the image the entry is removed from the map. | 136 // deleted while fetching the image the entry is removed from the map. |
112 LoadMap load_map_; | 137 LoadMap load_map_; |
113 | 138 |
114 content::NotificationRegistrar registrar_; | 139 content::NotificationRegistrar registrar_; |
115 | 140 |
116 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 141 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
117 }; | 142 }; |
118 | 143 |
119 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 144 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
OLD | NEW |