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/browser/printing/print_job.h" | 5 #include "chrome/browser/printing/print_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/printing/print_job_worker.h" | 14 #include "chrome/browser/printing/print_job_worker.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
17 #include "printing/printed_document.h" | 17 #include "printing/printed_document.h" |
18 #include "printing/printed_page.h" | 18 #include "printing/printed_page.h" |
19 | 19 |
| 20 #if defined(OS_WIN) |
| 21 #include "chrome/browser/printing/pdf_to_emf_converter.h" |
| 22 #include "printing/pdf_render_settings.h" |
| 23 #endif |
| 24 |
20 using base::TimeDelta; | 25 using base::TimeDelta; |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
24 // Helper function to ensure |owner| is valid until at least |callback| returns. | 29 // Helper function to ensure |owner| is valid until at least |callback| returns. |
25 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, | 30 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, |
26 const base::Closure& callback) { | 31 const base::Closure& callback) { |
27 callback.Run(); | 32 callback.Run(); |
28 } | 33 } |
29 | 34 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 212 } |
208 | 213 |
209 bool PrintJob::is_job_pending() const { | 214 bool PrintJob::is_job_pending() const { |
210 return is_job_pending_; | 215 return is_job_pending_; |
211 } | 216 } |
212 | 217 |
213 PrintedDocument* PrintJob::document() const { | 218 PrintedDocument* PrintJob::document() const { |
214 return document_.get(); | 219 return document_.get(); |
215 } | 220 } |
216 | 221 |
| 222 #if defined(OS_WIN) |
| 223 |
| 224 class PrintJob::PdfToEmfState { |
| 225 public: |
| 226 PdfToEmfState(const gfx::Size& page_size, const gfx::Rect& content_area) |
| 227 : page_count_(0), |
| 228 current_page_(0), |
| 229 pages_in_progress_(0), |
| 230 page_size_(page_size), |
| 231 content_area_(content_area), |
| 232 converter_(PdfToEmfConverter::CreateDefault()) {} |
| 233 |
| 234 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
| 235 const PdfRenderSettings& conversion_settings, |
| 236 const PdfToEmfConverter::StartCallback& start_callback) { |
| 237 converter_->Start(data, conversion_settings, start_callback); |
| 238 } |
| 239 |
| 240 void GetMorePages( |
| 241 const PdfToEmfConverter::GetPageCallback& get_page_callback) { |
| 242 const int kMaxNumberOfTempFilesPerDocument = 3; |
| 243 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && |
| 244 current_page_ < page_count_) { |
| 245 ++pages_in_progress_; |
| 246 converter_->GetPage(current_page_++, get_page_callback); |
| 247 } |
| 248 } |
| 249 |
| 250 void OnPageProcessed( |
| 251 const PdfToEmfConverter::GetPageCallback& get_page_callback) { |
| 252 --pages_in_progress_; |
| 253 GetMorePages(get_page_callback); |
| 254 // Release converter if we don't need this any more. |
| 255 if (!pages_in_progress_ && current_page_ >= page_count_) |
| 256 converter_.reset(); |
| 257 } |
| 258 |
| 259 void set_page_count(int page_count) { page_count_ = page_count; } |
| 260 gfx::Size page_size() const { return page_size_; } |
| 261 gfx::Rect content_area() const { return content_area_; } |
| 262 |
| 263 private: |
| 264 int page_count_; |
| 265 int current_page_; |
| 266 int pages_in_progress_; |
| 267 gfx::Size page_size_; |
| 268 gfx::Rect content_area_; |
| 269 scoped_ptr<PdfToEmfConverter> converter_; |
| 270 }; |
| 271 |
| 272 void PrintJob::StartPdfToEmfConversion( |
| 273 const scoped_refptr<base::RefCountedMemory>& bytes, |
| 274 const gfx::Size& page_size, |
| 275 const gfx::Rect& content_area) { |
| 276 DCHECK(!ptd_to_emf_state_.get()); |
| 277 ptd_to_emf_state_.reset(new PdfToEmfState(page_size, content_area)); |
| 278 const int kPrinterDpi = settings().dpi(); |
| 279 ptd_to_emf_state_->Start( |
| 280 bytes, |
| 281 printing::PdfRenderSettings(content_area, kPrinterDpi, true), |
| 282 base::Bind(&PrintJob::OnPdfToEmfStarted, this)); |
| 283 } |
| 284 |
| 285 void PrintJob::OnPdfToEmfStarted(int page_count) { |
| 286 if (page_count <= 0) { |
| 287 ptd_to_emf_state_.reset(); |
| 288 Cancel(); |
| 289 return; |
| 290 } |
| 291 ptd_to_emf_state_->set_page_count(page_count); |
| 292 ptd_to_emf_state_->GetMorePages( |
| 293 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); |
| 294 } |
| 295 |
| 296 void PrintJob::OnPdfToEmfPageConverted(int page_number, |
| 297 double scale_factor, |
| 298 scoped_ptr<MetafilePlayer> emf) { |
| 299 DCHECK(ptd_to_emf_state_); |
| 300 if (!document_.get() || !emf) { |
| 301 ptd_to_emf_state_.reset(); |
| 302 Cancel(); |
| 303 return; |
| 304 } |
| 305 |
| 306 // Update the rendered document. It will send notifications to the listener. |
| 307 document_->SetPage(page_number, |
| 308 emf.Pass(), |
| 309 scale_factor, |
| 310 ptd_to_emf_state_->page_size(), |
| 311 ptd_to_emf_state_->content_area()); |
| 312 |
| 313 ptd_to_emf_state_->GetMorePages( |
| 314 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); |
| 315 } |
| 316 |
| 317 #endif // OS_WIN |
| 318 |
217 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { | 319 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { |
218 if (document_.get() == new_document) | 320 if (document_.get() == new_document) |
219 return; | 321 return; |
220 | 322 |
221 document_ = new_document; | 323 document_ = new_document; |
222 | 324 |
223 if (document_.get()) { | 325 if (document_.get()) { |
224 settings_ = document_->settings(); | 326 settings_ = document_->settings(); |
225 } | 327 } |
226 | 328 |
(...skipping 18 matching lines...) Expand all Loading... |
245 break; | 347 break; |
246 } | 348 } |
247 case JobEventDetails::USER_INIT_DONE: | 349 case JobEventDetails::USER_INIT_DONE: |
248 case JobEventDetails::DEFAULT_INIT_DONE: | 350 case JobEventDetails::DEFAULT_INIT_DONE: |
249 case JobEventDetails::USER_INIT_CANCELED: { | 351 case JobEventDetails::USER_INIT_CANCELED: { |
250 DCHECK_EQ(event_details.document(), document_.get()); | 352 DCHECK_EQ(event_details.document(), document_.get()); |
251 break; | 353 break; |
252 } | 354 } |
253 case JobEventDetails::NEW_DOC: | 355 case JobEventDetails::NEW_DOC: |
254 case JobEventDetails::NEW_PAGE: | 356 case JobEventDetails::NEW_PAGE: |
255 case JobEventDetails::PAGE_DONE: | |
256 case JobEventDetails::JOB_DONE: | 357 case JobEventDetails::JOB_DONE: |
257 case JobEventDetails::ALL_PAGES_REQUESTED: { | 358 case JobEventDetails::ALL_PAGES_REQUESTED: { |
258 // Don't care. | 359 // Don't care. |
259 break; | 360 break; |
260 } | 361 } |
261 case JobEventDetails::DOC_DONE: { | 362 case JobEventDetails::DOC_DONE: { |
262 // This will call Stop() and broadcast a JOB_DONE message. | 363 // This will call Stop() and broadcast a JOB_DONE message. |
263 base::MessageLoop::current()->PostTask( | 364 base::MessageLoop::current()->PostTask( |
264 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); | 365 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); |
265 break; | 366 break; |
266 } | 367 } |
| 368 case JobEventDetails::PAGE_DONE: |
| 369 #if defined(OS_WIN) |
| 370 ptd_to_emf_state_->OnPageProcessed( |
| 371 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); |
| 372 #endif // OS_WIN |
| 373 break; |
267 default: { | 374 default: { |
268 NOTREACHED(); | 375 NOTREACHED(); |
269 break; | 376 break; |
270 } | 377 } |
271 } | 378 } |
272 } | 379 } |
273 | 380 |
274 void PrintJob::OnDocumentDone() { | 381 void PrintJob::OnDocumentDone() { |
275 // Be sure to live long enough. The instance could be destroyed by the | 382 // Be sure to live long enough. The instance could be destroyed by the |
276 // JOB_DONE broadcast. | 383 // JOB_DONE broadcast. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 } | 453 } |
347 | 454 |
348 JobEventDetails::~JobEventDetails() { | 455 JobEventDetails::~JobEventDetails() { |
349 } | 456 } |
350 | 457 |
351 PrintedDocument* JobEventDetails::document() const { return document_.get(); } | 458 PrintedDocument* JobEventDetails::document() const { return document_.get(); } |
352 | 459 |
353 PrintedPage* JobEventDetails::page() const { return page_.get(); } | 460 PrintedPage* JobEventDetails::page() const { return page_.get(); } |
354 | 461 |
355 } // namespace printing | 462 } // namespace printing |
OLD | NEW |