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