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