OLD | NEW |
(Empty) | |
| 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 // This class holds the URL to an image and the bitmap for the fetched image, |
| 6 // and has code to fetch the bitmap from the URL. |
| 7 |
| 8 #include "chrome/browser/notifications/sync_notifier/image_holder.h" |
| 9 |
| 10 namespace notifier { |
| 11 |
| 12 ImageHolder::ImageHolder(const GURL& low_dpi_url, |
| 13 const GURL& high_dpi_url, |
| 14 Profile* profile, |
| 15 ImageHolderDelegate* delegate) |
| 16 : low_dpi_url_(low_dpi_url), |
| 17 high_dpi_url_(high_dpi_url), |
| 18 low_dpi_fetched_(false), |
| 19 high_dpi_fetched_(false), |
| 20 delegate_(delegate), |
| 21 profile_(profile) { |
| 22 |
| 23 // If a URL is invalid, clear it so we don't try to fetch it. |
| 24 if (!low_dpi_url_.is_valid()) { |
| 25 low_dpi_url_ = GURL(); |
| 26 } |
| 27 if (!high_dpi_url_.is_valid()) { |
| 28 high_dpi_url_ = GURL(); |
| 29 } |
| 30 |
| 31 // Create a featcher for each URL that is set. |
| 32 if (!low_dpi_url_.is_empty()) { |
| 33 CreateBitmapFetcher(low_dpi_url_); |
| 34 } |
| 35 if (!high_dpi_url_.is_empty()) { |
| 36 CreateBitmapFetcher(high_dpi_url_); |
| 37 } |
| 38 } |
| 39 |
| 40 ImageHolder::~ImageHolder() {} |
| 41 |
| 42 // This will let us know if we have tried to fetch once and the try completed. |
| 43 // Currently there is no logic for retries. |
| 44 bool ImageHolder::IsFetchingDone() const { |
| 45 return ((low_dpi_url_.is_empty() || low_dpi_fetched_) && |
| 46 (high_dpi_url_.is_empty() || high_dpi_fetched_)); |
| 47 } |
| 48 |
| 49 // If this bitmap has a valid GURL, create a fetcher for it. |
| 50 void ImageHolder::CreateBitmapFetcher(const GURL& url) { |
| 51 // Check for dups, ignore any request for a dup. |
| 52 ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| 53 for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| 54 if ((*iter)->url() == url) |
| 55 return; |
| 56 } |
| 57 |
| 58 if (url.is_valid()) { |
| 59 fetchers_.push_back(new chrome::BitmapFetcher(url, this)); |
| 60 DVLOG(2) << __FUNCTION__ << "Pushing bitmap " << url; |
| 61 } |
| 62 } |
| 63 |
| 64 void ImageHolder::StartFetch() { |
| 65 // Now that we have queued them all, start the fetching. |
| 66 ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| 67 for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| 68 (*iter)->Start(profile_); |
| 69 } |
| 70 } |
| 71 |
| 72 // Method inherited from BitmapFetcher delegate. |
| 73 void ImageHolder::OnFetchComplete(const GURL url, const SkBitmap* bitmap) { |
| 74 // TODO(petewil): Should I retry if a fetch fails? |
| 75 // Match the bitmap to the URL to put it into the image with the correct scale |
| 76 // factor. |
| 77 if (url == low_dpi_url_) { |
| 78 low_dpi_fetched_ = true; |
| 79 if (bitmap != NULL) |
| 80 image_.AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0)); |
| 81 } else if (url == high_dpi_url_) { |
| 82 high_dpi_fetched_ = true; |
| 83 if (bitmap != NULL) |
| 84 image_.AddRepresentation(gfx::ImageSkiaRep(*bitmap, 2.0)); |
| 85 } else { |
| 86 DVLOG(2) << __FUNCTION__ << "Unmatched bitmap arrived " << url; |
| 87 } |
| 88 |
| 89 // Notify callback of bitmap arrival. |
| 90 delegate_->OnFetchComplete(); |
| 91 } |
| 92 |
| 93 } // namespace notifier. |
OLD | NEW |