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 |
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 WebKit's image decoder. | |
nasko
2015/07/02 08:41:07
s/WebKit/Blink/
leonhsl(Using Gerrit)
2015/07/06 02:15:25
Done.
| |
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()); | |
43 | |
44 return decoder.Decode(src_data, data.size()); | |
45 } | |
46 return SkBitmap(); | |
47 } | |
48 | |
31 // Proportionally resizes the |image| to fit in a box of size | 49 // Proportionally resizes the |image| to fit in a box of size |
32 // |max_image_size|. | 50 // |max_image_size|. |
33 SkBitmap ResizeImage(const SkBitmap& image, uint32_t max_image_size) { | 51 SkBitmap ResizeImage(const SkBitmap& image, uint32_t max_image_size) { |
34 if (max_image_size == 0) | 52 if (max_image_size == 0) |
35 return image; | 53 return image; |
36 uint32_t max_dimension = std::max(image.width(), image.height()); | 54 uint32_t max_dimension = std::max(image.width(), image.height()); |
37 if (max_dimension <= max_image_size) | 55 if (max_dimension <= max_image_size) |
38 return image; | 56 return image; |
39 // Proportionally resize the minimal image to fit in a box of size | 57 // Proportionally resize the minimal image to fit in a box of size |
40 // max_image_size. | 58 // max_image_size. |
41 return skia::ImageOperations::Resize( | 59 return skia::ImageOperations::Resize( |
42 image, | 60 image, skia::ImageOperations::RESIZE_BEST, |
43 skia::ImageOperations::RESIZE_BEST, | |
44 static_cast<uint64_t>(image.width()) * max_image_size / max_dimension, | 61 static_cast<uint64_t>(image.width()) * max_image_size / max_dimension, |
45 static_cast<uint64_t>(image.height()) * max_image_size / max_dimension); | 62 static_cast<uint64_t>(image.height()) * max_image_size / max_dimension); |
46 } | 63 } |
47 | 64 |
48 // Filters the array of bitmaps, removing all images that do not fit in a box of | 65 // 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, | 66 // 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 | 67 // find the smallest image in the array and resize it proportionally to fit |
51 // in a box of size |max_image_size|. | 68 // in a box of size |max_image_size|. |
52 // Sets |original_image_sizes| to the sizes of |images| before resizing. | 69 // Sets |original_image_sizes| to the sizes of |images| before resizing. |
53 void FilterAndResizeImagesForMaximalSize( | 70 void FilterAndResizeImagesForMaximalSize( |
54 const std::vector<SkBitmap>& unfiltered, | 71 const std::vector<SkBitmap>& unfiltered, |
55 uint32_t max_image_size, | 72 uint32_t max_image_size, |
56 std::vector<SkBitmap>* images, | 73 std::vector<SkBitmap>* images, |
57 std::vector<gfx::Size>* original_image_sizes) { | 74 std::vector<gfx::Size>* original_image_sizes) { |
58 images->clear(); | 75 images->clear(); |
59 original_image_sizes->clear(); | 76 original_image_sizes->clear(); |
60 | 77 |
61 if (!unfiltered.size()) | 78 if (!unfiltered.size()) |
62 return; | 79 return; |
63 | 80 |
64 if (max_image_size == 0) | 81 if (max_image_size == 0) |
65 max_image_size = std::numeric_limits<uint32_t>::max(); | 82 max_image_size = std::numeric_limits<uint32_t>::max(); |
66 | 83 |
67 const SkBitmap* min_image = NULL; | 84 const SkBitmap* min_image = NULL; |
68 uint32_t min_image_size = std::numeric_limits<uint32_t>::max(); | 85 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 | 86 // 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|. | 87 // in case all the images are bigger than |max_image_size|. |
71 for (std::vector<SkBitmap>::const_iterator it = unfiltered.begin(); | 88 for (std::vector<SkBitmap>::const_iterator it = unfiltered.begin(); |
72 it != unfiltered.end(); | 89 it != unfiltered.end(); ++it) { |
73 ++it) { | |
74 const SkBitmap& image = *it; | 90 const SkBitmap& image = *it; |
75 uint32_t current_size = std::max(it->width(), it->height()); | 91 uint32_t current_size = std::max(it->width(), it->height()); |
76 if (current_size < min_image_size) { | 92 if (current_size < min_image_size) { |
77 min_image = ℑ | 93 min_image = ℑ |
78 min_image_size = current_size; | 94 min_image_size = current_size; |
79 } | 95 } |
80 if (static_cast<uint32_t>(image.width()) <= max_image_size && | 96 if (static_cast<uint32_t>(image.width()) <= max_image_size && |
81 static_cast<uint32_t>(image.height()) <= max_image_size) { | 97 static_cast<uint32_t>(image.height()) <= max_image_size) { |
82 images->push_back(image); | 98 images->push_back(image); |
83 original_image_sizes->push_back(gfx::Size(image.width(), image.height())); | 99 original_image_sizes->push_back(gfx::Size(image.width(), image.height())); |
84 } | 100 } |
85 } | 101 } |
86 DCHECK(min_image); | 102 DCHECK(min_image); |
87 if (images->size()) | 103 if (images->size()) |
88 return; | 104 return; |
89 // Proportionally resize the minimal image to fit in a box of size | 105 // Proportionally resize the minimal image to fit in a box of size |
90 // |max_image_size|. | 106 // |max_image_size|. |
91 images->push_back(ResizeImage(*min_image, max_image_size)); | 107 images->push_back(ResizeImage(*min_image, max_image_size)); |
92 original_image_sizes->push_back( | 108 original_image_sizes->push_back( |
93 gfx::Size(min_image->width(), min_image->height())); | 109 gfx::Size(min_image->width(), min_image->height())); |
94 } | 110 } |
95 | 111 |
96 } // namespace | 112 } // namespace |
97 | 113 |
98 namespace content { | 114 namespace content { |
99 | 115 |
100 ImageLoadingHelper::ImageLoadingHelper(RenderFrame* render_frame) | 116 ImageDownloaderImpl::ImageDownloaderImpl( |
101 : RenderFrameObserver(render_frame) { | 117 RenderFrame* render_frame, |
118 mojo::InterfaceRequest<image_downloader::ImageDownloader> request) | |
119 : RenderFrameObserver(render_frame), binding_(this, request.Pass()) { | |
120 DCHECK(render_frame); | |
102 } | 121 } |
103 | 122 |
104 ImageLoadingHelper::~ImageLoadingHelper() { | 123 ImageDownloaderImpl::~ImageDownloaderImpl() { |
105 } | 124 } |
106 | 125 |
107 void ImageLoadingHelper::OnDownloadImage( | 126 // static |
108 int id, | 127 void ImageDownloaderImpl::CreateMojoService( |
109 const GURL& image_url, | 128 RenderFrame* render_frame, |
110 bool is_favicon, | 129 mojo::InterfaceRequest<image_downloader::ImageDownloader> request) { |
111 uint32_t max_image_size, | 130 DVLOG(1) << "ImageDownloaderImpl::CreateService"; |
112 bool bypass_cache) { | 131 DCHECK(render_frame); |
132 | |
133 new ImageDownloaderImpl(render_frame, request.Pass()); | |
134 } | |
135 | |
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; | |
144 | |
113 std::vector<SkBitmap> result_images; | 145 std::vector<SkBitmap> result_images; |
114 std::vector<gfx::Size> result_original_image_sizes; | 146 std::vector<gfx::Size> result_original_image_sizes; |
147 | |
115 if (image_url.SchemeIs(url::kDataScheme)) { | 148 if (image_url.SchemeIs(url::kDataScheme)) { |
116 SkBitmap data_image = ImageFromDataUrl(image_url); | 149 SkBitmap data_image = ImageFromDataUrl(image_url); |
117 if (!data_image.empty()) { | 150 if (!data_image.empty()) { |
118 result_images.push_back(ResizeImage(data_image, max_image_size)); | 151 result_images.push_back(ResizeImage(data_image, max_image_size)); |
119 result_original_image_sizes.push_back( | 152 result_original_image_sizes.push_back( |
120 gfx::Size(data_image.width(), data_image.height())); | 153 gfx::Size(data_image.width(), data_image.height())); |
121 } | 154 } |
122 } else { | 155 } else { |
123 if (DownloadImage(id, image_url, is_favicon, max_image_size, | 156 if (FetchImage(image_url, is_favicon, max_image_size, bypass_cache, |
124 bypass_cache)) { | 157 callback)) { |
125 // Will complete asynchronously via ImageLoadingHelper::DidDownloadImage | 158 // Will complete asynchronously via ImageDownloaderImpl::DidFetchImage |
126 return; | 159 return; |
127 } | 160 } |
128 } | 161 } |
129 | 162 |
130 Send(new ImageHostMsg_DidDownloadImage(routing_id(), | 163 ReplyDownloadResult(0, result_images, result_original_image_sizes, callback); |
131 id, | |
132 0, | |
133 image_url, | |
134 result_images, | |
135 result_original_image_sizes)); | |
136 } | 164 } |
137 | 165 |
138 bool ImageLoadingHelper::DownloadImage( | 166 bool ImageDownloaderImpl::FetchImage(const GURL& image_url, |
139 int id, | 167 bool is_favicon, |
140 const GURL& image_url, | 168 uint32_t max_image_size, |
141 bool is_favicon, | 169 bool bypass_cache, |
142 uint32_t max_image_size, | 170 const DownloadImageCallback& callback) { |
143 bool bypass_cache) { | 171 blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); |
172 if (!frame) | |
nasko
2015/07/02 08:41:07
There shouldn't be a case where render_frame()->Ge
leonhsl(Using Gerrit)
2015/07/06 02:15:25
Done. Changed to DCHECK.
| |
173 return false; | |
174 | |
144 // Create an image resource fetcher and assign it with a call back object. | 175 // Create an image resource fetcher and assign it with a call back object. |
145 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | 176 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( |
146 image_url, render_frame()->GetWebFrame(), id, | 177 image_url, frame, 0, is_favicon ? WebURLRequest::RequestContextFavicon |
147 is_favicon ? WebURLRequest::RequestContextFavicon | 178 : WebURLRequest::RequestContextImage, |
148 : WebURLRequest::RequestContextImage, | |
149 bypass_cache ? WebURLRequest::ReloadBypassingCache | 179 bypass_cache ? WebURLRequest::ReloadBypassingCache |
150 : WebURLRequest::UseProtocolCachePolicy, | 180 : WebURLRequest::UseProtocolCachePolicy, |
151 base::Bind(&ImageLoadingHelper::DidDownloadImage, base::Unretained(this), | 181 base::Bind(&ImageDownloaderImpl::DidFetchImage, base::Unretained(this), |
152 max_image_size))); | 182 max_image_size, callback))); |
153 return true; | 183 return true; |
154 } | 184 } |
155 | 185 |
156 void ImageLoadingHelper::DidDownloadImage( | 186 void ImageDownloaderImpl::DidFetchImage( |
157 uint32_t max_image_size, | 187 uint32_t max_image_size, |
188 const DownloadImageCallback& callback, | |
158 MultiResolutionImageResourceFetcher* fetcher, | 189 MultiResolutionImageResourceFetcher* fetcher, |
159 const std::vector<SkBitmap>& images) { | 190 const std::vector<SkBitmap>& images) { |
160 std::vector<SkBitmap> result_images; | 191 std::vector<SkBitmap> result_images; |
161 std::vector<gfx::Size> result_original_image_sizes; | 192 std::vector<gfx::Size> result_original_image_sizes; |
162 FilterAndResizeImagesForMaximalSize(images, max_image_size, &result_images, | 193 FilterAndResizeImagesForMaximalSize(images, max_image_size, &result_images, |
163 &result_original_image_sizes); | 194 &result_original_image_sizes); |
164 | 195 |
165 // Notify requester of image download status. | 196 ReplyDownloadResult(fetcher->http_status_code(), result_images, |
166 Send(new ImageHostMsg_DidDownloadImage( | 197 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 | 198 |
174 // Remove the image fetcher from our pending list. We're in the callback from | 199 // Remove the image fetcher from our pending list. We're in the callback from |
175 // MultiResolutionImageResourceFetcher, best to delay deletion. | 200 // MultiResolutionImageResourceFetcher, best to delay deletion. |
176 ImageResourceFetcherList::iterator iter = | 201 ImageResourceFetcherList::iterator iter = |
177 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | 202 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); |
178 if (iter != image_fetchers_.end()) { | 203 if (iter != image_fetchers_.end()) { |
179 image_fetchers_.weak_erase(iter); | 204 image_fetchers_.weak_erase(iter); |
180 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); | 205 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher); |
181 } | 206 } |
182 } | 207 } |
183 | 208 |
184 SkBitmap ImageLoadingHelper::ImageFromDataUrl(const GURL& url) const { | 209 void ImageDownloaderImpl::ReplyDownloadResult( |
185 std::string mime_type, char_set, data; | 210 int32_t http_status_code, |
186 if (net::DataURL::Parse(url, &mime_type, &char_set, &data) && !data.empty()) { | 211 const std::vector<SkBitmap>& result_images, |
187 // Decode the image using WebKit's image decoder. | 212 const std::vector<gfx::Size>& result_original_image_sizes, |
188 ImageDecoder decoder(gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize)); | 213 const DownloadImageCallback& callback) { |
189 const unsigned char* src_data = | 214 image_downloader::DownloadResultPtr result = |
190 reinterpret_cast<const unsigned char*>(&data[0]); | 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->images = mojo::Array<skia::BitmapPtr>::From(result_images); |
194 return SkBitmap(); | 219 result->original_image_sizes = |
195 } | 220 mojo::Array<mojo::SizePtr>::From(result_original_image_sizes); |
196 | 221 |
197 bool ImageLoadingHelper::OnMessageReceived(const IPC::Message& message) { | 222 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 } | 223 } |
206 | 224 |
207 } // namespace content | 225 } // namespace content |
OLD | NEW |