Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "android_webview/browser/icon_helper.h"
      6 
      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"
     14 
     15 using content::BrowserThread;
     16 using content::WebContents;
     17 
     18 namespace android_webview {
     19 
     20 IconHelper::IconHelper(WebContents* web_contents)
     21     : WebContentsObserver(web_contents),
     22       listener_(NULL) {
     23 }
     24 
     25 IconHelper::~IconHelper() {
     26 }
     27 
     28 void IconHelper::SetListener(Listener* listener) {
     29   listener_ = listener;
     30 }
     31 
     32 void IconHelper::DownloadFaviconCallback(
     33   int id, int http_status_code, 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;
     38   }
     39 
     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.
     43 
     44   // TODO(acleung): Pick the best icon to return based on size.
     45   if (listener_)
     46     listener_->OnReceivedIcon(image_url, bitmaps[0]);
     47 }
     48 
     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;
     56 
     57     switch(i->icon_type) {
     58       case content::FaviconURL::FAVICON:
     59         if (listener_ && !listener_->ShouldDownloadFavicon(i->icon_url)) break;
     60         web_contents()->DownloadImage(i->icon_url,
     61             true,  // Is a favicon
     62             0,  // No preferred size
     63             0,  // No maximum size
     64             base::Bind(
     65                 &IconHelper::DownloadFaviconCallback, base::Unretained(this)));
     66         break;
     67       case content::FaviconURL::TOUCH_ICON:
     68         if (listener_)
     69           listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false);
     70         break;
     71       case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
     72         if (listener_)
     73           listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true);
     74         break;
     75       case content::FaviconURL::INVALID_ICON:
     76         // Silently ignore it. Only trigger a callback on valid icons.
     77         break;
     78       default:
     79         NOTREACHED();
     80         break;
     81     }
     82   }
     83 }
     84 
     85 }  // namespace android_webview
     86