| 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 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 int page_number_; | 147 int page_number_; |
| 148 PdfToEmfConverter::GetPageCallback callback_; | 148 PdfToEmfConverter::GetPageCallback callback_; |
| 149 ScopedTempFile emf_; | 149 ScopedTempFile emf_; |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 ~PdfToEmfUtilityProcessHostClient() override; | 152 ~PdfToEmfUtilityProcessHostClient() override; |
| 153 | 153 |
| 154 bool Send(IPC::Message* msg); | 154 bool Send(IPC::Message* msg); |
| 155 | 155 |
| 156 // Message handlers. | 156 // Message handlers. |
| 157 void OnProcessStarted(); | |
| 158 void OnPageCount(int page_count); | 157 void OnPageCount(int page_count); |
| 159 void OnPageDone(bool success, float scale_factor); | 158 void OnPageDone(bool success, float scale_factor); |
| 160 | 159 |
| 161 void OnFailed(); | 160 void OnFailed(); |
| 162 void OnTempPdfReady(ScopedTempFile pdf); | 161 void OnTempPdfReady(ScopedTempFile pdf); |
| 163 void OnTempEmfReady(GetPageCallbackData* callback_data, ScopedTempFile emf); | 162 void OnTempEmfReady(GetPageCallbackData* callback_data, ScopedTempFile emf); |
| 164 | 163 |
| 165 scoped_refptr<RefCountedTempDir> temp_dir_; | 164 scoped_refptr<RefCountedTempDir> temp_dir_; |
| 166 | 165 |
| 167 // Used to suppress callbacks after PdfToEmfConverterImpl is deleted. | 166 // Used to suppress callbacks after PdfToEmfConverterImpl is deleted. |
| 168 base::WeakPtr<PdfToEmfConverterImpl> converter_; | 167 base::WeakPtr<PdfToEmfConverterImpl> converter_; |
| 169 PdfRenderSettings settings_; | 168 PdfRenderSettings settings_; |
| 170 scoped_refptr<base::RefCountedMemory> data_; | |
| 171 | 169 |
| 172 // Document loaded callback. | 170 // Document loaded callback. |
| 173 PdfToEmfConverter::StartCallback start_callback_; | 171 PdfToEmfConverter::StartCallback start_callback_; |
| 174 | 172 |
| 175 // Process host for IPC. | 173 // Process host for IPC. |
| 176 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 174 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 177 | 175 |
| 178 // Queue of callbacks for GetPage() requests. Utility process should reply | 176 // Queue of callbacks for GetPage() requests. Utility process should reply |
| 179 // with PageDone in the same order as requests were received. | 177 // with PageDone in the same order as requests were received. |
| 180 // Use containers that keeps element pointers valid after push() and pop(). | 178 // Use containers that keeps element pointers valid after push() and pop(). |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 const PdfToEmfConverter::StartCallback& start_callback) { | 292 const PdfToEmfConverter::StartCallback& start_callback) { |
| 295 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 293 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 296 BrowserThread::PostTask(BrowserThread::IO, | 294 BrowserThread::PostTask(BrowserThread::IO, |
| 297 FROM_HERE, | 295 FROM_HERE, |
| 298 base::Bind(&PdfToEmfUtilityProcessHostClient::Start, | 296 base::Bind(&PdfToEmfUtilityProcessHostClient::Start, |
| 299 this, | 297 this, |
| 300 data, | 298 data, |
| 301 start_callback)); | 299 start_callback)); |
| 302 return; | 300 return; |
| 303 } | 301 } |
| 304 data_ = data; | |
| 305 | 302 |
| 306 // Store callback before any OnFailed() call to make it called on failure. | 303 // Store callback before any OnFailed() call to make it called on failure. |
| 307 start_callback_ = start_callback; | 304 start_callback_ = start_callback; |
| 308 | 305 |
| 309 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load | 306 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load |
| 310 // gdiplus.dll, change how rendering happens, and not be able to correctly | 307 // gdiplus.dll, change how rendering happens, and not be able to correctly |
| 311 // generate when sent to a metafile DC. | 308 // generate when sent to a metafile DC. |
| 312 utility_process_host_ = | 309 utility_process_host_ = |
| 313 content::UtilityProcessHost::Create( | 310 content::UtilityProcessHost::Create( |
| 314 this, base::MessageLoop::current()->task_runner())->AsWeakPtr(); | 311 this, base::MessageLoop::current()->task_runner())->AsWeakPtr(); |
| 315 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 312 utility_process_host_->SetName(l10n_util::GetStringUTF16( |
| 316 IDS_UTILITY_PROCESS_EMF_CONVERTOR_NAME)); | 313 IDS_UTILITY_PROCESS_EMF_CONVERTOR_NAME)); |
| 317 // Should reply with OnProcessStarted(). | |
| 318 Send(new ChromeUtilityMsg_StartupPing); | |
| 319 } | |
| 320 | 314 |
| 321 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() { | |
| 322 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 323 if (!utility_process_host_) | |
| 324 return OnFailed(); | |
| 325 | |
| 326 scoped_refptr<base::RefCountedMemory> data = data_; | |
| 327 data_ = NULL; | |
| 328 BrowserThread::PostTaskAndReplyWithResult( | 315 BrowserThread::PostTaskAndReplyWithResult( |
| 329 BrowserThread::FILE, | 316 BrowserThread::FILE, |
| 330 FROM_HERE, | 317 FROM_HERE, |
| 331 base::Bind(&CreateTempPdfFile, data, &temp_dir_), | 318 base::Bind(&CreateTempPdfFile, data, &temp_dir_), |
| 332 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this)); | 319 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this)); |
| 333 } | 320 } |
| 334 | 321 |
| 335 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) { | 322 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 323 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 337 if (!utility_process_host_ || !pdf) | 324 if (!utility_process_host_ || !pdf) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 } | 426 } |
| 440 | 427 |
| 441 void PdfToEmfUtilityProcessHostClient::OnProcessLaunchFailed() { | 428 void PdfToEmfUtilityProcessHostClient::OnProcessLaunchFailed() { |
| 442 OnFailed(); | 429 OnFailed(); |
| 443 } | 430 } |
| 444 | 431 |
| 445 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( | 432 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( |
| 446 const IPC::Message& message) { | 433 const IPC::Message& message) { |
| 447 bool handled = true; | 434 bool handled = true; |
| 448 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) | 435 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) |
| 449 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) | |
| 450 IPC_MESSAGE_HANDLER( | 436 IPC_MESSAGE_HANDLER( |
| 451 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount) | 437 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount) |
| 452 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, | 438 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, |
| 453 OnPageDone) | 439 OnPageDone) |
| 454 IPC_MESSAGE_UNHANDLED(handled = false) | 440 IPC_MESSAGE_UNHANDLED(handled = false) |
| 455 IPC_END_MESSAGE_MAP() | 441 IPC_END_MESSAGE_MAP() |
| 456 return handled; | 442 return handled; |
| 457 } | 443 } |
| 458 | 444 |
| 459 bool PdfToEmfUtilityProcessHostClient::Send(IPC::Message* msg) { | 445 bool PdfToEmfUtilityProcessHostClient::Send(IPC::Message* msg) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 490 |
| 505 PdfToEmfConverter::~PdfToEmfConverter() { | 491 PdfToEmfConverter::~PdfToEmfConverter() { |
| 506 } | 492 } |
| 507 | 493 |
| 508 // static | 494 // static |
| 509 std::unique_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { | 495 std::unique_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { |
| 510 return std::unique_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); | 496 return std::unique_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); |
| 511 } | 497 } |
| 512 | 498 |
| 513 } // namespace printing | 499 } // namespace printing |
| OLD | NEW |