Chromium Code Reviews| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/chrome_utility_messages.h" | 14 #include "chrome/common/chrome_utility_messages.h" |
| 15 #include "ipc/ipc_switches.h" | 15 #include "ipc/ipc_switches.h" |
| 16 #include "printing/page_range.h" | 16 #include "printing/page_range.h" |
| 17 #include "ui/base/ui_base_switches.h" | 17 #include "ui/base/ui_base_switches.h" |
| 18 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
| 19 | 19 |
| 20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/win/scoped_handle.h" | 22 #include "base/win/scoped_handle.h" |
| 23 #include "content/common/child_process_messages.h" | |
| 23 #include "printing/emf_win.h" | 24 #include "printing/emf_win.h" |
| 24 #endif | 25 #endif |
| 25 | 26 |
| 26 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 27 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
| 27 Client* client, base::MessageLoopProxy* client_message_loop_proxy) | 28 Client* client, base::MessageLoopProxy* client_message_loop_proxy) |
| 28 : ServiceChildProcessHost(ChildProcessInfo::UTILITY_PROCESS), | 29 : ServiceChildProcessHost(ChildProcessInfo::UTILITY_PROCESS), |
| 29 client_(client), | 30 client_(client), |
| 30 client_message_loop_proxy_(client_message_loop_proxy), | 31 client_message_loop_proxy_(client_message_loop_proxy), |
| 31 waiting_for_reply_(false) { | 32 waiting_for_reply_(false), |
| 33 unique_process_id_(0) { | |
| 32 } | 34 } |
| 33 | 35 |
| 34 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 36 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
| 35 } | 37 } |
| 36 | 38 |
| 37 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 39 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
| 38 const FilePath& pdf_path, | 40 const FilePath& pdf_path, |
| 39 const gfx::Rect& render_area, | 41 const gfx::Rect& render_area, |
| 40 int render_dpi, | 42 int render_dpi, |
| 41 const std::vector<printing::PageRange>& page_ranges) { | 43 const std::vector<printing::PageRange>& page_ranges) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 | 87 |
| 86 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 88 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
| 87 const std::string& printer_name) { | 89 const std::string& printer_name) { |
| 88 FilePath exposed_path; | 90 FilePath exposed_path; |
| 89 if (!StartProcess(true, exposed_path)) | 91 if (!StartProcess(true, exposed_path)) |
| 90 return false; | 92 return false; |
| 91 waiting_for_reply_ = true; | 93 waiting_for_reply_ = true; |
| 92 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); | 94 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); |
| 93 } | 95 } |
| 94 | 96 |
| 97 int ServiceUtilityProcessHost::unique_process_id() { | |
| 98 return unique_process_id_; | |
| 99 } | |
| 100 | |
| 95 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox, | 101 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox, |
| 96 const FilePath& exposed_dir) { | 102 const FilePath& exposed_dir) { |
| 97 // Name must be set or metrics_service will crash in any test which | 103 // Name must be set or metrics_service will crash in any test which |
| 98 // launches a UtilityProcessHost. | 104 // launches a UtilityProcessHost. |
| 99 set_name(ASCIIToUTF16("utility process")); | 105 set_name(ASCIIToUTF16("utility process")); |
| 100 | 106 |
| 101 if (!CreateChannel()) | 107 if (!CreateChannel()) |
| 102 return false; | 108 return false; |
| 103 | 109 |
| 104 FilePath exe_path = GetUtilityProcessCmd(); | 110 FilePath exe_path = GetUtilityProcessCmd(); |
| 105 if (exe_path.empty()) { | 111 if (exe_path.empty()) { |
| 106 NOTREACHED() << "Unable to get utility process binary name."; | 112 NOTREACHED() << "Unable to get utility process binary name."; |
| 107 return false; | 113 return false; |
| 108 } | 114 } |
| 109 | 115 |
| 110 CommandLine cmd_line(exe_path); | 116 CommandLine cmd_line(exe_path); |
| 111 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 117 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
| 112 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id()); | 118 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id()); |
| 113 cmd_line.AppendSwitch(switches::kLang); | 119 cmd_line.AppendSwitch(switches::kLang); |
| 114 | 120 |
| 115 return Launch(&cmd_line, no_sandbox, exposed_dir); | 121 if (Launch(&cmd_line, no_sandbox, exposed_dir)) { |
| 122 unique_process_id_ = ChildProcessInfo::GenerateChildProcessUniqueId(); | |
|
jam
2011/09/19 16:31:17
we usually set this in the constructor. that way t
arthurhsu
2011/09/19 18:22:51
Done.
| |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 return false; | |
| 116 } | 127 } |
| 117 | 128 |
| 118 FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 129 FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
| 119 #if defined(OS_LINUX) | 130 #if defined(OS_LINUX) |
| 120 int flags = CHILD_ALLOW_SELF; | 131 int flags = CHILD_ALLOW_SELF; |
| 121 #else | 132 #else |
| 122 int flags = CHILD_NORMAL; | 133 int flags = CHILD_NORMAL; |
| 123 #endif | 134 #endif |
| 124 return GetChildPath(flags); | 135 return GetChildPath(flags); |
| 125 } | 136 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 138 } | 149 } |
| 139 // The base class implementation will delete |this|. | 150 // The base class implementation will delete |this|. |
| 140 ServiceChildProcessHost::OnChildDied(); | 151 ServiceChildProcessHost::OnChildDied(); |
| 141 } | 152 } |
| 142 | 153 |
| 143 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 154 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
| 144 bool msg_is_ok = false; | 155 bool msg_is_ok = false; |
| 145 IPC_BEGIN_MESSAGE_MAP_EX(ServiceUtilityProcessHost, message, msg_is_ok) | 156 IPC_BEGIN_MESSAGE_MAP_EX(ServiceUtilityProcessHost, message, msg_is_ok) |
| 146 #if defined(OS_WIN) // This hack is Windows-specific. | 157 #if defined(OS_WIN) // This hack is Windows-specific. |
| 147 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PreCacheFont, OnPreCacheFont) | 158 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PreCacheFont, OnPreCacheFont) |
| 159 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ReleaseCachedFonts, | |
| 160 OnReleaseCachedFonts) | |
| 148 #endif | 161 #endif |
| 149 IPC_MESSAGE_HANDLER( | 162 IPC_MESSAGE_HANDLER( |
| 150 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, | 163 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, |
| 151 OnRenderPDFPagesToMetafileSucceeded) | 164 OnRenderPDFPagesToMetafileSucceeded) |
| 152 IPC_MESSAGE_UNHANDLED(msg_is_ok__ = MessageForClient(message)) | 165 IPC_MESSAGE_UNHANDLED(msg_is_ok__ = MessageForClient(message)) |
| 153 IPC_END_MESSAGE_MAP_EX() | 166 IPC_END_MESSAGE_MAP_EX() |
| 154 return true; | 167 return true; |
| 155 } | 168 } |
| 156 | 169 |
| 157 bool ServiceUtilityProcessHost::MessageForClient(const IPC::Message& message) { | 170 bool ServiceUtilityProcessHost::MessageForClient(const IPC::Message& message) { |
| 158 DCHECK(waiting_for_reply_); | 171 DCHECK(waiting_for_reply_); |
| 159 bool ret = client_message_loop_proxy_->PostTask( | 172 bool ret = client_message_loop_proxy_->PostTask( |
| 160 FROM_HERE, | 173 FROM_HERE, |
| 161 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, | 174 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, |
| 162 message)); | 175 message)); |
| 163 waiting_for_reply_ = false; | 176 waiting_for_reply_ = false; |
| 164 return ret; | 177 return ret; |
| 165 } | 178 } |
| 166 | 179 |
| 167 #if defined(OS_WIN) // This hack is Windows-specific. | 180 #if defined(OS_WIN) // This hack is Windows-specific. |
| 168 void ServiceUtilityProcessHost::OnPreCacheFont(const LOGFONT& font) { | 181 void ServiceUtilityProcessHost::OnPreCacheFont(const LOGFONT& font) { |
| 169 PreCacheFont(font); | 182 PreCacheFont(font, unique_process_id()); |
| 183 } | |
| 184 | |
| 185 void ServiceUtilityProcessHost::OnReleaseCachedFonts() { | |
| 186 ReleaseCachedFonts(unique_process_id()); | |
| 170 } | 187 } |
| 171 #endif // OS_WIN | 188 #endif // OS_WIN |
| 172 | 189 |
| 173 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileSucceeded( | 190 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileSucceeded( |
| 174 int highest_rendered_page_number) { | 191 int highest_rendered_page_number) { |
| 175 DCHECK(waiting_for_reply_); | 192 DCHECK(waiting_for_reply_); |
| 176 // If the metafile was successfully created, we need to take our hands off the | 193 // If the metafile was successfully created, we need to take our hands off the |
| 177 // scratch metafile directory. The client will delete it when it is done with | 194 // scratch metafile directory. The client will delete it when it is done with |
| 178 // metafile. | 195 // metafile. |
| 179 scratch_metafile_dir_->Take(); | 196 scratch_metafile_dir_->Take(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 printing::Emf metafile; | 235 printing::Emf metafile; |
| 219 if (!metafile.InitFromFile(metafile_path)) { | 236 if (!metafile.InitFromFile(metafile_path)) { |
| 220 OnRenderPDFPagesToMetafileFailed(); | 237 OnRenderPDFPagesToMetafileFailed(); |
| 221 } else { | 238 } else { |
| 222 OnRenderPDFPagesToMetafileSucceeded(metafile, | 239 OnRenderPDFPagesToMetafileSucceeded(metafile, |
| 223 highest_rendered_page_number); | 240 highest_rendered_page_number); |
| 224 } | 241 } |
| 225 #endif // defined(OS_WIN) | 242 #endif // defined(OS_WIN) |
| 226 } | 243 } |
| 227 | 244 |
| OLD | NEW |