1 // Copyright 2014 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_HOLDER_H_ 6 #define CHROME_BROWSER_IMAGE_HOLDER_H_ 7 8 #include "base/memory/scoped_vector.h" 9 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" 10 #include "ui/gfx/image/image.h" 11 #include "ui/gfx/image/image_skia.h" 12 #include "url/gurl.h" 13 14 class Profile; 15 16 namespace chrome { 17 18 // This provides a callback so the ImageHolder can inform its parent when a 19 // bitmap arrives. 20 class ImageHolderDelegate { 21 public: 22 virtual void OnFetchComplete() = 0; 23 }; 24 25 // This class encapsulates the action of fetching a bitmap, reporting when it is 26 // fetched, and holding onto the bitmap until no longer needed. 27 class ImageHolder : public chrome::BitmapFetcherDelegate { 28 public: 29 ImageHolder(const GURL& low_dpi_url, 30 const GURL& high_dpi_url, 31 Profile* profile, 32 ImageHolderDelegate* delegate); 33 virtual ~ImageHolder(); 34 35 // Begin fetching of the URLs we have. 36 void StartFetch(); 37 38 // Check whether we have a response from the server for these resources, 39 // even if the response is a failed fetch. 40 bool IsFetchingDone() const; 41 42 // Inherited from BitmapFetcherDelegate 43 virtual void OnFetchComplete(const GURL url, const SkBitmap* bitmap) OVERRIDE; 44 45 // Accessors: 46 GURL low_dpi_url() const { return low_dpi_url_; } 47 GURL high_dpi_url() const { return high_dpi_url_; } 48 gfx::Image low_dpi_image() { return gfx::Image(image_); } 49 50 private: 51 // Helper function to create a bitmap fetcher (but not start the fetch). 52 void CreateBitmapFetcher(const GURL& url); 53 54 GURL low_dpi_url_; 55 GURL high_dpi_url_; 56 bool low_dpi_fetched_; 57 bool high_dpi_fetched_; 58 gfx::ImageSkia image_; 59 ImageHolderDelegate* delegate_; 60 ScopedVector<chrome::BitmapFetcher> fetchers_; 61 Profile* profile_; 62 63 FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, CreateBitmapFetcherTest); 64 FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, OnFetchCompleteTest); 65 FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, IsFetchingDoneTest); 66 67 DISALLOW_COPY_AND_ASSIGN(ImageHolder); 68 }; 69 70 } // namespace chrome. 71 72 #endif // CHROME_BROWSER_IMAGE_HOLDER_H_ 73