| 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 "content/renderer/favicon_helper.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "content/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/Platform/chromium/public/WebURLRequest.h" | |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
| 17 #include "ui/base/ui_base_switches.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 namespace content { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 bool TouchEnabled() { | |
| 37 // Based on the definition of chrome::kEnableTouchIcon. | |
| 38 #if defined(OS_ANDROID) | |
| 39 return true; | |
| 40 #else | |
| 41 return false; | |
| 42 #endif | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 | |
| 48 static FaviconURL::IconType ToFaviconType(WebIconURL::Type type) { | |
| 49 switch (type) { | |
| 50 case WebIconURL::TypeFavicon: | |
| 51 return FaviconURL::FAVICON; | |
| 52 case WebIconURL::TypeTouch: | |
| 53 return FaviconURL::TOUCH_ICON; | |
| 54 case WebIconURL::TypeTouchPrecomposed: | |
| 55 return FaviconURL::TOUCH_PRECOMPOSED_ICON; | |
| 56 case WebIconURL::TypeInvalid: | |
| 57 return FaviconURL::INVALID_ICON; | |
| 58 } | |
| 59 return FaviconURL::INVALID_ICON; | |
| 60 } | |
| 61 | |
| 62 FaviconHelper::FaviconHelper(RenderView* render_view) | |
| 63 : RenderViewObserver(render_view), | |
| 64 icon_types_changed_(WebIconURL::TypeInvalid), | |
| 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 66 } | |
| 67 | |
| 68 void FaviconHelper::DidChangeIcon(WebKit::WebFrame* frame, | |
| 69 WebKit::WebIconURL::Type icon_type) { | |
| 70 if (frame->parent()) | |
| 71 return; | |
| 72 | |
| 73 if (!TouchEnabled() && icon_type != WebIconURL::TypeFavicon) | |
| 74 return; | |
| 75 | |
| 76 DCHECK(!render_view()->GetWebView() || | |
| 77 frame == render_view()->GetWebView()->mainFrame()); | |
| 78 | |
| 79 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 80 icon_types_changed_ = | |
| 81 static_cast<WebKit::WebIconURL::Type>(icon_types_changed_ | icon_type); | |
| 82 MessageLoop::current()->PostTask(FROM_HERE, | |
| 83 base::Bind(&FaviconHelper::ProcessDidChangeIcon, | |
| 84 weak_ptr_factory_.GetWeakPtr())); | |
| 85 } | |
| 86 | |
| 87 FaviconHelper::~FaviconHelper() { | |
| 88 } | |
| 89 | |
| 90 void FaviconHelper::ProcessDidChangeIcon() { | |
| 91 WebKit::WebIconURL::Type icon_types = icon_types_changed_; | |
| 92 icon_types_changed_ = WebIconURL::TypeInvalid; | |
| 93 WebKit::WebView* web_view = render_view()->GetWebView(); | |
| 94 if (!web_view) { | |
| 95 return; | |
| 96 } | |
| 97 WebFrame* frame = web_view->mainFrame(); | |
| 98 if (!frame) { | |
| 99 return; | |
| 100 } | |
| 101 WebVector<WebIconURL> icon_urls = frame->iconURLs(icon_types); | |
| 102 std::vector<FaviconURL> urls; | |
| 103 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 104 urls.push_back(FaviconURL(icon_urls[i].iconURL(), | |
| 105 ToFaviconType(icon_urls[i].iconType()))); | |
| 106 } | |
| 107 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 108 } | |
| 109 | |
| 110 void FaviconHelper::OnDownloadFavicon(int id, | |
| 111 const GURL& image_url, | |
| 112 bool is_favicon, | |
| 113 int image_size) { | |
| 114 std::vector<SkBitmap> result_images; | |
| 115 if (image_url.SchemeIs("data")) { | |
| 116 SkBitmap data_image = ImageFromDataUrl(image_url); | |
| 117 if (!data_image.empty()) | |
| 118 result_images.push_back(data_image); | |
| 119 } else { | |
| 120 if (DownloadFavicon(id, image_url, is_favicon, image_size)) { | |
| 121 // Will complete asynchronously via FaviconHelper::DidDownloadFavicon | |
| 122 return; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
| 127 id, | |
| 128 image_url, | |
| 129 image_size, | |
| 130 result_images)); | |
| 131 } | |
| 132 | |
| 133 bool FaviconHelper::DownloadFavicon(int id, | |
| 134 const GURL& image_url, | |
| 135 bool is_favicon, | |
| 136 int image_size) { | |
| 137 // Make sure webview was not shut down. | |
| 138 if (!render_view()->GetWebView()) | |
| 139 return false; | |
| 140 // Create an image resource fetcher and assign it with a call back object. | |
| 141 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | |
| 142 image_url, | |
| 143 render_view()->GetWebView()->mainFrame(), | |
| 144 id, | |
| 145 is_favicon ? WebURLRequest::TargetIsFavicon : | |
| 146 WebURLRequest::TargetIsImage, | |
| 147 base::Bind(&FaviconHelper::DidDownloadFavicon, | |
| 148 base::Unretained(this), image_size))); | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 void FaviconHelper::DidDownloadFavicon( | |
| 153 int requested_size, | |
| 154 MultiResolutionImageResourceFetcher* fetcher, | |
| 155 const std::vector<SkBitmap>& images) { | |
| 156 // Notify requester of image download status. | |
| 157 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | |
| 158 fetcher->id(), | |
| 159 fetcher->image_url(), | |
| 160 requested_size, | |
| 161 images)); | |
| 162 | |
| 163 // Remove the image fetcher from our pending list. We're in the callback from | |
| 164 // MultiResolutionImageResourceFetcher, best to delay deletion. | |
| 165 ImageResourceFetcherList::iterator iter = | |
| 166 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | |
| 167 if (iter != image_fetchers_.end()) { | |
| 168 image_fetchers_.weak_erase(iter); | |
| 169 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const { | |
| 174 std::string mime_type, char_set, data; | |
| 175 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | |
| 176 // Decode the favicon using WebKit's image decoder. | |
| 177 webkit_glue::ImageDecoder decoder( | |
| 178 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | |
| 179 const unsigned char* src_data = | |
| 180 reinterpret_cast<const unsigned char*>(&data[0]); | |
| 181 | |
| 182 return decoder.Decode(src_data, data.size()); | |
| 183 } | |
| 184 return SkBitmap(); | |
| 185 } | |
| 186 | |
| 187 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id, | |
| 188 int32 page_id, | |
| 189 const std::vector<FaviconURL>& urls) { | |
| 190 if (!urls.empty()) | |
| 191 Send(new IconHostMsg_UpdateFaviconURL(routing_id, page_id, urls)); | |
| 192 } | |
| 193 | |
| 194 bool FaviconHelper::OnMessageReceived(const IPC::Message& message) { | |
| 195 bool handled = true; | |
| 196 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) | |
| 197 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) | |
| 198 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 199 IPC_END_MESSAGE_MAP() | |
| 200 | |
| 201 return handled; | |
| 202 } | |
| 203 | |
| 204 void FaviconHelper::DidStopLoading() { | |
| 205 int icon_types = WebIconURL::TypeFavicon; | |
| 206 if (TouchEnabled()) | |
| 207 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | |
| 208 | |
| 209 WebVector<WebIconURL> icon_urls = | |
| 210 render_view()->GetWebView()->mainFrame()->iconURLs(icon_types); | |
| 211 std::vector<FaviconURL> urls; | |
| 212 for (size_t i = 0; i < icon_urls.size(); i++) { | |
| 213 WebURL url = icon_urls[i].iconURL(); | |
| 214 if (!url.isEmpty()) | |
| 215 urls.push_back(FaviconURL(url, ToFaviconType(icon_urls[i].iconType()))); | |
| 216 } | |
| 217 SendUpdateFaviconURL(routing_id(), render_view()->GetPageId(), urls); | |
| 218 } | |
| 219 | |
| 220 } // namespace content | |
| OLD | NEW |