OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/renderer/favicon_helper.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/common/favicon_url.h" |
| 10 #include "chrome/common/icon_messages.h" |
| 11 #include "content/public/renderer/render_view.h" |
| 12 #include "net/base/data_url.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
| 17 #include "ui/gfx/favicon_size.h" |
| 18 #include "ui/gfx/size.h" |
| 19 #include "ui/gfx/skbitmap_operations.h" |
| 20 #include "webkit/glue/image_decoder.h" |
| 21 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" |
| 22 #include "webkit/glue/webkit_glue.h" |
| 23 |
| 24 using WebKit::WebURLRequest; |
| 25 using webkit_glue::MultiResolutionImageResourceFetcher; |
| 26 |
| 27 FaviconHelper::FaviconHelper(content::RenderView* render_view) |
| 28 : content::RenderViewObserver(render_view) { |
| 29 } |
| 30 |
| 31 FaviconHelper::~FaviconHelper() { |
| 32 } |
| 33 |
| 34 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id, |
| 35 int32 page_id, |
| 36 const std::vector<FaviconURL>& urls) { |
| 37 if (!urls.empty()) { |
| 38 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls)); |
| 39 } |
| 40 } |
| 41 |
| 42 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { |
| 43 bool handled = true; |
| 44 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) |
| 45 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) |
| 47 IPC_END_MESSAGE_MAP() |
| 48 |
| 49 return handled; |
| 50 } |
| 51 |
| 52 void FaviconHelper::OnDownloadFavicon(int id, |
| 53 const GURL& image_url, |
| 54 int image_size) { |
| 55 bool data_image_failed = false; |
| 56 if (image_url.SchemeIs("data")) { |
| 57 SkBitmap data_image = ImageFromDataUrl(image_url); |
| 58 data_image_failed = data_image.empty(); |
| 59 if (!data_image_failed) { |
| 60 std::vector<SkBitmap> images(1, data_image); |
| 61 Send(new IconHostMsg_DidDownloadFavicon( |
| 62 routing_id(), id, image_url, false, image_size, images)); |
| 63 } |
| 64 } |
| 65 |
| 66 if (data_image_failed || |
| 67 !DownloadFavicon(id, image_url, image_size)) { |
| 68 Send(new IconHostMsg_DidDownloadFavicon( |
| 69 routing_id(), id, image_url, true, image_size, |
| 70 std::vector<SkBitmap>())); |
| 71 } |
| 72 } |
| 73 |
| 74 bool FaviconHelper::DownloadFavicon(int id, |
| 75 const GURL& image_url, |
| 76 int image_size) { |
| 77 // Make sure webview was not shut down. |
| 78 if (!render_view()->GetWebView()) |
| 79 return false; |
| 80 // Create an image resource fetcher and assign it with a call back object. |
| 81 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( |
| 82 image_url, render_view()->GetWebView()->mainFrame(), id, |
| 83 WebURLRequest::TargetIsFavicon, |
| 84 base::Bind(&FaviconHelper::DidDownloadFavicon, |
| 85 base::Unretained(this), image_size))); |
| 86 return true; |
| 87 } |
| 88 |
| 89 void FaviconHelper::DidDownloadFavicon( |
| 90 int requested_size, |
| 91 MultiResolutionImageResourceFetcher* fetcher, |
| 92 const std::vector<SkBitmap>& images) { |
| 93 // Notify requester of image download status. |
| 94 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), |
| 95 fetcher->id(), |
| 96 fetcher->image_url(), |
| 97 images.empty(), |
| 98 requested_size, |
| 99 images)); |
| 100 |
| 101 // Remove the image fetcher from our pending list. We're in the callback from |
| 102 // MultiResolutionImageResourceFetcher, best to delay deletion. |
| 103 ImageResourceFetcherList::iterator iter = |
| 104 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); |
| 105 if (iter != image_fetchers_.end()) { |
| 106 image_fetchers_.erase(iter); |
| 107 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); |
| 108 } |
| 109 } |
| 110 |
| 111 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const { |
| 112 std::string mime_type, char_set, data; |
| 113 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { |
| 114 // Decode the favicon using WebKit's image decoder. |
| 115 webkit_glue::ImageDecoder decoder( |
| 116 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); |
| 117 const unsigned char* src_data = |
| 118 reinterpret_cast<const unsigned char*>(&data[0]); |
| 119 |
| 120 return decoder.Decode(src_data, data.size()); |
| 121 } |
| 122 return SkBitmap(); |
| 123 } |
OLD | NEW |