Chromium Code Reviews| 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() { | |
|
sky
2012/11/15 01:22:17
Position should match header (many of the methods
Cait (Slow)
2012/11/15 16:25:26
Done.
| |
| 51 } | |
| 52 | |
| 53 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id, | |
| 54 int32 page_id, | |
| 55 const std::vector<FaviconURL>& urls) { | |
| 56 if (!urls.empty()) { | |
|
sky
2012/11/15 01:22:17
nit: no {}
Cait (Slow)
2012/11/15 16:25:26
Done.
| |
| 57 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls)); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { | |
| 62 bool handled = true; | |
| 63 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) | |
| 64 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) | |
| 65 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 66 IPC_END_MESSAGE_MAP() | |
| 67 | |
| 68 return handled; | |
| 69 } | |
| 70 | |
| 71 void FaviconHelper::OnDownloadFavicon(int id, | |
| 72 const GURL& image_url, | |
| 73 int image_size) { | |
| 74 std::vector<SkBitmap> result_images; | |
| 75 if (image_url.SchemeIs("data")) { | |
| 76 SkBitmap data_image = ImageFromDataUrl(image_url); | |
| 77 if (!data_image.empty()) | |
| 78 result_images.push_back(data_image); | |
| 79 } else { | |
| 80 if (DownloadFavicon(id, image_url, image_size)) { | |
| 81 // Will complete asynchronously via FaviconHelper::DidDownloadFavicon | |
| 82 return; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 Send(new IconHostMsg_DidDownloadFavicon( | |
| 87 routing_id(), id, image_url, true, image_size, result_images)); | |
| 88 } | |
| 89 | |
| 90 bool FaviconHelper::DownloadFavicon(int id, | |
| 91 const GURL& image_url, | |
| 92 int image_size) { | |
| 93 // Make sure webview was not shut down. | |
| 94 if (!render_view()->GetWebView()) | |
| 95 return false; | |
| 96 // Create an image resource fetcher and assign it with a call back object. | |
| 97 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | |
| 98 image_url, render_view()->GetWebView()->mainFrame(), id, | |
| 99 WebURLRequest::TargetIsFavicon, | |
| 100 base::Bind(&FaviconHelper::DidDownloadFavicon, | |
| 101 base::Unretained(this), image_size))); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 void FaviconHelper::DidDownloadFavicon( | |
| 106 int requested_size, | |
| 107 MultiResolutionImageResourceFetcher* fetcher, | |
| 108 const std::vector<SkBitmap>& images) { | |
| 109 // Notify requester of image download status. | |
| 110 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
| 111 fetcher->id(), | |
| 112 fetcher->image_url(), | |
| 113 images.empty(), | |
| 114 requested_size, | |
| 115 images)); | |
| 116 | |
| 117 // Remove the image fetcher from our pending list. We're in the callback from | |
| 118 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
| 119 ImageResourceFetcherList::iterator iter = | |
| 120 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | |
| 121 if (iter != image_fetchers_.end()) { | |
| 122 image_fetchers_.weak_erase(iter); | |
| 123 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void FaviconHelper::DidStopLoading() { | |
| 128 int icon_types = WebIconURL::TypeFavicon; | |
| 129 if (chrome::kEnableTouchIcon) | |
| 130 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | |
| 131 | |
| 132 WebVector<WebIconURL> icon_urls = | |
| 133 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); | |
| 134 std::vector<FaviconURL> urls; | |
| 135 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 136 WebURL url = icon_urls[i].iconURL(); | |
| 137 if (!url.isEmpty()) | |
| 138 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); | |
| 139 } | |
| 140 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 141 } | |
| 142 | |
| 143 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame, | |
| 144 WebKit::WebIconURL::Type icon_type) { | |
| 145 if (frame->parent()) | |
| 146 return; | |
| 147 | |
| 148 if (!chrome::kEnableTouchIcon && | |
| 149 icon_type != WebIconURL::TypeFavicon) | |
| 150 return; | |
| 151 | |
| 152 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_type); | |
| 153 std::vector<FaviconURL> urls; | |
| 154 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 155 urls.push_back(FaviconURL(icon_urls[i].iconURL(), | |
| 156 ToFaviconType(icon_urls[i].iconType()))); | |
| 157 } | |
| 158 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 159 } | |
| 160 | |
| 161 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const { | |
| 162 std::string mime_type, char_set, data; | |
| 163 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
| 164 // Decode the favicon using WebKit's image decoder. | |
| 165 webkit_glue::ImageDecoder decoder( | |
| 166 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
| 167 const unsigned char* src_data = | |
| 168 reinterpret_cast<const unsigned char*>(&data[0]); | |
| 169 | |
| 170 return decoder.Decode(src_data, data.size()); | |
| 171 } | |
| 172 return SkBitmap(); | |
| 173 } | |
| OLD | NEW |