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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 | 215 |
| 216 bool PrintJob::is_job_pending() const { | 216 bool PrintJob::is_job_pending() const { |
| 217 return is_job_pending_; | 217 return is_job_pending_; |
| 218 } | 218 } |
| 219 | 219 |
| 220 PrintedDocument* PrintJob::document() const { | 220 PrintedDocument* PrintJob::document() const { |
| 221 return document_.get(); | 221 return document_.get(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
| 225 | 225 class PrintJob::PdfConversionState { |
| 226 class PrintJob::PdfToEmfState { | |
| 227 public: | 226 public: |
| 228 PdfToEmfState(const gfx::Size& page_size, const gfx::Rect& content_area) | 227 PdfConversionState() |
| 229 : page_count_(0), | 228 : page_count_(0), |
| 230 current_page_(0), | 229 current_page_(0), |
| 231 pages_in_progress_(0), | 230 pages_in_progress_(0), |
| 232 page_size_(page_size), | 231 page_size_(gfx::Size()), |
| 233 content_area_(content_area), | 232 content_area_(gfx::Rect()) {} |
| 234 converter_(PdfConverter::CreatePdfToEmfConverter()) {} | |
| 235 | 233 |
| 236 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 234 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
| 237 const PdfRenderSettings& conversion_settings, | 235 const PdfRenderSettings& conversion_settings, |
| 238 bool print_text_with_gdi, | 236 bool print_text_with_gdi, |
| 239 const PdfConverter::StartCallback& start_callback) { | 237 const PdfConverter::StartCallback& start_callback) { |
| 240 converter_->Start(data, conversion_settings, print_text_with_gdi, | 238 converter_->Start(data, conversion_settings, print_text_with_gdi, |
| 241 start_callback); | 239 start_callback); |
| 242 } | 240 } |
| 243 | 241 |
| 244 void GetMorePages(const PdfConverter::GetPageCallback& get_page_callback) { | 242 void GetMorePages(const PdfConverter::GetPageCallback& get_page_callback) { |
| 245 const int kMaxNumberOfTempFilesPerDocument = 3; | 243 const int kMaxNumberOfTempFilesPerDocument = 3; |
| 246 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && | 244 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && |
| 247 current_page_ < page_count_) { | 245 current_page_ < page_count_) { |
| 248 ++pages_in_progress_; | 246 ++pages_in_progress_; |
| 249 converter_->GetPage(current_page_++, get_page_callback); | 247 converter_->GetPage(current_page_++, get_page_callback); |
| 250 } | 248 } |
| 251 } | 249 } |
| 252 | 250 |
| 253 void OnPageProcessed(const PdfConverter::GetPageCallback& get_page_callback) { | 251 void OnPageProcessed(const PdfConverter::GetPageCallback& get_page_callback) { |
| 254 --pages_in_progress_; | 252 --pages_in_progress_; |
| 255 GetMorePages(get_page_callback); | 253 GetMorePages(get_page_callback); |
| 256 // Release converter if we don't need this any more. | 254 // Release converter if we don't need this any more. |
| 257 if (!pages_in_progress_ && current_page_ >= page_count_) | 255 if (!pages_in_progress_ && current_page_ >= page_count_) |
| 258 converter_.reset(); | 256 converter_.reset(); |
| 259 } | 257 } |
| 260 | 258 |
| 261 void set_page_count(int page_count) { page_count_ = page_count; } | 259 void set_page_count(int page_count) { page_count_ = page_count; } |
| 262 gfx::Size page_size() const { return page_size_; } | 260 const gfx::Size& page_size() const { return page_size_; } |
| 263 gfx::Rect content_area() const { return content_area_; } | 261 const gfx::Rect& content_area() const { return content_area_; } |
| 264 | 262 |
| 265 private: | 263 protected: |
|
Vitaly Buka (NO REVIEWS)
2017/01/20 08:36:47
I don't reason for exception here
https://google.g
rbpotter
2017/01/20 16:51:32
Done.
| |
| 266 int page_count_; | 264 int page_count_; |
| 267 int current_page_; | 265 int current_page_; |
| 268 int pages_in_progress_; | 266 int pages_in_progress_; |
| 269 gfx::Size page_size_; | 267 gfx::Size page_size_; |
| 270 gfx::Rect content_area_; | 268 gfx::Rect content_area_; |
| 271 std::unique_ptr<PdfConverter> converter_; | 269 std::unique_ptr<PdfConverter> converter_; |
| 272 }; | 270 }; |
| 273 | 271 |
| 272 class PrintJob::PdfToEmfState : public PrintJob::PdfConversionState { | |
| 273 public: | |
| 274 PdfToEmfState(const gfx::Size& page_size, const gfx::Rect& content_area) { | |
| 275 page_size_ = page_size; | |
|
Vitaly Buka (NO REVIEWS)
2017/01/20 08:36:47
Why not to just remove this class and
use somethi
rbpotter
2017/01/20 16:51:32
Done.
| |
| 276 content_area_ = content_area; | |
| 277 converter_.reset(PdfConverter::CreatePdfToEmfConverter().release()); | |
| 278 } | |
| 279 }; | |
| 280 | |
| 274 void PrintJob::AppendPrintedPage(int page_number) { | 281 void PrintJob::AppendPrintedPage(int page_number) { |
| 275 pdf_page_mapping_.push_back(page_number); | 282 pdf_page_mapping_.push_back(page_number); |
| 276 } | 283 } |
| 277 | 284 |
| 278 void PrintJob::StartPdfToEmfConversion( | 285 void PrintJob::StartPdfToEmfConversion( |
| 279 const scoped_refptr<base::RefCountedMemory>& bytes, | 286 const scoped_refptr<base::RefCountedMemory>& bytes, |
| 280 const gfx::Size& page_size, | 287 const gfx::Size& page_size, |
| 281 const gfx::Rect& content_area, | 288 const gfx::Rect& content_area, |
| 282 bool print_text_with_gdi) { | 289 bool print_text_with_gdi) { |
| 283 DCHECK(!pdf_to_emf_state_); | 290 DCHECK(!pdf_conversion_state_); |
| 284 pdf_to_emf_state_ = base::MakeUnique<PdfToEmfState>(page_size, content_area); | 291 pdf_conversion_state_ = |
| 292 base::MakeUnique<PdfToEmfState>(page_size, content_area); | |
| 285 const int kPrinterDpi = settings().dpi(); | 293 const int kPrinterDpi = settings().dpi(); |
| 286 pdf_to_emf_state_->Start( | 294 PdfRenderSettings settings(content_area, kPrinterDpi, true /* autorotate? */); |
| 287 bytes, PdfRenderSettings(content_area, kPrinterDpi, true), | 295 pdf_conversion_state_->Start( |
| 288 print_text_with_gdi, base::Bind(&PrintJob::OnPdfToEmfStarted, this)); | 296 bytes, settings, print_text_with_gdi, |
| 297 base::Bind(&PrintJob::OnPdfConversionStarted, this)); | |
| 289 } | 298 } |
| 290 | 299 |
| 291 void PrintJob::OnPdfToEmfStarted(int page_count) { | 300 void PrintJob::OnPdfConversionStarted(int page_count) { |
| 292 if (page_count <= 0) { | 301 if (page_count <= 0) { |
| 293 pdf_to_emf_state_.reset(); | 302 pdf_conversion_state_.reset(); |
| 294 Cancel(); | 303 Cancel(); |
| 295 return; | 304 return; |
| 296 } | 305 } |
| 297 pdf_to_emf_state_->set_page_count(page_count); | 306 pdf_conversion_state_->set_page_count(page_count); |
| 298 pdf_to_emf_state_->GetMorePages( | 307 pdf_conversion_state_->GetMorePages( |
| 299 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 308 base::Bind(&PrintJob::OnPdfPageConverted, this)); |
| 300 } | 309 } |
| 301 | 310 |
| 302 void PrintJob::OnPdfToEmfPageConverted(int page_number, | 311 void PrintJob::OnPdfPageConverted(int page_number, |
| 303 float scale_factor, | 312 float scale_factor, |
| 304 std::unique_ptr<MetafilePlayer> emf) { | 313 std::unique_ptr<MetafilePlayer> metafile) { |
| 305 DCHECK(pdf_to_emf_state_); | 314 DCHECK(pdf_conversion_state_); |
| 306 if (!document_.get() || !emf || page_number < 0 || | 315 if (!document_.get() || !metafile || page_number < 0 || |
| 307 static_cast<size_t>(page_number) >= pdf_page_mapping_.size()) { | 316 static_cast<size_t>(page_number) >= pdf_page_mapping_.size()) { |
| 308 pdf_to_emf_state_.reset(); | 317 pdf_conversion_state_.reset(); |
| 309 Cancel(); | 318 Cancel(); |
| 310 return; | 319 return; |
| 311 } | 320 } |
| 312 | 321 |
| 313 // Update the rendered document. It will send notifications to the listener. | 322 // Update the rendered document. It will send notifications to the listener. |
| 314 document_->SetPage(pdf_page_mapping_[page_number], std::move(emf), | 323 document_->SetPage(pdf_page_mapping_[page_number], std::move(metafile), |
| 315 scale_factor, pdf_to_emf_state_->page_size(), | 324 scale_factor, pdf_conversion_state_->page_size(), |
| 316 pdf_to_emf_state_->content_area()); | 325 pdf_conversion_state_->content_area()); |
| 317 | 326 |
| 318 pdf_to_emf_state_->GetMorePages( | 327 pdf_conversion_state_->GetMorePages( |
| 319 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 328 base::Bind(&PrintJob::OnPdfPageConverted, this)); |
| 320 } | 329 } |
| 321 | 330 #endif // defined(OS_WIN) |
| 322 #endif // OS_WIN | |
| 323 | 331 |
| 324 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { | 332 void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) { |
| 325 if (document_.get() == new_document) | 333 if (document_.get() == new_document) |
| 326 return; | 334 return; |
| 327 | 335 |
| 328 document_ = new_document; | 336 document_ = new_document; |
| 329 | 337 |
| 330 if (document_.get()) | 338 if (document_.get()) |
| 331 settings_ = document_->settings(); | 339 settings_ = document_->settings(); |
| 332 | 340 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 363 break; | 371 break; |
| 364 } | 372 } |
| 365 case JobEventDetails::DOC_DONE: { | 373 case JobEventDetails::DOC_DONE: { |
| 366 // This will call Stop() and broadcast a JOB_DONE message. | 374 // This will call Stop() and broadcast a JOB_DONE message. |
| 367 base::ThreadTaskRunnerHandle::Get()->PostTask( | 375 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 368 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); | 376 FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this)); |
| 369 break; | 377 break; |
| 370 } | 378 } |
| 371 case JobEventDetails::PAGE_DONE: | 379 case JobEventDetails::PAGE_DONE: |
| 372 #if defined(OS_WIN) | 380 #if defined(OS_WIN) |
| 373 pdf_to_emf_state_->OnPageProcessed( | 381 if (pdf_conversion_state_) { |
| 374 base::Bind(&PrintJob::OnPdfToEmfPageConverted, this)); | 382 pdf_conversion_state_->OnPageProcessed( |
| 383 base::Bind(&PrintJob::OnPdfPageConverted, this)); | |
| 384 } | |
| 375 #endif // defined(OS_WIN) | 385 #endif // defined(OS_WIN) |
| 376 break; | 386 break; |
| 377 default: { | 387 default: { |
| 378 NOTREACHED(); | 388 NOTREACHED(); |
| 379 break; | 389 break; |
| 380 } | 390 } |
| 381 } | 391 } |
| 382 } | 392 } |
| 383 | 393 |
| 384 void PrintJob::OnDocumentDone() { | 394 void PrintJob::OnDocumentDone() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 455 } | 465 } |
| 456 | 466 |
| 457 JobEventDetails::~JobEventDetails() { | 467 JobEventDetails::~JobEventDetails() { |
| 458 } | 468 } |
| 459 | 469 |
| 460 PrintedDocument* JobEventDetails::document() const { return document_.get(); } | 470 PrintedDocument* JobEventDetails::document() const { return document_.get(); } |
| 461 | 471 |
| 462 PrintedPage* JobEventDetails::page() const { return page_.get(); } | 472 PrintedPage* JobEventDetails::page() const { return page_.get(); } |
| 463 | 473 |
| 464 } // namespace printing | 474 } // namespace printing |
| OLD | NEW |