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"
9 #include "base/logging.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/common/favicon_url.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/gfx/size.h"
16 using content::BrowserThread
;
17 using content::WebContents
;
19 namespace android_webview
{
21 IconHelper::IconHelper(WebContents
* web_contents
)
22 : WebContentsObserver(web_contents
),
26 IconHelper::~IconHelper() {
29 void IconHelper::SetListener(Listener
* listener
) {
33 void IconHelper::DownloadFaviconCallback(
36 const GURL
& image_url
,
37 const std::vector
<SkBitmap
>& bitmaps
,
38 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
40 if (bitmaps
.size() == 0) {
44 // We can protentially have multiple frames of the icon
45 // in different sizes. We need more fine grain API spec
46 // to let clients pick out the frame they want.
48 // TODO(acleung): Pick the best icon to return based on size.
50 listener_
->OnReceivedIcon(image_url
, bitmaps
[0]);
53 void IconHelper::DidUpdateFaviconURL(int32 page_id
,
54 const std::vector
<content::FaviconURL
>& candidates
) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
56 for (std::vector
<content::FaviconURL
>::const_iterator i
= candidates
.begin();
57 i
!= candidates
.end(); ++i
) {
58 if (!i
->icon_url
.is_valid())
61 switch(i
->icon_type
) {
62 case content::FaviconURL::FAVICON
:
63 if (listener_
&& !listener_
->ShouldDownloadFavicon(i
->icon_url
)) break;
64 web_contents()->DownloadImage(i
->icon_url
,
68 &IconHelper::DownloadFaviconCallback
, base::Unretained(this)));
70 case content::FaviconURL::TOUCH_ICON
:
72 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), false);
74 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON
:
76 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), true);
78 case content::FaviconURL::INVALID_ICON
:
79 // Silently ignore it. Only trigger a callback on valid icons.
88 } // namespace android_webview