| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/favicon_helper.h" | 5 #include "content/renderer/image_loading_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 10 #include "content/common/icon_messages.h" | 9 #include "content/common/image_messages.h" |
| 10 #include "content/public/common/url_constants.h" |
| 11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
| 12 #include "net/base/data_url.h" | 12 #include "net/base/data_url.h" |
| 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" | 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" | 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.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" | 17 #include "ui/gfx/favicon_size.h" |
| 19 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
| 20 #include "ui/gfx/skbitmap_operations.h" | 19 #include "ui/gfx/skbitmap_operations.h" |
| 21 #include "webkit/glue/image_decoder.h" | 20 #include "webkit/glue/image_decoder.h" |
| 22 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" | 21 #include "webkit/glue/multi_resolution_image_resource_fetcher.h" |
| 23 #include "webkit/glue/webkit_glue.h" | 22 #include "webkit/glue/webkit_glue.h" |
| 24 | 23 |
| 25 using WebKit::WebFrame; | 24 using WebKit::WebFrame; |
| 26 using WebKit::WebIconURL; | |
| 27 using WebKit::WebVector; | 25 using WebKit::WebVector; |
| 28 using WebKit::WebURL; | 26 using WebKit::WebURL; |
| 29 using WebKit::WebURLRequest; | 27 using WebKit::WebURLRequest; |
| 30 using webkit_glue::MultiResolutionImageResourceFetcher; | 28 using webkit_glue::MultiResolutionImageResourceFetcher; |
| 31 | 29 |
| 32 namespace content { | 30 namespace content { |
| 33 | 31 |
| 34 namespace { | 32 ImageLoadingHelper::ImageLoadingHelper(RenderView* render_view) |
| 35 | 33 : RenderViewObserver(render_view) { |
| 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 } | 34 } |
| 44 | 35 |
| 45 } // namespace | 36 ImageLoadingHelper::~ImageLoadingHelper() { |
| 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 } | 37 } |
| 61 | 38 |
| 62 FaviconHelper::FaviconHelper(RenderView* render_view) | 39 void ImageLoadingHelper::OnDownloadImage(int id, |
| 63 : RenderViewObserver(render_view), | 40 const GURL& image_url, |
| 64 icon_types_changed_(WebIconURL::TypeInvalid), | 41 bool is_favicon, |
| 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 42 int image_size) { |
| 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; | 43 std::vector<SkBitmap> result_images; |
| 115 if (image_url.SchemeIs("data")) { | 44 if (image_url.SchemeIs(chrome::kDataScheme)) { |
| 116 SkBitmap data_image = ImageFromDataUrl(image_url); | 45 SkBitmap data_image = ImageFromDataUrl(image_url); |
| 117 if (!data_image.empty()) | 46 if (!data_image.empty()) |
| 118 result_images.push_back(data_image); | 47 result_images.push_back(data_image); |
| 119 } else { | 48 } else { |
| 120 if (DownloadFavicon(id, image_url, is_favicon, image_size)) { | 49 if (DownloadImage(id, image_url, is_favicon, image_size)) { |
| 121 // Will complete asynchronously via FaviconHelper::DidDownloadFavicon | 50 // Will complete asynchronously via ImageLoadingHelper::DidDownloadImage |
| 122 return; | 51 return; |
| 123 } | 52 } |
| 124 } | 53 } |
| 125 | 54 |
| 126 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | 55 Send(new ImageHostMsg_DidDownloadImage(routing_id(), |
| 127 id, | 56 id, |
| 128 image_url, | 57 image_url, |
| 129 image_size, | 58 image_size, |
| 130 result_images)); | 59 result_images)); |
| 131 } | 60 } |
| 132 | 61 |
| 133 bool FaviconHelper::DownloadFavicon(int id, | 62 bool ImageLoadingHelper::DownloadImage(int id, |
| 134 const GURL& image_url, | 63 const GURL& image_url, |
| 135 bool is_favicon, | 64 bool is_favicon, |
| 136 int image_size) { | 65 int image_size) { |
| 137 // Make sure webview was not shut down. | 66 // Make sure webview was not shut down. |
| 138 if (!render_view()->GetWebView()) | 67 if (!render_view()->GetWebView()) |
| 139 return false; | 68 return false; |
| 140 // Create an image resource fetcher and assign it with a call back object. | 69 // Create an image resource fetcher and assign it with a call back object. |
| 141 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | 70 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( |
| 142 image_url, | 71 image_url, |
| 143 render_view()->GetWebView()->mainFrame(), | 72 render_view()->GetWebView()->mainFrame(), |
| 144 id, | 73 id, |
| 145 is_favicon ? WebURLRequest::TargetIsFavicon : | 74 is_favicon ? WebURLRequest::TargetIsFavicon : |
| 146 WebURLRequest::TargetIsImage, | 75 WebURLRequest::TargetIsImage, |
| 147 base::Bind(&FaviconHelper::DidDownloadFavicon, | 76 base::Bind(&ImageLoadingHelper::DidDownloadImage, |
| 148 base::Unretained(this), image_size))); | 77 base::Unretained(this), image_size))); |
| 149 return true; | 78 return true; |
| 150 } | 79 } |
| 151 | 80 |
| 152 void FaviconHelper::DidDownloadFavicon( | 81 void ImageLoadingHelper::DidDownloadImage( |
| 153 int requested_size, | 82 int requested_size, |
| 154 MultiResolutionImageResourceFetcher* fetcher, | 83 MultiResolutionImageResourceFetcher* fetcher, |
| 155 const std::vector<SkBitmap>& images) { | 84 const std::vector<SkBitmap>& images) { |
| 156 // Notify requester of image download status. | 85 // Notify requester of image download status. |
| 157 Send(new IconHostMsg_DidDownloadFavicon(routing_id(), | 86 Send(new ImageHostMsg_DidDownloadImage(routing_id(), |
| 158 fetcher->id(), | 87 fetcher->id(), |
| 159 fetcher->image_url(), | 88 fetcher->image_url(), |
| 160 requested_size, | 89 requested_size, |
| 161 images)); | 90 images)); |
| 162 | 91 |
| 163 // Remove the image fetcher from our pending list. We're in the callback from | 92 // Remove the image fetcher from our pending list. We're in the callback from |
| 164 // MultiResolutionImageResourceFetcher, best to delay deletion. | 93 // MultiResolutionImageResourceFetcher, best to delay deletion. |
| 165 ImageResourceFetcherList::iterator iter = | 94 ImageResourceFetcherList::iterator iter = |
| 166 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | 95 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); |
| 167 if (iter != image_fetchers_.end()) { | 96 if (iter != image_fetchers_.end()) { |
| 168 image_fetchers_.weak_erase(iter); | 97 image_fetchers_.weak_erase(iter); |
| 169 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | 98 MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); |
| 170 } | 99 } |
| 171 } | 100 } |
| 172 | 101 |
| 173 SkBitmap FaviconHelper::ImageFromDataUrl(const GURL& url) const { | 102 SkBitmap ImageLoadingHelper::ImageFromDataUrl(const GURL& url) const { |
| 174 std::string mime_type, char_set, data; | 103 std::string mime_type, char_set, data; |
| 175 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | 104 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { |
| 176 // Decode the favicon using WebKit's image decoder. | 105 // Decode the image using WebKit's image decoder. |
| 177 webkit_glue::ImageDecoder decoder( | 106 webkit_glue::ImageDecoder decoder( |
| 178 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | 107 gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); |
| 179 const unsigned char* src_data = | 108 const unsigned char* src_data = |
| 180 reinterpret_cast<const unsigned char*>(&data[0]); | 109 reinterpret_cast<const unsigned char*>(&data[0]); |
| 181 | 110 |
| 182 return decoder.Decode(src_data, data.size()); | 111 return decoder.Decode(src_data, data.size()); |
| 183 } | 112 } |
| 184 return SkBitmap(); | 113 return SkBitmap(); |
| 185 } | 114 } |
| 186 | 115 |
| 187 void FaviconHelper::SendUpdateFaviconURL(int32 routing_id, | 116 bool ImageLoadingHelper::OnMessageReceived(const IPC::Message& message) { |
| 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; | 117 bool handled = true; |
| 196 IPC_BEGIN_MESSAGE_MAP(FaviconHelper, message) | 118 IPC_BEGIN_MESSAGE_MAP(ImageLoadingHelper, message) |
| 197 IPC_MESSAGE_HANDLER(IconMsg_DownloadFavicon, OnDownloadFavicon) | 119 IPC_MESSAGE_HANDLER(ImageMsg_DownloadImage, OnDownloadImage) |
| 198 IPC_MESSAGE_UNHANDLED(handled = false) | 120 IPC_MESSAGE_UNHANDLED(handled = false) |
| 199 IPC_END_MESSAGE_MAP() | 121 IPC_END_MESSAGE_MAP() |
| 200 | 122 |
| 201 return handled; | 123 return handled; |
| 202 } | 124 } |
| 203 | 125 |
| 204 void FaviconHelper::DidStopLoading() { | 126 } // namespace content |
| 205 int icon_types = WebIconURL::TypeFavicon; | |
| 206 if (TouchEnabled()) | |
| 207 icon_types |= WebIconURL::TypeTouchPrecomposed | WebIconURL::TypeTouch; | |
| 208 | 127 |
| 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 |