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
3 // found in the LICENSE file.
5 #include "chrome/utility/chrome_content_utility_client.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/json/json_reader.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/time/time.h"
12 #include "chrome/common/chrome_utility_messages.h"
13 #include "chrome/common/safe_browsing/zip_analyzer.h"
14 #include "chrome/common/safe_browsing/zip_analyzer_results.h"
15 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h"
16 #include "chrome/utility/utility_message_handler.h"
17 #include "content/public/child/image_decoder_utils.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/service_registry.h"
20 #include "content/public/utility/utility_thread.h"
21 #include "courgette/courgette.h"
22 #include "courgette/third_party/bsdiff.h"
23 #include "ipc/ipc_channel.h"
24 #include "skia/ext/image_operations.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "third_party/zlib/google/zip.h"
27 #include "ui/gfx/codec/jpeg_codec.h"
28 #include "ui/gfx/geometry/size.h"
30 #if !defined(OS_ANDROID)
31 #include "chrome/utility/profile_import_handler.h"
32 #include "net/proxy/mojo_proxy_resolver_factory_impl.h"
35 #if defined(OS_ANDROID) && defined(USE_SECCOMP_BPF)
36 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
40 #include "chrome/utility/font_cache_handler_win.h"
41 #include "chrome/utility/shell_handler_win.h"
44 #if defined(ENABLE_EXTENSIONS)
45 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
46 #include "chrome/utility/extensions/extensions_handler.h"
47 #include "chrome/utility/image_writer/image_writer_handler.h"
48 #include "chrome/utility/media_galleries/ipc_data_source.h"
49 #include "chrome/utility/media_galleries/media_metadata_parser.h"
52 #if defined(ENABLE_PRINT_PREVIEW) || defined(OS_WIN)
53 #include "chrome/utility/printing_handler.h"
56 #if defined(ENABLE_MDNS)
57 #include "chrome/utility/local_discovery/service_discovery_message_handler.h"
62 bool Send(IPC::Message
* message
) {
63 return content::UtilityThread::Get()->Send(message
);
66 void ReleaseProcessIfNeeded() {
67 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
70 #if defined(ENABLE_EXTENSIONS)
71 void FinishParseMediaMetadata(
72 metadata::MediaMetadataParser
* /* parser */,
73 const extensions::api::media_galleries::MediaMetadata
& metadata
,
74 const std::vector
<metadata::AttachedImage
>& attached_images
) {
75 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished(
76 true, *metadata
.ToValue(), attached_images
));
77 ReleaseProcessIfNeeded();
81 #if !defined(OS_ANDROID)
82 void CreateProxyResolverFactory(
83 mojo::InterfaceRequest
<net::interfaces::ProxyResolverFactory
> request
) {
84 // MojoProxyResolverFactoryImpl is strongly bound to the Mojo message pipe it
85 // is connected to. When that message pipe is closed, either explicitly on the
86 // other end (in the browser process), or by a connection error, this object
88 new net::MojoProxyResolverFactoryImpl(request
.Pass());
94 int64_t ChromeContentUtilityClient::max_ipc_message_size_
=
95 IPC::Channel::kMaximumMessageSize
;
97 ChromeContentUtilityClient::ChromeContentUtilityClient()
98 : filter_messages_(false) {
99 #if !defined(OS_ANDROID)
100 handlers_
.push_back(new ProfileImportHandler());
103 #if defined(ENABLE_EXTENSIONS)
104 handlers_
.push_back(new extensions::ExtensionsHandler());
105 handlers_
.push_back(new image_writer::ImageWriterHandler());
108 #if defined(ENABLE_PRINT_PREVIEW) || defined(OS_WIN)
109 handlers_
.push_back(new PrintingHandler());
112 #if defined(ENABLE_MDNS)
113 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
114 switches::kUtilityProcessEnableMDns
)) {
115 handlers_
.push_back(new local_discovery::ServiceDiscoveryMessageHandler());
120 handlers_
.push_back(new ShellHandler());
121 handlers_
.push_back(new FontCacheHandler());
125 ChromeContentUtilityClient::~ChromeContentUtilityClient() {
128 void ChromeContentUtilityClient::UtilityThreadStarted() {
129 #if defined(ENABLE_EXTENSIONS)
130 extensions::UtilityHandler::UtilityThreadStarted();
133 if (kMessageWhitelistSize
> 0) {
134 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
135 if (command_line
->HasSwitch(switches::kUtilityProcessRunningElevated
)) {
136 message_id_whitelist_
.insert(kMessageWhitelist
,
137 kMessageWhitelist
+ kMessageWhitelistSize
);
138 filter_messages_
= true;
143 bool ChromeContentUtilityClient::OnMessageReceived(
144 const IPC::Message
& message
) {
145 if (filter_messages_
&& !ContainsKey(message_id_whitelist_
, message
.type()))
149 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient
, message
)
150 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage
, OnDecodeImage
)
151 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage
,
152 OnRobustJPEGDecodeImage
)
153 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON
, OnParseJSON
)
154 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff
,
156 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileCourgette
,
157 OnPatchFileCourgette
)
158 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing
, OnStartupPing
)
159 #if defined(FULL_SAFE_BROWSING)
160 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection
,
161 OnAnalyzeZipFileForDownloadProtection
)
163 #if defined(ENABLE_EXTENSIONS)
164 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseMediaMetadata
,
165 OnParseMediaMetadata
)
167 #if defined(OS_CHROMEOS)
168 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CreateZipFile
, OnCreateZipFile
)
170 #if defined(OS_ANDROID) && defined(USE_SECCOMP_BPF)
171 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DetectSeccompSupport
,
172 OnDetectSeccompSupport
)
174 IPC_MESSAGE_UNHANDLED(handled
= false)
175 IPC_END_MESSAGE_MAP()
177 for (Handlers::iterator it
= handlers_
.begin();
178 !handled
&& it
!= handlers_
.end(); ++it
) {
179 handled
= (*it
)->OnMessageReceived(message
);
185 void ChromeContentUtilityClient::RegisterMojoServices(
186 content::ServiceRegistry
* registry
) {
187 #if !defined(OS_ANDROID)
188 registry
->AddService
<net::interfaces::ProxyResolverFactory
>(
189 base::Bind(CreateProxyResolverFactory
));
194 void ChromeContentUtilityClient::PreSandboxStartup() {
195 #if defined(ENABLE_EXTENSIONS)
196 extensions::ExtensionsHandler::PreSandboxStartup();
199 #if defined(ENABLE_MDNS)
200 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kUtilityProcessEnableMDns
)) {
202 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup();
204 #endif // ENABLE_MDNS
208 SkBitmap
ChromeContentUtilityClient::DecodeImage(
209 const std::vector
<unsigned char>& encoded_data
, bool shrink_to_fit
) {
210 SkBitmap decoded_image
= content::DecodeImage(&encoded_data
[0],
212 encoded_data
.size());
214 int64_t struct_size
= sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded
);
215 int64_t image_size
= decoded_image
.computeSize64();
217 while (struct_size
+ (image_size
>> 2*halves
) > max_ipc_message_size_
)
221 // If decoded image is too large for IPC message, shrink it by halves.
222 // This prevents quality loss, and should never overshrink on displays
223 // smaller than 3600x2400.
224 // TODO (Issue 416916): Instead of shrinking, return via shared memory
225 decoded_image
= skia::ImageOperations::Resize(
226 decoded_image
, skia::ImageOperations::RESIZE_LANCZOS3
,
227 decoded_image
.width() >> halves
, decoded_image
.height() >> halves
);
229 // Image too big for IPC message, but caller didn't request resize;
230 // pre-delete image so DecodeImageAndSend() will send an error.
231 decoded_image
.reset();
232 LOG(ERROR
) << "Decoded image too large for IPC message";
236 return decoded_image
;
240 void ChromeContentUtilityClient::DecodeImageAndSend(
241 const std::vector
<unsigned char>& encoded_data
,
244 SkBitmap decoded_image
= DecodeImage(encoded_data
, shrink_to_fit
);
246 if (decoded_image
.empty()) {
247 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id
));
249 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image
,
252 ReleaseProcessIfNeeded();
255 void ChromeContentUtilityClient::OnDecodeImage(
256 const std::vector
<unsigned char>& encoded_data
,
259 DecodeImageAndSend(encoded_data
, shrink_to_fit
, request_id
);
262 #if defined(OS_CHROMEOS)
263 void ChromeContentUtilityClient::OnCreateZipFile(
264 const base::FilePath
& src_dir
,
265 const std::vector
<base::FilePath
>& src_relative_paths
,
266 const base::FileDescriptor
& dest_fd
) {
267 // dest_fd should be closed in the function. See ipc/ipc_message_util.h for
269 base::ScopedFD
fd_closer(dest_fd
.fd
);
270 bool succeeded
= true;
272 // Check sanity of source relative paths. Reject if path is absolute or
273 // contains any attempt to reference a parent directory ("../" tricks).
274 for (std::vector
<base::FilePath
>::const_iterator iter
=
275 src_relative_paths
.begin(); iter
!= src_relative_paths
.end();
277 if (iter
->IsAbsolute() || iter
->ReferencesParent()) {
284 succeeded
= zip::ZipFiles(src_dir
, src_relative_paths
, dest_fd
.fd
);
287 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded());
289 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed());
290 ReleaseProcessIfNeeded();
292 #endif // defined(OS_CHROMEOS)
294 #if defined(OS_ANDROID) && defined(USE_SECCOMP_BPF)
295 void ChromeContentUtilityClient::OnDetectSeccompSupport() {
296 bool supports_prctl
= sandbox::SandboxBPF::SupportsSeccompSandbox(
297 sandbox::SandboxBPF::SeccompLevel::SINGLE_THREADED
);
298 Send(new ChromeUtilityHostMsg_DetectSeccompSupport_ResultPrctl(
301 bool supports_syscall
= sandbox::SandboxBPF::SupportsSeccompSandbox(
302 sandbox::SandboxBPF::SeccompLevel::MULTI_THREADED
);
303 Send(new ChromeUtilityHostMsg_DetectSeccompSupport_ResultSyscall(
306 ReleaseProcessIfNeeded();
308 #endif // defined(OS_ANDROID) && defined(USE_SECCOMP_BPF)
310 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage(
311 const std::vector
<unsigned char>& encoded_data
,
313 // Our robust jpeg decoding is using IJG libjpeg.
314 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG
&&
315 !encoded_data
.empty()) {
316 scoped_ptr
<SkBitmap
> decoded_image(gfx::JPEGCodec::Decode(
317 &encoded_data
[0], encoded_data
.size()));
318 if (!decoded_image
.get() || decoded_image
->empty()) {
319 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id
));
321 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image
,
325 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id
));
327 ReleaseProcessIfNeeded();
330 void ChromeContentUtilityClient::OnParseJSON(const std::string
& json
) {
333 base::Value
* value
= base::JSONReader::ReadAndReturnError(
334 json
, base::JSON_PARSE_RFC
, &error_code
, &error
);
336 base::ListValue wrapper
;
337 wrapper
.Append(value
);
338 Send(new ChromeUtilityHostMsg_ParseJSON_Succeeded(wrapper
));
340 Send(new ChromeUtilityHostMsg_ParseJSON_Failed(error
));
342 ReleaseProcessIfNeeded();
345 void ChromeContentUtilityClient::OnPatchFileBsdiff(
346 const base::FilePath
& input_file
,
347 const base::FilePath
& patch_file
,
348 const base::FilePath
& output_file
) {
349 if (input_file
.empty() || patch_file
.empty() || output_file
.empty()) {
350 Send(new ChromeUtilityHostMsg_PatchFile_Finished(-1));
352 const int patch_status
= courgette::ApplyBinaryPatch(input_file
,
355 Send(new ChromeUtilityHostMsg_PatchFile_Finished(patch_status
));
357 ReleaseProcessIfNeeded();
360 void ChromeContentUtilityClient::OnPatchFileCourgette(
361 const base::FilePath
& input_file
,
362 const base::FilePath
& patch_file
,
363 const base::FilePath
& output_file
) {
364 if (input_file
.empty() || patch_file
.empty() || output_file
.empty()) {
365 Send(new ChromeUtilityHostMsg_PatchFile_Finished(-1));
367 const int patch_status
= courgette::ApplyEnsemblePatch(
368 input_file
.value().c_str(),
369 patch_file
.value().c_str(),
370 output_file
.value().c_str());
371 Send(new ChromeUtilityHostMsg_PatchFile_Finished(patch_status
));
373 ReleaseProcessIfNeeded();
376 void ChromeContentUtilityClient::OnStartupPing() {
377 Send(new ChromeUtilityHostMsg_ProcessStarted
);
378 // Don't release the process, we assume further messages are on the way.
381 #if defined(FULL_SAFE_BROWSING)
382 void ChromeContentUtilityClient::OnAnalyzeZipFileForDownloadProtection(
383 const IPC::PlatformFileForTransit
& zip_file
,
384 const IPC::PlatformFileForTransit
& temp_file
) {
385 safe_browsing::zip_analyzer::Results results
;
386 safe_browsing::zip_analyzer::AnalyzeZipFile(
387 IPC::PlatformFileForTransitToFile(zip_file
),
388 IPC::PlatformFileForTransitToFile(temp_file
), &results
);
389 Send(new ChromeUtilityHostMsg_AnalyzeZipFileForDownloadProtection_Finished(
391 ReleaseProcessIfNeeded();
395 #if defined(ENABLE_EXTENSIONS)
396 // TODO(thestig): Try to move this to
397 // chrome/utility/extensions/extensions_handler.cc.
398 void ChromeContentUtilityClient::OnParseMediaMetadata(
399 const std::string
& mime_type
, int64 total_size
, bool get_attached_images
) {
400 // Only one IPCDataSource may be created and added to the list of handlers.
401 metadata::IPCDataSource
* source
= new metadata::IPCDataSource(total_size
);
402 handlers_
.push_back(source
);
404 metadata::MediaMetadataParser
* parser
= new metadata::MediaMetadataParser(
405 source
, mime_type
, get_attached_images
);
406 parser
->Start(base::Bind(&FinishParseMediaMetadata
, base::Owned(parser
)));