Chromium Code Reviews| 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 | 9 |
| 10 class ExtensionResource; | 10 class ExtensionResource; |
| 11 class SkBitmap; | 11 class SkBitmap; |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 class Size; | 14 class Size; |
| 15 } | 15 } |
| 16 | 16 |
| 17 // The views need to load their icons asynchronously but might be deleted before | 17 // 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 | 18 // 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 | 19 // alive while the request is in progress (manages its own lifetime) and keeps |
| 20 // cares about the icon loading. | 20 // track of whether the view still cares about the icon loading. |
|
Aaron Boodman
2010/03/23 19:10:55
It would be good to have an example usage here.
| |
| 21 // Consider abstracting out a FilePathProvider (ExtensionResource) and moving | 21 class ImageLoadingTracker { |
| 22 // back to chrome/browser/ if other subsystems want to use it. | |
| 23 class ImageLoadingTracker | |
| 24 : public base::RefCountedThreadSafe<ImageLoadingTracker> { | |
| 25 public: | 22 public: |
| 26 class Observer { | 23 class Observer { |
| 27 public: | 24 public: |
| 28 // Will be called when the image with the given index has loaded. | 25 // 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 | 26 // The |image| is owned by the tracker, so the observer should make a copy |
| 30 // if they need to access it after this call. | 27 // if they need to access it after this call. |
|
Aaron Boodman
2010/03/23 19:10:55
You should make it clear that if the image could n
| |
| 31 virtual void OnImageLoaded(SkBitmap* image, size_t index) = 0; | 28 virtual void OnImageLoaded(SkBitmap* image, int index) = 0; |
| 32 }; | 29 }; |
| 33 | 30 |
| 34 ImageLoadingTracker(Observer* observer, size_t image_count) | 31 explicit ImageLoadingTracker(Observer* observer); |
| 35 : observer_(observer), image_count_(image_count), posted_count_(0) { | 32 ~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 | 33 |
| 41 // If there are remaining images to be loaded, the observing object should | 34 // 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 | 35 // |max_size| it will be resized to those dimensions. |index| will be returned |
| 43 // to make any more callbacks to it. | 36 // back to the caller through OnImageLoaded. |
| 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. | |
| 52 void PostLoadImageTask(const ExtensionResource& resource, | 37 void PostLoadImageTask(const ExtensionResource& resource, |
| 53 const gfx::Size& max_size); | 38 const gfx::Size& max_size, |
| 39 int index); | |
|
Aaron Boodman
2010/03/23 19:10:55
Recommend removing this param and changing OnImage
| |
| 54 | 40 |
| 55 private: | 41 private: |
| 56 class LoadImageTask; | 42 class ImageLoader; |
| 57 | 43 |
| 58 friend class base::RefCountedThreadSafe<ImageLoadingTracker>; | 44 // When an image has finished loaded and been resized on the file thread, it |
| 59 | 45 // is posted back to this method on the original thread. This method then |
| 60 ~ImageLoadingTracker() {} | 46 // calls the observer's OnImageLoaded and deletes the ImageLoadingTracker if |
| 61 | 47 // 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. | 48 // |image| may be null if the file failed to decode. |
| 67 void OnImageLoaded(SkBitmap* image, size_t index); | 49 void OnImageLoaded(SkBitmap* image, int index); |
| 68 | 50 |
| 69 // The view that is waiting for the image to load. | 51 // The view that is waiting for the image to load. |
| 70 Observer* observer_; | 52 Observer* observer_; |
| 71 | 53 |
| 72 // The number of images this ImageTracker should keep track of. This is | 54 // The object responsible for loading the image on the File thread. |
| 73 // decremented as each image finishes loading, and the tracker will delete | 55 scoped_refptr<ImageLoader> loader_; |
| 74 // itself when it reaches zero. | |
| 75 size_t image_count_; | |
| 76 | |
| 77 // The number of tasks that have been posted so far. | |
| 78 size_t posted_count_; | |
| 79 | 56 |
| 80 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 57 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
| 81 }; | 58 }; |
| 82 | 59 |
| 83 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ | 60 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
| OLD | NEW |