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 #include "ui/gfx/size.h"
     15 
     16 using content::BrowserThread;
     17 using content::WebContents;
     18 
     19 namespace android_webview {
     20 
     21 IconHelper::IconHelper(WebContents* web_contents)
     22     : WebContentsObserver(web_contents),
     23       listener_(NULL) {
     24 }
     25 
     26 IconHelper::~IconHelper() {
     27 }
     28 
     29 void IconHelper::SetListener(Listener* listener) {
     30   listener_ = listener;
     31 }
     32 
     33 void IconHelper::DownloadFaviconCallback(
     34     int id,
     35     int http_status_code,
     36     const GURL& image_url,
     37     const std::vector<SkBitmap>& bitmaps,
     38     const std::vector<gfx::Size>& original_bitmap_sizes) {
     39   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     40   if (bitmaps.size() == 0) {
     41     return;
     42   }
     43 
     44   // We can protentially have multiple frames of the icon
     45   // in different sizes. We need more fine grain API spec
     46   // to let clients pick out the frame they want.
     47 
     48   // TODO(acleung): Pick the best icon to return based on size.
     49   if (listener_)
     50     listener_->OnReceivedIcon(image_url, bitmaps[0]);
     51 }
     52 
     53 void IconHelper::DidUpdateFaviconURL(int32 page_id,
     54     const std::vector<content::FaviconURL>& candidates) {
     55   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     56   for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin();
     57        i != candidates.end(); ++i) {
     58     if (!i->icon_url.is_valid())
     59       continue;
     60 
     61     switch(i->icon_type) {
     62       case content::FaviconURL::FAVICON:
     63         if (listener_ && !listener_->ShouldDownloadFavicon(i->icon_url)) break;
     64         web_contents()->DownloadImage(i->icon_url,
     65             true,  // Is a favicon
     66             0,  // No maximum size
     67             base::Bind(
     68                 &IconHelper::DownloadFaviconCallback, base::Unretained(this)));
     69         break;
     70       case content::FaviconURL::TOUCH_ICON:
     71         if (listener_)
     72           listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false);
     73         break;
     74       case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
     75         if (listener_)
     76           listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true);
     77         break;
     78       case content::FaviconURL::INVALID_ICON:
     79         // Silently ignore it. Only trigger a callback on valid icons.
     80         break;
     81       default:
     82         NOTREACHED();
     83         break;
     84     }
     85   }
     86 }
     87 
     88 }  // namespace android_webview
     89