1 // Copyright (c) 2013 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 #include "android_webview/browser/icon_helper.h"
8 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/favicon_url.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/geometry/size.h"
17 using content::BrowserThread
;
18 using content::WebContents
;
20 namespace android_webview
{
22 IconHelper::IconHelper(WebContents
* web_contents
)
23 : WebContentsObserver(web_contents
),
25 missing_favicon_urls_() {
28 IconHelper::~IconHelper() {
31 void IconHelper::SetListener(Listener
* listener
) {
35 void IconHelper::DownloadFaviconCallback(
38 const GURL
& image_url
,
39 const std::vector
<SkBitmap
>& bitmaps
,
40 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
42 if (http_status_code
== 404) {
43 MarkUnableToDownloadFavicon(image_url
);
47 if (bitmaps
.size() == 0) {
51 // We can protentially have multiple frames of the icon
52 // in different sizes. We need more fine grain API spec
53 // to let clients pick out the frame they want.
55 // TODO(acleung): Pick the best icon to return based on size.
57 listener_
->OnReceivedIcon(image_url
, bitmaps
[0]);
60 void IconHelper::DidUpdateFaviconURL(
61 const std::vector
<content::FaviconURL
>& candidates
) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
63 for (std::vector
<content::FaviconURL
>::const_iterator i
= candidates
.begin();
64 i
!= candidates
.end(); ++i
) {
65 if (!i
->icon_url
.is_valid())
68 switch(i
->icon_type
) {
69 case content::FaviconURL::FAVICON
:
70 if ((listener_
&& !listener_
->ShouldDownloadFavicon(i
->icon_url
)) ||
71 WasUnableToDownloadFavicon(i
->icon_url
)) {
74 web_contents()->DownloadImage(i
->icon_url
,
77 false, // Normal cache policy
79 &IconHelper::DownloadFaviconCallback
, base::Unretained(this)));
81 case content::FaviconURL::TOUCH_ICON
:
83 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), false);
85 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON
:
87 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), true);
89 case content::FaviconURL::INVALID_ICON
:
90 // Silently ignore it. Only trigger a callback on valid icons.
99 void IconHelper::DidStartNavigationToPendingEntry(
101 content::NavigationController::ReloadType reload_type
) {
102 if (reload_type
== content::NavigationController::RELOAD_IGNORING_CACHE
)
103 ClearUnableToDownloadFavicons();
106 void IconHelper::MarkUnableToDownloadFavicon(const GURL
& icon_url
) {
107 MissingFaviconURLHash url_hash
= base::Hash(icon_url
.spec());
108 missing_favicon_urls_
.insert(url_hash
);
111 bool IconHelper::WasUnableToDownloadFavicon(const GURL
& icon_url
) const {
112 MissingFaviconURLHash url_hash
= base::Hash(icon_url
.spec());
113 return missing_favicon_urls_
.find(url_hash
) != missing_favicon_urls_
.end();
116 void IconHelper::ClearUnableToDownloadFavicons() {
117 missing_favicon_urls_
.clear();
120 } // namespace android_webview