From b8da3a35c5ec40581963920758c63136110a9f05 Mon Sep 17 00:00:00 2001
From: "acleung@chromium.org"
 <acleung@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 22 Jan 2013 23:54:08 +0000
Subject: Implements onReceivedIcon

This patch implements both WebChromeClient.onReceivedIcon and WebView.getFavIcon, and provides most the  support for WebChromeClient.onReceivedTouchIcon.

It also includes instrumentation tests for onReceivedIcon.

What is still missing:

Ability to turn it on and off.
TouchIcon currently isn't working.
Navigation history is not updated as of this patch.

BUG=

Review URL: https://chromiumcodereview.appspot.com/11575022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178158 0039d316-1c4b-4281-b951-d872f2087c98
---
 android_webview/browser/icon_helper.cc | 79 ++++++++++++++++++++++++++++++++++
 android_webview/browser/icon_helper.h  | 51 ++++++++++++++++++++++
 2 files changed, 130 insertions(+)
 create mode 100644 android_webview/browser/icon_helper.cc
 create mode 100644 android_webview/browser/icon_helper.h

(limited to 'android_webview/browser')

diff --git a/android_webview/browser/icon_helper.cc b/android_webview/browser/icon_helper.cc
new file mode 100644
index 0000000..4cbe07a
--- /dev/null
+++ b/android_webview/browser/icon_helper.cc
@@ -0,0 +1,79 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "android_webview/browser/icon_helper.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/logging.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/favicon_url.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+using content::BrowserThread;
+using content::WebContents;
+
+namespace android_webview {
+
+IconHelper::IconHelper(WebContents* web_contents)
+    : WebContentsObserver(web_contents),
+      listener_(NULL) {
+}
+
+IconHelper::~IconHelper() {
+}
+
+void IconHelper::SetListener(Listener* listener) {
+  listener_ = listener;
+}
+
+void IconHelper::DownloadFaviconCallback(
+  int id, const GURL& image_url, int requested_size,
+  const std::vector<SkBitmap>& bitmaps) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  if (bitmaps.size() == 0) {
+    return;
+  }
+
+  // We can protentially have multiple frames of the icon
+  // in different sizes. We need more fine grain API spec
+  // to let clients pick out the frame they want.
+
+  // TODO(acleung): Pick the best icon to return based on size.
+  if (listener_)
+    listener_->OnReceivedIcon(bitmaps[0]);
+}
+
+void IconHelper::DidUpdateFaviconURL(int32 page_id,
+    const std::vector<content::FaviconURL>& candidates) {
+  for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin();
+       i != candidates.end(); ++i) {
+    if (i->icon_url.is_empty())
+      continue;
+
+    switch(i->icon_type) {
+      case content::FaviconURL::FAVICON:
+        // TODO(acleung): only fetch the URL if favicon downloading is enabled.
+        // (currently that is, the app has called WebIconDatabase.open()
+        // but we should decouple that setting via a boolean setting)
+        web_contents()->DownloadFavicon(i->icon_url, 0, base::Bind(
+            &IconHelper::DownloadFaviconCallback, base::Unretained(this)));
+        break;
+      // TODO(acleung): Touch Icon doesn't seem to work ATM.
+      case content::FaviconURL::TOUCH_ICON:
+        break;
+      case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
+        break;
+      case content::FaviconURL::INVALID_ICON:
+        // Silently ignore it. Only trigger a callback on valid icons.
+        break;
+      default:
+        NOTREACHED();
+        break;
+    }
+  }
+}
+
+}  // namespace android_webview
diff --git a/android_webview/browser/icon_helper.h b/android_webview/browser/icon_helper.h
new file mode 100644
index 0000000..e684cdf
--- /dev/null
+++ b/android_webview/browser/icon_helper.h
@@ -0,0 +1,51 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_
+#define ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_
+
+#include <string>
+#include "content/public/browser/web_contents_observer.h"
+
+class SkBitmap;
+
+namespace content {
+struct FaviconURL;
+}
+
+namespace android_webview {
+
+// A helper that observes favicon changes for Webview.
+class IconHelper : public content::WebContentsObserver {
+ public:
+  class Listener {
+   public:
+    virtual void OnReceivedIcon(const SkBitmap& bitmap) = 0;
+    virtual void OnReceivedTouchIconUrl(const std::string& url,
+                                        const bool precomposed) = 0;
+   protected:
+    virtual ~Listener() {}
+  };
+
+  explicit IconHelper(content::WebContents* web_contents);
+  virtual ~IconHelper();
+
+  void SetListener(Listener* listener);
+
+  // From WebContentsObserver
+  virtual void DidUpdateFaviconURL(int32 page_id,
+      const std::vector<content::FaviconURL>& candidates) OVERRIDE;
+
+  void DownloadFaviconCallback(int id, const GURL& image_url,
+      int requested_size, const std::vector<SkBitmap>& bitmaps);
+
+ private:
+  Listener* listener_;
+
+  DISALLOW_COPY_AND_ASSIGN(IconHelper);
+};
+
+}  // namespace android_webview
+
+#endif  // ANDROID_WEBVIEW_BROWSER_ICON_HELPER_H_
-- 
cgit v1.1