OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/utility/chrome_content_utility_client.h" | 5 #include "chrome/utility/chrome_content_utility_client.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "chrome/common/chrome_utility_messages.h" | 11 #include "chrome/common/chrome_utility_messages.h" |
12 #include "chrome/common/safe_browsing/zip_analyzer.h" | 12 #include "chrome/common/safe_browsing/zip_analyzer.h" |
13 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h" | 13 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h" |
14 #include "chrome/utility/utility_message_handler.h" | 14 #include "chrome/utility/utility_message_handler.h" |
15 #include "chrome/utility/web_resource_unpacker.h" | 15 #include "chrome/utility/web_resource_unpacker.h" |
16 #include "content/public/child/image_decoder_utils.h" | 16 #include "content/public/child/image_decoder_utils.h" |
17 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
18 #include "content/public/utility/utility_thread.h" | 18 #include "content/public/utility/utility_thread.h" |
19 #include "courgette/courgette.h" | 19 #include "courgette/courgette.h" |
20 #include "courgette/third_party/bsdiff.h" | 20 #include "courgette/third_party/bsdiff.h" |
21 #include "ipc/ipc_channel.h" | |
22 #include "skia/ext/image_operations.h" | |
21 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
22 #include "third_party/zlib/google/zip.h" | 24 #include "third_party/zlib/google/zip.h" |
23 #include "ui/gfx/codec/jpeg_codec.h" | 25 #include "ui/gfx/codec/jpeg_codec.h" |
24 #include "ui/gfx/size.h" | 26 #include "ui/gfx/size.h" |
25 | 27 |
26 #if !defined(OS_ANDROID) | 28 #if !defined(OS_ANDROID) |
27 #include "chrome/utility/profile_import_handler.h" | 29 #include "chrome/utility/profile_import_handler.h" |
28 #endif | 30 #endif |
29 | 31 |
30 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 #endif | 167 #endif |
166 | 168 |
167 #if defined(ENABLE_MDNS) | 169 #if defined(ENABLE_MDNS) |
168 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 170 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
169 switches::kUtilityProcessEnableMDns)) { | 171 switches::kUtilityProcessEnableMDns)) { |
170 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); | 172 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); |
171 } | 173 } |
172 #endif // ENABLE_MDNS | 174 #endif // ENABLE_MDNS |
173 } | 175 } |
174 | 176 |
177 // Compute size of ChromeUtilityHostMsg_DecodeImage_Succeeded message with | |
178 // image argument, and compare with maximum IPC message size | |
179 size_t IsMessageTooBig(const SkBitmap& image, int width, int height) { | |
180 // Slightly different image size formula from SkImageInfo::getSafeSize64() | |
181 int64_t msg_size = sk_64_mul(height, width * image.bytesPerPixel()); | |
182 msg_size += sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); | |
183 return msg_size >= static_cast<int64_t>(IPC::Channel::kMaximumMessageSize); | |
184 } | |
185 | |
175 // static | 186 // static |
176 void ChromeContentUtilityClient::DecodeImage( | 187 SkBitmap ChromeContentUtilityClient::DecodeImage( |
177 const std::vector<unsigned char>& encoded_data) { | 188 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit) { |
178 const SkBitmap& decoded_image = content::DecodeImage(&encoded_data[0], | 189 SkBitmap decoded_image = content::DecodeImage(&encoded_data[0], |
179 gfx::Size(), | 190 gfx::Size(), |
180 encoded_data.size()); | 191 encoded_data.size()); |
bshe
2014/08/29 19:56:26
nit: indent
Greg Levin
2014/09/04 16:39:19
Done.
| |
192 | |
193 // If decoded image is too large for IPC message, shrink it by halves | |
Tom Sepez
2014/08/29 19:47:13
looks like using SkBitmap::computeSize64() would a
bshe
2014/08/29 19:56:26
Ideally, I think we should shrink to best fit. Not
Greg Levin
2014/09/04 16:39:18
Rewrote size checking logic, hopefully addressed a
Greg Levin
2014/09/04 16:39:19
Discussed off-line, decided shrinking by halves wa
| |
194 int width = decoded_image.width(); | |
195 int height = decoded_image.height(); | |
196 if (shrink_to_fit && IsMessageTooBig(decoded_image, width, height)) { | |
197 do { | |
Tom Sepez
2014/08/29 19:47:13
A while loop may get rid of having to call IsMessa
Greg Levin
2014/09/04 16:39:18
Done.
| |
198 width = width/2; | |
Tom Sepez
2014/08/29 19:47:13
... having done the above, then in this loop, you'
Greg Levin
2014/09/04 16:39:18
Done.
| |
199 height = height/2; | |
bshe
2014/08/29 19:56:26
nit: width /= 2 or width = width / 2;. same for he
Greg Levin
2014/09/04 16:39:18
Replaced division with bit shifts
| |
200 } while (IsMessageTooBig(decoded_image, width, height)); | |
201 decoded_image = skia::ImageOperations::Resize( | |
Tom Sepez
2014/08/29 19:47:13
Lastly, you can extract and divide width, height a
Greg Levin
2014/09/04 16:39:19
Done.
| |
202 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, | |
203 width, height); | |
204 } | |
205 | |
206 return decoded_image; | |
207 } | |
208 | |
209 // static | |
210 void ChromeContentUtilityClient::DecodeImageAndSend( | |
211 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit){ | |
212 SkBitmap decoded_image = DecodeImage(encoded_data, shrink_to_fit); | |
213 | |
bshe
2014/08/29 19:56:26
Add an error message for over IPC limit case would
Greg Levin
2014/09/04 16:39:19
Improved error handling and LOG'ed error
| |
181 if (decoded_image.empty()) { | 214 if (decoded_image.empty()) { |
182 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); | 215 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
183 } else { | 216 } else { |
184 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image)); | 217 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image)); |
185 } | 218 } |
186 ReleaseProcessIfNeeded(); | 219 ReleaseProcessIfNeeded(); |
187 } | 220 } |
188 | 221 |
189 void ChromeContentUtilityClient::OnUnpackWebResource( | 222 void ChromeContentUtilityClient::OnUnpackWebResource( |
190 const std::string& resource_data) { | 223 const std::string& resource_data) { |
191 // Parse json data. | 224 // Parse json data. |
192 // TODO(mrc): Add the possibility of a template that controls parsing, and | 225 // TODO(mrc): Add the possibility of a template that controls parsing, and |
193 // the ability to download and verify images. | 226 // the ability to download and verify images. |
194 WebResourceUnpacker unpacker(resource_data); | 227 WebResourceUnpacker unpacker(resource_data); |
195 if (unpacker.Run()) { | 228 if (unpacker.Run()) { |
196 Send(new ChromeUtilityHostMsg_UnpackWebResource_Succeeded( | 229 Send(new ChromeUtilityHostMsg_UnpackWebResource_Succeeded( |
197 *unpacker.parsed_json())); | 230 *unpacker.parsed_json())); |
198 } else { | 231 } else { |
199 Send(new ChromeUtilityHostMsg_UnpackWebResource_Failed( | 232 Send(new ChromeUtilityHostMsg_UnpackWebResource_Failed( |
200 unpacker.error_message())); | 233 unpacker.error_message())); |
201 } | 234 } |
202 | 235 |
203 ReleaseProcessIfNeeded(); | 236 ReleaseProcessIfNeeded(); |
204 } | 237 } |
205 | 238 |
206 void ChromeContentUtilityClient::OnDecodeImage( | 239 void ChromeContentUtilityClient::OnDecodeImage( |
207 const std::vector<unsigned char>& encoded_data) { | 240 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit) { |
208 DecodeImage(encoded_data); | 241 DecodeImageAndSend(encoded_data, shrink_to_fit); |
209 } | 242 } |
210 | 243 |
211 #if defined(OS_CHROMEOS) | 244 #if defined(OS_CHROMEOS) |
212 void ChromeContentUtilityClient::OnCreateZipFile( | 245 void ChromeContentUtilityClient::OnCreateZipFile( |
213 const base::FilePath& src_dir, | 246 const base::FilePath& src_dir, |
214 const std::vector<base::FilePath>& src_relative_paths, | 247 const std::vector<base::FilePath>& src_relative_paths, |
215 const base::FileDescriptor& dest_fd) { | 248 const base::FileDescriptor& dest_fd) { |
216 bool succeeded = true; | 249 bool succeeded = true; |
217 | 250 |
218 // Check sanity of source relative paths. Reject if path is absolute or | 251 // Check sanity of source relative paths. Reject if path is absolute or |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 const std::string& mime_type, int64 total_size, bool get_attached_images) { | 343 const std::string& mime_type, int64 total_size, bool get_attached_images) { |
311 // Only one IPCDataSource may be created and added to the list of handlers. | 344 // Only one IPCDataSource may be created and added to the list of handlers. |
312 metadata::IPCDataSource* source = new metadata::IPCDataSource(total_size); | 345 metadata::IPCDataSource* source = new metadata::IPCDataSource(total_size); |
313 handlers_.push_back(source); | 346 handlers_.push_back(source); |
314 | 347 |
315 metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser( | 348 metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser( |
316 source, mime_type, get_attached_images); | 349 source, mime_type, get_attached_images); |
317 parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser))); | 350 parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser))); |
318 } | 351 } |
319 #endif | 352 #endif |
OLD | NEW |