Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/browser/printing/pdf_to_emf_converter.cc

Issue 2633573002: Add Postscript Printing (Closed)
Patch Set: Rebase Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 public: 70 public:
71 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file) 71 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file)
72 : temp_dir_(temp_dir), file_(std::move(file)) { 72 : temp_dir_(temp_dir), file_(std::move(file)) {
73 CHECK(file_); 73 CHECK(file_);
74 } 74 }
75 ~LazyEmf() override { Close(); } 75 ~LazyEmf() override { Close(); }
76 76
77 protected: 77 protected:
78 // MetafilePlayer: 78 // MetafilePlayer:
79 bool SafePlayback(HDC hdc) const override; 79 bool SafePlayback(HDC hdc) const override;
80
81 void Close() const; 80 void Close() const;
82 bool LoadEmf(Emf* emf) const; 81 bool LoadEmf(Emf* emf) const;
83 82
84 private: 83 private:
85 mutable scoped_refptr<RefCountedTempDir> temp_dir_; 84 mutable scoped_refptr<RefCountedTempDir> temp_dir_;
86 mutable ScopedTempFile file_; // Mutable because of consts in base class. 85 mutable ScopedTempFile file_; // Mutable because of consts in base class.
87 86
88 bool GetDataAsVector(std::vector<char>* buffer) const override; 87 bool GetDataAsVector(std::vector<char>* buffer) const override;
89 bool SaveTo(base::File* file) const override; 88 bool SaveTo(base::File* file) const override;
90 89
(...skipping 14 matching lines...) Expand all
105 // All these steps work sequentially, so no data should be accessed 104 // All these steps work sequentially, so no data should be accessed
106 // simultaneously by several threads. 105 // simultaneously by several threads.
107 class PdfConverterUtilityProcessHostClient 106 class PdfConverterUtilityProcessHostClient
108 : public content::UtilityProcessHostClient { 107 : public content::UtilityProcessHostClient {
109 public: 108 public:
110 PdfConverterUtilityProcessHostClient( 109 PdfConverterUtilityProcessHostClient(
111 base::WeakPtr<PdfConverterImpl> converter, 110 base::WeakPtr<PdfConverterImpl> converter,
112 const PdfRenderSettings& settings); 111 const PdfRenderSettings& settings);
113 112
114 void Start(const scoped_refptr<base::RefCountedMemory>& data, 113 void Start(const scoped_refptr<base::RefCountedMemory>& data,
115 bool print_text_with_gdi,
116 const PdfConverter::StartCallback& start_callback); 114 const PdfConverter::StartCallback& start_callback);
117 115
118 void GetPage(int page_number, 116 void GetPage(int page_number,
119 const PdfConverter::GetPageCallback& get_page_callback); 117 const PdfConverter::GetPageCallback& get_page_callback);
120 118
121 void Stop(); 119 void Stop();
122 120
123 // UtilityProcessHostClient implementation. 121 // UtilityProcessHostClient implementation.
124 void OnProcessCrashed(int exit_code) override; 122 void OnProcessCrashed(int exit_code) override;
125 void OnProcessLaunchFailed(int exit_code) override; 123 void OnProcessLaunchFailed(int exit_code) override;
(...skipping 19 matching lines...) Expand all
145 return *this; 143 return *this;
146 } 144 }
147 145
148 int page_number() const { return page_number_; } 146 int page_number() const { return page_number_; }
149 const PdfConverter::GetPageCallback& callback() const { return callback_; } 147 const PdfConverter::GetPageCallback& callback() const { return callback_; }
150 ScopedTempFile TakeFile() { return std::move(file_); } 148 ScopedTempFile TakeFile() { return std::move(file_); }
151 void set_file(ScopedTempFile file) { file_ = std::move(file); } 149 void set_file(ScopedTempFile file) { file_ = std::move(file); }
152 150
153 private: 151 private:
154 int page_number_; 152 int page_number_;
155
156 PdfConverter::GetPageCallback callback_; 153 PdfConverter::GetPageCallback callback_;
157 ScopedTempFile file_; 154 ScopedTempFile file_;
158 155
159 DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData); 156 DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData);
160 }; 157 };
161 158
162 ~PdfConverterUtilityProcessHostClient() override; 159 ~PdfConverterUtilityProcessHostClient() override;
163 160
164 // Helper functions: must be overridden by subclasses 161 // Helper functions: must be overridden by subclasses
165 // Set the process name 162 // Set the process name
166 virtual base::string16 GetName() const = 0; 163 virtual base::string16 GetName() const = 0;
167 // Create a metafileplayer subclass file from a temporary file. 164 // Create a metafileplayer subclass file from a temporary file.
168 virtual std::unique_ptr<MetafilePlayer> GetFileFromTemp( 165 virtual std::unique_ptr<MetafilePlayer> GetFileFromTemp(
169 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> 166 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread>
170 temp_file) = 0; 167 temp_file) = 0;
171 // Send the messages to Start, GetPage, and Stop. 168 // Send the messages to Start, GetPage, and Stop.
172 virtual void SendStartMessage(IPC::PlatformFileForTransit transit, 169 virtual void SendStartMessage(IPC::PlatformFileForTransit transit) = 0;
173 bool print_text_with_gdi) = 0;
174 virtual void SendGetPageMessage(int page_number, 170 virtual void SendGetPageMessage(int page_number,
175 IPC::PlatformFileForTransit transit) = 0; 171 IPC::PlatformFileForTransit transit) = 0;
176 virtual void SendStopMessage() = 0; 172 virtual void SendStopMessage() = 0;
177 173
178 // Message handlers: 174 // Message handlers:
179 void OnPageCount(int page_count); 175 void OnPageCount(int page_count);
180 void OnPageDone(bool success, float scale_factor); 176 void OnPageDone(bool success, float scale_factor);
181 177
182 void OnFailed(); 178 void OnFailed();
183 void OnTempPdfReady(bool print_text_with_gdi, ScopedTempFile pdf); 179 void OnTempPdfReady(ScopedTempFile pdf);
184 void OnTempFileReady(GetPageCallbackData* callback_data, 180 void OnTempFileReady(GetPageCallbackData* callback_data,
185 ScopedTempFile temp_file); 181 ScopedTempFile temp_file);
186
187 scoped_refptr<RefCountedTempDir> temp_dir_; 182 scoped_refptr<RefCountedTempDir> temp_dir_;
188 183
189 // Used to suppress callbacks after PdfConverter is deleted. 184 // Used to suppress callbacks after PdfConverter is deleted.
190 base::WeakPtr<PdfConverterImpl> converter_; 185 base::WeakPtr<PdfConverterImpl> converter_;
191 PdfRenderSettings settings_; 186 PdfRenderSettings settings_;
192 187
193 // Document loaded callback. 188 // Document loaded callback.
194 PdfConverter::StartCallback start_callback_; 189 PdfConverter::StartCallback start_callback_;
195 190
196 // Process host for IPC. 191 // Process host for IPC.
(...skipping 16 matching lines...) Expand all
213 208
214 bool OnMessageReceived(const IPC::Message& message) override; 209 bool OnMessageReceived(const IPC::Message& message) override;
215 210
216 private: 211 private:
217 ~PdfToEmfUtilityProcessHostClient() override; 212 ~PdfToEmfUtilityProcessHostClient() override;
218 // Helpers to send messages and set process name 213 // Helpers to send messages and set process name
219 base::string16 GetName() const override; 214 base::string16 GetName() const override;
220 std::unique_ptr<MetafilePlayer> GetFileFromTemp( 215 std::unique_ptr<MetafilePlayer> GetFileFromTemp(
221 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread> 216 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread>
222 temp_file) override; 217 temp_file) override;
223 void SendStartMessage(IPC::PlatformFileForTransit transit, 218 void SendStartMessage(IPC::PlatformFileForTransit transit) override;
224 bool print_text_with_gdi) override;
225 void SendGetPageMessage(int page_number, 219 void SendGetPageMessage(int page_number,
226 IPC::PlatformFileForTransit transit) override; 220 IPC::PlatformFileForTransit transit) override;
227 void SendStopMessage() override; 221 void SendStopMessage() override;
228 222
229 // Additional message handler needed for Pdf to Emf 223 // Additional message handler needed for Pdf to Emf
230 void OnPreCacheFontCharacters(const LOGFONT& log_font, 224 void OnPreCacheFontCharacters(const LOGFONT& log_font,
231 const base::string16& characters); 225 const base::string16& characters);
232 226
233 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); 227 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient);
234 }; 228 };
235 229
230 // Converts PDF into PostScript.
231 class PdfToPostScriptUtilityProcessHostClient
232 : public PdfConverterUtilityProcessHostClient {
233 public:
234 PdfToPostScriptUtilityProcessHostClient(
235 base::WeakPtr<PdfConverterImpl> converter,
236 const PdfRenderSettings& settings)
237 : PdfConverterUtilityProcessHostClient(converter, settings) {}
238
239 // UtilityProcessHostClient implementation.
240 bool OnMessageReceived(const IPC::Message& message) override;
241
242 private:
243 // Helpers to send messages and set process name
244 base::string16 GetName() const override;
245 std::unique_ptr<MetafilePlayer> GetFileFromTemp(
246 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread>
247 temp_file) override;
248 void SendStartMessage(IPC::PlatformFileForTransit transit) override;
249 void SendGetPageMessage(int page_number,
250 IPC::PlatformFileForTransit transit) override;
251 void SendStopMessage() override;
252
253 // Additional message handler needed for Pdf to PostScript
254 void OnPageDone(bool success);
255
256 DISALLOW_COPY_AND_ASSIGN(PdfToPostScriptUtilityProcessHostClient);
257 };
258
236 class PdfConverterImpl : public PdfConverter { 259 class PdfConverterImpl : public PdfConverter {
237 public: 260 public:
238 PdfConverterImpl(); 261 PdfConverterImpl();
239 262
240 ~PdfConverterImpl() override; 263 ~PdfConverterImpl() override;
241 264
242 void Start(const scoped_refptr<base::RefCountedMemory>& data, 265 void Start(const scoped_refptr<base::RefCountedMemory>& data,
243 const PdfRenderSettings& conversion_settings, 266 const PdfRenderSettings& conversion_settings,
244 bool print_text_with_gdi,
245 const StartCallback& start_callback) override; 267 const StartCallback& start_callback) override;
246 268
247 void GetPage(int page_number, 269 void GetPage(int page_number,
248 const GetPageCallback& get_page_callback) override; 270 const GetPageCallback& get_page_callback) override;
249 271
250 // Helps to cancel callbacks if this object is destroyed. 272 // Helps to cancel callbacks if this object is destroyed.
251 void RunCallback(const base::Closure& callback); 273 void RunCallback(const base::Closure& callback);
252 274
253 protected: 275 protected:
254 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; 276 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_;
255 277
256 private: 278 private:
257 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); 279 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl);
258 }; 280 };
259 281
260 class PdfToEmfConverterImpl : public PdfConverterImpl { 282 class PdfToEmfConverterImpl : public PdfConverterImpl {
261 public: 283 public:
262 PdfToEmfConverterImpl(); 284 PdfToEmfConverterImpl();
263 285
264 ~PdfToEmfConverterImpl() override; 286 ~PdfToEmfConverterImpl() override;
265 287
266 void Start(const scoped_refptr<base::RefCountedMemory>& data, 288 void Start(const scoped_refptr<base::RefCountedMemory>& data,
267 const PdfRenderSettings& conversion_settings, 289 const PdfRenderSettings& conversion_settings,
268 bool print_text_with_gdi,
269 const StartCallback& start_callback) override; 290 const StartCallback& start_callback) override;
270 291
271 private: 292 private:
272 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; 293 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_;
294 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl);
295 };
273 296
274 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); 297 class PdfToPostScriptConverterImpl : public PdfConverterImpl {
298 public:
299 PdfToPostScriptConverterImpl();
300 ~PdfToPostScriptConverterImpl() {}
301
302 void Start(const scoped_refptr<base::RefCountedMemory>& data,
303 const PdfRenderSettings& conversion_settings,
304 const StartCallback& start_callback) override;
305
306 private:
307 base::WeakPtrFactory<PdfToPostScriptConverterImpl> weak_ptr_factory_;
308 DISALLOW_COPY_AND_ASSIGN(PdfToPostScriptConverterImpl);
275 }; 309 };
276 310
277 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { 311 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) {
278 if (!temp_dir->get()) 312 if (!temp_dir->get())
279 *temp_dir = new RefCountedTempDir(); 313 *temp_dir = new RefCountedTempDir();
280 ScopedTempFile file; 314 ScopedTempFile file;
281 if (!(*temp_dir)->IsValid()) 315 if (!(*temp_dir)->IsValid())
282 return file; 316 return file;
283 base::FilePath path; 317 base::FilePath path;
284 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) { 318 if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 file_->Seek(base::File::FROM_BEGIN, 0); 379 file_->Seek(base::File::FROM_BEGIN, 0);
346 int64_t size = file_->GetLength(); 380 int64_t size = file_->GetLength();
347 if (size <= 0) 381 if (size <= 0)
348 return false; 382 return false;
349 std::vector<char> data(size); 383 std::vector<char> data(size);
350 if (file_->ReadAtCurrentPos(data.data(), data.size()) != size) 384 if (file_->ReadAtCurrentPos(data.data(), data.size()) != size)
351 return false; 385 return false;
352 return emf->InitFromData(data.data(), data.size()); 386 return emf->InitFromData(data.data(), data.size());
353 } 387 }
354 388
389 // Postscript metafile subclass to override SafePlayback.
390 class PostScriptMetaFile : public LazyEmf {
391 public:
392 PostScriptMetaFile(const scoped_refptr<RefCountedTempDir>& temp_dir,
393 ScopedTempFile file)
394 : LazyEmf(temp_dir, std::move(file)) {}
395 ~PostScriptMetaFile() {}
396
397 protected:
398 // MetafilePlayer:
399 bool SafePlayback(HDC hdc) const override;
400
401 DISALLOW_COPY_AND_ASSIGN(PostScriptMetaFile);
402 };
403
404 bool PostScriptMetaFile::SafePlayback(HDC hdc) const {
405 // TODO(thestig): Fix destruction of metafiles. For some reasons
406 // instances of Emf are not deleted. https://crbug.com/260806
407 // It's known that the Emf going to be played just once to a printer. So just
408 // release |file_| before returning.
409 Emf emf;
410 if (!LoadEmf(&emf)) {
411 Close();
412 return false;
413 }
414
415 {
416 // Ensure enumerator destruction before calling Close() below.
417 Emf::Enumerator emf_enum(emf, nullptr, nullptr);
Vitaly Buka (NO REVIEWS) 2017/01/27 00:38:49 Why postscript is encoded as GDI comment into EMF?
rbpotter 2017/01/27 16:37:21 This was originally done by thestig@, so I am not
418 for (const Emf::Record& record : emf_enum) {
419 auto* emf_record = record.record();
420 if (emf_record->iType != EMR_GDICOMMENT)
421 continue;
422
423 const EMRGDICOMMENT* comment =
424 reinterpret_cast<const EMRGDICOMMENT*>(emf_record);
425 const char* data = reinterpret_cast<const char*>(comment->Data);
426 const uint16_t* ptr = reinterpret_cast<const uint16_t*>(data);
427 int ret = ExtEscape(hdc, PASSTHROUGH, 2 + *ptr, data, 0, nullptr);
428 DCHECK_EQ(*ptr, ret);
429 }
430 }
431 Close();
432 return true;
433 }
434
355 PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient( 435 PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient(
356 base::WeakPtr<PdfConverterImpl> converter, 436 base::WeakPtr<PdfConverterImpl> converter,
357 const PdfRenderSettings& settings) 437 const PdfRenderSettings& settings)
358 : converter_(converter), settings_(settings) {} 438 : converter_(converter), settings_(settings) {}
359 439
360 PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {} 440 PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {}
361 441
362 void PdfConverterUtilityProcessHostClient::Start( 442 void PdfConverterUtilityProcessHostClient::Start(
363 const scoped_refptr<base::RefCountedMemory>& data, 443 const scoped_refptr<base::RefCountedMemory>& data,
364 bool print_text_with_gdi,
365 const PdfConverter::StartCallback& start_callback) { 444 const PdfConverter::StartCallback& start_callback) {
366 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { 445 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
367 BrowserThread::PostTask( 446 BrowserThread::PostTask(
368 BrowserThread::IO, FROM_HERE, 447 BrowserThread::IO, FROM_HERE,
369 base::Bind(&PdfConverterUtilityProcessHostClient::Start, this, data, 448 base::Bind(&PdfConverterUtilityProcessHostClient::Start, this, data,
370 print_text_with_gdi, start_callback)); 449 start_callback));
371 return; 450 return;
372 } 451 }
373 452
374 // Store callback before any OnFailed() call to make it called on failure. 453 // Store callback before any OnFailed() call to make it called on failure.
375 start_callback_ = start_callback; 454 start_callback_ = start_callback;
376 455
377 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load 456 // 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 457 // gdiplus.dll, change how rendering happens, and not be able to correctly
379 // generate when sent to a metafile DC. 458 // generate when sent to a metafile DC.
380 utility_process_host_ = content::UtilityProcessHost::Create( 459 utility_process_host_ = content::UtilityProcessHost::Create(
381 this, base::ThreadTaskRunnerHandle::Get()) 460 this, base::ThreadTaskRunnerHandle::Get())
382 ->AsWeakPtr(); 461 ->AsWeakPtr();
383 utility_process_host_->SetName(GetName()); 462 utility_process_host_->SetName(GetName());
384 463
385 BrowserThread::PostTaskAndReplyWithResult( 464 BrowserThread::PostTaskAndReplyWithResult(
386 BrowserThread::FILE, FROM_HERE, 465 BrowserThread::FILE, FROM_HERE,
387 base::Bind(&CreateTempPdfFile, data, &temp_dir_), 466 base::Bind(&CreateTempPdfFile, data, &temp_dir_),
388 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this, 467 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this));
389 print_text_with_gdi));
390 } 468 }
391 469
392 void PdfConverterUtilityProcessHostClient::OnTempPdfReady( 470 void PdfConverterUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) {
393 bool print_text_with_gdi,
394 ScopedTempFile pdf) {
395 DCHECK_CURRENTLY_ON(BrowserThread::IO); 471 DCHECK_CURRENTLY_ON(BrowserThread::IO);
396 if (!utility_process_host_ || !pdf) 472 if (!utility_process_host_ || !pdf)
397 return OnFailed(); 473 return OnFailed();
398 // Should reply with OnPageCount(). 474 // Should reply with OnPageCount().
399 SendStartMessage( 475 SendStartMessage(
400 IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false), 476 IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false));
401 print_text_with_gdi);
402 } 477 }
403 478
404 void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) { 479 void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) {
405 DCHECK_CURRENTLY_ON(BrowserThread::IO); 480 DCHECK_CURRENTLY_ON(BrowserThread::IO);
406 if (start_callback_.is_null()) 481 if (start_callback_.is_null())
407 return OnFailed(); 482 return OnFailed();
408 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 483 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
409 base::Bind(&PdfConverterImpl::RunCallback, converter_, 484 base::Bind(&PdfConverterImpl::RunCallback, converter_,
410 base::Bind(start_callback_, page_count))); 485 base::Bind(start_callback_, page_count)));
411 start_callback_.Reset(); 486 start_callback_.Reset();
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 } 642 }
568 643
569 void PdfToEmfUtilityProcessHostClient::SendGetPageMessage( 644 void PdfToEmfUtilityProcessHostClient::SendGetPageMessage(
570 int page_number, 645 int page_number,
571 IPC::PlatformFileForTransit transit) { 646 IPC::PlatformFileForTransit transit) {
572 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number, 647 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number,
573 transit)); 648 transit));
574 } 649 }
575 650
576 void PdfToEmfUtilityProcessHostClient::SendStartMessage( 651 void PdfToEmfUtilityProcessHostClient::SendStartMessage(
577 IPC::PlatformFileForTransit transit, 652 IPC::PlatformFileForTransit transit) {
578 bool print_text_with_gdi) { 653 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, settings_));
579 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, settings_,
580 print_text_with_gdi));
581 } 654 }
582 655
583 void PdfToEmfUtilityProcessHostClient::SendStopMessage() { 656 void PdfToEmfUtilityProcessHostClient::SendStopMessage() {
584 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); 657 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop());
585 } 658 }
586 659
660 // Pdf to PostScript
661 bool PdfToPostScriptUtilityProcessHostClient::OnMessageReceived(
662 const IPC::Message& message) {
663 bool handled = true;
664 IPC_BEGIN_MESSAGE_MAP(PdfToPostScriptUtilityProcessHostClient, message)
665 IPC_MESSAGE_HANDLER(
666 ChromeUtilityHostMsg_RenderPDFPagesToPostScript_PageCount, OnPageCount)
667 IPC_MESSAGE_HANDLER(
668 ChromeUtilityHostMsg_RenderPDFPagesToPostScript_PageDone, OnPageDone)
669 IPC_MESSAGE_UNHANDLED(handled = false)
670 IPC_END_MESSAGE_MAP()
671 return handled;
672 }
673
674 base::string16 PdfToPostScriptUtilityProcessHostClient::GetName() const {
675 return l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PS_CONVERTOR_NAME);
676 }
677
678 std::unique_ptr<MetafilePlayer>
679 PdfToPostScriptUtilityProcessHostClient::GetFileFromTemp(
680 std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread>
681 temp_file) {
682 return base::MakeUnique<PostScriptMetaFile>(temp_dir_, std::move(temp_file));
683 }
684
685 void PdfToPostScriptUtilityProcessHostClient::SendGetPageMessage(
686 int page_number,
687 IPC::PlatformFileForTransit transit) {
688 Send(new ChromeUtilityMsg_RenderPDFPagesToPostScript_GetPage(page_number,
689 transit));
690 }
691
692 void PdfToPostScriptUtilityProcessHostClient::SendStartMessage(
693 IPC::PlatformFileForTransit transit) {
694 Send(new ChromeUtilityMsg_RenderPDFPagesToPostScript_Start(transit,
695 settings_));
696 }
697
698 void PdfToPostScriptUtilityProcessHostClient::SendStopMessage() {
699 Send(new ChromeUtilityMsg_RenderPDFPagesToPostScript_Stop());
700 }
701
702 void PdfToPostScriptUtilityProcessHostClient::OnPageDone(bool success) {
Vitaly Buka (NO REVIEWS) 2017/01/27 00:38:49 Maybe we can have single PdfConverterUtilityProces
rbpotter 2017/01/27 16:37:21 Sorry, I am not sure what you mean exactly. Do you
703 PdfConverterUtilityProcessHostClient::OnPageDone(success, 0.0f);
704 }
705
587 // Pdf Converter Impl and subclasses 706 // Pdf Converter Impl and subclasses
588 PdfConverterImpl::PdfConverterImpl() {} 707 PdfConverterImpl::PdfConverterImpl() {}
589 708
590 PdfConverterImpl::~PdfConverterImpl() {} 709 PdfConverterImpl::~PdfConverterImpl() {}
591 710
592 void PdfConverterImpl::Start(const scoped_refptr<base::RefCountedMemory>& data, 711 void PdfConverterImpl::Start(const scoped_refptr<base::RefCountedMemory>& data,
593 const PdfRenderSettings& conversion_settings, 712 const PdfRenderSettings& conversion_settings,
594 bool print_text_with_gdi,
595 const StartCallback& start_callback) { 713 const StartCallback& start_callback) {
596 DCHECK(!utility_client_.get()); 714 DCHECK(!utility_client_.get());
597 } 715 }
598 716
599 void PdfConverterImpl::GetPage(int page_number, 717 void PdfConverterImpl::GetPage(int page_number,
600 const GetPageCallback& get_page_callback) { 718 const GetPageCallback& get_page_callback) {
601 utility_client_->GetPage(page_number, get_page_callback); 719 utility_client_->GetPage(page_number, get_page_callback);
602 } 720 }
603 721
604 void PdfConverterImpl::RunCallback(const base::Closure& callback) { 722 void PdfConverterImpl::RunCallback(const base::Closure& callback) {
605 DCHECK_CURRENTLY_ON(BrowserThread::UI); 723 DCHECK_CURRENTLY_ON(BrowserThread::UI);
606 callback.Run(); 724 callback.Run();
607 } 725 }
608 726
609 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) {} 727 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) {}
610 728
611 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { 729 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() {
612 if (utility_client_.get()) 730 if (utility_client_.get())
613 utility_client_->Stop(); 731 utility_client_->Stop();
614 } 732 }
615 733
616 void PdfToEmfConverterImpl::Start( 734 void PdfToEmfConverterImpl::Start(
617 const scoped_refptr<base::RefCountedMemory>& data, 735 const scoped_refptr<base::RefCountedMemory>& data,
618 const PdfRenderSettings& conversion_settings, 736 const PdfRenderSettings& conversion_settings,
619 bool print_text_with_gdi,
620 const StartCallback& start_callback) { 737 const StartCallback& start_callback) {
621 DCHECK(!utility_client_.get()); 738 DCHECK(!utility_client_.get());
622 utility_client_ = new PdfToEmfUtilityProcessHostClient( 739 utility_client_ = new PdfToEmfUtilityProcessHostClient(
623 weak_ptr_factory_.GetWeakPtr(), conversion_settings); 740 weak_ptr_factory_.GetWeakPtr(), conversion_settings);
624 utility_client_->Start(data, print_text_with_gdi, start_callback); 741 utility_client_->Start(data, start_callback);
742 }
743
744 PdfToPostScriptConverterImpl::PdfToPostScriptConverterImpl()
745 : weak_ptr_factory_(this) {}
746
747 void PdfToPostScriptConverterImpl::Start(
748 const scoped_refptr<base::RefCountedMemory>& data,
749 const PdfRenderSettings& conversion_settings,
750 const StartCallback& start_callback) {
751 DCHECK(!utility_client_.get());
Vitaly Buka (NO REVIEWS) 2017/01/27 00:38:49 There is also https://cs.chromium.org/chromium/src
rbpotter 2017/01/27 16:37:21 Should we add postscript printing there now or in
752 utility_client_ = new PdfToPostScriptUtilityProcessHostClient(
753 weak_ptr_factory_.GetWeakPtr(), conversion_settings);
754 utility_client_->Start(data, start_callback);
625 } 755 }
626 756
627 } // namespace 757 } // namespace
628 758
629 PdfConverter::~PdfConverter() {} 759 PdfConverter::~PdfConverter() {}
630 760
631 // static 761 // static
632 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() { 762 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() {
633 return base::MakeUnique<PdfToEmfConverterImpl>(); 763 return base::MakeUnique<PdfToEmfConverterImpl>();
634 } 764 }
635 765
766 // static
767 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToPostScriptConverter() {
768 return base::MakeUnique<PdfToPostScriptConverterImpl>();
769 }
770
636 } // namespace printing 771 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698