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.
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.
8 #include "chrome/browser/image_holder.h"
10 #include "chrome/browser/profiles/profile.h"
14 ImageHolder::ImageHolder(const GURL
& low_dpi_url
,
15 const GURL
& high_dpi_url
,
17 ImageHolderDelegate
* delegate
)
18 : low_dpi_url_(low_dpi_url
),
19 high_dpi_url_(high_dpi_url
),
20 low_dpi_fetched_(false),
21 high_dpi_fetched_(false),
25 // If a URL is invalid, clear it so we don't try to fetch it.
26 if (!low_dpi_url_
.is_valid()) {
27 low_dpi_url_
= GURL();
29 if (!high_dpi_url_
.is_valid()) {
30 high_dpi_url_
= GURL();
33 // Create a featcher for each URL that is set.
34 if (!low_dpi_url_
.is_empty()) {
35 CreateBitmapFetcher(low_dpi_url_
);
37 if (!high_dpi_url_
.is_empty()) {
38 CreateBitmapFetcher(high_dpi_url_
);
42 ImageHolder::~ImageHolder() {}
44 // This will let us know if we have tried to fetch once and the try completed.
45 // Currently there is no logic for retries.
46 bool ImageHolder::IsFetchingDone() const {
47 return ((low_dpi_url_
.is_empty() || low_dpi_fetched_
) &&
48 (high_dpi_url_
.is_empty() || high_dpi_fetched_
));
51 // If this bitmap has a valid GURL, create a fetcher for it.
52 void ImageHolder::CreateBitmapFetcher(const GURL
& url
) {
53 // Check for dups, ignore any request for a dup.
54 ScopedVector
<chrome::BitmapFetcher
>::iterator iter
;
55 for (iter
= fetchers_
.begin(); iter
!= fetchers_
.end(); ++iter
) {
56 if ((*iter
)->url() == url
)
61 fetchers_
.push_back(new chrome::BitmapFetcher(url
, this));
62 DVLOG(2) << __FUNCTION__
<< "Pushing bitmap " << url
;
66 void ImageHolder::StartFetch() {
67 // Now that we have queued them all, start the fetching.
68 ScopedVector
<chrome::BitmapFetcher
>::iterator iter
;
69 for (iter
= fetchers_
.begin(); iter
!= fetchers_
.end(); ++iter
) {
70 (*iter
)->Start(profile_
->GetRequestContext());
74 // Method inherited from BitmapFetcher delegate.
75 void ImageHolder::OnFetchComplete(const GURL url
, const SkBitmap
* bitmap
) {
76 // TODO(petewil): Should I retry if a fetch fails?
77 // Match the bitmap to the URL to put it into the image with the correct scale
79 if (url
== low_dpi_url_
) {
80 low_dpi_fetched_
= true;
82 image_
.AddRepresentation(gfx::ImageSkiaRep(*bitmap
, 1.0));
83 } else if (url
== high_dpi_url_
) {
84 high_dpi_fetched_
= true;
86 image_
.AddRepresentation(gfx::ImageSkiaRep(*bitmap
, 2.0));
88 DVLOG(2) << __FUNCTION__
<< "Unmatched bitmap arrived " << url
;
91 // Notify callback of bitmap arrival.
92 delegate_
->OnFetchComplete();
95 } // namespace chrome.