| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_IMAGE_LOADING_TRACKER_H_ |
| 6 #define CHROME_BROWSER_IMAGE_LOADING_TRACKER_H_ |
| 7 |
| 8 #include "base/ref_counted.h" |
| 9 |
| 10 class FilePath; |
| 11 class SkBitmap; |
| 12 |
| 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 |
| 15 // progress (manages its own lifetime) and keeps track of whether the view still |
| 16 // cares about the icon loading. |
| 17 class ImageLoadingTracker |
| 18 : public base::RefCountedThreadSafe<ImageLoadingTracker> { |
| 19 public: |
| 20 class Observer { |
| 21 public: |
| 22 // 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 |
| 24 // if they need to access it after this call. |
| 25 virtual void OnImageLoaded(SkBitmap* image, size_t index) = 0; |
| 26 }; |
| 27 |
| 28 ImageLoadingTracker(Observer* observer, size_t image_count) |
| 29 : 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 |
| 31 // get deleted until we get a response from image loading (see |
| 32 // ImageLoadingDone). |
| 33 } |
| 34 ~ImageLoadingTracker() {} |
| 35 |
| 36 // 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 |
| 38 // to make any more callbacks to it. |
| 39 void StopTrackingImageLoad() { |
| 40 observer_ = NULL; |
| 41 } |
| 42 |
| 43 // Specify path of image to load. This method must be called a number of |
| 44 // times equal to the |image_count| arugment to the constructor. Calling it |
| 45 // any more or less than that is an error. |
| 46 void PostLoadImageTask(FilePath path); |
| 47 |
| 48 private: |
| 49 class LoadImageTask; |
| 50 |
| 51 // 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 |
| 53 // the observer's OnImageLoaded and deletes the ImageLoadingTracker if it was |
| 54 // the last image in the list. |
| 55 // |image| may be null if the file failed to decode. |
| 56 void OnImageLoaded(SkBitmap* image, size_t index); |
| 57 |
| 58 // The view that is waiting for the image to load. |
| 59 Observer* observer_; |
| 60 |
| 61 // The number of images this ImageTracker should keep track of. This is |
| 62 // decremented as each image finishes loading, and the tracker will delete |
| 63 // itself when it reaches zero. |
| 64 size_t image_count_; |
| 65 |
| 66 // The number of tasks that have been posted so far. |
| 67 size_t posted_count_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(ImageLoadingTracker); |
| 70 }; |
| 71 |
| 72 #endif // CHROME_BROWSER_IMAGE_LOADING_TRACKER_H_ |
| OLD | NEW |