Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / android_webview / browser / icon_helper.cc
blob13091f280d8d0faeb623330c5ba8fb05d06ab888
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"
7 #include "base/bind.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),
22 listener_(NULL) {
25 IconHelper::~IconHelper() {
28 void IconHelper::SetListener(Listener* listener) {
29 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) {
37 return;
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.
45 if (listener_)
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())
55 continue;
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,
63 true,
65 base::Bind(
66 &IconHelper::DownloadFaviconCallback, base::Unretained(this)));
67 break;
68 case content::FaviconURL::TOUCH_ICON:
69 if (listener_)
70 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false);
71 break;
72 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
73 if (listener_)
74 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true);
75 break;
76 case content::FaviconURL::INVALID_ICON:
77 // Silently ignore it. Only trigger a callback on valid icons.
78 break;
79 default:
80 NOTREACHED();
81 break;
86 } // namespace android_webview