| 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 <queue> | 7 #include <queue> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 typedef scoped_ptr<base::File, BrowserThread::DeleteOnFileThread> | 53 typedef scoped_ptr<base::File, BrowserThread::DeleteOnFileThread> |
| 54 ScopedTempFile; | 54 ScopedTempFile; |
| 55 | 55 |
| 56 // Wrapper for Emf to keep only file handle in memory, and load actual data only | 56 // Wrapper for Emf to keep only file handle in memory, and load actual data only |
| 57 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it | 57 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it |
| 58 // can't open file handles. We need file handles to reliably delete temporary | 58 // can't open file handles. We need file handles to reliably delete temporary |
| 59 // files, and to efficiently interact with utility process. | 59 // files, and to efficiently interact with utility process. |
| 60 class LazyEmf : public MetafilePlayer { | 60 class LazyEmf : public MetafilePlayer { |
| 61 public: | 61 public: |
| 62 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file) | 62 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file) |
| 63 : temp_dir_(temp_dir), file_(file.Pass()) {} | 63 : temp_dir_(temp_dir), file_(file.Pass()) { |
| 64 CHECK(file_); |
| 65 } |
| 64 ~LazyEmf() override { Close(); } | 66 ~LazyEmf() override { Close(); } |
| 65 | 67 |
| 66 bool SafePlayback(HDC hdc) const override; | 68 bool SafePlayback(HDC hdc) const override; |
| 67 bool SaveTo(base::File* file) const override; | 69 bool SaveTo(base::File* file) const override; |
| 68 | 70 |
| 69 private: | 71 private: |
| 70 void Close() const; | 72 void Close() const; |
| 71 bool LoadEmf(Emf* emf) const; | 73 bool LoadEmf(Emf* emf) const; |
| 72 | 74 |
| 73 mutable scoped_refptr<RefCountedTempDir> temp_dir_; | 75 mutable scoped_refptr<RefCountedTempDir> temp_dir_; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 page_number_ = rhs.object->page_number_; | 128 page_number_ = rhs.object->page_number_; |
| 127 callback_ = rhs.object->callback_; | 129 callback_ = rhs.object->callback_; |
| 128 emf_ = rhs.object->emf_.Pass(); | 130 emf_ = rhs.object->emf_.Pass(); |
| 129 return *this; | 131 return *this; |
| 130 } | 132 } |
| 131 | 133 |
| 132 int page_number() const { return page_number_; } | 134 int page_number() const { return page_number_; } |
| 133 const PdfToEmfConverter::GetPageCallback& callback() const { | 135 const PdfToEmfConverter::GetPageCallback& callback() const { |
| 134 return callback_; | 136 return callback_; |
| 135 } | 137 } |
| 136 ScopedTempFile emf() { return emf_.Pass(); } | 138 ScopedTempFile TakeEmf() { return emf_.Pass(); } |
| 137 void set_emf(ScopedTempFile emf) { emf_ = emf.Pass(); } | 139 void set_emf(ScopedTempFile emf) { emf_ = emf.Pass(); } |
| 138 | 140 |
| 139 private: | 141 private: |
| 140 int page_number_; | 142 int page_number_; |
| 141 PdfToEmfConverter::GetPageCallback callback_; | 143 PdfToEmfConverter::GetPageCallback callback_; |
| 142 ScopedTempFile emf_; | 144 ScopedTempFile emf_; |
| 143 }; | 145 }; |
| 144 | 146 |
| 145 ~PdfToEmfUtilityProcessHostClient() override; | 147 ~PdfToEmfUtilityProcessHostClient() override; |
| 146 | 148 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // Should reply with OnPageDone(). | 393 // Should reply with OnPageDone(). |
| 392 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( | 394 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( |
| 393 callback_data->page_number(), transit)); | 395 callback_data->page_number(), transit)); |
| 394 } | 396 } |
| 395 | 397 |
| 396 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, | 398 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, |
| 397 float scale_factor) { | 399 float scale_factor) { |
| 398 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 400 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 399 if (get_page_callbacks_.empty()) | 401 if (get_page_callbacks_.empty()) |
| 400 return OnFailed(); | 402 return OnFailed(); |
| 403 GetPageCallbackData& data = get_page_callbacks_.front(); |
| 401 scoped_ptr<MetafilePlayer> emf; | 404 scoped_ptr<MetafilePlayer> emf; |
| 402 GetPageCallbackData& data = get_page_callbacks_.front(); | 405 |
| 403 if (success) | 406 if (success) { |
| 404 emf.reset(new LazyEmf(temp_dir_, data.emf().Pass())); | 407 ScopedTempFile temp_emf = data.TakeEmf(); |
| 408 if (!temp_emf) // Unexpected message from utility process. |
| 409 return OnFailed(); |
| 410 emf.reset(new LazyEmf(temp_dir_, temp_emf.Pass())); |
| 411 } |
| 412 |
| 405 BrowserThread::PostTask(BrowserThread::UI, | 413 BrowserThread::PostTask(BrowserThread::UI, |
| 406 FROM_HERE, | 414 FROM_HERE, |
| 407 base::Bind(&PdfToEmfConverterImpl::RunCallback, | 415 base::Bind(&PdfToEmfConverterImpl::RunCallback, |
| 408 converter_, | 416 converter_, |
| 409 base::Bind(data.callback(), | 417 base::Bind(data.callback(), |
| 410 data.page_number(), | 418 data.page_number(), |
| 411 scale_factor, | 419 scale_factor, |
| 412 base::Passed(&emf)))); | 420 base::Passed(&emf)))); |
| 413 get_page_callbacks_.pop(); | 421 get_page_callbacks_.pop(); |
| 414 } | 422 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 | 502 |
| 495 PdfToEmfConverter::~PdfToEmfConverter() { | 503 PdfToEmfConverter::~PdfToEmfConverter() { |
| 496 } | 504 } |
| 497 | 505 |
| 498 // static | 506 // static |
| 499 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { | 507 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { |
| 500 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); | 508 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); |
| 501 } | 509 } |
| 502 | 510 |
| 503 } // namespace printing | 511 } // namespace printing |
| OLD | NEW |