Chromium Code Reviews| Index: chrome/browser/notifications/sync_notifier/image_holder.cc |
| diff --git a/chrome/browser/notifications/sync_notifier/image_holder.cc b/chrome/browser/notifications/sync_notifier/image_holder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..387c9af645b32703d15feb0f8d1f8c493e27bbc4 |
| --- /dev/null |
| +++ b/chrome/browser/notifications/sync_notifier/image_holder.cc |
| @@ -0,0 +1,97 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This class holds the URL to an image and the bitmap for the fetched image, |
| +// and has code to fetch the bitmap from the URL. |
| + |
| +#include "chrome/browser/notifications/sync_notifier/image_holder.h" |
| + |
| +// #include "ui/gfx/image/image_skia_rep.h" |
|
dewittj
2014/03/25 22:31:14
nit: fix this include.
Pete Williamson
2014/03/26 18:12:55
Done.
|
| + |
| +namespace notifier { |
| + |
| +ImageHolder::ImageHolder(const GURL& low_dpi_url, |
| + const GURL& high_dpi_url, |
| + Profile* profile, |
| + ImageHolderDelegate* delegate) |
| + : low_dpi_url_(low_dpi_url), |
| + high_dpi_url_(high_dpi_url), |
| + low_dpi_fetched_(false), |
| + high_dpi_fetched_(false), |
| + delegate_(delegate), |
| + profile_(profile) { |
| + |
| + // If a URL is invalid, clear it so we don't try to fetch it. |
| + if (!low_dpi_url_.is_valid()) { |
| + low_dpi_url_ = GURL(); |
| + } |
| + if (!high_dpi_url_.is_valid()) { |
| + high_dpi_url_ = GURL(); |
| + } |
| + |
| + // Create a featcher for each URL that is set. |
| + if (!low_dpi_url_.is_empty()) { |
| + CreateBitmapFetcher(low_dpi_url_); |
| + } |
| + if (!high_dpi_url_.is_empty()) { |
| + CreateBitmapFetcher(high_dpi_url_); |
| + } |
| +} |
| + |
| +ImageHolder::~ImageHolder() {} |
| + |
| +// This will let us know if we have tried to fetch once and the try completed. |
| +// Currently there is no logic for retries. |
| +bool ImageHolder::IsFetchingDone() const { |
| + return ( (low_dpi_url_.is_empty() || low_dpi_fetched_) && |
|
dewittj
2014/03/25 22:31:14
nit: no whitespace between ( ( and ) )
Pete Williamson
2014/03/26 18:12:55
Done.
|
| + (high_dpi_url_.is_empty() || high_dpi_fetched_) ); |
| +} |
| + |
| +// If this bitmap has a valid GURL, create a fetcher for it. |
| +void ImageHolder::CreateBitmapFetcher(const GURL& url) { |
| + // Check for dups, ignore any request for a dup. |
| + ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| + for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| + if ((*iter)->url() == url) |
| + return; |
| + } |
| + |
| + if (url.is_valid()) { |
| + fetchers_.push_back(new chrome::BitmapFetcher(url, this)); |
| + DVLOG(2) << __FUNCTION__ << "Pushing bitmap " << url; |
| + } |
| +} |
| + |
| +void ImageHolder::StartFetch() { |
| + // Now that we have queued them all, start the fetching. |
| + ScopedVector<chrome::BitmapFetcher>::iterator iter; |
| + for (iter = fetchers_.begin(); iter != fetchers_.end(); ++iter) { |
| + (*iter)->Start(profile_); |
| + } |
| +} |
| + |
| +// Method inherited from BitmapFetcher delegate. |
| +void ImageHolder::OnFetchComplete(const GURL url, |
| + const SkBitmap* bitmap) { |
|
dewittj
2014/03/25 22:31:14
nit:no newline
Pete Williamson
2014/03/26 18:12:55
Done.
dewittj
2014/03/26 18:38:39
This is not done. No newline between url, and con
Pete Williamson
2014/03/26 21:03:50
Done.
|
| + // TODO(petewil): Should I retry if a fetch fails? |
|
dewittj
2014/03/25 22:31:14
Please open a bug for this, maybe even mark launch
Pete Williamson
2014/03/26 18:12:55
crbug.com/256466
|
| + |
| + // Match the bitmap to the URL to put it into the image with the correct scale |
| + // factor. |
| + if (url == low_dpi_url_) { |
| + low_dpi_fetched_ = true; |
| + if (bitmap != NULL) |
| + image_.AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0)); |
| + } else if (url == high_dpi_url_) { |
| + high_dpi_fetched_ = true; |
| + if (bitmap != NULL) |
| + image_.AddRepresentation(gfx::ImageSkiaRep(*bitmap, 2.0)); |
| + } else { |
| + DVLOG(2) << __FUNCTION__ << "Unmatched bitmap arrived " << url; |
| + } |
| + |
| + // Notify callback of bitmap arrival. |
| + delegate_->OnFetchComplete(); |
| +} |
| + |
| +} // namespace notifier. |