| 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/service/service_utility_process_host.h" | 5 #include "chrome/service/service_utility_process_host.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
| 21 #include "base/process/launch.h" | 21 #include "base/process/launch.h" |
| 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/task_runner_util.h" | 23 #include "base/task_runner_util.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "base/win/win_util.h" |
| 24 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 25 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/chrome_utility_printing_messages.h" | 28 #include "chrome/common/chrome_utility_printing_messages.h" |
| 27 #include "content/public/common/child_process_host.h" | 29 #include "content/public/common/child_process_host.h" |
| 28 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
| 31 #include "content/public/common/mojo_channel_switches.h" |
| 29 #include "content/public/common/result_codes.h" | 32 #include "content/public/common/result_codes.h" |
| 30 #include "content/public/common/sandbox_init.h" | 33 #include "content/public/common/sandbox_init.h" |
| 31 #include "content/public/common/sandboxed_process_launcher_delegate.h" | 34 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
| 32 #include "ipc/ipc_switches.h" | 35 #include "ipc/ipc_switches.h" |
| 36 #include "mojo/edk/embedder/embedder.h" |
| 37 #include "mojo/edk/embedder/named_platform_channel_pair.h" |
| 38 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 39 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 33 #include "printing/emf_win.h" | 40 #include "printing/emf_win.h" |
| 34 #include "sandbox/win/src/sandbox_policy.h" | 41 #include "sandbox/win/src/sandbox_policy.h" |
| 35 #include "sandbox/win/src/sandbox_types.h" | 42 #include "sandbox/win/src/sandbox_types.h" |
| 36 #include "ui/base/ui_base_switches.h" | 43 #include "ui/base/ui_base_switches.h" |
| 37 | 44 |
| 38 namespace { | 45 namespace { |
| 39 | 46 |
| 40 using content::ChildProcessHost; | 47 using content::ChildProcessHost; |
| 41 | 48 |
| 42 enum ServiceUtilityProcessHostEvent { | 49 enum ServiceUtilityProcessHostEvent { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 int current_page_; | 162 int current_page_; |
| 156 int pages_in_progress_; | 163 int pages_in_progress_; |
| 157 }; | 164 }; |
| 158 | 165 |
| 159 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 166 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
| 160 Client* client, | 167 Client* client, |
| 161 base::SingleThreadTaskRunner* client_task_runner) | 168 base::SingleThreadTaskRunner* client_task_runner) |
| 162 : client_(client), | 169 : client_(client), |
| 163 client_task_runner_(client_task_runner), | 170 client_task_runner_(client_task_runner), |
| 164 waiting_for_reply_(false), | 171 waiting_for_reply_(false), |
| 172 mojo_child_token_(mojo::edk::GenerateRandomToken()), |
| 165 weak_ptr_factory_(this) { | 173 weak_ptr_factory_(this) { |
| 166 child_process_host_.reset(ChildProcessHost::Create(this)); | 174 child_process_host_.reset(ChildProcessHost::Create(this)); |
| 167 } | 175 } |
| 168 | 176 |
| 169 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 177 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
| 170 // We need to kill the child process when the host dies. | 178 // We need to kill the child process when the host dies. |
| 171 process_.Terminate(content::RESULT_CODE_NORMAL_EXIT, false); | 179 process_.Terminate(content::RESULT_CODE_NORMAL_EXIT, false); |
| 172 } | 180 } |
| 173 | 181 |
| 174 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 182 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 205 start_time_ = base::Time::Now(); | 213 start_time_ = base::Time::Now(); |
| 206 if (!StartProcess(true)) | 214 if (!StartProcess(true)) |
| 207 return false; | 215 return false; |
| 208 DCHECK(!waiting_for_reply_); | 216 DCHECK(!waiting_for_reply_); |
| 209 waiting_for_reply_ = true; | 217 waiting_for_reply_ = true; |
| 210 return Send( | 218 return Send( |
| 211 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); | 219 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); |
| 212 } | 220 } |
| 213 | 221 |
| 214 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox) { | 222 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox) { |
| 215 std::string channel_id = child_process_host_->CreateChannel(); | 223 std::string mojo_channel_token = |
| 216 if (channel_id.empty()) | 224 child_process_host_->CreateChannelMojo(mojo_child_token_); |
| 225 if (mojo_channel_token.empty()) |
| 217 return false; | 226 return false; |
| 218 | 227 |
| 219 base::FilePath exe_path = GetUtilityProcessCmd(); | 228 base::FilePath exe_path = GetUtilityProcessCmd(); |
| 220 if (exe_path.empty()) { | 229 if (exe_path.empty()) { |
| 221 NOTREACHED() << "Unable to get utility process binary name."; | 230 NOTREACHED() << "Unable to get utility process binary name."; |
| 222 return false; | 231 return false; |
| 223 } | 232 } |
| 224 | 233 |
| 225 base::CommandLine cmd_line(exe_path); | 234 base::CommandLine cmd_line(exe_path); |
| 226 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 235 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
| 227 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); | 236 cmd_line.AppendSwitchASCII(switches::kMojoChannelToken, mojo_channel_token); |
| 228 cmd_line.AppendSwitch(switches::kLang); | 237 cmd_line.AppendSwitch(switches::kLang); |
| 229 cmd_line.AppendArg(switches::kPrefetchArgumentOther); | 238 cmd_line.AppendArg(switches::kPrefetchArgumentOther); |
| 230 | 239 |
| 231 if (Launch(&cmd_line, no_sandbox)) { | 240 if (Launch(&cmd_line, no_sandbox)) { |
| 232 ReportUmaEvent(SERVICE_UTILITY_STARTED); | 241 ReportUmaEvent(SERVICE_UTILITY_STARTED); |
| 233 return true; | 242 return true; |
| 234 } | 243 } |
| 235 ReportUmaEvent(SERVICE_UTILITY_FAILED_TO_START); | 244 ReportUmaEvent(SERVICE_UTILITY_FAILED_TO_START); |
| 236 return false; | 245 return false; |
| 237 } | 246 } |
| 238 | 247 |
| 239 bool ServiceUtilityProcessHost::Launch(base::CommandLine* cmd_line, | 248 bool ServiceUtilityProcessHost::Launch(base::CommandLine* cmd_line, |
| 240 bool no_sandbox) { | 249 bool no_sandbox) { |
| 250 mojo::edk::ScopedPlatformHandle parent_handle; |
| 251 bool success = false; |
| 241 if (no_sandbox) { | 252 if (no_sandbox) { |
| 253 mojo::edk::NamedPlatformChannelPair named_pair; |
| 254 parent_handle = named_pair.PassServerHandle(); |
| 255 named_pair.PrepareToPassClientHandleToChildProcess(cmd_line); |
| 256 |
| 242 cmd_line->AppendSwitch(switches::kNoSandbox); | 257 cmd_line->AppendSwitch(switches::kNoSandbox); |
| 243 process_ = base::LaunchProcess(*cmd_line, base::LaunchOptions()); | 258 process_ = base::LaunchProcess(*cmd_line, base::LaunchOptions()); |
| 244 return process_.IsValid(); | 259 success = process_.IsValid(); |
| 245 } else { | 260 } else { |
| 261 mojo::edk::PlatformChannelPair channel_pair; |
| 262 parent_handle = channel_pair.PassServerHandle(); |
| 263 mojo::edk::ScopedPlatformHandle client_handle = |
| 264 channel_pair.PassClientHandle(); |
| 265 base::HandlesToInheritVector handles; |
| 266 handles.push_back(client_handle.get().handle); |
| 267 cmd_line->AppendSwitchASCII( |
| 268 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, |
| 269 base::UintToString(base::win::HandleToUint32(handles[0]))); |
| 270 |
| 246 ServiceSandboxedProcessLauncherDelegate delegate; | 271 ServiceSandboxedProcessLauncherDelegate delegate; |
| 247 base::Process process; | 272 base::Process process; |
| 248 sandbox::ResultCode result = content::StartSandboxedProcess( | 273 sandbox::ResultCode result = content::StartSandboxedProcess( |
| 249 &delegate, cmd_line, base::HandlesToInheritVector(), &process); | 274 &delegate, cmd_line, handles, &process); |
| 250 if (result == sandbox::SBOX_ALL_OK) { | 275 if (result == sandbox::SBOX_ALL_OK) { |
| 251 process_ = std::move(process); | 276 process_ = std::move(process); |
| 252 return true; | 277 success = true; |
| 253 } | 278 } |
| 254 return false; | |
| 255 } | 279 } |
| 280 |
| 281 if (success) { |
| 282 mojo::edk::ChildProcessLaunched(process_.Handle(), |
| 283 std::move(parent_handle), |
| 284 mojo_child_token_); |
| 285 } else { |
| 286 mojo::edk::ChildProcessLaunchFailed(mojo_child_token_); |
| 287 } |
| 288 return success; |
| 256 } | 289 } |
| 257 | 290 |
| 258 bool ServiceUtilityProcessHost::Send(IPC::Message* msg) { | 291 bool ServiceUtilityProcessHost::Send(IPC::Message* msg) { |
| 259 if (child_process_host_) | 292 if (child_process_host_) |
| 260 return child_process_host_->Send(msg); | 293 return child_process_host_->Send(msg); |
| 261 delete msg; | 294 delete msg; |
| 262 return false; | 295 return false; |
| 263 } | 296 } |
| 264 | 297 |
| 265 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 298 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 return false; | 457 return false; |
| 425 } | 458 } |
| 426 printing::Emf emf; | 459 printing::Emf emf; |
| 427 if (!emf.InitFromData(data.data(), data.size())) { | 460 if (!emf.InitFromData(data.data(), data.size())) { |
| 428 OnRenderPDFPagesToMetafileDone(false); | 461 OnRenderPDFPagesToMetafileDone(false); |
| 429 return false; | 462 return false; |
| 430 } | 463 } |
| 431 OnRenderPDFPagesToMetafilePageDone(scale_factor, emf); | 464 OnRenderPDFPagesToMetafilePageDone(scale_factor, emf); |
| 432 return true; | 465 return true; |
| 433 } | 466 } |
| OLD | NEW |