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