| 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/chrome_constants.h" | |
| 10 #include "chrome/common/favicon_url.h" | |
| 11 #include "chrome/common/icon_messages.h" | |
| 12 #include "content/public/renderer/render_view.h" | |
| 13 #include "net/base/data_url.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | |
| 18 #include "ui/gfx/favicon_size.h" | |
| 19 #include "ui/gfx/size.h" | |
| 20 #include "ui/gfx/skbitmap_operations.h" | |
| 21 #include "webkit/glue/image_decoder.h" | |
| 22 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" | |
| 23 #include "webkit/glue/webkit_glue.h" | |
| 24 | |
| 25 using WebKit::WebFrame; | |
| 26 using WebKit::WebIconURL; | |
| 27 using WebKit::WebVector; | |
| 28 using WebKit::WebURL; | |
| 29 using WebKit::WebURLRequest; | |
| 30 using webkit_glue::MultiResolutionImageResourceFetcher; | |
| 31 | |
| 32 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) { | |
| 33 switch (type) { | |
| 34 case WebIconURL::TypeFavicon: | |
| 35 return FaviconURL::FAVICON; | |
| 36 case WebIconURL::TypeTouch: | |
| 37 return FaviconURL::TOUCH_ICON; | |
| 38 case WebIconURL::TypeTouchPrecomposed: | |
| 39 return FaviconURL::TOUCH_PRECOMPOSED_ICON; | |
| 40 case WebIconURL::TypeInvalid: | |
| 41 return FaviconURL::INVALID_ICON; | |
| 42 } | |
| 43 return FaviconURL::INVALID_ICON; | |
| 44 } | |
| 45 | |
| 46 FaviconHelper::FaviconHelper(content::RenderView* render_view) | |
| 47 : content::RenderViewObserver(render_view) { | |
| 48 } | |
| 49 | |
| 50 FaviconHelper::~FaviconHelper() { | |
| 51 } | |
| 52 | |
| 53 void FaviconHelper::OnDownloadFavicon(int id, | |
| 54 const GURL& image_url, | |
| 55 int image_size) { | |
| 56 std::vector<SkBitmap> result_images; | |
| 57 if (image_url.SchemeIs("data")) { | |
| 58 SkBitmap data_image = ImageFromDataUrl(image_url); | |
| 59 if (!data_image.empty()) | |
| 60 result_images.push_back(data_image); | |
| 61 } else { | |
| 62 if (DownloadFavicon(id, image_url, image_size)) { | |
| 63 // Will complete asynchronously via FaviconHelper::DidDownloadFavicon | |
| 64 return; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 Send(new IconHostMsg_DidDownloadFavicon( | |
| 69 routing_id(), id, image_url, true, image_size, result_images)); | |
| 70 } | |
| 71 | |
| 72 bool FaviconHelper::DownloadFavicon(int id, | |
| 73 const GURL& image_url, | |
| 74 int image_size) { | |
| 75 // Make sure webview was not shut down. | |
| 76 if (!render_view()->GetWebView()) | |
| 77 return false; | |
| 78 // Create an image resource fetcher and assign it with a call back object. | |
| 79 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | |
| 80 image_url, render_view()->GetWebView()->mainFrame(), id, | |
| 81 WebURLRequest::TargetIsFavicon, | |
| 82 base::Bind(&FaviconHelper::DidDownloadFavicon, | |
| 83 base::Unretained(this), image_size))); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 void FaviconHelper::DidDownloadFavicon( | |
| 88 int requested_size, | |
| 89 MultiResolutionImageResourceFetcher* fetcher, | |
| 90 const std::vector<SkBitmap>& images) { | |
| 91 // Notify requester of image download status. | |
| 92 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
| 93 fetcher->id(), | |
| 94 fetcher->image_url(), | |
| 95 images.empty(), | |
| 96 requested_size, | |
| 97 images)); | |
| 98 | |
| 99 // Remove the image fetcher from our pending list. We're in the callback from | |
| 100 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
| 101 ImageResourceFetcherList::iterator iter = | |
| 102 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | |
| 103 if (iter != image_fetchers_.end()) { | |
| 104 image_fetchers_.weak_erase(iter); | |
| 105 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const { | |
| 110 std::string mime_type, char_set, data; | |
| 111 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
| 112 // Decode the favicon using WebKit's image decoder. | |
| 113 webkit_glue::ImageDecoder decoder( | |
| 114 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
| 115 const unsigned char* src_data = | |
| 116 reinterpret_cast<const unsigned char*>(&data[0]); | |
| 117 | |
| 118 return decoder.Decode(src_data, data.size()); | |
| 119 } | |
| 120 return SkBitmap(); | |
| 121 } | |
| 122 | |
| 123 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id, | |
| 124 int32 page_id, | |
| 125 const std::vector<FaviconURL>& urls) { | |
| 126 if (!urls.empty()) | |
| 127 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls)); | |
| 128 } | |
| 129 | |
| 130 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { | |
| 131 bool handled = true; | |
| 132 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) | |
| 133 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) | |
| 134 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 135 IPC_END_MESSAGE_MAP() | |
| 136 | |
| 137 return handled; | |
| 138 } | |
| 139 | |
| 140 void FaviconHelper::DidStopLoading() { | |
| 141 int icon_types = WebIconURL::TypeFavicon; | |
| 142 if (chrome::kEnableTouchIcon) | |
| 143 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | |
| 144 | |
| 145 WebVector<WebIconURL> icon_urls = | |
| 146 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); | |
| 147 std::vector<FaviconURL> urls; | |
| 148 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 149 WebURL url = icon_urls[i].iconURL(); | |
| 150 if (!url.isEmpty()) | |
| 151 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); | |
| 152 } | |
| 153 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 154 } | |
| 155 | |
| 156 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame, | |
| 157 WebKit::WebIconURL::Type icon_type) { | |
| 158 if (frame->parent()) | |
| 159 return; | |
| 160 | |
| 161 if (!chrome::kEnableTouchIcon && | |
| 162 icon_type != WebIconURL::TypeFavicon) | |
| 163 return; | |
| 164 | |
| 165 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type); | |
| 166 std::vector<FaviconURL> urls; | |
| 167 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 168 urls.push_back(FaviconURL(icon_urls[i].iconURL(), | |
| 169 ToFaviconType(icon_urls[i].iconType()))); | |
| 170 } | |
| 171 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 172 } | |
| OLD | NEW |