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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // All these steps work sequentially, so no data should be accessed | 105 // All these steps work sequentially, so no data should be accessed |
106 // simultaneously by several threads. | 106 // simultaneously by several threads. |
107 class PdfConverterUtilityProcessHostClient | 107 class PdfConverterUtilityProcessHostClient |
108 : public content::UtilityProcessHostClient { | 108 : public content::UtilityProcessHostClient { |
109 public: | 109 public: |
110 PdfConverterUtilityProcessHostClient( | 110 PdfConverterUtilityProcessHostClient( |
111 base::WeakPtr<PdfConverterImpl> converter, | 111 base::WeakPtr<PdfConverterImpl> converter, |
112 const PdfRenderSettings& settings); | 112 const PdfRenderSettings& settings); |
113 | 113 |
114 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 114 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
115 bool print_text_with_gdi, | |
116 const PdfConverter::StartCallback& start_callback); | 115 const PdfConverter::StartCallback& start_callback); |
117 | 116 |
118 void GetPage(int page_number, | 117 void GetPage(int page_number, |
119 const PdfConverter::GetPageCallback& get_page_callback); | 118 const PdfConverter::GetPageCallback& get_page_callback); |
120 | 119 |
121 void Stop(); | 120 void Stop(); |
122 | 121 |
123 // UtilityProcessHostClient implementation. | 122 // UtilityProcessHostClient implementation. |
124 void OnProcessCrashed(int exit_code) override; | 123 void OnProcessCrashed(int exit_code) override; |
125 void OnProcessLaunchFailed(int exit_code) override; | 124 void OnProcessLaunchFailed(int exit_code) override; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 ~PdfConverterUtilityProcessHostClient() override; | 161 ~PdfConverterUtilityProcessHostClient() override; |
163 | 162 |
164 // Helper functions: must be overridden by subclasses | 163 // Helper functions: must be overridden by subclasses |
165 // Set the process name | 164 // Set the process name |
166 virtual base::string16 GetName() const = 0; | 165 virtual base::string16 GetName() const = 0; |
167 // Create a metafileplayer subclass file from a temporary file. | 166 // Create a metafileplayer subclass file from a temporary file. |
168 virtual std::unique_ptr<MetafilePlayer> GetFileFromTemp( | 167 virtual std::unique_ptr<MetafilePlayer> GetFileFromTemp( |
169 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> | 168 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> |
170 temp_file) = 0; | 169 temp_file) = 0; |
171 // Send the messages to Start, GetPage, and Stop. | 170 // Send the messages to Start, GetPage, and Stop. |
172 virtual void SendStartMessage(IPC::PlatformFileForTransit transit, | 171 virtual void SendStartMessage(IPC::PlatformFileForTransit transit) = 0; |
173 bool print_text_with_gdi) = 0; | |
174 virtual void SendGetPageMessage(int page_number, | 172 virtual void SendGetPageMessage(int page_number, |
175 IPC::PlatformFileForTransit transit) = 0; | 173 IPC::PlatformFileForTransit transit) = 0; |
176 virtual void SendStopMessage() = 0; | 174 virtual void SendStopMessage() = 0; |
177 | 175 |
178 // Message handlers: | 176 // Message handlers: |
179 void OnPageCount(int page_count); | 177 void OnPageCount(int page_count); |
180 void OnPageDone(bool success, float scale_factor); | 178 void OnPageDone(bool success, float scale_factor); |
181 | 179 |
182 void OnFailed(); | 180 void OnFailed(); |
183 void OnTempPdfReady(bool print_text_with_gdi, ScopedTempFile pdf); | 181 void OnTempPdfReady(ScopedTempFile pdf); |
184 void OnTempFileReady(GetPageCallbackData* callback_data, | 182 void OnTempFileReady(GetPageCallbackData* callback_data, |
185 ScopedTempFile temp_file); | 183 ScopedTempFile temp_file); |
186 | 184 |
187 scoped_refptr<RefCountedTempDir> temp_dir_; | 185 scoped_refptr<RefCountedTempDir> temp_dir_; |
188 | 186 |
189 // Used to suppress callbacks after PdfConverter is deleted. | 187 // Used to suppress callbacks after PdfConverter is deleted. |
190 base::WeakPtr<PdfConverterImpl> converter_; | 188 base::WeakPtr<PdfConverterImpl> converter_; |
191 PdfRenderSettings settings_; | 189 PdfRenderSettings settings_; |
192 | 190 |
193 // Document loaded callback. | 191 // Document loaded callback. |
(...skipping 19 matching lines...) Expand all Loading... |
213 | 211 |
214 bool OnMessageReceived(const IPC::Message& message) override; | 212 bool OnMessageReceived(const IPC::Message& message) override; |
215 | 213 |
216 private: | 214 private: |
217 ~PdfToEmfUtilityProcessHostClient() override; | 215 ~PdfToEmfUtilityProcessHostClient() override; |
218 // Helpers to send messages and set process name | 216 // Helpers to send messages and set process name |
219 base::string16 GetName() const override; | 217 base::string16 GetName() const override; |
220 std::unique_ptr<MetafilePlayer> GetFileFromTemp( | 218 std::unique_ptr<MetafilePlayer> GetFileFromTemp( |
221 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> | 219 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> |
222 temp_file) override; | 220 temp_file) override; |
223 void SendStartMessage(IPC::PlatformFileForTransit transit, | 221 void SendStartMessage(IPC::PlatformFileForTransit transit) override; |
224 bool print_text_with_gdi) override; | |
225 void SendGetPageMessage(int page_number, | 222 void SendGetPageMessage(int page_number, |
226 IPC::PlatformFileForTransit transit) override; | 223 IPC::PlatformFileForTransit transit) override; |
227 void SendStopMessage() override; | 224 void SendStopMessage() override; |
228 | 225 |
229 // Additional message handler needed for Pdf to Emf | 226 // Additional message handler needed for Pdf to Emf |
230 void OnPreCacheFontCharacters(const LOGFONT& log_font, | 227 void OnPreCacheFontCharacters(const LOGFONT& log_font, |
231 const base::string16& characters); | 228 const base::string16& characters); |
232 | 229 |
233 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); | 230 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); |
234 }; | 231 }; |
235 | 232 |
236 class PdfConverterImpl : public PdfConverter { | 233 class PdfConverterImpl : public PdfConverter { |
237 public: | 234 public: |
238 PdfConverterImpl(); | 235 PdfConverterImpl(); |
239 | 236 |
240 ~PdfConverterImpl() override; | 237 ~PdfConverterImpl() override; |
241 | 238 |
242 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 239 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
243 const PdfRenderSettings& conversion_settings, | 240 const PdfRenderSettings& conversion_settings, |
244 bool print_text_with_gdi, | |
245 const StartCallback& start_callback) override; | 241 const StartCallback& start_callback) override; |
246 | 242 |
247 void GetPage(int page_number, | 243 void GetPage(int page_number, |
248 const GetPageCallback& get_page_callback) override; | 244 const GetPageCallback& get_page_callback) override; |
249 | 245 |
250 // Helps to cancel callbacks if this object is destroyed. | 246 // Helps to cancel callbacks if this object is destroyed. |
251 void RunCallback(const base::Closure& callback); | 247 void RunCallback(const base::Closure& callback); |
252 | 248 |
253 protected: | 249 protected: |
254 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; | 250 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; |
255 | 251 |
256 private: | 252 private: |
257 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); | 253 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); |
258 }; | 254 }; |
259 | 255 |
260 class PdfToEmfConverterImpl : public PdfConverterImpl { | 256 class PdfToEmfConverterImpl : public PdfConverterImpl { |
261 public: | 257 public: |
262 PdfToEmfConverterImpl(); | 258 PdfToEmfConverterImpl(); |
263 | 259 |
264 ~PdfToEmfConverterImpl() override; | 260 ~PdfToEmfConverterImpl() override; |
265 | 261 |
266 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 262 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
267 const PdfRenderSettings& conversion_settings, | 263 const PdfRenderSettings& conversion_settings, |
268 bool print_text_with_gdi, | |
269 const StartCallback& start_callback) override; | 264 const StartCallback& start_callback) override; |
270 | 265 |
271 private: | 266 private: |
272 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; | 267 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; |
273 | 268 |
274 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | 269 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); |
275 }; | 270 }; |
276 | 271 |
277 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { | 272 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { |
278 if (!temp_dir->get()) | 273 if (!temp_dir->get()) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 349 |
355 PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient( | 350 PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient( |
356 base::WeakPtr<PdfConverterImpl> converter, | 351 base::WeakPtr<PdfConverterImpl> converter, |
357 const PdfRenderSettings& settings) | 352 const PdfRenderSettings& settings) |
358 : converter_(converter), settings_(settings) {} | 353 : converter_(converter), settings_(settings) {} |
359 | 354 |
360 PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {} | 355 PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {} |
361 | 356 |
362 void PdfConverterUtilityProcessHostClient::Start( | 357 void PdfConverterUtilityProcessHostClient::Start( |
363 const scoped_refptr<base::RefCountedMemory>& data, | 358 const scoped_refptr<base::RefCountedMemory>& data, |
364 bool print_text_with_gdi, | |
365 const PdfConverter::StartCallback& start_callback) { | 359 const PdfConverter::StartCallback& start_callback) { |
366 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 360 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
367 BrowserThread::PostTask( | 361 BrowserThread::PostTask( |
368 BrowserThread::IO, FROM_HERE, | 362 BrowserThread::IO, FROM_HERE, |
369 base::Bind(&PdfConverterUtilityProcessHostClient::Start, this, data, | 363 base::Bind(&PdfConverterUtilityProcessHostClient::Start, this, data, |
370 print_text_with_gdi, start_callback)); | 364 start_callback)); |
371 return; | 365 return; |
372 } | 366 } |
373 | 367 |
374 // Store callback before any OnFailed() call to make it called on failure. | 368 // Store callback before any OnFailed() call to make it called on failure. |
375 start_callback_ = start_callback; | 369 start_callback_ = start_callback; |
376 | 370 |
377 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load | 371 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load |
378 // gdiplus.dll, change how rendering happens, and not be able to correctly | 372 // gdiplus.dll, change how rendering happens, and not be able to correctly |
379 // generate when sent to a metafile DC. | 373 // generate when sent to a metafile DC. |
380 utility_process_host_ = content::UtilityProcessHost::Create( | 374 utility_process_host_ = content::UtilityProcessHost::Create( |
381 this, base::ThreadTaskRunnerHandle::Get()) | 375 this, base::ThreadTaskRunnerHandle::Get()) |
382 ->AsWeakPtr(); | 376 ->AsWeakPtr(); |
383 utility_process_host_->SetName(GetName()); | 377 utility_process_host_->SetName(GetName()); |
384 | 378 |
385 BrowserThread::PostTaskAndReplyWithResult( | 379 BrowserThread::PostTaskAndReplyWithResult( |
386 BrowserThread::FILE, FROM_HERE, | 380 BrowserThread::FILE, FROM_HERE, |
387 base::Bind(&CreateTempPdfFile, data, &temp_dir_), | 381 base::Bind(&CreateTempPdfFile, data, &temp_dir_), |
388 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this, | 382 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this)); |
389 print_text_with_gdi)); | |
390 } | 383 } |
391 | 384 |
392 void PdfConverterUtilityProcessHostClient::OnTempPdfReady( | 385 void PdfConverterUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) { |
393 bool print_text_with_gdi, | |
394 ScopedTempFile pdf) { | |
395 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 386 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
396 if (!utility_process_host_ || !pdf) | 387 if (!utility_process_host_ || !pdf) |
397 return OnFailed(); | 388 return OnFailed(); |
398 // Should reply with OnPageCount(). | 389 // Should reply with OnPageCount(). |
399 SendStartMessage( | 390 SendStartMessage( |
400 IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false), | 391 IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false)); |
401 print_text_with_gdi); | |
402 } | 392 } |
403 | 393 |
404 void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) { | 394 void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) { |
405 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 395 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
406 if (start_callback_.is_null()) | 396 if (start_callback_.is_null()) |
407 return OnFailed(); | 397 return OnFailed(); |
408 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 398 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
409 base::Bind(&PdfConverterImpl::RunCallback, converter_, | 399 base::Bind(&PdfConverterImpl::RunCallback, converter_, |
410 base::Bind(start_callback_, page_count))); | 400 base::Bind(start_callback_, page_count))); |
411 start_callback_.Reset(); | 401 start_callback_.Reset(); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 } | 557 } |
568 | 558 |
569 void PdfToEmfUtilityProcessHostClient::SendGetPageMessage( | 559 void PdfToEmfUtilityProcessHostClient::SendGetPageMessage( |
570 int page_number, | 560 int page_number, |
571 IPC::PlatformFileForTransit transit) { | 561 IPC::PlatformFileForTransit transit) { |
572 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number, | 562 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number, |
573 transit)); | 563 transit)); |
574 } | 564 } |
575 | 565 |
576 void PdfToEmfUtilityProcessHostClient::SendStartMessage( | 566 void PdfToEmfUtilityProcessHostClient::SendStartMessage( |
577 IPC::PlatformFileForTransit transit, | 567 IPC::PlatformFileForTransit transit) { |
578 bool print_text_with_gdi) { | 568 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, settings_)); |
579 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, settings_, | |
580 print_text_with_gdi)); | |
581 } | 569 } |
582 | 570 |
583 void PdfToEmfUtilityProcessHostClient::SendStopMessage() { | 571 void PdfToEmfUtilityProcessHostClient::SendStopMessage() { |
584 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); | 572 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); |
585 } | 573 } |
586 | 574 |
587 // Pdf Converter Impl and subclasses | 575 // Pdf Converter Impl and subclasses |
588 PdfConverterImpl::PdfConverterImpl() {} | 576 PdfConverterImpl::PdfConverterImpl() {} |
589 | 577 |
590 PdfConverterImpl::~PdfConverterImpl() {} | 578 PdfConverterImpl::~PdfConverterImpl() {} |
591 | 579 |
592 void PdfConverterImpl::Start(const scoped_refptr<base::RefCountedMemory>& data, | 580 void PdfConverterImpl::Start(const scoped_refptr<base::RefCountedMemory>& data, |
593 const PdfRenderSettings& conversion_settings, | 581 const PdfRenderSettings& conversion_settings, |
594 bool print_text_with_gdi, | |
595 const StartCallback& start_callback) { | 582 const StartCallback& start_callback) { |
596 DCHECK(!utility_client_.get()); | 583 DCHECK(!utility_client_.get()); |
597 } | 584 } |
598 | 585 |
599 void PdfConverterImpl::GetPage(int page_number, | 586 void PdfConverterImpl::GetPage(int page_number, |
600 const GetPageCallback& get_page_callback) { | 587 const GetPageCallback& get_page_callback) { |
601 utility_client_->GetPage(page_number, get_page_callback); | 588 utility_client_->GetPage(page_number, get_page_callback); |
602 } | 589 } |
603 | 590 |
604 void PdfConverterImpl::RunCallback(const base::Closure& callback) { | 591 void PdfConverterImpl::RunCallback(const base::Closure& callback) { |
605 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 592 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
606 callback.Run(); | 593 callback.Run(); |
607 } | 594 } |
608 | 595 |
609 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) {} | 596 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) {} |
610 | 597 |
611 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | 598 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { |
612 if (utility_client_.get()) | 599 if (utility_client_.get()) |
613 utility_client_->Stop(); | 600 utility_client_->Stop(); |
614 } | 601 } |
615 | 602 |
616 void PdfToEmfConverterImpl::Start( | 603 void PdfToEmfConverterImpl::Start( |
617 const scoped_refptr<base::RefCountedMemory>& data, | 604 const scoped_refptr<base::RefCountedMemory>& data, |
618 const PdfRenderSettings& conversion_settings, | 605 const PdfRenderSettings& conversion_settings, |
619 bool print_text_with_gdi, | |
620 const StartCallback& start_callback) { | 606 const StartCallback& start_callback) { |
621 DCHECK(!utility_client_.get()); | 607 DCHECK(!utility_client_.get()); |
622 utility_client_ = new PdfToEmfUtilityProcessHostClient( | 608 utility_client_ = new PdfToEmfUtilityProcessHostClient( |
623 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | 609 weak_ptr_factory_.GetWeakPtr(), conversion_settings); |
624 utility_client_->Start(data, print_text_with_gdi, start_callback); | 610 utility_client_->Start(data, start_callback); |
625 } | 611 } |
626 | 612 |
627 } // namespace | 613 } // namespace |
628 | 614 |
629 PdfConverter::~PdfConverter() {} | 615 PdfConverter::~PdfConverter() {} |
630 | 616 |
631 // static | 617 // static |
632 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() { | 618 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() { |
633 return base::MakeUnique<PdfToEmfConverterImpl>(); | 619 return base::MakeUnique<PdfToEmfConverterImpl>(); |
634 } | 620 } |
635 | 621 |
636 } // namespace printing | 622 } // namespace printing |
OLD | NEW |