| 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 delegate_(delegate), |
| 19 profile_(profile) { |
| 20 |
| 21 // Create a featcher for each URL that is set. |
| 22 if (!low_dpi_url_.is_empty()) { |
| 23 CreateBitmapFetcher(low_dpi_url_); |
| 24 } |
| 25 if (!high_dpi_url_.is_empty()) { |
| 26 CreateBitmapFetcher(high_dpi_url_); |
| 27 } |
| 28 } |
| 29 |
| 30 ImageHolder::~ImageHolder() {} |
| 31 |
| 32 // This will let us know if we have tried to fetch once and the try completed. |
| 33 // Currently there is no logic for retries. |
| 34 bool ImageHolder::IsFetchingDone() const { |
| 35 return ( (low_dpi_url_.is_empty() || low_dpi_fetched_) && |
| 36 (high_dpi_url_.is_empty() || high_dpi_fetched_) ); |
| 37 } |
| 38 |
| 39 // If this bitmap has a valid GURL, create a fetcher for it. |
| 40 void ImageHolder::CreateBitmapFetcher(const GURL& url) { |
| 41 // Check for dups, ignore any request for a dup. |
| 42 ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| 43 for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| 44 if ((*iter)->url() == url) |
| 45 return; |
| 46 } |
| 47 |
| 48 if (url.is_valid()) { |
| 49 fetchers_.push_back(new chrome::BitmapFetcher(url, this)); |
| 50 DVLOG(2) << __FUNCTION__ << "Pushing bitmap " << url; |
| 51 } |
| 52 } |
| 53 |
| 54 void ImageHolder::StartFetch() { |
| 55 // Now that we have queued them all, start the fetching. |
| 56 ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| 57 for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| 58 (*iter)->Start(profile_); |
| 59 } |
| 60 } |
| 61 |
| 62 // Method inherited from BitmapFetcher delegate. |
| 63 void ImageHolder::OnFetchComplete(const GURL url, |
| 64 const SkBitmap* bitmap) { |
| 65 // TODO(petewil): Should I retry if a fetch fails? |
| 66 |
| 67 // TODO(petewil): |
| 68 // Use the ImageSkia functions instead and use 2.0 as scale for the hi dpi |
| 69 // versions, then combine into one gfx::Image, which I pass to the |
| 70 // Notification Center. |
| 71 |
| 72 // Match the bitmap to the URL to put it into the right variable. |
| 73 if (url == low_dpi_url_) { |
| 74 low_dpi_fetched_ = true; |
| 75 if (bitmap != NULL) |
| 76 low_dpi_image_ = gfx::Image::CreateFrom1xBitmap(*bitmap); |
| 77 } else if (url == high_dpi_url_) { |
| 78 high_dpi_fetched_ = true; |
| 79 if (bitmap != NULL) |
| 80 high_dpi_image_ = gfx::Image::CreateFrom1xBitmap(*bitmap); |
| 81 } else { |
| 82 DVLOG(2) << __FUNCTION__ << "Unmatched bitmap arrived " << url; |
| 83 } |
| 84 |
| 85 // Notify callback of bitmap arrival. |
| 86 delegate_->OnFetchComplete(); |
| 87 } |
| 88 |
| 89 } // namespace notifier. |
| OLD | NEW |