| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/image_loading_helper.h" | 5 #include "content/renderer/image_downloader/image_downloader_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "content/child/image_decoder.h" | 10 #include "content/child/image_decoder.h" |
| 10 #include "content/common/image_messages.h" | |
| 11 #include "content/public/renderer/render_frame.h" | 11 #include "content/public/renderer/render_frame.h" |
| 12 #include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.h" | 12 #include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.h" |
| 13 #include "mojo/common/url_type_converters.h" |
| 14 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 13 #include "net/base/data_url.h" | 15 #include "net/base/data_url.h" |
| 14 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
| 17 #include "skia/public/type_converters.h" |
| 15 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 18 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 16 #include "third_party/WebKit/public/platform/WebVector.h" | 19 #include "third_party/WebKit/public/platform/WebVector.h" |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 18 #include "third_party/WebKit/public/web/WebView.h" | 21 #include "third_party/WebKit/public/web/WebView.h" |
| 19 #include "ui/gfx/favicon_size.h" | 22 #include "ui/gfx/favicon_size.h" |
| 20 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
| 21 #include "ui/gfx/skbitmap_operations.h" | 24 #include "ui/gfx/skbitmap_operations.h" |
| 22 #include "url/url_constants.h" | 25 #include "url/url_constants.h" |
| 23 | 26 |
| 24 using blink::WebFrame; | 27 using blink::WebFrame; |
| 25 using blink::WebVector; | 28 using blink::WebVector; |
| 26 using blink::WebURL; | 29 using blink::WebURL; |
| 27 using blink::WebURLRequest; | 30 using blink::WebURLRequest; |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 // Proportionally resizes the |image| to fit in a box of size | 34 // Proportionally resizes the |image| to fit in a box of size |
| 32 // |max_image_size|. | 35 // |max_image_size|. |
| 33 SkBitmap ResizeImage(const SkBitmap& image, uint32_t max_image_size) { | 36 SkBitmap ResizeImage(const SkBitmap& image, uint32_t max_image_size) { |
| 34 if (max_image_size == 0) | 37 if (max_image_size == 0) |
| 35 return image; | 38 return image; |
| 36 uint32_t max_dimension = std::max(image.width(), image.height()); | 39 uint32_t max_dimension = std::max(image.width(), image.height()); |
| 37 if (max_dimension <= max_image_size) | 40 if (max_dimension <= max_image_size) |
| 38 return image; | 41 return image; |
| 39 // Proportionally resize the minimal image to fit in a box of size | 42 // Proportionally resize the minimal image to fit in a box of size |
| 40 // max_image_size. | 43 // max_image_size. |
| 41 return skia::ImageOperations::Resize( | 44 return skia::ImageOperations::Resize( |
| 42 image, | 45 image, skia::ImageOperations::RESIZE_BEST, |
| 43 skia::ImageOperations::RESIZE_BEST, | |
| 44 static_cast<uint64_t>(image.width()) * max_image_size / max_dimension, | 46 static_cast<uint64_t>(image.width()) * max_image_size / max_dimension, |
| 45 static_cast<uint64_t>(image.height()) * max_image_size / max_dimension); | 47 static_cast<uint64_t>(image.height()) * max_image_size / max_dimension); |
| 46 } | 48 } |
| 47 | 49 |
| 48 // Filters the array of bitmaps, removing all images that do not fit in a box of | 50 // Filters the array of bitmaps, removing all images that do not fit in a box of |
| 49 // size |max_image_size|. Returns the result if it is not empty. Otherwise, | 51 // size |max_image_size|. Returns the result if it is not empty. Otherwise, |
| 50 // find the smallest image in the array and resize it proportionally to fit | 52 // find the smallest image in the array and resize it proportionally to fit |
| 51 // in a box of size |max_image_size|. | 53 // in a box of size |max_image_size|. |
| 52 // Sets |original_image_sizes| to the sizes of |images| before resizing. | 54 // Sets |original_image_sizes| to the sizes of |images| before resizing. |
| 53 void FilterAndResizeImagesForMaximalSize( | 55 void FilterAndResizeImagesForMaximalSize( |
| 54 const std::vector<SkBitmap>& unfiltered, | 56 const std::vector<SkBitmap>& unfiltered, |
| 55 uint32_t max_image_size, | 57 uint32_t max_image_size, |
| 56 std::vector<SkBitmap>* images, | 58 std::vector<SkBitmap>* images, |
| 57 std::vector<gfx::Size>* original_image_sizes) { | 59 std::vector<gfx::Size>* original_image_sizes) { |
| 58 images->clear(); | 60 images->clear(); |
| 59 original_image_sizes->clear(); | 61 original_image_sizes->clear(); |
| 60 | 62 |
| 61 if (!unfiltered.size()) | 63 if (!unfiltered.size()) |
| 62 return; | 64 return; |
| 63 | 65 |
| 64 if (max_image_size == 0) | 66 if (max_image_size == 0) |
| 65 max_image_size = std::numeric_limits<uint32_t>::max(); | 67 max_image_size = std::numeric_limits<uint32_t>::max(); |
| 66 | 68 |
| 67 const SkBitmap* min_image = NULL; | 69 const SkBitmap* min_image = NULL; |
| 68 uint32_t min_image_size = std::numeric_limits<uint32_t>::max(); | 70 uint32_t min_image_size = std::numeric_limits<uint32_t>::max(); |
| 69 // Filter the images by |max_image_size|, and also identify the smallest image | 71 // Filter the images by |max_image_size|, and also identify the smallest image |
| 70 // in case all the images are bigger than |max_image_size|. | 72 // in case all the images are bigger than |max_image_size|. |
| 71 for (std::vector<SkBitmap>::const_iterator it = unfiltered.begin(); | 73 for (std::vector<SkBitmap>::const_iterator it = unfiltered.begin(); |
| 72 it != unfiltered.end(); | 74 it != unfiltered.end(); ++it) { |
| 73 ++it) { | |
| 74 const SkBitmap& image = *it; | 75 const SkBitmap& image = *it; |
| 75 uint32_t current_size = std::max(it->width(), it->height()); | 76 uint32_t current_size = std::max(it->width(), it->height()); |
| 76 if (current_size < min_image_size) { | 77 if (current_size < min_image_size) { |
| 77 min_image = ℑ | 78 min_image = ℑ |
| 78 min_image_size = current_size; | 79 min_image_size = current_size; |
| 79 } | 80 } |
| 80 if (static_cast<uint32_t>(image.width()) <= max_image_size && | 81 if (static_cast<uint32_t>(image.width()) <= max_image_size && |
| 81 static_cast<uint32_t>(image.height()) <= max_image_size) { | 82 static_cast<uint32_t>(image.height()) <= max_image_size) { |
| 82 images->push_back(image); | 83 images->push_back(image); |
| 83 original_image_sizes->push_back(gfx::Size(image.width(), image.height())); | 84 original_image_sizes->push_back(gfx::Size(image.width(), image.height())); |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 DCHECK(min_image); | 87 DCHECK(min_image); |
| 87 if (images->size()) | 88 if (images->size()) |
| 88 return; | 89 return; |
| 89 // Proportionally resize the minimal image to fit in a box of size | 90 // Proportionally resize the minimal image to fit in a box of size |
| 90 // |max_image_size|. | 91 // |max_image_size|. |
| 91 images->push_back(ResizeImage(*min_image, max_image_size)); | 92 images->push_back(ResizeImage(*min_image, max_image_size)); |
| 92 original_image_sizes->push_back( | 93 original_image_sizes->push_back( |
| 93 gfx::Size(min_image->width(), min_image->height())); | 94 gfx::Size(min_image->width(), min_image->height())); |
| 94 } | 95 } |
| 95 | 96 |
| 96 } // namespace | 97 } // namespace |
| 97 | 98 |
| 98 namespace content { | 99 namespace content { |
| 99 | 100 |
| 100 ImageLoadingHelper::ImageLoadingHelper(RenderFrame* render_frame) | 101 ImageDownloaderImpl::ImageDownloaderImpl( |
| 101 : RenderFrameObserver(render_frame) { | 102 RenderFrame* render_frame, |
| 103 mojo::InterfaceRequest<image_downloader::ImageDownloader> request) |
| 104 : RenderFrameObserver(render_frame), binding_(this, request.Pass()) { |
| 105 DCHECK(render_frame); |
| 102 } | 106 } |
| 103 | 107 |
| 104 ImageLoadingHelper::~ImageLoadingHelper() { | 108 ImageDownloaderImpl::~ImageDownloaderImpl() { |
| 105 } | 109 } |
| 106 | 110 |
| 107 void ImageLoadingHelper::OnDownloadImage( | 111 // static |
| 108 int id, | 112 void ImageDownloaderImpl::CreateMojoService( |
| 109 const GURL& image_url, | 113 RenderFrame* render_frame, |
| 110 bool is_favicon, | 114 mojo::InterfaceRequest<image_downloader::ImageDownloader> request) { |
| 111 uint32_t max_image_size, | 115 DVLOG(1) << "ImageDownloaderImpl::CreateService"; |
| 112 bool bypass_cache) { | 116 DCHECK(render_frame); |
| 117 |
| 118 new ImageDownloaderImpl(render_frame, request.Pass()); |
| 119 } |
| 120 |
| 121 // ImageDownloader methods: |
| 122 void ImageDownloaderImpl::DownloadImage( |
| 123 image_downloader::DownloadReqPtr req, |
| 124 const DownloadResultCallback& callback) { |
| 125 const GURL image_url = req->url.To<GURL>(); |
| 126 bool is_favicon = req->is_favicon; |
| 127 uint32_t max_image_size = req->max_bitmap_size; |
| 128 bool bypass_cache = req->bypass_cache; |
| 129 |
| 113 std::vector<SkBitmap> result_images; | 130 std::vector<SkBitmap> result_images; |
| 114 std::vector<gfx::Size> result_original_image_sizes; | 131 std::vector<gfx::Size> result_original_image_sizes; |
| 132 |
| 115 if (image_url.SchemeIs(url::kDataScheme)) { | 133 if (image_url.SchemeIs(url::kDataScheme)) { |
| 116 SkBitmap data_image = ImageFromDataUrl(image_url); | 134 SkBitmap data_image = ImageFromDataUrl(image_url); |
| 117 if (!data_image.empty()) { | 135 if (!data_image.empty()) { |
| 118 result_images.push_back(ResizeImage(data_image, max_image_size)); | 136 result_images.push_back(ResizeImage(data_image, max_image_size)); |
| 119 result_original_image_sizes.push_back( | 137 result_original_image_sizes.push_back( |
| 120 gfx::Size(data_image.width(), data_image.height())); | 138 gfx::Size(data_image.width(), data_image.height())); |
| 121 } | 139 } |
| 122 } else { | 140 } else { |
| 123 if (DownloadImage(id, image_url, is_favicon, max_image_size, | 141 if (FetchImage(image_url, is_favicon, max_image_size, bypass_cache, |
| 124 bypass_cache)) { | 142 callback)) { |
| 125 // Will complete asynchronously via ImageLoadingHelper::DidDownloadImage | 143 // Will complete asynchronously via ImageDownloaderImpl::DidFetchImage |
| 126 return; | 144 return; |
| 127 } | 145 } |
| 128 } | 146 } |
| 129 | 147 |
| 130 Send(new ImageHostMsg_DidDownloadImage(routing_id(), | 148 ReplyDownloadResult(0, image_url, result_images, result_original_image_sizes, |
| 131 id, | 149 callback); |
| 132 0, | |
| 133 image_url, | |
| 134 result_images, | |
| 135 result_original_image_sizes)); | |
| 136 } | 150 } |
| 137 | 151 |
| 138 bool ImageLoadingHelper::DownloadImage( | 152 SkBitmap ImageDownloaderImpl::ImageFromDataUrl(const GURL& url) const { |
| 139 int id, | 153 std::string mime_type, char_set, data; |
| 140 const GURL& image_url, | 154 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { |
| 141 bool is_favicon, | 155 // Decode the image using WebKit's image decoder. |
| 142 uint32_t max_image_size, | 156 ImageDecoder decoder(gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); |
| 143 bool bypass_cache) { | 157 const unsigned char* src_data = |
| 158 reinterpret_cast<const unsigned char*>(&data[0]); |
| 159 |
| 160 return decoder.Decode(src_data, data.size()); |
| 161 } |
| 162 return SkBitmap(); |
| 163 } |
| 164 |
| 165 bool ImageDownloaderImpl::FetchImage(const GURL& image_url, |
| 166 bool is_favicon, |
| 167 uint32_t max_image_size, |
| 168 bool bypass_cache, |
| 169 const DownloadResultCallback& callback) { |
| 170 blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); |
| 171 if (!frame) |
| 172 return false; |
| 173 |
| 144 // Create an image resource fetcher and assign it with a call back object. | 174 // Create an image resource fetcher and assign it with a call back object. |
| 145 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | 175 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( |
| 146 image_url, render_frame()->GetWebFrame(), id, | 176 image_url, frame, 0, is_favicon ? WebURLRequest::RequestContextFavicon |
| 147 is_favicon ? WebURLRequest::RequestContextFavicon | 177 : WebURLRequest::RequestContextImage, |
| 148 : WebURLRequest::RequestContextImage, | |
| 149 bypass_cache ? WebURLRequest::ReloadBypassingCache | 178 bypass_cache ? WebURLRequest::ReloadBypassingCache |
| 150 : WebURLRequest::UseProtocolCachePolicy, | 179 : WebURLRequest::UseProtocolCachePolicy, |
| 151 base::Bind(&ImageLoadingHelper::DidDownloadImage, base::Unretained(this), | 180 base::Bind(&ImageDownloaderImpl::DidFetchImage, base::Unretained(this), |
| 152 max_image_size))); | 181 max_image_size, callback))); |
| 153 return true; | 182 return true; |
| 154 } | 183 } |
| 155 | 184 |
| 156 void ImageLoadingHelper::DidDownloadImage( | 185 void ImageDownloaderImpl::DidFetchImage( |
| 157 uint32_t max_image_size, | 186 uint32_t max_image_size, |
| 187 const DownloadResultCallback& callback, |
| 158 MultiResolutionImageResourceFetcher* fetcher, | 188 MultiResolutionImageResourceFetcher* fetcher, |
| 159 const std::vector<SkBitmap>& images) { | 189 const std::vector<SkBitmap>& images) { |
| 160 std::vector<SkBitmap> result_images; | 190 std::vector<SkBitmap> result_images; |
| 161 std::vector<gfx::Size> result_original_image_sizes; | 191 std::vector<gfx::Size> result_original_image_sizes; |
| 162 FilterAndResizeImagesForMaximalSize(images, max_image_size, &result_images, | 192 FilterAndResizeImagesForMaximalSize(images, max_image_size, &result_images, |
| 163 &result_original_image_sizes); | 193 &result_original_image_sizes); |
| 164 | 194 |
| 165 // Notify requester of image download status. | 195 ReplyDownloadResult(fetcher->http_status_code(), fetcher->image_url(), |
| 166 Send(new ImageHostMsg_DidDownloadImage( | 196 result_images, result_original_image_sizes, callback); |
| 167 routing_id(), | |
| 168 fetcher->id(), | |
| 169 fetcher->http_status_code(), | |
| 170 fetcher->image_url(), | |
| 171 result_images, | |
| 172 result_original_image_sizes)); | |
| 173 | 197 |
| 174 // Remove the image fetcher from our pending list. We're in the callback from | 198 // Remove the image fetcher from our pending list. We're in the callback from |
| 175 // MultiResolutionImageResourceFetcher, best to delay deletion. | 199 // MultiResolutionImageResourceFetcher, best to delay deletion. |
| 176 ImageResourceFetcherList::iterator iter = | 200 ImageResourceFetcherList::iterator iter = |
| 177 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | 201 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); |
| 178 if (iter != image_fetchers_.end()) { | 202 if (iter != image_fetchers_.end()) { |
| 179 image_fetchers_.weak_erase(iter); | 203 image_fetchers_.weak_erase(iter); |
| 180 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | 204 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); |
| 181 } | 205 } |
| 182 } | 206 } |
| 183 | 207 |
| 184 SkBitmap ImageLoadingHelper::ImageFromDataUrl(const GURL& url) const { | 208 void ImageDownloaderImpl::ReplyDownloadResult( |
| 185 std::string mime_type, char_set, data; | 209 int32_t http_status_code, |
| 186 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | 210 const GURL& image_url, |
| 187 // Decode the image using WebKit's image decoder. | 211 const std::vector<SkBitmap>& result_images, |
| 188 ImageDecoder decoder(gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | 212 const std::vector<gfx::Size>& result_original_image_sizes, |
| 189 const unsigned char* src_data = | 213 const DownloadResultCallback& callback) { |
| 190 reinterpret_cast<const unsigned char*>(&data[0]); | 214 image_downloader::DownloadResultPtr result = |
| 215 image_downloader::DownloadResult::New(); |
| 191 | 216 |
| 192 return decoder.Decode(src_data, data.size()); | 217 result->http_status_code = http_status_code; |
| 193 } | 218 result->url = mojo::String::From(image_url); |
| 194 return SkBitmap(); | 219 result->images = mojo::Array<skia::BitmapPtr>::From(result_images); |
| 195 } | 220 result->original_image_sizes = |
| 221 mojo::Array<mojo::SizePtr>::From(result_original_image_sizes); |
| 196 | 222 |
| 197 bool ImageLoadingHelper::OnMessageReceived(const IPC::Message& message) { | 223 callback.Run(result.Pass()); |
| 198 bool handled = true; | |
| 199 IPC_BEGIN_MESSAGE_MAP(ImageLoadingHelper, message) | |
| 200 IPC_MESSAGE_HANDLER(ImageMsg_DownloadImage, OnDownloadImage) | |
| 201 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 202 IPC_END_MESSAGE_MAP() | |
| 203 | |
| 204 return handled; | |
| 205 } | 224 } |
| 206 | 225 |
| 207 } // namespace content | 226 } // namespace content |
| OLD | NEW |