Upstream version 10.38.217.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / android / xwalk_icon_helper.cc
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.
5
6 #include "xwalk/runtime/browser/android/xwalk_icon_helper.h"
7
8 #include "base/bind.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"
15
16 using content::BrowserThread;
17 using content::WebContents;
18
19 namespace xwalk {
20
21 XWalkIconHelper::XWalkIconHelper(WebContents* web_contents)
22     : content::WebContentsObserver(web_contents),
23       listener_(NULL) {
24 }
25
26 XWalkIconHelper::~XWalkIconHelper() {
27 }
28
29 void XWalkIconHelper::SetListener(Listener* listener) {
30   listener_ = listener;
31 }
32
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)));
37 }
38
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())
45       continue;
46
47     switch (i->icon_type) {
48       case content::FaviconURL::FAVICON:
49         if (listener_) listener_->OnIconAvailable(i->icon_url);
50         break;
51       case content::FaviconURL::TOUCH_ICON:
52         break;
53       case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
54         break;
55       case content::FaviconURL::INVALID_ICON:
56         break;
57       default:
58         NOTREACHED();
59         break;
60     }
61   }
62 }
63
64 void XWalkIconHelper::DownloadFaviconCallback(
65     int id,
66     int http_status_code,
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;
72
73   if (listener_) listener_->OnReceivedIcon(image_url, bitmaps[0]);
74 }
75
76 }  // namespace xwalk