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 <queue> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/file.h" |
| 12 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
11 #include "base/logging.h" | 15 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
15 #include "base/process/kill.h" | 18 #include "base/process/kill.h" |
16 #include "base/strings/utf_string_conversions.h" | 19 #include "base/process/launch.h" |
| 20 #include "base/task_runner_util.h" |
17 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/chrome_utility_printing_messages.h" | 22 #include "chrome/common/chrome_utility_printing_messages.h" |
19 #include "content/public/common/child_process_host.h" | 23 #include "content/public/common/child_process_host.h" |
20 #include "content/public/common/result_codes.h" | 24 #include "content/public/common/result_codes.h" |
21 #include "content/public/common/sandbox_init.h" | 25 #include "content/public/common/sandbox_init.h" |
| 26 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
22 #include "ipc/ipc_switches.h" | 27 #include "ipc/ipc_switches.h" |
23 #include "printing/page_range.h" | |
24 #include "ui/base/ui_base_switches.h" | |
25 #include "ui/gfx/rect.h" | |
26 | |
27 #if defined(OS_WIN) | |
28 #include "base/files/file_path.h" | |
29 #include "base/memory/scoped_ptr.h" | |
30 #include "base/process/launch.h" | |
31 #include "base/win/scoped_handle.h" | |
32 #include "content/public/common/sandbox_init.h" | |
33 #include "content/public/common/sandboxed_process_launcher_delegate.h" | |
34 #include "printing/emf_win.h" | 28 #include "printing/emf_win.h" |
35 #include "sandbox/win/src/sandbox_policy_base.h" | 29 #include "sandbox/win/src/sandbox_policy_base.h" |
| 30 #include "ui/base/ui_base_switches.h" |
36 | 31 |
37 namespace { | 32 namespace { |
38 | 33 |
| 34 using content::ChildProcessHost; |
| 35 |
| 36 enum ServiceUtilityProcessHostEvent { |
| 37 SERVICE_UTILITY_STARTED, |
| 38 SERVICE_UTILITY_DISCONNECTED, |
| 39 SERVICE_UTILITY_METAFILE_REQUEST, |
| 40 SERVICE_UTILITY_METAFILE_SUCCEEDED, |
| 41 SERVICE_UTILITY_METAFILE_FAILED, |
| 42 SERVICE_UTILITY_CAPS_REQUEST, |
| 43 SERVICE_UTILITY_CAPS_SUCCEEDED, |
| 44 SERVICE_UTILITY_CAPS_FAILED, |
| 45 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, |
| 46 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, |
| 47 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, |
| 48 SERVICE_UTILITY_FAILED_TO_START, |
| 49 SERVICE_UTILITY_EVENT_MAX, |
| 50 }; |
| 51 |
39 // NOTE: changes to this class need to be reviewed by the security team. | 52 // NOTE: changes to this class need to be reviewed by the security team. |
40 class ServiceSandboxedProcessLauncherDelegate | 53 class ServiceSandboxedProcessLauncherDelegate |
41 : public content::SandboxedProcessLauncherDelegate { | 54 : public content::SandboxedProcessLauncherDelegate { |
42 public: | 55 public: |
43 explicit ServiceSandboxedProcessLauncherDelegate( | 56 ServiceSandboxedProcessLauncherDelegate() {} |
44 const base::FilePath& exposed_dir) | |
45 : exposed_dir_(exposed_dir) { | |
46 } | |
47 | |
48 virtual void PreSandbox(bool* disable_default_policy, | |
49 base::FilePath* exposed_dir) OVERRIDE { | |
50 *exposed_dir = exposed_dir_; | |
51 } | |
52 | 57 |
53 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, | 58 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, |
54 bool* success) OVERRIDE { | 59 bool* success) OVERRIDE { |
55 // Service process may run as windows service and it fails to create a | 60 // Service process may run as windows service and it fails to create a |
56 // window station. | 61 // window station. |
57 policy->SetAlternateDesktop(false); | 62 policy->SetAlternateDesktop(false); |
58 } | 63 } |
59 | 64 |
60 private: | 65 private: |
61 base::FilePath exposed_dir_; | 66 DISALLOW_COPY_AND_ASSIGN(ServiceSandboxedProcessLauncherDelegate); |
62 }; | 67 }; |
63 | 68 |
64 } // namespace | 69 } // namespace |
65 | 70 |
66 #endif // OS_WIN | 71 class ServiceUtilityProcessHost::PdfToEmfState { |
| 72 public: |
| 73 explicit PdfToEmfState(ServiceUtilityProcessHost* host) |
| 74 : host_(host), page_count_(0), current_page_(0), pages_in_progress_(0) {} |
| 75 ~PdfToEmfState() { Stop(); } |
67 | 76 |
68 using content::ChildProcessHost; | 77 bool Start(base::File pdf_file, |
| 78 const printing::PdfRenderSettings& conversion_settings) { |
| 79 if (!temp_dir_.CreateUniqueTempDir()) |
| 80 return false; |
| 81 return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles( |
| 82 IPC::TakeFileHandleForProcess(pdf_file.Pass(), host_->handle()), |
| 83 conversion_settings)); |
| 84 } |
69 | 85 |
70 namespace { | 86 void GetMorePages() { |
71 enum ServiceUtilityProcessHostEvent { | 87 const int kMaxNumberOfTempFilesPerDocument = 3; |
72 SERVICE_UTILITY_STARTED, | 88 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && |
73 SERVICE_UTILITY_DISCONNECTED, | 89 current_page_ < page_count_) { |
74 SERVICE_UTILITY_METAFILE_REQUEST, | 90 ++pages_in_progress_; |
75 SERVICE_UTILITY_METAFILE_SUCCEEDED, | 91 emf_files_.push(CreateTempFile()); |
76 SERVICE_UTILITY_METAFILE_FAILED, | 92 host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( |
77 SERVICE_UTILITY_CAPS_REQUEST, | 93 current_page_++, |
78 SERVICE_UTILITY_CAPS_SUCCEEDED, | 94 IPC::GetFileHandleForProcess( |
79 SERVICE_UTILITY_CAPS_FAILED, | 95 emf_files_.back().GetPlatformFile(), host_->handle(), false))); |
80 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 96 } |
81 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, | 97 } |
82 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, | 98 |
83 SERVICE_UTILITY_EVENT_MAX, | 99 // Returns true if all pages processed and client should not expect more |
| 100 // results. |
| 101 bool OnPageProcessed() { |
| 102 --pages_in_progress_; |
| 103 GetMorePages(); |
| 104 if (pages_in_progress_ || current_page_ < page_count_) |
| 105 return false; |
| 106 Stop(); |
| 107 return true; |
| 108 } |
| 109 |
| 110 base::File TakeNextFile() { |
| 111 DCHECK(!emf_files_.empty()); |
| 112 base::File file; |
| 113 if (!emf_files_.empty()) |
| 114 file = emf_files_.front().Pass(); |
| 115 emf_files_.pop(); |
| 116 return file.Pass(); |
| 117 } |
| 118 |
| 119 void set_page_count(int page_count) { page_count_ = page_count; } |
| 120 bool has_page_count() { return page_count_ > 0; } |
| 121 |
| 122 private: |
| 123 void Stop() { |
| 124 host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); |
| 125 } |
| 126 |
| 127 base::File CreateTempFile() { |
| 128 base::FilePath path; |
| 129 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &path)) |
| 130 return base::File(); |
| 131 return base::File(path, |
| 132 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 133 base::File::FLAG_READ | |
| 134 base::File::FLAG_DELETE_ON_CLOSE | |
| 135 base::File::FLAG_TEMPORARY); |
| 136 } |
| 137 |
| 138 base::ScopedTempDir temp_dir_; |
| 139 ServiceUtilityProcessHost* host_; |
| 140 std::queue<base::File> emf_files_; |
| 141 int page_count_; |
| 142 int current_page_; |
| 143 int pages_in_progress_; |
84 }; | 144 }; |
85 } // namespace | |
86 | 145 |
87 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 146 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
88 Client* client, base::MessageLoopProxy* client_message_loop_proxy) | 147 Client* client, |
89 : handle_(base::kNullProcessHandle), | 148 base::MessageLoopProxy* client_message_loop_proxy) |
90 client_(client), | 149 : handle_(base::kNullProcessHandle), |
91 client_message_loop_proxy_(client_message_loop_proxy), | 150 client_(client), |
92 waiting_for_reply_(false) { | 151 client_message_loop_proxy_(client_message_loop_proxy), |
| 152 waiting_for_reply_(false), |
| 153 weak_ptr_factory_(this) { |
93 child_process_host_.reset(ChildProcessHost::Create(this)); | 154 child_process_host_.reset(ChildProcessHost::Create(this)); |
94 } | 155 } |
95 | 156 |
96 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 157 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
97 // We need to kill the child process when the host dies. | 158 // We need to kill the child process when the host dies. |
98 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); | 159 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); |
99 } | 160 } |
100 | 161 |
101 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 162 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
102 const base::FilePath& pdf_path, | 163 const base::FilePath& pdf_path, |
103 const printing::PdfRenderSettings& render_settings, | 164 const printing::PdfRenderSettings& render_settings) { |
104 const std::vector<printing::PageRange>& page_ranges) { | |
105 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 165 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
106 SERVICE_UTILITY_METAFILE_REQUEST, | 166 SERVICE_UTILITY_METAFILE_REQUEST, |
107 SERVICE_UTILITY_EVENT_MAX); | 167 SERVICE_UTILITY_EVENT_MAX); |
108 start_time_ = base::Time::Now(); | 168 start_time_ = base::Time::Now(); |
109 #if !defined(OS_WIN) | 169 base::File pdf_file(pdf_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
110 // This is only implemented on Windows (because currently it is only needed | 170 if (!pdf_file.IsValid() || !StartProcess(false)) |
111 // on Windows). Will add implementations on other platforms when needed. | |
112 NOTIMPLEMENTED(); | |
113 return false; | |
114 #else // !defined(OS_WIN) | |
115 scratch_metafile_dir_.reset(new base::ScopedTempDir); | |
116 if (!scratch_metafile_dir_->CreateUniqueTempDir()) | |
117 return false; | |
118 metafile_path_ = scratch_metafile_dir_->path().AppendASCII("output.emf"); | |
119 if (!StartProcess(false, scratch_metafile_dir_->path())) | |
120 return false; | 171 return false; |
121 | 172 |
122 base::File pdf_file( | |
123 pdf_path, | |
124 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); | |
125 DCHECK(!waiting_for_reply_); | 173 DCHECK(!waiting_for_reply_); |
126 waiting_for_reply_ = true; | 174 waiting_for_reply_ = true; |
127 return child_process_host_->Send( | 175 |
128 new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | 176 pdf_to_emf_state_.reset(new PdfToEmfState(this)); |
129 IPC::TakeFileHandleForProcess(pdf_file.Pass(), handle()), | 177 return pdf_to_emf_state_->Start(pdf_file.Pass(), render_settings); |
130 metafile_path_, | |
131 render_settings, | |
132 page_ranges)); | |
133 #endif // !defined(OS_WIN) | |
134 } | 178 } |
135 | 179 |
136 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 180 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
137 const std::string& printer_name) { | 181 const std::string& printer_name) { |
138 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 182 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
139 SERVICE_UTILITY_CAPS_REQUEST, | 183 SERVICE_UTILITY_CAPS_REQUEST, |
140 SERVICE_UTILITY_EVENT_MAX); | 184 SERVICE_UTILITY_EVENT_MAX); |
141 start_time_ = base::Time::Now(); | 185 start_time_ = base::Time::Now(); |
142 base::FilePath exposed_path; | 186 if (!StartProcess(true)) |
143 if (!StartProcess(true, exposed_path)) | |
144 return false; | 187 return false; |
145 DCHECK(!waiting_for_reply_); | 188 DCHECK(!waiting_for_reply_); |
146 waiting_for_reply_ = true; | 189 waiting_for_reply_ = true; |
147 return child_process_host_->Send( | 190 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); |
148 new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); | |
149 } | 191 } |
150 | 192 |
151 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( | 193 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( |
152 const std::string& printer_name) { | 194 const std::string& printer_name) { |
153 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 195 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
154 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 196 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, |
155 SERVICE_UTILITY_EVENT_MAX); | 197 SERVICE_UTILITY_EVENT_MAX); |
156 start_time_ = base::Time::Now(); | 198 start_time_ = base::Time::Now(); |
157 base::FilePath exposed_path; | 199 if (!StartProcess(true)) |
158 if (!StartProcess(true, exposed_path)) | |
159 return false; | 200 return false; |
160 DCHECK(!waiting_for_reply_); | 201 DCHECK(!waiting_for_reply_); |
161 waiting_for_reply_ = true; | 202 waiting_for_reply_ = true; |
162 return child_process_host_->Send( | 203 return Send( |
163 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); | 204 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); |
164 } | 205 } |
165 | 206 |
166 bool ServiceUtilityProcessHost::StartProcess( | 207 bool ServiceUtilityProcessHost::Send(IPC::Message* msg) { |
167 bool no_sandbox, | 208 if (child_process_host_) |
168 const base::FilePath& exposed_dir) { | 209 return child_process_host_->Send(msg); |
| 210 delete msg; |
| 211 return false; |
| 212 } |
| 213 |
| 214 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox) { |
169 std::string channel_id = child_process_host_->CreateChannel(); | 215 std::string channel_id = child_process_host_->CreateChannel(); |
170 if (channel_id.empty()) | 216 if (channel_id.empty()) |
171 return false; | 217 return false; |
172 | 218 |
173 base::FilePath exe_path = GetUtilityProcessCmd(); | 219 base::FilePath exe_path = GetUtilityProcessCmd(); |
174 if (exe_path.empty()) { | 220 if (exe_path.empty()) { |
175 NOTREACHED() << "Unable to get utility process binary name."; | 221 NOTREACHED() << "Unable to get utility process binary name."; |
176 return false; | 222 return false; |
177 } | 223 } |
178 | 224 |
179 CommandLine cmd_line(exe_path); | 225 base::CommandLine cmd_line(exe_path); |
180 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 226 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
181 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); | 227 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
182 cmd_line.AppendSwitch(switches::kLang); | 228 cmd_line.AppendSwitch(switches::kLang); |
183 | 229 |
184 if (Launch(&cmd_line, no_sandbox, exposed_dir)) { | 230 if (Launch(&cmd_line, no_sandbox)) { |
185 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 231 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
186 SERVICE_UTILITY_STARTED, | 232 SERVICE_UTILITY_STARTED, |
187 SERVICE_UTILITY_EVENT_MAX); | 233 SERVICE_UTILITY_EVENT_MAX); |
188 return true; | 234 return true; |
189 } | 235 } |
| 236 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
| 237 SERVICE_UTILITY_FAILED_TO_START, |
| 238 SERVICE_UTILITY_EVENT_MAX); |
190 return false; | 239 return false; |
191 } | 240 } |
192 | 241 |
193 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, | 242 bool ServiceUtilityProcessHost::Launch(base::CommandLine* cmd_line, |
194 bool no_sandbox, | 243 bool no_sandbox) { |
195 const base::FilePath& exposed_dir) { | |
196 #if !defined(OS_WIN) | |
197 // TODO(sanjeevr): Implement for non-Windows OSes. | |
198 NOTIMPLEMENTED(); | |
199 return false; | |
200 #else // !defined(OS_WIN) | |
201 | |
202 if (no_sandbox) { | 244 if (no_sandbox) { |
203 base::ProcessHandle process = base::kNullProcessHandle; | 245 base::ProcessHandle process = base::kNullProcessHandle; |
204 cmd_line->AppendSwitch(switches::kNoSandbox); | 246 cmd_line->AppendSwitch(switches::kNoSandbox); |
205 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); | 247 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); |
206 } else { | 248 } else { |
207 ServiceSandboxedProcessLauncherDelegate delegate(exposed_dir); | 249 ServiceSandboxedProcessLauncherDelegate delegate; |
208 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); | 250 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); |
209 } | 251 } |
210 return (handle_ != base::kNullProcessHandle); | 252 return (handle_ != base::kNullProcessHandle); |
211 #endif // !defined(OS_WIN) | |
212 } | 253 } |
213 | 254 |
214 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 255 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
215 #if defined(OS_LINUX) | 256 #if defined(OS_LINUX) |
216 int flags = ChildProcessHost::CHILD_ALLOW_SELF; | 257 int flags = ChildProcessHost::CHILD_ALLOW_SELF; |
217 #else | 258 #else |
218 int flags = ChildProcessHost::CHILD_NORMAL; | 259 int flags = ChildProcessHost::CHILD_NORMAL; |
219 #endif | 260 #endif |
220 return ChildProcessHost::GetChildPath(flags); | 261 return ChildProcessHost::GetChildPath(flags); |
221 } | 262 } |
222 | 263 |
223 void ServiceUtilityProcessHost::OnChildDisconnected() { | 264 void ServiceUtilityProcessHost::OnChildDisconnected() { |
224 if (waiting_for_reply_) { | 265 if (waiting_for_reply_) { |
225 // If we are yet to receive a reply then notify the client that the | 266 // If we are yet to receive a reply then notify the client that the |
226 // child died. | 267 // child died. |
227 client_message_loop_proxy_->PostTask( | 268 client_message_loop_proxy_->PostTask( |
228 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); | 269 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); |
229 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 270 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
230 SERVICE_UTILITY_DISCONNECTED, | 271 SERVICE_UTILITY_DISCONNECTED, |
231 SERVICE_UTILITY_EVENT_MAX); | 272 SERVICE_UTILITY_EVENT_MAX); |
232 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", | 273 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", |
233 base::Time::Now() - start_time_); | 274 base::Time::Now() - start_time_); |
234 } | 275 } |
235 delete this; | 276 delete this; |
236 } | 277 } |
237 | 278 |
238 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 279 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
239 bool handled = true; | 280 bool handled = true; |
240 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) | 281 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) |
241 #if defined(OS_WIN) | |
242 IPC_MESSAGE_HANDLER( | 282 IPC_MESSAGE_HANDLER( |
243 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, | 283 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, |
244 OnRenderPDFPagesToMetafilesSucceeded) | 284 OnRenderPDFPagesToMetafilesPageCount) |
245 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | 285 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, |
246 OnRenderPDFPagesToMetafileFailed) | 286 OnRenderPDFPagesToMetafilesPageDone) |
247 #endif | |
248 IPC_MESSAGE_HANDLER( | 287 IPC_MESSAGE_HANDLER( |
249 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, | 288 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, |
250 OnGetPrinterCapsAndDefaultsSucceeded) | 289 OnGetPrinterCapsAndDefaultsSucceeded) |
251 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, | 290 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, |
252 OnGetPrinterCapsAndDefaultsFailed) | 291 OnGetPrinterCapsAndDefaultsFailed) |
253 IPC_MESSAGE_HANDLER( | 292 IPC_MESSAGE_HANDLER( |
254 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, | 293 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, |
255 OnGetPrinterSemanticCapsAndDefaultsSucceeded) | 294 OnGetPrinterSemanticCapsAndDefaultsSucceeded) |
256 IPC_MESSAGE_HANDLER( | 295 IPC_MESSAGE_HANDLER( |
257 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, | 296 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, |
258 OnGetPrinterSemanticCapsAndDefaultsFailed) | 297 OnGetPrinterSemanticCapsAndDefaultsFailed) |
259 IPC_MESSAGE_UNHANDLED(handled = false) | 298 IPC_MESSAGE_UNHANDLED(handled = false) |
260 IPC_END_MESSAGE_MAP() | 299 IPC_END_MESSAGE_MAP() |
261 return handled; | 300 return handled; |
262 } | 301 } |
263 | 302 |
264 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { | 303 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { |
265 return handle_; | 304 return handle_; |
266 } | 305 } |
267 | 306 |
268 #if defined(OS_WIN) | 307 void ServiceUtilityProcessHost::OnMetafileSpooled(bool success) { |
269 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesSucceeded( | 308 if (!success || pdf_to_emf_state_->OnPageProcessed()) |
270 const std::vector<printing::PageRange>& page_ranges, | 309 OnPDFToEmfFinished(success); |
| 310 } |
| 311 |
| 312 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesPageCount( |
| 313 int page_count) { |
| 314 DCHECK(waiting_for_reply_); |
| 315 if (!pdf_to_emf_state_ || page_count <= 0 || |
| 316 pdf_to_emf_state_->has_page_count()) { |
| 317 return OnPDFToEmfFinished(false); |
| 318 } |
| 319 pdf_to_emf_state_->set_page_count(page_count); |
| 320 pdf_to_emf_state_->GetMorePages(); |
| 321 } |
| 322 |
| 323 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesPageDone( |
| 324 bool success, |
271 double scale_factor) { | 325 double scale_factor) { |
272 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | |
273 SERVICE_UTILITY_METAFILE_SUCCEEDED, | |
274 SERVICE_UTILITY_EVENT_MAX); | |
275 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", | |
276 base::Time::Now() - start_time_); | |
277 DCHECK(waiting_for_reply_); | 326 DCHECK(waiting_for_reply_); |
278 waiting_for_reply_ = false; | 327 if (!pdf_to_emf_state_ || !success) |
279 // If the metafile was successfully created, we need to take our hands off the | 328 return OnPDFToEmfFinished(false); |
280 // scratch metafile directory. The client will delete it when it is done with | 329 base::File emf_file = pdf_to_emf_state_->TakeNextFile(); |
281 // metafile. | 330 base::PostTaskAndReplyWithResult( |
282 scratch_metafile_dir_->Take(); | 331 client_message_loop_proxy_, |
283 | |
284 // TODO(vitalybuka|scottmg): http://crbug.com/170859: Currently, only one | |
285 // page is printed at a time. This would need to be refactored to change | |
286 // this. | |
287 CHECK_EQ(1u, page_ranges.size()); | |
288 CHECK_EQ(page_ranges[0].from, page_ranges[0].to); | |
289 int page_number = page_ranges[0].from; | |
290 client_message_loop_proxy_->PostTask( | |
291 FROM_HERE, | 332 FROM_HERE, |
292 base::Bind(&Client::MetafileAvailable, | 333 base::Bind(&Client::MetafileAvailable, |
293 client_.get(), | 334 client_.get(), |
294 metafile_path_.InsertBeforeExtensionASCII( | 335 scale_factor, |
295 base::StringPrintf(".%d", page_number)), | 336 base::Passed(&emf_file)), |
296 page_number, | 337 base::Bind(&ServiceUtilityProcessHost::OnMetafileSpooled, |
297 scale_factor)); | 338 weak_ptr_factory_.GetWeakPtr())); |
298 } | 339 } |
299 | 340 |
300 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { | 341 void ServiceUtilityProcessHost::OnPDFToEmfFinished(bool success) { |
301 DCHECK(waiting_for_reply_); | 342 if (!waiting_for_reply_) |
302 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 343 return; |
303 SERVICE_UTILITY_METAFILE_FAILED, | |
304 SERVICE_UTILITY_EVENT_MAX); | |
305 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", | |
306 base::Time::Now() - start_time_); | |
307 waiting_for_reply_ = false; | 344 waiting_for_reply_ = false; |
| 345 if (success) { |
| 346 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
| 347 SERVICE_UTILITY_METAFILE_SUCCEEDED, |
| 348 SERVICE_UTILITY_EVENT_MAX); |
| 349 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", |
| 350 base::Time::Now() - start_time_); |
| 351 } else { |
| 352 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
| 353 SERVICE_UTILITY_METAFILE_FAILED, |
| 354 SERVICE_UTILITY_EVENT_MAX); |
| 355 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", |
| 356 base::Time::Now() - start_time_); |
| 357 } |
308 client_message_loop_proxy_->PostTask( | 358 client_message_loop_proxy_->PostTask( |
309 FROM_HERE, | 359 FROM_HERE, |
310 base::Bind(&Client::OnRenderPDFPagesToMetafileFailed, client_.get())); | 360 base::Bind( |
| 361 &Client::OnRenderPDFPagesToMetafileDone, client_.get(), success)); |
| 362 pdf_to_emf_state_.reset(); |
311 } | 363 } |
312 #endif // defined(OS_WIN) | |
313 | 364 |
314 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( | 365 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( |
315 const std::string& printer_name, | 366 const std::string& printer_name, |
316 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 367 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
317 DCHECK(waiting_for_reply_); | 368 DCHECK(waiting_for_reply_); |
318 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 369 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
319 SERVICE_UTILITY_CAPS_SUCCEEDED, | 370 SERVICE_UTILITY_CAPS_SUCCEEDED, |
320 SERVICE_UTILITY_EVENT_MAX); | 371 SERVICE_UTILITY_EVENT_MAX); |
321 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", | 372 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", |
322 base::Time::Now() - start_time_); | 373 base::Time::Now() - start_time_); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", | 418 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", |
368 base::Time::Now() - start_time_); | 419 base::Time::Now() - start_time_); |
369 waiting_for_reply_ = false; | 420 waiting_for_reply_ = false; |
370 client_message_loop_proxy_->PostTask( | 421 client_message_loop_proxy_->PostTask( |
371 FROM_HERE, | 422 FROM_HERE, |
372 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, | 423 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, |
373 client_.get(), false, printer_name, | 424 client_.get(), false, printer_name, |
374 printing::PrinterSemanticCapsAndDefaults())); | 425 printing::PrinterSemanticCapsAndDefaults())); |
375 } | 426 } |
376 | 427 |
377 void ServiceUtilityProcessHost::Client::MetafileAvailable( | 428 bool ServiceUtilityProcessHost::Client::MetafileAvailable(double scale_factor, |
378 const base::FilePath& metafile_path, | 429 base::File file) { |
379 int highest_rendered_page_number, | 430 file.Seek(base::File::FROM_BEGIN, 0); |
380 double scale_factor) { | 431 int64 size = file.GetLength(); |
381 // The metafile was created in a temp folder which needs to get deleted after | 432 if (size <= 0) { |
382 // we have processed it. | 433 OnRenderPDFPagesToMetafileDone(false); |
383 base::ScopedTempDir scratch_metafile_dir; | 434 return false; |
384 if (!scratch_metafile_dir.Set(metafile_path.DirName())) | |
385 LOG(WARNING) << "Unable to set scratch metafile directory"; | |
386 #if defined(OS_WIN) | |
387 // It's important that metafile is declared after scratch_metafile_dir so | |
388 // that the metafile destructor closes the file before the base::ScopedTempDir | |
389 // destructor tries to remove the directory. | |
390 printing::Emf metafile; | |
391 if (!metafile.InitFromFile(metafile_path)) { | |
392 OnRenderPDFPagesToMetafileFailed(); | |
393 } else { | |
394 OnRenderPDFPagesToMetafileSucceeded(metafile, | |
395 highest_rendered_page_number, | |
396 scale_factor); | |
397 } | 435 } |
398 #endif // defined(OS_WIN) | 436 std::vector<char> data(size); |
| 437 if (file.ReadAtCurrentPos(data.data(), data.size()) != size) { |
| 438 OnRenderPDFPagesToMetafileDone(false); |
| 439 return false; |
| 440 } |
| 441 printing::Emf emf; |
| 442 if (!emf.InitFromData(data.data(), data.size())) { |
| 443 OnRenderPDFPagesToMetafileDone(false); |
| 444 return false; |
| 445 } |
| 446 OnRenderPDFPagesToMetafilePageDone(scale_factor, emf); |
| 447 return true; |
399 } | 448 } |
400 | |
OLD | NEW |