| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/process_util.h" |
| 12 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 13 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/chrome_utility_messages.h" | 17 #include "chrome/common/chrome_utility_messages.h" |
| 18 #include "content/common/child_process_host.h" |
| 19 #include "content/public/common/result_codes.h" |
| 16 #include "ipc/ipc_switches.h" | 20 #include "ipc/ipc_switches.h" |
| 17 #include "printing/page_range.h" | 21 #include "printing/page_range.h" |
| 18 #include "ui/base/ui_base_switches.h" | 22 #include "ui/base/ui_base_switches.h" |
| 19 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
| 20 | 24 |
| 21 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 #include "base/file_path.h" |
| 22 #include "base/memory/scoped_ptr.h" | 27 #include "base/memory/scoped_ptr.h" |
| 23 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
| 29 #include "content/common/sandbox_policy.h" |
| 24 #include "printing/emf_win.h" | 30 #include "printing/emf_win.h" |
| 25 #endif | 31 #endif |
| 26 | 32 |
| 27 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 33 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
| 28 Client* client, base::MessageLoopProxy* client_message_loop_proxy) | 34 Client* client, base::MessageLoopProxy* client_message_loop_proxy) |
| 29 : client_(client), | 35 : handle_(base::kNullProcessHandle), |
| 36 client_(client), |
| 30 client_message_loop_proxy_(client_message_loop_proxy), | 37 client_message_loop_proxy_(client_message_loop_proxy), |
| 31 waiting_for_reply_(false) { | 38 waiting_for_reply_(false) { |
| 39 child_process_host_.reset(new ChildProcessHost(this)); |
| 32 } | 40 } |
| 33 | 41 |
| 34 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 42 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
| 43 // We need to kill the child process when the host dies. |
| 44 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); |
| 35 } | 45 } |
| 36 | 46 |
| 37 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 47 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
| 38 const FilePath& pdf_path, | 48 const FilePath& pdf_path, |
| 39 const printing::PdfRenderSettings& render_settings, | 49 const printing::PdfRenderSettings& render_settings, |
| 40 const std::vector<printing::PageRange>& page_ranges) { | 50 const std::vector<printing::PageRange>& page_ranges) { |
| 41 #if !defined(OS_WIN) | 51 #if !defined(OS_WIN) |
| 42 // This is only implemented on Windows (because currently it is only needed | 52 // This is only implemented on Windows (because currently it is only needed |
| 43 // on Windows). Will add implementations on other platforms when needed. | 53 // on Windows). Will add implementations on other platforms when needed. |
| 44 NOTIMPLEMENTED(); | 54 NOTIMPLEMENTED(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 65 NULL)); | 75 NULL)); |
| 66 if (pdf_file == INVALID_HANDLE_VALUE) | 76 if (pdf_file == INVALID_HANDLE_VALUE) |
| 67 return false; | 77 return false; |
| 68 HANDLE pdf_file_in_utility_process = NULL; | 78 HANDLE pdf_file_in_utility_process = NULL; |
| 69 ::DuplicateHandle(::GetCurrentProcess(), pdf_file, handle(), | 79 ::DuplicateHandle(::GetCurrentProcess(), pdf_file, handle(), |
| 70 &pdf_file_in_utility_process, 0, false, | 80 &pdf_file_in_utility_process, 0, false, |
| 71 DUPLICATE_SAME_ACCESS); | 81 DUPLICATE_SAME_ACCESS); |
| 72 if (!pdf_file_in_utility_process) | 82 if (!pdf_file_in_utility_process) |
| 73 return false; | 83 return false; |
| 74 waiting_for_reply_ = true; | 84 waiting_for_reply_ = true; |
| 75 return Send( | 85 return child_process_host_->Send( |
| 76 new ChromeUtilityMsg_RenderPDFPagesToMetafile( | 86 new ChromeUtilityMsg_RenderPDFPagesToMetafile( |
| 77 pdf_file_in_utility_process, | 87 pdf_file_in_utility_process, |
| 78 metafile_path_, | 88 metafile_path_, |
| 79 render_settings, | 89 render_settings, |
| 80 page_ranges)); | 90 page_ranges)); |
| 81 #endif // !defined(OS_WIN) | 91 #endif // !defined(OS_WIN) |
| 82 } | 92 } |
| 83 | 93 |
| 84 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 94 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
| 85 const std::string& printer_name) { | 95 const std::string& printer_name) { |
| 86 FilePath exposed_path; | 96 FilePath exposed_path; |
| 87 if (!StartProcess(true, exposed_path)) | 97 if (!StartProcess(true, exposed_path)) |
| 88 return false; | 98 return false; |
| 89 waiting_for_reply_ = true; | 99 waiting_for_reply_ = true; |
| 90 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); | 100 return child_process_host_->Send( |
| 101 new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); |
| 91 } | 102 } |
| 92 | 103 |
| 93 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox, | 104 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox, |
| 94 const FilePath& exposed_dir) { | 105 const FilePath& exposed_dir) { |
| 95 if (!CreateChannel()) | 106 if (!child_process_host_->CreateChannel()) |
| 96 return false; | 107 return false; |
| 97 | 108 |
| 98 FilePath exe_path = GetUtilityProcessCmd(); | 109 FilePath exe_path = GetUtilityProcessCmd(); |
| 99 if (exe_path.empty()) { | 110 if (exe_path.empty()) { |
| 100 NOTREACHED() << "Unable to get utility process binary name."; | 111 NOTREACHED() << "Unable to get utility process binary name."; |
| 101 return false; | 112 return false; |
| 102 } | 113 } |
| 103 | 114 |
| 104 CommandLine cmd_line(exe_path); | 115 CommandLine cmd_line(exe_path); |
| 105 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 116 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
| 106 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id()); | 117 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, |
| 118 child_process_host_->channel_id()); |
| 107 cmd_line.AppendSwitch(switches::kLang); | 119 cmd_line.AppendSwitch(switches::kLang); |
| 108 | 120 |
| 109 return Launch(&cmd_line, no_sandbox, exposed_dir); | 121 return Launch(&cmd_line, no_sandbox, exposed_dir); |
| 110 } | 122 } |
| 111 | 123 |
| 124 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, |
| 125 bool no_sandbox, |
| 126 const FilePath& exposed_dir) { |
| 127 #if !defined(OS_WIN) |
| 128 // TODO(sanjeevr): Implement for non-Windows OSes. |
| 129 NOTIMPLEMENTED(); |
| 130 return false; |
| 131 #else // !defined(OS_WIN) |
| 132 |
| 133 if (no_sandbox) { |
| 134 base::ProcessHandle process = base::kNullProcessHandle; |
| 135 cmd_line->AppendSwitch(switches::kNoSandbox); |
| 136 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); |
| 137 } else { |
| 138 handle_ = sandbox::StartProcessWithAccess(cmd_line, exposed_dir); |
| 139 } |
| 140 return (handle_ != base::kNullProcessHandle); |
| 141 #endif // !defined(OS_WIN) |
| 142 } |
| 143 |
| 112 FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 144 FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
| 113 #if defined(OS_LINUX) | 145 #if defined(OS_LINUX) |
| 114 int flags = CHILD_ALLOW_SELF; | 146 int flags = ChildProcessHost::CHILD_ALLOW_SELF; |
| 115 #else | 147 #else |
| 116 int flags = CHILD_NORMAL; | 148 int flags = ChildProcessHost::CHILD_NORMAL; |
| 117 #endif | 149 #endif |
| 118 return GetChildPath(flags); | 150 return ChildProcessHost::GetChildPath(flags); |
| 119 } | 151 } |
| 120 | 152 |
| 121 bool ServiceUtilityProcessHost::CanShutdown() { | 153 bool ServiceUtilityProcessHost::CanShutdown() { |
| 122 return true; | 154 return true; |
| 123 } | 155 } |
| 124 | 156 |
| 125 void ServiceUtilityProcessHost::OnChildDied() { | 157 void ServiceUtilityProcessHost::OnChildDisconnected() { |
| 126 if (waiting_for_reply_) { | 158 if (waiting_for_reply_) { |
| 127 // If we are yet to receive a reply then notify the client that the | 159 // If we are yet to receive a reply then notify the client that the |
| 128 // child died. | 160 // child died. |
| 129 client_message_loop_proxy_->PostTask( | 161 client_message_loop_proxy_->PostTask( |
| 130 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); | 162 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); |
| 131 } | 163 } |
| 132 // The base class implementation will delete |this|. | 164 delete this; |
| 133 ServiceChildProcessHost::OnChildDied(); | 165 } |
| 166 |
| 167 void ServiceUtilityProcessHost::ShutdownStarted() { |
| 134 } | 168 } |
| 135 | 169 |
| 136 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 170 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
| 137 bool handled = true; | 171 bool handled = true; |
| 138 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) | 172 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) |
| 139 IPC_MESSAGE_HANDLER( | 173 IPC_MESSAGE_HANDLER( |
| 140 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, | 174 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, |
| 141 OnRenderPDFPagesToMetafileSucceeded) | 175 OnRenderPDFPagesToMetafileSucceeded) |
| 142 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | 176 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, |
| 143 OnRenderPDFPagesToMetafileFailed) | 177 OnRenderPDFPagesToMetafileFailed) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 printing::Emf metafile; | 243 printing::Emf metafile; |
| 210 if (!metafile.InitFromFile(metafile_path)) { | 244 if (!metafile.InitFromFile(metafile_path)) { |
| 211 OnRenderPDFPagesToMetafileFailed(); | 245 OnRenderPDFPagesToMetafileFailed(); |
| 212 } else { | 246 } else { |
| 213 OnRenderPDFPagesToMetafileSucceeded(metafile, | 247 OnRenderPDFPagesToMetafileSucceeded(metafile, |
| 214 highest_rendered_page_number); | 248 highest_rendered_page_number); |
| 215 } | 249 } |
| 216 #endif // defined(OS_WIN) | 250 #endif // defined(OS_WIN) |
| 217 } | 251 } |
| 218 | 252 |
| OLD | NEW |