OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/pdf_to_emf_converter.h" | 5 #include "chrome/browser/printing/pdf_to_emf_converter.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 void SendStopMessage() override; | 254 void SendStopMessage() override; |
255 | 255 |
256 // Additional message handler needed for Pdf to PostScript | 256 // Additional message handler needed for Pdf to PostScript |
257 void OnPageDone(bool success); | 257 void OnPageDone(bool success); |
258 | 258 |
259 DISALLOW_COPY_AND_ASSIGN(PdfToPostScriptUtilityProcessHostClient); | 259 DISALLOW_COPY_AND_ASSIGN(PdfToPostScriptUtilityProcessHostClient); |
260 }; | 260 }; |
261 | 261 |
262 class PdfConverterImpl : public PdfConverter { | 262 class PdfConverterImpl : public PdfConverter { |
263 public: | 263 public: |
264 PdfConverterImpl(); | 264 PdfConverterImpl() : weak_ptr_factory_(this) {} |
265 | 265 |
266 ~PdfConverterImpl() override; | 266 base::WeakPtr<PdfConverterImpl> GetWeakPtr() { |
267 | 267 return weak_ptr_factory_.GetWeakPtr(); |
268 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 268 } |
269 const PdfRenderSettings& conversion_settings, | |
270 const StartCallback& start_callback) override; | |
271 | 269 |
272 void GetPage(int page_number, | 270 void GetPage(int page_number, |
273 const GetPageCallback& get_page_callback) override; | 271 const GetPageCallback& get_page_callback) override; |
274 | 272 |
275 // Helps to cancel callbacks if this object is destroyed. | 273 // Helps to cancel callbacks if this object is destroyed. |
276 void RunCallback(const base::Closure& callback); | 274 void RunCallback(const base::Closure& callback); |
277 | 275 |
278 protected: | 276 void Start( |
| 277 const scoped_refptr<PdfConverterUtilityProcessHostClient>& utility_client, |
| 278 const scoped_refptr<base::RefCountedMemory>& data, |
| 279 const StartCallback& start_callback) { |
| 280 DCHECK(!utility_client_); |
| 281 utility_client_ = utility_client; |
| 282 utility_client_->Start(data, start_callback); |
| 283 } |
| 284 |
| 285 private: |
279 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; | 286 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; |
280 | 287 |
281 private: | 288 base::WeakPtrFactory<PdfConverterImpl> weak_ptr_factory_; |
| 289 |
282 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); | 290 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); |
283 }; | 291 }; |
284 | 292 |
285 class PdfToEmfConverterImpl : public PdfConverterImpl { | |
286 public: | |
287 PdfToEmfConverterImpl(); | |
288 | |
289 ~PdfToEmfConverterImpl() override; | |
290 | |
291 void Start(const scoped_refptr<base::RefCountedMemory>& data, | |
292 const PdfRenderSettings& conversion_settings, | |
293 const StartCallback& start_callback) override; | |
294 | |
295 private: | |
296 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; | |
297 | |
298 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | |
299 }; | |
300 | |
301 class PdfToPostScriptConverterImpl : public PdfConverterImpl { | |
302 public: | |
303 PdfToPostScriptConverterImpl(); | |
304 ~PdfToPostScriptConverterImpl() override; | |
305 | |
306 void Start(const scoped_refptr<base::RefCountedMemory>& data, | |
307 const PdfRenderSettings& conversion_settings, | |
308 const StartCallback& start_callback) override; | |
309 | |
310 private: | |
311 base::WeakPtrFactory<PdfToPostScriptConverterImpl> weak_ptr_factory_; | |
312 DISALLOW_COPY_AND_ASSIGN(PdfToPostScriptConverterImpl); | |
313 }; | |
314 | |
315 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { | 293 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { |
316 if (!temp_dir->get()) | 294 if (!temp_dir->get()) |
317 *temp_dir = new RefCountedTempDir(); | 295 *temp_dir = new RefCountedTempDir(); |
318 ScopedTempFile file; | 296 ScopedTempFile file; |
319 if (!(*temp_dir)->IsValid()) | 297 if (!(*temp_dir)->IsValid()) |
320 return file; | 298 return file; |
321 base::FilePath path; | 299 base::FilePath path; |
322 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) { | 300 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) { |
323 PLOG(ERROR) << "Failed to create file in " | 301 PLOG(ERROR) << "Failed to create file in " |
324 << (*temp_dir)->GetPath().value(); | 302 << (*temp_dir)->GetPath().value(); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 } | 680 } |
703 | 681 |
704 void PdfToPostScriptUtilityProcessHostClient::SendStopMessage() { | 682 void PdfToPostScriptUtilityProcessHostClient::SendStopMessage() { |
705 Send(new ChromeUtilityMsg_RenderPDFPagesToPostScript_Stop()); | 683 Send(new ChromeUtilityMsg_RenderPDFPagesToPostScript_Stop()); |
706 } | 684 } |
707 | 685 |
708 void PdfToPostScriptUtilityProcessHostClient::OnPageDone(bool success) { | 686 void PdfToPostScriptUtilityProcessHostClient::OnPageDone(bool success) { |
709 PdfConverterUtilityProcessHostClient::OnPageDone(success, 0.0f); | 687 PdfConverterUtilityProcessHostClient::OnPageDone(success, 0.0f); |
710 } | 688 } |
711 | 689 |
712 // Pdf Converter Impl and subclasses | |
713 PdfConverterImpl::PdfConverterImpl() {} | |
714 | |
715 PdfConverterImpl::~PdfConverterImpl() {} | |
716 | |
717 void PdfConverterImpl::Start(const scoped_refptr<base::RefCountedMemory>& data, | |
718 const PdfRenderSettings& conversion_settings, | |
719 const StartCallback& start_callback) { | |
720 DCHECK(!utility_client_.get()); | |
721 } | |
722 | |
723 void PdfConverterImpl::GetPage(int page_number, | 690 void PdfConverterImpl::GetPage(int page_number, |
724 const GetPageCallback& get_page_callback) { | 691 const GetPageCallback& get_page_callback) { |
725 utility_client_->GetPage(page_number, get_page_callback); | 692 utility_client_->GetPage(page_number, get_page_callback); |
726 } | 693 } |
727 | 694 |
728 void PdfConverterImpl::RunCallback(const base::Closure& callback) { | 695 void PdfConverterImpl::RunCallback(const base::Closure& callback) { |
729 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 696 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
730 callback.Run(); | 697 callback.Run(); |
731 } | 698 } |
732 | 699 |
733 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) {} | |
734 | |
735 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | |
736 if (utility_client_.get()) | |
737 utility_client_->Stop(); | |
738 } | |
739 | |
740 void PdfToEmfConverterImpl::Start( | |
741 const scoped_refptr<base::RefCountedMemory>& data, | |
742 const PdfRenderSettings& conversion_settings, | |
743 const StartCallback& start_callback) { | |
744 DCHECK(!utility_client_.get()); | |
745 utility_client_ = new PdfToEmfUtilityProcessHostClient( | |
746 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | |
747 utility_client_->Start(data, start_callback); | |
748 } | |
749 | |
750 PdfToPostScriptConverterImpl::PdfToPostScriptConverterImpl() | |
751 : weak_ptr_factory_(this) {} | |
752 | |
753 PdfToPostScriptConverterImpl::~PdfToPostScriptConverterImpl() { | |
754 if (utility_client_.get()) | |
755 utility_client_->Stop(); | |
756 } | |
757 | |
758 void PdfToPostScriptConverterImpl::Start( | |
759 const scoped_refptr<base::RefCountedMemory>& data, | |
760 const PdfRenderSettings& conversion_settings, | |
761 const StartCallback& start_callback) { | |
762 DCHECK(!utility_client_.get()); | |
763 utility_client_ = new PdfToPostScriptUtilityProcessHostClient( | |
764 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | |
765 utility_client_->Start(data, start_callback); | |
766 } | |
767 | |
768 } // namespace | 700 } // namespace |
769 | 701 |
770 PdfConverter::~PdfConverter() {} | 702 PdfConverter::~PdfConverter() {} |
771 | 703 |
772 // static | 704 // static |
773 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() { | 705 std::unique_ptr<PdfConverter> PdfConverter::StartPdfToEmfConverter( |
774 return base::MakeUnique<PdfToEmfConverterImpl>(); | 706 const scoped_refptr<base::RefCountedMemory>& data, |
| 707 const PdfRenderSettings& conversion_settings, |
| 708 const StartCallback& start_callback) { |
| 709 std::unique_ptr<PdfConverterImpl> converter = |
| 710 base::MakeUnique<PdfConverterImpl>(); |
| 711 converter->Start(new PdfToEmfUtilityProcessHostClient(converter->GetWeakPtr(), |
| 712 conversion_settings), |
| 713 data, start_callback); |
| 714 return std::move(converter); |
775 } | 715 } |
776 | 716 |
777 // static | 717 // static |
778 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToPostScriptConverter() { | 718 std::unique_ptr<PdfConverter> PdfConverter::StartPdfToPostScriptConverter( |
779 return base::MakeUnique<PdfToPostScriptConverterImpl>(); | 719 const scoped_refptr<base::RefCountedMemory>& data, |
| 720 const PdfRenderSettings& conversion_settings, |
| 721 const StartCallback& start_callback) { |
| 722 std::unique_ptr<PdfConverterImpl> converter = |
| 723 base::MakeUnique<PdfConverterImpl>(); |
| 724 converter->Start(new PdfToPostScriptUtilityProcessHostClient( |
| 725 converter->GetWeakPtr(), conversion_settings), |
| 726 data, start_callback); |
| 727 return std::move(converter); |
780 } | 728 } |
781 | 729 |
782 } // namespace printing | 730 } // namespace printing |
OLD | NEW |