[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / chrome / utility / chrome_content_utility_client.cc
blob9671be86427ef4c633dadcc7efaa03b4caec723c
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/memory/ref_counted.h"
10 #include "base/time/time.h"
11 #include "chrome/common/chrome_utility_messages.h"
12 #include "chrome/common/safe_browsing/zip_analyzer.h"
13 #include "chrome/common/safe_browsing/zip_analyzer_results.h"
14 #include "chrome/utility/chrome_content_utility_ipc_whitelist.h"
15 #include "chrome/utility/safe_json_parser_handler.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/geometry/size.h"
29 #if defined(OS_CHROMEOS)
30 #include "ui/gfx/chromeos/codec/jpeg_codec_robust_slow.h"
31 #endif
33 #if !defined(OS_ANDROID)
34 #include "chrome/common/resource_usage_reporter.mojom.h"
35 #include "chrome/utility/profile_import_handler.h"
36 #include "net/proxy/mojo_proxy_resolver_factory_impl.h"
37 #include "net/proxy/proxy_resolver_v8.h"
38 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
39 #endif
41 #if defined(OS_WIN)
42 #include "chrome/utility/font_cache_handler_win.h"
43 #include "chrome/utility/shell_handler_win.h"
44 #endif
46 #if defined(ENABLE_EXTENSIONS)
47 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
48 #include "chrome/utility/extensions/extensions_handler.h"
49 #include "chrome/utility/image_writer/image_writer_handler.h"
50 #include "chrome/utility/media_galleries/ipc_data_source.h"
51 #include "chrome/utility/media_galleries/media_metadata_parser.h"
52 #endif
54 #if defined(ENABLE_PRINT_PREVIEW) || defined(OS_WIN)
55 #include "chrome/utility/printing_handler.h"
56 #endif
58 #if defined(ENABLE_MDNS)
59 #include "chrome/utility/local_discovery/service_discovery_message_handler.h"
60 #endif
62 namespace {
64 bool Send(IPC::Message* message) {
65 return content::UtilityThread::Get()->Send(message);
68 void ReleaseProcessIfNeeded() {
69 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
72 #if defined(ENABLE_EXTENSIONS)
73 void FinishParseMediaMetadata(
74 metadata::MediaMetadataParser* /* parser */,
75 const extensions::api::media_galleries::MediaMetadata& metadata,
76 const std::vector<metadata::AttachedImage>& attached_images) {
77 Send(new ChromeUtilityHostMsg_ParseMediaMetadata_Finished(
78 true, *metadata.ToValue(), attached_images));
79 ReleaseProcessIfNeeded();
81 #endif
83 #if !defined(OS_ANDROID)
84 void CreateProxyResolverFactory(
85 mojo::InterfaceRequest<net::interfaces::ProxyResolverFactory> request) {
86 // MojoProxyResolverFactoryImpl is strongly bound to the Mojo message pipe it
87 // is connected to. When that message pipe is closed, either explicitly on the
88 // other end (in the browser process), or by a connection error, this object
89 // will be destroyed.
90 new net::MojoProxyResolverFactoryImpl(request.Pass());
93 class ResourceUsageReporterImpl : public ResourceUsageReporter {
94 public:
95 explicit ResourceUsageReporterImpl(
96 mojo::InterfaceRequest<ResourceUsageReporter> req)
97 : binding_(this, req.Pass()) {}
98 ~ResourceUsageReporterImpl() override {}
100 private:
101 void GetUsageData(
102 const mojo::Callback<void(ResourceUsageDataPtr)>& callback) override {
103 ResourceUsageDataPtr data = ResourceUsageData::New();
104 size_t total_heap_size = net::ProxyResolverV8::GetTotalHeapSize();
105 if (total_heap_size) {
106 data->reports_v8_stats = true;
107 data->v8_bytes_allocated = total_heap_size;
108 data->v8_bytes_used = net::ProxyResolverV8::GetUsedHeapSize();
110 callback.Run(data.Pass());
113 mojo::StrongBinding<ResourceUsageReporter> binding_;
116 void CreateResourceUsageReporter(
117 mojo::InterfaceRequest<ResourceUsageReporter> request) {
118 new ResourceUsageReporterImpl(request.Pass());
120 #endif // OS_ANDROID
122 } // namespace
124 int64_t ChromeContentUtilityClient::max_ipc_message_size_ =
125 IPC::Channel::kMaximumMessageSize;
127 ChromeContentUtilityClient::ChromeContentUtilityClient()
128 : filter_messages_(false) {
129 #if !defined(OS_ANDROID)
130 handlers_.push_back(new ProfileImportHandler());
131 #endif
133 #if defined(ENABLE_EXTENSIONS)
134 handlers_.push_back(new extensions::ExtensionsHandler());
135 handlers_.push_back(new image_writer::ImageWriterHandler());
136 #endif
138 #if defined(ENABLE_PRINT_PREVIEW) || defined(OS_WIN)
139 handlers_.push_back(new PrintingHandler());
140 #endif
142 #if defined(ENABLE_MDNS)
143 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kUtilityProcessEnableMDns)) {
145 handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler());
147 #endif
149 #if defined(OS_WIN)
150 handlers_.push_back(new ShellHandler());
151 handlers_.push_back(new FontCacheHandler());
152 #endif
154 handlers_.push_back(new SafeJsonParserHandler());
157 ChromeContentUtilityClient::~ChromeContentUtilityClient() {
160 void ChromeContentUtilityClient::UtilityThreadStarted() {
161 #if defined(ENABLE_EXTENSIONS)
162 extensions::UtilityHandler::UtilityThreadStarted();
163 #endif
165 if (kMessageWhitelistSize > 0) {
166 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
167 if (command_line->HasSwitch(switches::kUtilityProcessRunningElevated)) {
168 message_id_whitelist_.insert(kMessageWhitelist,
169 kMessageWhitelist + kMessageWhitelistSize);
170 filter_messages_ = true;
175 bool ChromeContentUtilityClient::OnMessageReceived(
176 const IPC::Message& message) {
177 if (filter_messages_ && !ContainsKey(message_id_whitelist_, message.type()))
178 return false;
180 bool handled = true;
181 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message)
182 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage)
183 #if defined(OS_CHROMEOS)
184 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage,
185 OnRobustJPEGDecodeImage)
186 #endif // defined(OS_CHROMEOS)
187 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff,
188 OnPatchFileBsdiff)
189 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileCourgette,
190 OnPatchFileCourgette)
191 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing, OnStartupPing)
192 #if defined(FULL_SAFE_BROWSING)
193 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection,
194 OnAnalyzeZipFileForDownloadProtection)
195 #endif
196 #if defined(ENABLE_EXTENSIONS)
197 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseMediaMetadata,
198 OnParseMediaMetadata)
199 #endif
200 #if defined(OS_CHROMEOS)
201 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_CreateZipFile, OnCreateZipFile)
202 #endif
203 IPC_MESSAGE_UNHANDLED(handled = false)
204 IPC_END_MESSAGE_MAP()
206 for (Handlers::iterator it = handlers_.begin();
207 !handled && it != handlers_.end(); ++it) {
208 handled = (*it)->OnMessageReceived(message);
211 return handled;
214 void ChromeContentUtilityClient::RegisterMojoServices(
215 content::ServiceRegistry* registry) {
216 #if !defined(OS_ANDROID)
217 registry->AddService<net::interfaces::ProxyResolverFactory>(
218 base::Bind(CreateProxyResolverFactory));
219 registry->AddService<ResourceUsageReporter>(
220 base::Bind(CreateResourceUsageReporter));
221 #endif
224 // static
225 void ChromeContentUtilityClient::PreSandboxStartup() {
226 #if defined(ENABLE_EXTENSIONS)
227 extensions::ExtensionsHandler::PreSandboxStartup();
228 #endif
230 #if defined(ENABLE_MDNS)
231 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
232 switches::kUtilityProcessEnableMDns)) {
233 local_discovery::ServiceDiscoveryMessageHandler::PreSandboxStartup();
235 #endif // ENABLE_MDNS
238 // static
239 SkBitmap ChromeContentUtilityClient::DecodeImage(
240 const std::vector<unsigned char>& encoded_data, bool shrink_to_fit) {
241 SkBitmap decoded_image;
242 if (encoded_data.empty())
243 return decoded_image;
245 decoded_image = content::DecodeImage(&encoded_data[0],
246 gfx::Size(),
247 encoded_data.size());
249 int64_t struct_size = sizeof(ChromeUtilityHostMsg_DecodeImage_Succeeded);
250 int64_t image_size = decoded_image.computeSize64();
251 int halves = 0;
252 while (struct_size + (image_size >> 2*halves) > max_ipc_message_size_)
253 halves++;
254 if (halves) {
255 if (shrink_to_fit) {
256 // If decoded image is too large for IPC message, shrink it by halves.
257 // This prevents quality loss, and should never overshrink on displays
258 // smaller than 3600x2400.
259 // TODO (Issue 416916): Instead of shrinking, return via shared memory
260 decoded_image = skia::ImageOperations::Resize(
261 decoded_image, skia::ImageOperations::RESIZE_LANCZOS3,
262 decoded_image.width() >> halves, decoded_image.height() >> halves);
263 } else {
264 // Image too big for IPC message, but caller didn't request resize;
265 // pre-delete image so DecodeImageAndSend() will send an error.
266 decoded_image.reset();
267 LOG(ERROR) << "Decoded image too large for IPC message";
271 return decoded_image;
274 // static
275 void ChromeContentUtilityClient::DecodeImageAndSend(
276 const std::vector<unsigned char>& encoded_data,
277 bool shrink_to_fit,
278 int request_id) {
279 SkBitmap decoded_image = DecodeImage(encoded_data, shrink_to_fit);
281 if (decoded_image.empty()) {
282 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id));
283 } else {
284 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(decoded_image,
285 request_id));
287 ReleaseProcessIfNeeded();
290 void ChromeContentUtilityClient::OnDecodeImage(
291 const std::vector<unsigned char>& encoded_data,
292 bool shrink_to_fit,
293 int request_id) {
294 DecodeImageAndSend(encoded_data, shrink_to_fit, request_id);
297 #if defined(OS_CHROMEOS)
298 void ChromeContentUtilityClient::OnCreateZipFile(
299 const base::FilePath& src_dir,
300 const std::vector<base::FilePath>& src_relative_paths,
301 const base::FileDescriptor& dest_fd) {
302 // dest_fd should be closed in the function. See ipc/ipc_message_util.h for
303 // details.
304 base::ScopedFD fd_closer(dest_fd.fd);
305 bool succeeded = true;
307 // Check sanity of source relative paths. Reject if path is absolute or
308 // contains any attempt to reference a parent directory ("../" tricks).
309 for (std::vector<base::FilePath>::const_iterator iter =
310 src_relative_paths.begin(); iter != src_relative_paths.end();
311 ++iter) {
312 if (iter->IsAbsolute() || iter->ReferencesParent()) {
313 succeeded = false;
314 break;
318 if (succeeded)
319 succeeded = zip::ZipFiles(src_dir, src_relative_paths, dest_fd.fd);
321 if (succeeded)
322 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded());
323 else
324 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed());
325 ReleaseProcessIfNeeded();
327 #endif // defined(OS_CHROMEOS)
329 #if defined(OS_CHROMEOS)
330 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage(
331 const std::vector<unsigned char>& encoded_data,
332 int request_id) {
333 // Our robust jpeg decoding is using IJG libjpeg.
334 if (!encoded_data.empty()) {
335 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodecRobustSlow::Decode(
336 &encoded_data[0], encoded_data.size()));
337 if (!decoded_image.get() || decoded_image->empty()) {
338 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id));
339 } else {
340 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image,
341 request_id));
343 } else {
344 Send(new ChromeUtilityHostMsg_DecodeImage_Failed(request_id));
346 ReleaseProcessIfNeeded();
348 #endif // defined(OS_CHROMEOS)
350 void ChromeContentUtilityClient::OnPatchFileBsdiff(
351 const base::FilePath& input_file,
352 const base::FilePath& patch_file,
353 const base::FilePath& output_file) {
354 if (input_file.empty() || patch_file.empty() || output_file.empty()) {
355 Send(new ChromeUtilityHostMsg_PatchFile_Finished(-1));
356 } else {
357 const int patch_status = courgette::ApplyBinaryPatch(input_file,
358 patch_file,
359 output_file);
360 Send(new ChromeUtilityHostMsg_PatchFile_Finished(patch_status));
362 ReleaseProcessIfNeeded();
365 void ChromeContentUtilityClient::OnPatchFileCourgette(
366 const base::FilePath& input_file,
367 const base::FilePath& patch_file,
368 const base::FilePath& output_file) {
369 if (input_file.empty() || patch_file.empty() || output_file.empty()) {
370 Send(new ChromeUtilityHostMsg_PatchFile_Finished(-1));
371 } else {
372 const int patch_status = courgette::ApplyEnsemblePatch(
373 input_file.value().c_str(),
374 patch_file.value().c_str(),
375 output_file.value().c_str());
376 Send(new ChromeUtilityHostMsg_PatchFile_Finished(patch_status));
378 ReleaseProcessIfNeeded();
381 void ChromeContentUtilityClient::OnStartupPing() {
382 Send(new ChromeUtilityHostMsg_ProcessStarted);
383 // Don't release the process, we assume further messages are on the way.
386 #if defined(FULL_SAFE_BROWSING)
387 void ChromeContentUtilityClient::OnAnalyzeZipFileForDownloadProtection(
388 const IPC::PlatformFileForTransit& zip_file,
389 const IPC::PlatformFileForTransit& temp_file) {
390 safe_browsing::zip_analyzer::Results results;
391 safe_browsing::zip_analyzer::AnalyzeZipFile(
392 IPC::PlatformFileForTransitToFile(zip_file),
393 IPC::PlatformFileForTransitToFile(temp_file), &results);
394 Send(new ChromeUtilityHostMsg_AnalyzeZipFileForDownloadProtection_Finished(
395 results));
396 ReleaseProcessIfNeeded();
398 #endif
400 #if defined(ENABLE_EXTENSIONS)
401 // TODO(thestig): Try to move this to
402 // chrome/utility/extensions/extensions_handler.cc.
403 void ChromeContentUtilityClient::OnParseMediaMetadata(
404 const std::string& mime_type, int64 total_size, bool get_attached_images) {
405 // Only one IPCDataSource may be created and added to the list of handlers.
406 metadata::IPCDataSource* source = new metadata::IPCDataSource(total_size);
407 handlers_.push_back(source);
409 metadata::MediaMetadataParser* parser = new metadata::MediaMetadataParser(
410 source, mime_type, get_attached_images);
411 parser->Start(base::Bind(&FinishParseMediaMetadata, base::Owned(parser)));
413 #endif