| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_IMAGE_LOADING_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
| 6 #define CHROME_BROWSER_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 FilePath; | 10 class ExtensionResource; |
| 11 class SkBitmap; | 11 class SkBitmap; |
| 12 | 12 |
| 13 // The views need to load their icons asynchronously but might be deleted before | 13 // The views need to load their icons asynchronously but might be deleted before |
| 14 // the images have loaded. This class stays alive while the request is in | 14 // the images have loaded. This class stays alive while the request is in |
| 15 // progress (manages its own lifetime) and keeps track of whether the view still | 15 // progress (manages its own lifetime) and keeps track of whether the view still |
| 16 // cares about the icon loading. | 16 // cares about the icon loading. |
| 17 // Consider abstracting out a FilePathProvider (ExtensionResource) and moving |
| 18 // back to chrome/browser/ if other subsystems want to use it. |
| 17 class ImageLoadingTracker | 19 class ImageLoadingTracker |
| 18 : public base::RefCountedThreadSafe<ImageLoadingTracker> { | 20 : public base::RefCountedThreadSafe<ImageLoadingTracker> { |
| 19 public: | 21 public: |
| 20 class Observer { | 22 class Observer { |
| 21 public: | 23 public: |
| 22 // Will be called when the image with the given index has loaded. | 24 // Will be called when the image with the given index has loaded. |
| 23 // The |image| is owned by the tracker, so the observer should make a copy | 25 // The |image| is owned by the tracker, so the observer should make a copy |
| 24 // if they need to access it after this call. | 26 // if they need to access it after this call. |
| 25 virtual void OnImageLoaded(SkBitmap* image, size_t index) = 0; | 27 virtual void OnImageLoaded(SkBitmap* image, size_t index) = 0; |
| 26 }; | 28 }; |
| 27 | 29 |
| 28 ImageLoadingTracker(Observer* observer, size_t image_count) | 30 ImageLoadingTracker(Observer* observer, size_t image_count) |
| 29 : observer_(observer), image_count_(image_count), posted_count_(0) { | 31 : observer_(observer), image_count_(image_count), posted_count_(0) { |
| 30 AddRef(); // We hold on to a reference to ourself to make sure we don't | 32 AddRef(); // We hold on to a reference to ourself to make sure we don't |
| 31 // get deleted until we get a response from image loading (see | 33 // get deleted until we get a response from image loading (see |
| 32 // ImageLoadingDone). | 34 // ImageLoadingDone). |
| 33 } | 35 } |
| 34 ~ImageLoadingTracker() {} | 36 ~ImageLoadingTracker() {} |
| 35 | 37 |
| 36 // If there are remaining images to be loaded, the observing object should | 38 // If there are remaining images to be loaded, the observing object should |
| 37 // call this method on its destruction, so that the tracker will not attempt | 39 // call this method on its destruction, so that the tracker will not attempt |
| 38 // to make any more callbacks to it. | 40 // to make any more callbacks to it. |
| 39 void StopTrackingImageLoad() { | 41 void StopTrackingImageLoad() { |
| 40 observer_ = NULL; | 42 observer_ = NULL; |
| 41 } | 43 } |
| 42 | 44 |
| 43 // Specify path of image to load. This method must be called a number of | 45 // Specify image resource to load. This method must be called a number of |
| 44 // times equal to the |image_count| arugment to the constructor. Calling it | 46 // times equal to the |image_count| arugment to the constructor. Calling it |
| 45 // any more or less than that is an error. | 47 // any more or less than that is an error. |
| 46 void PostLoadImageTask(FilePath path); | 48 void PostLoadImageTask(const ExtensionResource& resource); |
| 47 | 49 |
| 48 private: | 50 private: |
| 49 class LoadImageTask; | 51 class LoadImageTask; |
| 50 | 52 |
| 51 // When an image has finished loaded and scaled on the file thread, it is | 53 // When an image has finished loaded and scaled on the file thread, it is |
| 52 // posted back to this method on the original thread. This method then calls | 54 // posted back to this method on the original thread. This method then calls |
| 53 // the observer's OnImageLoaded and deletes the ImageLoadingTracker if it was | 55 // the observer's OnImageLoaded and deletes the ImageLoadingTracker if it was |
| 54 // the last image in the list. | 56 // the last image in the list. |
| 55 // |image| may be null if the file failed to decode. | 57 // |image| may be null if the file failed to decode. |
| 56 void OnImageLoaded(SkBitmap* image, size_t index); | 58 void OnImageLoaded(SkBitmap* image, size_t index); |
| 57 | 59 |
| 58 // The view that is waiting for the image to load. | 60 // The view that is waiting for the image to load. |
| 59 Observer* observer_; | 61 Observer* observer_; |
| 60 | 62 |
| 61 // The number of images this ImageTracker should keep track of. This is | 63 // The number of images this ImageTracker should keep track of. This is |
| 62 // decremented as each image finishes loading, and the tracker will delete | 64 // decremented as each image finishes loading, and the tracker will delete |
| 63 // itself when it reaches zero. | 65 // itself when it reaches zero. |
| 64 size_t image_count_; | 66 size_t image_count_; |
| 65 | 67 |
| 66 // The number of tasks that have been posted so far. | 68 // The number of tasks that have been posted so far. |
| 67 size_t posted_count_; | 69 size_t posted_count_; |
| 68 | 70 |
| 69 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); | 71 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 #endif // CHROME_BROWSER_IMAGE_LOADING_TRACKER_H_ | 74 #endif // CHROME_BROWSER_EXTENSIONS_IMAGE_LOADING_TRACKER_H_ |
| OLD | NEW |