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 "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/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 start_time_ = base::Time::Now(); | 100 start_time_ = base::Time::Now(); |
101 #if !defined(OS_WIN) | 101 #if !defined(OS_WIN) |
102 // This is only implemented on Windows (because currently it is only needed | 102 // This is only implemented on Windows (because currently it is only needed |
103 // on Windows). Will add implementations on other platforms when needed. | 103 // on Windows). Will add implementations on other platforms when needed. |
104 NOTIMPLEMENTED(); | 104 NOTIMPLEMENTED(); |
105 return false; | 105 return false; |
106 #else // !defined(OS_WIN) | 106 #else // !defined(OS_WIN) |
107 scratch_metafile_dir_.reset(new base::ScopedTempDir); | 107 scratch_metafile_dir_.reset(new base::ScopedTempDir); |
108 if (!scratch_metafile_dir_->CreateUniqueTempDir()) | 108 if (!scratch_metafile_dir_->CreateUniqueTempDir()) |
109 return false; | 109 return false; |
110 if (!base::CreateTemporaryFileInDir(scratch_metafile_dir_->path(), | 110 metafile_path_ = scratch_metafile_dir_->path().AppendASCII("output.emf"); |
111 &metafile_path_)) { | |
112 return false; | |
113 } | |
114 | |
115 if (!StartProcess(false, scratch_metafile_dir_->path())) | 111 if (!StartProcess(false, scratch_metafile_dir_->path())) |
116 return false; | 112 return false; |
117 | 113 |
118 base::win::ScopedHandle pdf_file( | 114 base::File pdf_file( |
119 ::CreateFile(pdf_path.value().c_str(), | 115 pdf_path, |
120 GENERIC_READ, | 116 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); |
121 FILE_SHARE_READ | FILE_SHARE_WRITE, | |
122 NULL, | |
123 OPEN_EXISTING, | |
124 FILE_ATTRIBUTE_NORMAL, | |
125 NULL)); | |
126 if (pdf_file == INVALID_HANDLE_VALUE) | |
127 return false; | |
128 HANDLE pdf_file_in_utility_process = NULL; | |
129 ::DuplicateHandle(::GetCurrentProcess(), pdf_file, handle(), | |
130 &pdf_file_in_utility_process, 0, false, | |
131 DUPLICATE_SAME_ACCESS); | |
132 if (!pdf_file_in_utility_process) | |
133 return false; | |
134 DCHECK(!waiting_for_reply_); | 117 DCHECK(!waiting_for_reply_); |
135 waiting_for_reply_ = true; | 118 waiting_for_reply_ = true; |
136 return child_process_host_->Send( | 119 return child_process_host_->Send( |
137 new ChromeUtilityMsg_RenderPDFPagesToMetafile( | 120 new ChromeUtilityMsg_RenderPDFPagesToMetafiles( |
138 pdf_file_in_utility_process, | 121 IPC::TakeFileHandleForProcess(pdf_file.Pass(), handle()), |
139 metafile_path_, | 122 metafile_path_, |
140 render_settings, | 123 render_settings, |
141 page_ranges)); | 124 page_ranges)); |
142 #endif // !defined(OS_WIN) | 125 #endif // !defined(OS_WIN) |
143 } | 126 } |
144 | 127 |
145 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 128 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
146 const std::string& printer_name) { | 129 const std::string& printer_name) { |
147 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 130 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
148 SERVICE_UTILITY_CAPS_REQUEST, | 131 SERVICE_UTILITY_CAPS_REQUEST, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", | 224 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", |
242 base::Time::Now() - start_time_); | 225 base::Time::Now() - start_time_); |
243 } | 226 } |
244 delete this; | 227 delete this; |
245 } | 228 } |
246 | 229 |
247 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 230 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
248 bool handled = true; | 231 bool handled = true; |
249 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) | 232 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) |
250 IPC_MESSAGE_HANDLER( | 233 IPC_MESSAGE_HANDLER( |
251 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, | 234 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, |
252 OnRenderPDFPagesToMetafileSucceeded) | 235 OnRenderPDFPagesToMetafilesSucceeded) |
253 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | 236 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, |
254 OnRenderPDFPagesToMetafileFailed) | 237 OnRenderPDFPagesToMetafileFailed) |
255 IPC_MESSAGE_HANDLER( | 238 IPC_MESSAGE_HANDLER( |
256 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, | 239 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, |
257 OnGetPrinterCapsAndDefaultsSucceeded) | 240 OnGetPrinterCapsAndDefaultsSucceeded) |
258 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, | 241 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, |
259 OnGetPrinterCapsAndDefaultsFailed) | 242 OnGetPrinterCapsAndDefaultsFailed) |
260 IPC_MESSAGE_HANDLER( | 243 IPC_MESSAGE_HANDLER( |
261 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, | 244 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, |
262 OnGetPrinterSemanticCapsAndDefaultsSucceeded) | 245 OnGetPrinterSemanticCapsAndDefaultsSucceeded) |
263 IPC_MESSAGE_HANDLER( | 246 IPC_MESSAGE_HANDLER( |
264 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, | 247 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, |
265 OnGetPrinterSemanticCapsAndDefaultsFailed) | 248 OnGetPrinterSemanticCapsAndDefaultsFailed) |
266 IPC_MESSAGE_UNHANDLED(handled = false) | 249 IPC_MESSAGE_UNHANDLED(handled = false) |
267 IPC_END_MESSAGE_MAP() | 250 IPC_END_MESSAGE_MAP() |
268 return handled; | 251 return handled; |
269 } | 252 } |
270 | 253 |
271 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { | 254 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { |
272 return handle_; | 255 return handle_; |
273 } | 256 } |
274 | 257 |
275 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileSucceeded( | 258 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesSucceeded( |
276 int highest_rendered_page_number, | 259 const std::vector<printing::PageRange>& page_ranges, |
277 double scale_factor) { | 260 double scale_factor) { |
278 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 261 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
279 SERVICE_UTILITY_METAFILE_SUCCEEDED, | 262 SERVICE_UTILITY_METAFILE_SUCCEEDED, |
280 SERVICE_UTILITY_EVENT_MAX); | 263 SERVICE_UTILITY_EVENT_MAX); |
281 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", | 264 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", |
282 base::Time::Now() - start_time_); | 265 base::Time::Now() - start_time_); |
283 DCHECK(waiting_for_reply_); | 266 DCHECK(waiting_for_reply_); |
284 waiting_for_reply_ = false; | 267 waiting_for_reply_ = false; |
285 // If the metafile was successfully created, we need to take our hands off the | 268 // If the metafile was successfully created, we need to take our hands off the |
286 // scratch metafile directory. The client will delete it when it is done with | 269 // scratch metafile directory. The client will delete it when it is done with |
287 // metafile. | 270 // metafile. |
288 scratch_metafile_dir_->Take(); | 271 scratch_metafile_dir_->Take(); |
| 272 |
| 273 // TODO(vitalybuka|scottmg): http://crbug.com/170859: Currently, only one |
| 274 // page is printed at a time. This would need to be refactored to change |
| 275 // this. |
| 276 CHECK_EQ(1u, page_ranges.size()); |
| 277 CHECK_EQ(page_ranges[0].from, page_ranges[0].to); |
| 278 int page_number = page_ranges[0].from; |
289 client_message_loop_proxy_->PostTask( | 279 client_message_loop_proxy_->PostTask( |
290 FROM_HERE, | 280 FROM_HERE, |
291 base::Bind(&Client::MetafileAvailable, client_.get(), metafile_path_, | 281 base::Bind(&Client::MetafileAvailable, |
292 highest_rendered_page_number, scale_factor)); | 282 client_.get(), |
| 283 metafile_path_.InsertBeforeExtensionASCII( |
| 284 base::StringPrintf(".%d", page_number)), |
| 285 page_number, |
| 286 scale_factor)); |
293 } | 287 } |
294 | 288 |
295 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { | 289 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { |
296 DCHECK(waiting_for_reply_); | 290 DCHECK(waiting_for_reply_); |
297 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 291 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
298 SERVICE_UTILITY_METAFILE_FAILED, | 292 SERVICE_UTILITY_METAFILE_FAILED, |
299 SERVICE_UTILITY_EVENT_MAX); | 293 SERVICE_UTILITY_EVENT_MAX); |
300 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", | 294 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", |
301 base::Time::Now() - start_time_); | 295 base::Time::Now() - start_time_); |
302 waiting_for_reply_ = false; | 296 waiting_for_reply_ = false; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 if (!metafile.InitFromFile(metafile_path)) { | 379 if (!metafile.InitFromFile(metafile_path)) { |
386 OnRenderPDFPagesToMetafileFailed(); | 380 OnRenderPDFPagesToMetafileFailed(); |
387 } else { | 381 } else { |
388 OnRenderPDFPagesToMetafileSucceeded(metafile, | 382 OnRenderPDFPagesToMetafileSucceeded(metafile, |
389 highest_rendered_page_number, | 383 highest_rendered_page_number, |
390 scale_factor); | 384 scale_factor); |
391 } | 385 } |
392 #endif // defined(OS_WIN) | 386 #endif // defined(OS_WIN) |
393 } | 387 } |
394 | 388 |
OLD | NEW |