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
3 // found in the LICENSE file.
5 #include "content/renderer/image_downloader/image_downloader_impl.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/child/image_decoder.h"
11 #include "content/public/renderer/render_frame.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"
15 #include "net/base/data_url.h"
16 #include "skia/ext/image_operations.h"
17 #include "skia/public/type_converters.h"
18 #include "third_party/WebKit/public/platform/WebURLRequest.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebView.h"
22 #include "ui/gfx/favicon_size.h"
23 #include "ui/gfx/geometry/size.h"
24 #include "ui/gfx/skbitmap_operations.h"
25 #include "url/url_constants.h"
27 using blink::WebFrame
;
28 using blink::WebVector
;
30 using blink::WebURLRequest
;
34 // Decodes a data: URL image or returns an empty image in case of failure.
35 SkBitmap
ImageFromDataUrl(const GURL
& url
) {
36 std::string mime_type
, char_set
, data
;
37 if (net::DataURL::Parse(url
, &mime_type
, &char_set
, &data
) && !data
.empty()) {
38 // Decode the image using Blink's image decoder.
39 content::ImageDecoder
decoder(
40 gfx::Size(gfx::kFaviconSize
, gfx::kFaviconSize
));
41 const unsigned char* src_data
=
42 reinterpret_cast<const unsigned char*>(data
.data());
44 return decoder
.Decode(src_data
, data
.size());
49 // Proportionally resizes the |image| to fit in a box of size
51 SkBitmap
ResizeImage(const SkBitmap
& image
, uint32_t max_image_size
) {
52 if (max_image_size
== 0)
54 uint32_t max_dimension
= std::max(image
.width(), image
.height());
55 if (max_dimension
<= max_image_size
)
57 // Proportionally resize the minimal image to fit in a box of size
59 return skia::ImageOperations::Resize(
60 image
, skia::ImageOperations::RESIZE_BEST
,
61 static_cast<uint64_t>(image
.width()) * max_image_size
/ max_dimension
,
62 static_cast<uint64_t>(image
.height()) * max_image_size
/ max_dimension
);
65 // Filters the array of bitmaps, removing all images that do not fit in a box of
66 // size |max_image_size|. Returns the result if it is not empty. Otherwise,
67 // find the smallest image in the array and resize it proportionally to fit
68 // in a box of size |max_image_size|.
69 // Sets |original_image_sizes| to the sizes of |images| before resizing.
70 void FilterAndResizeImagesForMaximalSize(
71 const std::vector
<SkBitmap
>& unfiltered
,
72 uint32_t max_image_size
,
73 std::vector
<SkBitmap
>* images
,
74 std::vector
<gfx::Size
>* original_image_sizes
) {
76 original_image_sizes
->clear();
78 if (!unfiltered
.size())
81 if (max_image_size
== 0)
82 max_image_size
= std::numeric_limits
<uint32_t>::max();
84 const SkBitmap
* min_image
= NULL
;
85 uint32_t min_image_size
= std::numeric_limits
<uint32_t>::max();
86 // Filter the images by |max_image_size|, and also identify the smallest image
87 // in case all the images are bigger than |max_image_size|.
88 for (std::vector
<SkBitmap
>::const_iterator it
= unfiltered
.begin();
89 it
!= unfiltered
.end(); ++it
) {
90 const SkBitmap
& image
= *it
;
91 uint32_t current_size
= std::max(it
->width(), it
->height());
92 if (current_size
< min_image_size
) {
94 min_image_size
= current_size
;
96 if (static_cast<uint32_t>(image
.width()) <= max_image_size
&&
97 static_cast<uint32_t>(image
.height()) <= max_image_size
) {
98 images
->push_back(image
);
99 original_image_sizes
->push_back(gfx::Size(image
.width(), image
.height()));
105 // Proportionally resize the minimal image to fit in a box of size
107 images
->push_back(ResizeImage(*min_image
, max_image_size
));
108 original_image_sizes
->push_back(
109 gfx::Size(min_image
->width(), min_image
->height()));
116 ImageDownloaderImpl::ImageDownloaderImpl(
117 RenderFrame
* render_frame
,
118 mojo::InterfaceRequest
<image_downloader::ImageDownloader
> request
)
119 : RenderFrameObserver(render_frame
), binding_(this, request
.Pass()) {
120 DCHECK(render_frame
);
123 ImageDownloaderImpl::~ImageDownloaderImpl() {
127 void ImageDownloaderImpl::CreateMojoService(
128 RenderFrame
* render_frame
,
129 mojo::InterfaceRequest
<image_downloader::ImageDownloader
> request
) {
130 DVLOG(1) << "ImageDownloaderImpl::CreateService";
131 DCHECK(render_frame
);
133 new ImageDownloaderImpl(render_frame
, request
.Pass());
136 // ImageDownloader methods:
137 void ImageDownloaderImpl::DownloadImage(
138 image_downloader::DownloadRequestPtr req
,
139 const DownloadImageCallback
& callback
) {
140 const GURL image_url
= req
->url
.To
<GURL
>();
141 bool is_favicon
= req
->is_favicon
;
142 uint32_t max_image_size
= req
->max_bitmap_size
;
143 bool bypass_cache
= req
->bypass_cache
;
145 std::vector
<SkBitmap
> result_images
;
146 std::vector
<gfx::Size
> result_original_image_sizes
;
148 if (image_url
.SchemeIs(url::kDataScheme
)) {
149 SkBitmap data_image
= ImageFromDataUrl(image_url
);
150 if (!data_image
.empty()) {
151 result_images
.push_back(ResizeImage(data_image
, max_image_size
));
152 result_original_image_sizes
.push_back(
153 gfx::Size(data_image
.width(), data_image
.height()));
156 if (FetchImage(image_url
, is_favicon
, max_image_size
, bypass_cache
,
158 // Will complete asynchronously via ImageDownloaderImpl::DidFetchImage
163 ReplyDownloadResult(0, result_images
, result_original_image_sizes
, callback
);
166 bool ImageDownloaderImpl::FetchImage(const GURL
& image_url
,
168 uint32_t max_image_size
,
170 const DownloadImageCallback
& callback
) {
171 blink::WebLocalFrame
* frame
= render_frame()->GetWebFrame();
174 // Create an image resource fetcher and assign it with a call back object.
175 image_fetchers_
.push_back(new MultiResolutionImageResourceFetcher(
176 image_url
, frame
, 0, is_favicon
? WebURLRequest::RequestContextFavicon
177 : WebURLRequest::RequestContextImage
,
178 bypass_cache
? WebURLRequest::ReloadBypassingCache
179 : WebURLRequest::UseProtocolCachePolicy
,
180 base::Bind(&ImageDownloaderImpl::DidFetchImage
, base::Unretained(this),
181 max_image_size
, callback
)));
185 void ImageDownloaderImpl::DidFetchImage(
186 uint32_t max_image_size
,
187 const DownloadImageCallback
& callback
,
188 MultiResolutionImageResourceFetcher
* fetcher
,
189 const std::vector
<SkBitmap
>& images
) {
190 std::vector
<SkBitmap
> result_images
;
191 std::vector
<gfx::Size
> result_original_image_sizes
;
192 FilterAndResizeImagesForMaximalSize(images
, max_image_size
, &result_images
,
193 &result_original_image_sizes
);
195 ReplyDownloadResult(fetcher
->http_status_code(), result_images
,
196 result_original_image_sizes
, callback
);
198 // Remove the image fetcher from our pending list. We're in the callback from
199 // MultiResolutionImageResourceFetcher, best to delay deletion.
200 ImageResourceFetcherList::iterator iter
=
201 std::find(image_fetchers_
.begin(), image_fetchers_
.end(), fetcher
);
202 if (iter
!= image_fetchers_
.end()) {
203 image_fetchers_
.weak_erase(iter
);
204 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, fetcher
);
208 void ImageDownloaderImpl::ReplyDownloadResult(
209 int32_t http_status_code
,
210 const std::vector
<SkBitmap
>& result_images
,
211 const std::vector
<gfx::Size
>& result_original_image_sizes
,
212 const DownloadImageCallback
& callback
) {
213 image_downloader::DownloadResultPtr result
=
214 image_downloader::DownloadResult::New();
216 result
->http_status_code
= http_status_code
;
217 result
->images
= mojo::Array
<skia::BitmapPtr
>::From(result_images
);
218 result
->original_image_sizes
=
219 mojo::Array
<mojo::SizePtr
>::From(result_original_image_sizes
);
221 callback
.Run(result
.Pass());
224 } // namespace content