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"
15 using content::BrowserThread
;
16 using content::WebContents
;
18 namespace android_webview
{
20 IconHelper::IconHelper(WebContents
* web_contents
)
21 : WebContentsObserver(web_contents
),
25 IconHelper::~IconHelper() {
28 void IconHelper::SetListener(Listener
* listener
) {
32 void IconHelper::DownloadFaviconCallback(
33 int id
, const GURL
& image_url
, int requested_size
,
34 const std::vector
<SkBitmap
>& bitmaps
) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
36 if (bitmaps
.size() == 0) {
40 // We can protentially have multiple frames of the icon
41 // in different sizes. We need more fine grain API spec
42 // to let clients pick out the frame they want.
44 // TODO(acleung): Pick the best icon to return based on size.
46 listener_
->OnReceivedIcon(bitmaps
[0]);
49 void IconHelper::DidUpdateFaviconURL(int32 page_id
,
50 const std::vector
<content::FaviconURL
>& candidates
) {
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
52 for (std::vector
<content::FaviconURL
>::const_iterator i
= candidates
.begin();
53 i
!= candidates
.end(); ++i
) {
54 if (!i
->icon_url
.is_valid())
57 switch(i
->icon_type
) {
58 case content::FaviconURL::FAVICON
:
59 // TODO(acleung): only fetch the URL if favicon downloading is enabled.
60 // (currently that is, the app has called WebIconDatabase.open()
61 // but we should decouple that setting via a boolean setting)
62 web_contents()->DownloadImage(i
->icon_url
,
66 &IconHelper::DownloadFaviconCallback
, base::Unretained(this)));
68 case content::FaviconURL::TOUCH_ICON
:
70 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), false);
72 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON
:
74 listener_
->OnReceivedTouchIconUrl(i
->icon_url
.spec(), true);
76 case content::FaviconURL::INVALID_ICON
:
77 // Silently ignore it. Only trigger a callback on valid icons.
86 } // namespace android_webview