| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
| 9 #include "chrome/common/extensions/extension.h" |
| 9 | 10 |
| 10 class ExtensionResource; | 11 class ExtensionResource; |
| 11 class SkBitmap; | 12 class SkBitmap; |
| 12 | 13 |
| 13 namespace gfx { | 14 namespace gfx { |
| 14 class Size; | 15 class Size; |
| 15 } | 16 } |
| 16 | 17 |
| 17 // The views need to load their icons asynchronously but might be deleted before | 18 // The views need to load their icons asynchronously but might be deleted before |
| 18 // the images have loaded. This class stays alive while the request is in | 19 // the images have loaded. This class encapsulates a loader class that stays |
| 19 // progress (manages its own lifetime) and keeps track of whether the view still | 20 // alive while the request is in progress (manages its own lifetime) and keeps |
| 20 // cares about the icon loading. | 21 // track of whether the view still cares about the icon loading. |
| 21 // Consider abstracting out a FilePathProvider (ExtensionResource) and moving | 22 // |
| 22 // back to chrome/browser/ if other subsystems want to use it. | 23 // To use this class, have your class derive from ImageLoadingTracker::Observer, |
| 23 class ImageLoadingTracker | 24 // and add a member variable ImageLoadingTracker tracker_. Then override |
| 24 : public base::RefCountedThreadSafe<ImageLoadingTracker> { | 25 // Observer::OnImageLoaded and call: |
| 26 // tracker_.LoadImage(resource, max_size); |
| 27 // ... and wait for OnImageLoaded to be called back on you with a pointer to the |
| 28 // SkBitmap loaded. |
| 29 class ImageLoadingTracker { |
| 25 public: | 30 public: |
| 26 class Observer { | 31 class Observer { |
| 27 public: | 32 public: |
| 28 // Will be called when the image with the given index has loaded. | 33 // Will be called when the image with the given index has loaded. |
| 29 // The |image| is owned by the tracker, so the observer should make a copy | 34 // The |image| is owned by the tracker, so the observer should make a copy |
| 30 // if they need to access it after this call. | 35 // if they need to access it after this call. |image| can be null if valid |
| 31 virtual void OnImageLoaded(SkBitmap* image, size_t index) = 0; | 36 // image was not found or it failed to decode. |resource| is the |
| 37 // ExtensionResource where the |image| came from and the |index| represents |
| 38 // the index of the image just loaded (starts at 0 and increments every |
| 39 // time this function is called). |
| 40 virtual void OnImageLoaded(SkBitmap* image, ExtensionResource resource, |
| 41 int index) = 0; |
| 32 }; | 42 }; |
| 33 | 43 |
| 34 ImageLoadingTracker(Observer* observer, size_t image_count) | 44 explicit ImageLoadingTracker(Observer* observer); |
| 35 : observer_(observer), image_count_(image_count), posted_count_(0) { | 45 ~ImageLoadingTracker(); |
| 36 AddRef(); // We hold on to a reference to ourself to make sure we don't | |
| 37 // get deleted until we get a response from image loading (see | |
| 38 // ImageLoadingDone). | |
| 39 } | |
| 40 | 46 |
| 41 // If there are remaining images to be loaded, the observing object should | 47 // Specify image resource to load. If the loaded image is larger than |
| 42 // call this method on its destruction, so that the tracker will not attempt | |
| 43 // to make any more callbacks to it. | |
| 44 void StopTrackingImageLoad() { | |
| 45 observer_ = NULL; | |
| 46 } | |
| 47 | |
| 48 // Specify image resource to load. This method must be called a number of | |
| 49 // times equal to the |image_count| arugment to the constructor. Calling it | |
| 50 // any more or less than that is an error. If the loaded image is larger than | |
| 51 // |max_size| it will be resized to those dimensions. | 48 // |max_size| it will be resized to those dimensions. |
| 52 void PostLoadImageTask(const ExtensionResource& resource, | 49 void LoadImage(const ExtensionResource& resource, |
| 53 const gfx::Size& max_size); | 50 gfx::Size max_size); |
| 54 | 51 |
| 55 private: | 52 private: |
| 56 class LoadImageTask; | 53 class ImageLoader; |
| 57 | 54 |
| 58 friend class base::RefCountedThreadSafe<ImageLoadingTracker>; | 55 // When an image has finished loaded and been resized on the file thread, it |
| 59 | 56 // is posted back to this method on the original thread. This method then |
| 60 ~ImageLoadingTracker() {} | 57 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if |
| 61 | 58 // it was the last image in the list. |
| 62 // When an image has finished loaded and scaled on the file thread, it is | |
| 63 // posted back to this method on the original thread. This method then calls | |
| 64 // the observer's OnImageLoaded and deletes the ImageLoadingTracker if it was | |
| 65 // the last image in the list. | |
| 66 // |image| may be null if the file failed to decode. | 59 // |image| may be null if the file failed to decode. |
| 67 void OnImageLoaded(SkBitmap* image, size_t index); | 60 void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource); |
| 68 | 61 |
| 69 // The view that is waiting for the image to load. | 62 // The view that is waiting for the image to load. |
| 70 Observer* observer_; | 63 Observer* observer_; |
| 71 | 64 |
| 72 // The number of images this ImageTracker should keep track of. This is | 65 // The number of times we've reported back. |
| 73 // decremented as each image finishes loading, and the tracker will delete | 66 int responses_; |
| 74 // itself when it reaches zero. | |
| 75 size_t image_count_; | |
| 76 | 67 |
| 77 // The number of tasks that have been posted so far. | 68 // The object responsible for loading the image on the File thread. |
| 78 size_t posted_count_; | 69 scoped_refptr<ImageLoader> loader_; |
| 79 | 70 |
| 80 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 71 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
| 81 }; | 72 }; |
| 82 | 73 |
| 83 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 74 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
| OLD | NEW |