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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const extensions::api::media_galleries::MediaMetadata& metadata, | 65 const extensions::api::media_galleries::MediaMetadata& metadata, |
64 const std::vector<metadata::AttachedImage>& attached_images) { | 66 const std::vector<metadata::AttachedImage>& attached_images) { |
65 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished( | 67 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished( |
66 true, *metadata.ToValue(), attached_images)); | 68 true, *metadata.ToValue(), attached_images)); |
67 ReleaseProcessIfNeeded(); | 69 ReleaseProcessIfNeeded(); |
68 } | 70 } |
69 #endif | 71 #endif |
70 | 72 |
71 } // namespace | 73 } // namespace |
72 | 74 |
| 75 int64_t ChromeContentUtilityClient::max_ipc_message_size_ = |
| 76 IPC::Channel::kMaximumMessageSize; |
| 77 |
73 ChromeContentUtilityClient::ChromeContentUtilityClient() | 78 ChromeContentUtilityClient::ChromeContentUtilityClient() |
74 : filter_messages_(false) { | 79 : filter_messages_(false) { |
75 #if !defined(OS_ANDROID) | 80 #if !defined(OS_ANDROID) |
76 handlers_.push_back(new ProfileImportHandler()); | 81 handlers_.push_back(new ProfileImportHandler()); |
77 #endif | 82 #endif |
78 | 83 |
79 #if defined(ENABLE_EXTENSIONS) | 84 #if defined(ENABLE_EXTENSIONS) |
80 handlers_.push_back(new extensions::ExtensionsHandler()); | 85 handlers_.push_back(new extensions::ExtensionsHandler()); |
81 handlers_.push_back(new image_writer::ImageWriterHandler()); | 86 handlers_.push_back(new image_writer::ImageWriterHandler()); |
82 #endif | 87 #endif |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 171 |
167 #if defined(ENABLE_MDNS) | 172 #if defined(ENABLE_MDNS) |
168 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 173 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
169 switches::kUtilityProcessEnableMDns)) { | 174 switches::kUtilityProcessEnableMDns)) { |
170 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); | 175 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup(); |
171 } | 176 } |
172 #endif // ENABLE_MDNS | 177 #endif // ENABLE_MDNS |
173 } | 178 } |
174 | 179 |
175 // static | 180 // static |
176 void ChromeContentUtilityClient::DecodeImage( | 181 SkBitmap ChromeContentUtilityClient::DecodeImage( |
177 const std::vector<unsigned char>& encoded_data) { | 182 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit) { |
178 const SkBitmap& decoded_image = content::DecodeImage(&encoded_data[0], | 183 SkBitmap decoded_image = content::DecodeImage(&encoded_data[0], |
179 gfx::Size(), | 184 gfx::Size(), |
180 encoded_data.size()); | 185 encoded_data.size()); |
| 186 |
| 187 int64_t struct_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded); |
| 188 int64_t image_size = decoded_image.computeSize64(); |
| 189 int halves = 0; |
| 190 while (struct_size + (image_size >> 2*halves) > max_ipc_message_size_) |
| 191 halves++; |
| 192 if (halves) { |
| 193 if (shrink_to_fit) { |
| 194 // If decoded image is too large for IPC message, shrink it by halves. |
| 195 // This prevents quality loss, and should never overshrink on displays |
| 196 // smaller than 3600x2400. |
| 197 // TODO (Issue 416916): Instead of shrinking, return via shared memory |
| 198 decoded_image = skia::ImageOperations::Resize( |
| 199 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3, |
| 200 decoded_image.width() >> halves, decoded_image.height() >> halves); |
| 201 } else { |
| 202 // Image too big for IPC message, but caller didn't request resize; |
| 203 // pre-delete image so DecodeImageAndSend() will send an error. |
| 204 decoded_image.reset(); |
| 205 LOG(ERROR) << "Decoded image too large for IPC message"; |
| 206 } |
| 207 } |
| 208 |
| 209 return decoded_image; |
| 210 } |
| 211 |
| 212 // static |
| 213 void ChromeContentUtilityClient::DecodeImageAndSend( |
| 214 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit){ |
| 215 SkBitmap decoded_image = DecodeImage(encoded_data, shrink_to_fit); |
| 216 |
181 if (decoded_image.empty()) { | 217 if (decoded_image.empty()) { |
182 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); | 218 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); |
183 } else { | 219 } else { |
184 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image)); | 220 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image)); |
185 } | 221 } |
186 ReleaseProcessIfNeeded(); | 222 ReleaseProcessIfNeeded(); |
187 } | 223 } |
188 | 224 |
189 void ChromeContentUtilityClient::OnUnpackWebResource( | 225 void ChromeContentUtilityClient::OnUnpackWebResource( |
190 const std::string& resource_data) { | 226 const std::string& resource_data) { |
191 // Parse json data. | 227 // Parse json data. |
192 // TODO(mrc): Add the possibility of a template that controls parsing, and | 228 // TODO(mrc): Add the possibility of a template that controls parsing, and |
193 // the ability to download and verify images. | 229 // the ability to download and verify images. |
194 WebResourceUnpacker unpacker(resource_data); | 230 WebResourceUnpacker unpacker(resource_data); |
195 if (unpacker.Run()) { | 231 if (unpacker.Run()) { |
196 Send(new ChromeUtilityHostMsg_UnpackWebResource_Succeeded( | 232 Send(new ChromeUtilityHostMsg_UnpackWebResource_Succeeded( |
197 *unpacker.parsed_json())); | 233 *unpacker.parsed_json())); |
198 } else { | 234 } else { |
199 Send(new ChromeUtilityHostMsg_UnpackWebResource_Failed( | 235 Send(new ChromeUtilityHostMsg_UnpackWebResource_Failed( |
200 unpacker.error_message())); | 236 unpacker.error_message())); |
201 } | 237 } |
202 | 238 |
203 ReleaseProcessIfNeeded(); | 239 ReleaseProcessIfNeeded(); |
204 } | 240 } |
205 | 241 |
206 void ChromeContentUtilityClient::OnDecodeImage( | 242 void ChromeContentUtilityClient::OnDecodeImage( |
207 const std::vector<unsigned char>& encoded_data) { | 243 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit) { |
208 DecodeImage(encoded_data); | 244 DecodeImageAndSend(encoded_data, shrink_to_fit); |
209 } | 245 } |
210 | 246 |
211 #if defined(OS_CHROMEOS) | 247 #if defined(OS_CHROMEOS) |
212 void ChromeContentUtilityClient::OnCreateZipFile( | 248 void ChromeContentUtilityClient::OnCreateZipFile( |
213 const base::FilePath& src_dir, | 249 const base::FilePath& src_dir, |
214 const std::vector<base::FilePath>& src_relative_paths, | 250 const std::vector<base::FilePath>& src_relative_paths, |
215 const base::FileDescriptor& dest_fd) { | 251 const base::FileDescriptor& dest_fd) { |
216 bool succeeded = true; | 252 bool succeeded = true; |
217 | 253 |
218 // Check sanity of source relative paths. Reject if path is absolute or | 254 // 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) { | 346 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. | 347 // Only one IPCDataSource may be created and added to the list of handlers. |
312 metadata::IPCDataSource* source = new metadata::IPCDataSource(total_size); | 348 metadata::IPCDataSource* source = new metadata::IPCDataSource(total_size); |
313 handlers_.push_back(source); | 349 handlers_.push_back(source); |
314 | 350 |
315 metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser( | 351 metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser( |
316 source, mime_type, get_attached_images); | 352 source, mime_type, get_attached_images); |
317 parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser))); | 353 parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser))); |
318 } | 354 } |
319 #endif | 355 #endif |
OLD | NEW |