1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2014 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/runtime/browser/android/xwalk_icon_helper.h"
9 #include "base/callback.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;
21 XWalkIconHelper::XWalkIconHelper(WebContents* web_contents)
22 : content::WebContentsObserver(web_contents),
26 XWalkIconHelper::~XWalkIconHelper() {
29 void XWalkIconHelper::SetListener(Listener* listener) {
33 void XWalkIconHelper::DownloadIcon(const GURL& icon_url) {
34 web_contents()->DownloadImage(icon_url, true, 0,
35 base::Bind(&XWalkIconHelper::DownloadFaviconCallback,
36 base::Unretained(this)));
39 void XWalkIconHelper::DidUpdateFaviconURL(
40 const std::vector<content::FaviconURL>& candidates) {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
42 for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin();
43 i != candidates.end(); ++i) {
44 if (!i->icon_url.is_valid())
47 switch (i->icon_type) {
48 case content::FaviconURL::FAVICON:
49 if (listener_) listener_->OnIconAvailable(i->icon_url);
51 case content::FaviconURL::TOUCH_ICON:
53 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
55 case content::FaviconURL::INVALID_ICON:
64 void XWalkIconHelper::DownloadFaviconCallback(
67 const GURL& image_url,
68 const std::vector<SkBitmap>& bitmaps,
69 const std::vector<gfx::Size>& original_bitmap_sizes) {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 if (http_status_code == 404 || bitmaps.size() == 0) return;
73 if (listener_) listener_->OnReceivedIcon(image_url, bitmaps[0]);