| 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 17 matching lines...) Expand all Loading... |
| 28 class PdfToEmfConverterImpl; | 28 class PdfToEmfConverterImpl; |
| 29 | 29 |
| 30 // Allows to delete temporary directory after all temporary files created inside | 30 // Allows to delete temporary directory after all temporary files created inside |
| 31 // are closed. Windows cannot delete directory with opened files. Directory is | 31 // are closed. Windows cannot delete directory with opened files. Directory is |
| 32 // used to store PDF and metafiles. PDF should be gone by the time utility | 32 // used to store PDF and metafiles. PDF should be gone by the time utility |
| 33 // process exits. Metafiles should be gone when all LazyEmf destroyed. | 33 // process exits. Metafiles should be gone when all LazyEmf destroyed. |
| 34 class RefCountedTempDir | 34 class RefCountedTempDir |
| 35 : public base::RefCountedThreadSafe<RefCountedTempDir, | 35 : public base::RefCountedThreadSafe<RefCountedTempDir, |
| 36 BrowserThread::DeleteOnFileThread> { | 36 BrowserThread::DeleteOnFileThread> { |
| 37 public: | 37 public: |
| 38 RefCountedTempDir() { temp_dir_.CreateUniqueTempDir(); } | 38 RefCountedTempDir() { ignore_result(temp_dir_.CreateUniqueTempDir()); } |
| 39 bool IsValid() const { return temp_dir_.IsValid(); } | 39 bool IsValid() const { return temp_dir_.IsValid(); } |
| 40 const base::FilePath& GetPath() const { return temp_dir_.path(); } | 40 const base::FilePath& GetPath() const { return temp_dir_.path(); } |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 43 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; |
| 44 friend class base::DeleteHelper<RefCountedTempDir>; | 44 friend class base::DeleteHelper<RefCountedTempDir>; |
| 45 ~RefCountedTempDir() {}; | 45 ~RefCountedTempDir() {} |
| 46 | 46 |
| 47 base::ScopedTempDir temp_dir_; | 47 base::ScopedTempDir temp_dir_; |
| 48 DISALLOW_COPY_AND_ASSIGN(RefCountedTempDir); | 48 DISALLOW_COPY_AND_ASSIGN(RefCountedTempDir); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 typedef scoped_ptr<base::File, BrowserThread::DeleteOnFileThread> | 51 typedef scoped_ptr<base::File, BrowserThread::DeleteOnFileThread> |
| 52 ScopedTempFile; | 52 ScopedTempFile; |
| 53 | 53 |
| 54 // Wrapper for Emf to keep only file handle in memory, and load actual data only | 54 // Wrapper for Emf to keep only file handle in memory, and load actual data only |
| 55 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it | 55 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 void RunCallback(const base::Closure& callback); | 192 void RunCallback(const base::Closure& callback); |
| 193 | 193 |
| 194 private: | 194 private: |
| 195 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_; | 195 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_; |
| 196 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; | 196 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; |
| 197 | 197 |
| 198 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | 198 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); |
| 199 }; | 199 }; |
| 200 | 200 |
| 201 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { | 201 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { |
| 202 if (!(*temp_dir)) | 202 if (!temp_dir->get()) |
| 203 *temp_dir = new RefCountedTempDir(); | 203 *temp_dir = new RefCountedTempDir(); |
| 204 ScopedTempFile file; | 204 ScopedTempFile file; |
| 205 if (!(*temp_dir)->IsValid()) | 205 if (!(*temp_dir)->IsValid()) |
| 206 return file.Pass(); | 206 return file.Pass(); |
| 207 base::FilePath path; | 207 base::FilePath path; |
| 208 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) | 208 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) |
| 209 return file.Pass(); | 209 return file.Pass(); |
| 210 file.reset(new base::File(path, | 210 file.reset(new base::File(path, |
| 211 base::File::FLAG_CREATE_ALWAYS | | 211 base::File::FLAG_CREATE_ALWAYS | |
| 212 base::File::FLAG_WRITE | | 212 base::File::FLAG_WRITE | |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 return OnFailed(); | 302 return OnFailed(); |
| 303 // Should reply with OnProcessStarted(). | 303 // Should reply with OnProcessStarted(). |
| 304 Send(new ChromeUtilityMsg_StartupPing); | 304 Send(new ChromeUtilityMsg_StartupPing); |
| 305 } | 305 } |
| 306 | 306 |
| 307 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() { | 307 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() { |
| 308 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 308 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 309 if (!utility_process_host_) | 309 if (!utility_process_host_) |
| 310 return OnFailed(); | 310 return OnFailed(); |
| 311 | 311 |
| 312 base::ProcessHandle process = utility_process_host_->GetData().handle; | |
| 313 scoped_refptr<base::RefCountedMemory> data = data_; | 312 scoped_refptr<base::RefCountedMemory> data = data_; |
| 314 data_ = NULL; | 313 data_ = NULL; |
| 315 BrowserThread::PostTaskAndReplyWithResult( | 314 BrowserThread::PostTaskAndReplyWithResult( |
| 316 BrowserThread::FILE, | 315 BrowserThread::FILE, |
| 317 FROM_HERE, | 316 FROM_HERE, |
| 318 base::Bind(&CreateTempPdfFile, data, &temp_dir_), | 317 base::Bind(&CreateTempPdfFile, data, &temp_dir_), |
| 319 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this)); | 318 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this)); |
| 320 } | 319 } |
| 321 | 320 |
| 322 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) { | 321 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // Should reply with OnPageDone(). | 383 // Should reply with OnPageDone(). |
| 385 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( | 384 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( |
| 386 callback_data->page_number(), transit)); | 385 callback_data->page_number(), transit)); |
| 387 } | 386 } |
| 388 | 387 |
| 389 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, | 388 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, |
| 390 double scale_factor) { | 389 double scale_factor) { |
| 391 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 390 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 392 if (get_page_callbacks_.empty()) | 391 if (get_page_callbacks_.empty()) |
| 393 return OnFailed(); | 392 return OnFailed(); |
| 394 scoped_ptr<LazyEmf> emf; | 393 scoped_ptr<MetafilePlayer> emf; |
| 395 GetPageCallbackData& data = get_page_callbacks_.front(); | 394 GetPageCallbackData& data = get_page_callbacks_.front(); |
| 396 if (success) | 395 if (success) |
| 397 emf.reset(new LazyEmf(temp_dir_, data.emf().Pass())); | 396 emf.reset(new LazyEmf(temp_dir_, data.emf().Pass())); |
| 398 BrowserThread::PostTask(BrowserThread::UI, | 397 BrowserThread::PostTask(BrowserThread::UI, |
| 399 FROM_HERE, | 398 FROM_HERE, |
| 400 base::Bind(&PdfToEmfConverterImpl::RunCallback, | 399 base::Bind(&PdfToEmfConverterImpl::RunCallback, |
| 401 converter_, | 400 converter_, |
| 402 base::Bind(data.callback(), | 401 base::Bind(data.callback(), |
| 403 data.page_number(), | 402 data.page_number(), |
| 404 scale_factor, | 403 scale_factor, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 OnPageCount(0); | 451 OnPageCount(0); |
| 453 while (!get_page_callbacks_.empty()) | 452 while (!get_page_callbacks_.empty()) |
| 454 OnPageDone(false, 0.0); | 453 OnPageDone(false, 0.0); |
| 455 utility_process_host_.reset(); | 454 utility_process_host_.reset(); |
| 456 } | 455 } |
| 457 | 456 |
| 458 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) { | 457 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) { |
| 459 } | 458 } |
| 460 | 459 |
| 461 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | 460 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { |
| 462 if (utility_client_) | 461 if (utility_client_.get()) |
| 463 utility_client_->Stop(); | 462 utility_client_->Stop(); |
| 464 } | 463 } |
| 465 | 464 |
| 466 void PdfToEmfConverterImpl::Start( | 465 void PdfToEmfConverterImpl::Start( |
| 467 const scoped_refptr<base::RefCountedMemory>& data, | 466 const scoped_refptr<base::RefCountedMemory>& data, |
| 468 const PdfRenderSettings& conversion_settings, | 467 const PdfRenderSettings& conversion_settings, |
| 469 const StartCallback& start_callback) { | 468 const StartCallback& start_callback) { |
| 470 DCHECK(!utility_client_); | 469 DCHECK(!utility_client_.get()); |
| 471 utility_client_ = new PdfToEmfUtilityProcessHostClient( | 470 utility_client_ = new PdfToEmfUtilityProcessHostClient( |
| 472 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | 471 weak_ptr_factory_.GetWeakPtr(), conversion_settings); |
| 473 utility_client_->Start(data, start_callback); | 472 utility_client_->Start(data, start_callback); |
| 474 } | 473 } |
| 475 | 474 |
| 476 void PdfToEmfConverterImpl::GetPage(int page_number, | 475 void PdfToEmfConverterImpl::GetPage(int page_number, |
| 477 const GetPageCallback& get_page_callback) { | 476 const GetPageCallback& get_page_callback) { |
| 478 utility_client_->GetPage(page_number, get_page_callback); | 477 utility_client_->GetPage(page_number, get_page_callback); |
| 479 } | 478 } |
| 480 | 479 |
| 481 void PdfToEmfConverterImpl::RunCallback(const base::Closure& callback) { | 480 void PdfToEmfConverterImpl::RunCallback(const base::Closure& callback) { |
| 482 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 481 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 483 callback.Run(); | 482 callback.Run(); |
| 484 } | 483 } |
| 485 | 484 |
| 486 } // namespace | 485 } // namespace |
| 487 | 486 |
| 488 PdfToEmfConverter::~PdfToEmfConverter() { | 487 PdfToEmfConverter::~PdfToEmfConverter() { |
| 489 } | 488 } |
| 490 | 489 |
| 491 // static | 490 // static |
| 492 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { | 491 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { |
| 493 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); | 492 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); |
| 494 } | 493 } |
| 495 | 494 |
| 496 } // namespace printing | 495 } // namespace printing |
| OLD | NEW |