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 <memory> | 7 #include <memory> | 
| 8 #include <utility> | 8 #include <utility> | 
| 9 | 9 | 
| 10 #include "base/bind.h" | 10 #include "base/bind.h" | 
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 PdfToEmfState(const gfx::Size& page_size, const gfx::Rect& content_area) | 228 PdfToEmfState(const gfx::Size& page_size, const gfx::Rect& content_area) | 
| 229 : page_count_(0), | 229 : page_count_(0), | 
| 230 current_page_(0), | 230 current_page_(0), | 
| 231 pages_in_progress_(0), | 231 pages_in_progress_(0), | 
| 232 page_size_(page_size), | 232 page_size_(page_size), | 
| 233 content_area_(content_area), | 233 content_area_(content_area), | 
| 234 converter_(PdfToEmfConverter::CreateDefault()) {} | 234 converter_(PdfToEmfConverter::CreateDefault()) {} | 
| 235 | 235 | 
| 236 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 236 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 
| 237 const PdfRenderSettings& conversion_settings, | 237 const PdfRenderSettings& conversion_settings, | 
| 238 bool print_text_with_gdi, | |
| 239 const PdfToEmfConverter::StartCallback& start_callback) { | 238 const PdfToEmfConverter::StartCallback& start_callback) { | 
| 240 converter_->Start(data, conversion_settings, print_text_with_gdi, | 239 converter_->Start(data, conversion_settings, start_callback); | 
| 241 start_callback); | |
| 242 } | 240 } | 
| 243 | 241 | 
| 244 void GetMorePages( | 242 void GetMorePages( | 
| 245 const PdfToEmfConverter::GetPageCallback& get_page_callback) { | 243 const PdfToEmfConverter::GetPageCallback& get_page_callback) { | 
| 246 const int kMaxNumberOfTempFilesPerDocument = 3; | 244 const int kMaxNumberOfTempFilesPerDocument = 3; | 
| 247 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && | 245 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && | 
| 248 current_page_ < page_count_) { | 246 current_page_ < page_count_) { | 
| 249 ++pages_in_progress_; | 247 ++pages_in_progress_; | 
| 250 converter_->GetPage(current_page_++, get_page_callback); | 248 converter_->GetPage(current_page_++, get_page_callback); | 
| 251 } | 249 } | 
| 252 } | 250 } | 
| 253 | 251 | 
| 254 void OnPageProcessed( | 252 void OnPageProcessed( | 
| 255 const PdfToEmfConverter::GetPageCallback& get_page_callback) { | 253 const PdfToEmfConverter::GetPageCallback& get_page_callback) { | 
| 256 --pages_in_progress_; | 254 --pages_in_progress_; | 
| 257 GetMorePages(get_page_callback); | 255 GetMorePages(get_page_callback); | 
| 258 // Release converter if we don't need this any more. | 256 // Release converter if we don't need this any more. | 
| 259 if (!pages_in_progress_ && current_page_ >= page_count_) | 257 if (!pages_in_progress_ && current_page_ >= page_count_) | 
| 260 converter_.reset(); | 258 converter_.reset(); | 
| 261 } | 259 } | 
| 262 | 260 | 
| 263 void set_page_count(int page_count) { page_count_ = page_count; } | 261 void set_page_count(int page_count) { page_count_ = page_count; } | 
| 264 gfx::Size page_size() const { return page_size_; } | 262 const gfx::Size& page_size() const { return page_size_; } | 
| 265 gfx::Rect content_area() const { return content_area_; } | 263 const gfx::Rect& content_area() const { return content_area_; } | 
| 266 | 264 | 
| 267 private: | 265 private: | 
| 268 int page_count_; | 266 int page_count_; | 
| 269 int current_page_; | 267 int current_page_; | 
| 270 int pages_in_progress_; | 268 int pages_in_progress_; | 
| 271 gfx::Size page_size_; | 269 gfx::Size page_size_; | 
| 272 gfx::Rect content_area_; | 270 gfx::Rect content_area_; | 
| 273 std::unique_ptr<PdfToEmfConverter> converter_; | 271 std::unique_ptr<PdfToEmfConverter> converter_; | 
| 274 }; | 272 }; | 
| 275 | 273 | 
| 274 class PrintJob::PdfToPostScriptState { | |
| 275 public: | |
| 276 PdfToPostScriptState() | |
| 277 : page_count_(0), | |
| 278 current_page_(0), | |
| 279 pages_in_progress_(0), | |
| 280 converter_(PdfToPostScriptConverter::CreateDefault()) {} | |
| 281 | |
| 282 void Start(const scoped_refptr<base::RefCountedMemory>& data, | |
| 283 const PdfRenderSettings& conversion_settings, | |
| 284 const PdfToPostScriptConverter::StartCallback& start_callback) { | |
| 285 converter_->Start(data, conversion_settings, start_callback); | |
| 286 } | |
| 287 | |
| 288 void GetMorePages( | |
| 289 const PdfToPostScriptConverter::GetPageCallback& get_page_callback) { | |
| 290 const int kMaxNumberOfTempFilesPerDocument = 3; | |
| 291 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && | |
| 292 current_page_ < page_count_) { | |
| 293 ++pages_in_progress_; | |
| 294 converter_->GetPage(current_page_++, get_page_callback); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 void OnPageProcessed( | |
| 299 const PdfToPostScriptConverter::GetPageCallback& get_page_callback) { | |
| 300 --pages_in_progress_; | |
| 301 GetMorePages(get_page_callback); | |
| 302 // Release converter if we don't need this any more. | |
| 303 if (!pages_in_progress_ && current_page_ >= page_count_) | |
| 304 converter_.reset(); | |
| 
 
dsinclair
2017/01/17 17:10:00
Shoudl this be .reset(PdfToPostScriptConverter::Cr
 
 | |
| 305 } | |
| 306 | |
| 307 void set_page_count(int page_count) { page_count_ = page_count; } | |
| 308 | |
| 309 private: | |
| 310 int page_count_; | |
| 311 int current_page_; | |
| 312 int pages_in_progress_; | |
| 313 std::unique_ptr<PdfToPostScriptConverter> converter_; | |
| 314 }; | |
| 315 | |
| 276 void PrintJob::AppendPrintedPage(int page_number) { | 316 void PrintJob::AppendPrintedPage(int page_number) { | 
| 277 pdf_page_mapping_.push_back(page_number); | 317 pdf_page_mapping_.push_back(page_number); | 
| 278 } | 318 } | 
| 279 | 319 | 
| 280 void PrintJob::StartPdfToEmfConversion( | 320 void PrintJob::StartPdfToEmfConversion( | 
| 281 const scoped_refptr<base::RefCountedMemory>& bytes, | 321 const scoped_refptr<base::RefCountedMemory>& bytes, | 
| 282 const gfx::Size& page_size, | 322 const gfx::Size& page_size, | 
| 283 const gfx::Rect& content_area, | 323 const gfx::Rect& content_area, | 
| 284 bool print_text_with_gdi) { | 324 bool print_text_with_gdi) { | 
| 285 DCHECK(!pdf_to_emf_state_); | 325 DCHECK(!pdf_to_emf_state_); | 
| 286 pdf_to_emf_state_ = base::MakeUnique<PdfToEmfState>(page_size, content_area); | 326 pdf_to_emf_state_ = base::MakeUnique<PdfToEmfState>(page_size, content_area); | 
| 287 const int kPrinterDpi = settings().dpi(); | 327 const int kPrinterDpi = settings().dpi(); | 
| 288 pdf_to_emf_state_->Start( | 328 PdfRenderSettings settings( | 
| 289 bytes, PdfRenderSettings(content_area, kPrinterDpi, true), | 329 content_area, gfx::Point(0, 0), kPrinterDpi, true /* autorotate? */, | 
| 290 print_text_with_gdi, base::Bind(&PrintJob::OnPdfToEmfStarted, this)); | 330 print_text_with_gdi ? PdfRenderSettings::Mode::GDI_TEXT | 
| 331 : PdfRenderSettings::Mode::NORMAL); | |
| 332 pdf_to_emf_state_->Start(bytes, settings, | |
| 333 base::Bind(&PrintJob::OnPdfToEmfStarted, this)); | |
| 291 } | 334 } | 
| 292 | 335 | 
| 293 void PrintJob::OnPdfToEmfStarted(int page_count) { | 336 void PrintJob::OnPdfToEmfStarted(int page_count) { | 
| 294 if (page_count <= 0) { | 337 if (page_count <= 0) { | 
| 295 pdf_to_emf_state_.reset(); | 338 pdf_to_emf_state_.reset(); | 
| 296 Cancel(); | 339 Cancel(); | 
| 297 return; | 340 return; | 
| 298 } | 341 } | 
| 299 pdf_to_emf_state_->set_page_count(page_count); | 342 pdf_to_emf_state_->set_page_count(page_count); | 
| 300 pdf_to_emf_state_->GetMorePages( | 343 pdf_to_emf_state_->GetMorePages( | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 314 | 357 | 
| 315 // Update the rendered document. It will send notifications to the listener. | 358 // Update the rendered document. It will send notifications to the listener. | 
| 316 document_->SetPage(pdf_page_mapping_[page_number], std::move(emf), | 359 document_->SetPage(pdf_page_mapping_[page_number], std::move(emf), | 
| 317 scale_factor, pdf_to_emf_state_->page_size(), | 360 scale_factor, pdf_to_emf_state_->page_size(), | 
| 318 pdf_to_emf_state_->content_area()); | 361 pdf_to_emf_state_->content_area()); | 
| 319 | 362 | 
| 320 pdf_to_emf_state_->GetMorePages( | 363 pdf_to_emf_state_->GetMorePages( | 
| 321 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 364 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 
| 322 } | 365 } | 
| 323 | 366 | 
| 324 #endif // OS_WIN | 367 void PrintJob::StartPdfToPostScriptConversion( | 
| 368 const scoped_refptr<base::RefCountedMemory>& bytes, | |
| 369 const gfx::Rect& content_area, | |
| 370 const gfx::Point& physical_offsets, | |
| 371 bool ps_level2) { | |
| 372 DCHECK(!pdf_to_ps_state_); | |
| 373 pdf_to_ps_state_ = base::MakeUnique<PdfToPostScriptState>(); | |
| 374 const int kPrinterDpi = settings().dpi(); | |
| 375 PdfRenderSettings settings( | |
| 376 content_area, physical_offsets, kPrinterDpi, true /* autorotate? */, | |
| 377 ps_level2 ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 | |
| 378 : PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3); | |
| 379 pdf_to_ps_state_->Start( | |
| 380 bytes, settings, base::Bind(&PrintJob::OnPdfToPostScriptStarted, this)); | |
| 381 } | |
| 382 | |
| 383 void PrintJob::OnPdfToPostScriptStarted(int page_count) { | |
| 384 if (page_count <= 0) { | |
| 385 pdf_to_ps_state_.reset(); | |
| 386 Cancel(); | |
| 387 return; | |
| 388 } | |
| 389 pdf_to_ps_state_->set_page_count(page_count); | |
| 390 pdf_to_ps_state_->GetMorePages( | |
| 391 base::Bind(&PrintJob::OnPdfToPostScriptPageConverted, this)); | |
| 392 } | |
| 393 | |
| 394 void PrintJob::OnPdfToPostScriptPageConverted( | |
| 395 int page_number, | |
| 396 std::unique_ptr<MetafilePlayer> ps_metafile) { | |
| 397 DCHECK(pdf_to_ps_state_); | |
| 398 if (!document_.get() || !ps_metafile || page_number < 0 || | |
| 399 static_cast<size_t>(page_number) >= pdf_page_mapping_.size()) { | |
| 400 pdf_to_ps_state_.reset(); | |
| 401 Cancel(); | |
| 402 return; | |
| 403 } | |
| 404 | |
| 405 // Update the rendered document. It will send notifications to the listener. | |
| 406 document_->SetPage(pdf_page_mapping_[page_number], std::move(ps_metafile), | |
| 407 0 /* dummy shrink_factor */, | |
| 408 gfx::Size() /* dummy page_size */, | |
| 409 gfx::Rect() /* dummy page_content_rect */); | |
| 410 | |
| 411 pdf_to_ps_state_->GetMorePages( | |
| 412 base::Bind(&PrintJob::OnPdfToPostScriptPageConverted, this)); | |
| 413 } | |
| 414 #endif // defined(OS_WIN) | |
| 325 | 415 | 
| 326 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { | 416 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { | 
| 327 if (document_.get() == new_document) | 417 if (document_.get() == new_document) | 
| 328 return; | 418 return; | 
| 329 | 419 | 
| 330 document_ = new_document; | 420 document_ = new_document; | 
| 331 | 421 | 
| 332 if (document_.get()) | 422 if (document_.get()) | 
| 333 settings_ = document_->settings(); | 423 settings_ = document_->settings(); | 
| 334 | 424 | 
| (...skipping 30 matching lines...) Expand all Loading... | |
| 365 break; | 455 break; | 
| 366 } | 456 } | 
| 367 case JobEventDetails::DOC_DONE: { | 457 case JobEventDetails::DOC_DONE: { | 
| 368 // This will call Stop() and broadcast a JOB_DONE message. | 458 // This will call Stop() and broadcast a JOB_DONE message. | 
| 369 base::ThreadTaskRunnerHandle::Get()->PostTask( | 459 base::ThreadTaskRunnerHandle::Get()->PostTask( | 
| 370 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); | 460 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); | 
| 371 break; | 461 break; | 
| 372 } | 462 } | 
| 373 case JobEventDetails::PAGE_DONE: | 463 case JobEventDetails::PAGE_DONE: | 
| 374 #if defined(OS_WIN) | 464 #if defined(OS_WIN) | 
| 375 pdf_to_emf_state_->OnPageProcessed( | 465 if (pdf_to_emf_state_) { | 
| 376 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 466 pdf_to_emf_state_->OnPageProcessed( | 
| 377 #endif // defined(OS_WIN) | 467 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 
| 468 } else { | |
| 469 pdf_to_ps_state_->OnPageProcessed( | |
| 470 base::Bind(&PrintJob::OnPdfToPostScriptPageConverted, this)); | |
| 471 } | |
| 472 #endif | |
| 378 break; | 473 break; | 
| 379 default: { | 474 default: { | 
| 380 NOTREACHED(); | 475 NOTREACHED(); | 
| 381 break; | 476 break; | 
| 382 } | 477 } | 
| 383 } | 478 } | 
| 384 } | 479 } | 
| 385 | 480 | 
| 386 void PrintJob::OnDocumentDone() { | 481 void PrintJob::OnDocumentDone() { | 
| 387 // Be sure to live long enough. The instance could be destroyed by the | 482 // Be sure to live long enough. The instance could be destroyed by the | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 } | 552 } | 
| 458 | 553 | 
| 459 JobEventDetails::~JobEventDetails() { | 554 JobEventDetails::~JobEventDetails() { | 
| 460 } | 555 } | 
| 461 | 556 | 
| 462 PrintedDocument* JobEventDetails::document() const { return document_.get(); } | 557 PrintedDocument* JobEventDetails::document() const { return document_.get(); } | 
| 463 | 558 | 
| 464 PrintedPage* JobEventDetails::page() const { return page_.get(); } | 559 PrintedPage* JobEventDetails::page() const { return page_.get(); } | 
| 465 | 560 | 
| 466 } // namespace printing | 561 } // namespace printing | 
| OLD | NEW |