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

Side by Side Diff: chrome/utility/chrome_content_utility_client.cc

Issue 255543006: Printing on Windows via PDF (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cloud print and converter refactor Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/utility/chrome_content_utility_client.h" 5 #include "chrome/utility/chrome_content_utility_client.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 return false; 163 return false;
164 return get_pdf_doc_info_func_(pdf_buffer, buffer_size, page_count, 164 return get_pdf_doc_info_func_(pdf_buffer, buffer_size, page_count,
165 max_page_width); 165 max_page_width);
166 } 166 }
167 167
168 protected: 168 protected:
169 virtual bool PlatformInit( 169 virtual bool PlatformInit(
170 const base::FilePath& pdf_module_path, 170 const base::FilePath& pdf_module_path,
171 const base::ScopedNativeLibrary& pdf_lib) { 171 const base::ScopedNativeLibrary& pdf_lib) {
172 return true; 172 return true;
173 }; 173 }
174 174
175 private: 175 private:
176 // Exported by PDF plugin. 176 // Exported by PDF plugin.
177 typedef bool (*RenderPDFPageToBitmapProc)(const void* pdf_buffer, 177 typedef bool (*RenderPDFPageToBitmapProc)(const void* pdf_buffer,
178 int pdf_buffer_size, 178 int pdf_buffer_size,
179 int page_number, 179 int page_number,
180 void* bitmap_buffer, 180 void* bitmap_buffer,
181 int bitmap_width, 181 int bitmap_width,
182 int bitmap_height, 182 int bitmap_height,
183 int dpi_x, 183 int dpi_x,
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 356
357 bool handled = true; 357 bool handled = true;
358 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) 358 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message)
359 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) 359 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension)
360 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource, 360 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource,
361 OnUnpackWebResource) 361 OnUnpackWebResource)
362 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, 362 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest,
363 OnParseUpdateManifest) 363 OnParseUpdateManifest)
364 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) 364 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage)
365 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) 365 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64)
366 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile, 366 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
367 OnRenderPDFPagesToMetafile) 367 OnRenderPDFPagesToMetafile)
368 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, 368 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster,
369 OnRenderPDFPagesToPWGRaster) 369 OnRenderPDFPagesToPWGRaster)
370 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, 370 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage,
371 OnRobustJPEGDecodeImage) 371 OnRobustJPEGDecodeImage)
372 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) 372 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON)
373 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, 373 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults,
374 OnGetPrinterCapsAndDefaults) 374 OnGetPrinterCapsAndDefaults)
375 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, 375 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults,
376 OnGetPrinterSemanticCapsAndDefaults) 376 OnGetPrinterSemanticCapsAndDefaults)
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 558
559 if (succeeded) 559 if (succeeded)
560 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded()); 560 Send(new ChromeUtilityHostMsg_CreateZipFile_Succeeded());
561 else 561 else
562 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed()); 562 Send(new ChromeUtilityHostMsg_CreateZipFile_Failed());
563 ReleaseProcessIfNeeded(); 563 ReleaseProcessIfNeeded();
564 } 564 }
565 #endif // defined(OS_CHROMEOS) 565 #endif // defined(OS_CHROMEOS)
566 566
567 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile( 567 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafile(
568 base::PlatformFile pdf_file, 568 IPC::PlatformFileForTransit pdf_transit,
569 const base::FilePath& metafile_path, 569 const base::FilePath& metafile_path,
570 const printing::PdfRenderSettings& settings, 570 const printing::PdfRenderSettings& settings,
571 const std::vector<printing::PageRange>& page_ranges) { 571 const std::vector<printing::PageRange>& page_ranges_const) {
572 bool succeeded = false; 572 bool succeeded = false;
573 #if defined(OS_WIN) 573 #if defined(OS_WIN)
574 base::PlatformFile pdf_file =
575 IPC::PlatformFileForTransitToPlatformFile(pdf_transit);
574 int highest_rendered_page_number = 0; 576 int highest_rendered_page_number = 0;
575 double scale_factor = 1.0; 577 double scale_factor = 1.0;
578 std::vector<printing::PageRange> page_ranges = page_ranges_const;
576 succeeded = RenderPDFToWinMetafile(pdf_file, 579 succeeded = RenderPDFToWinMetafile(pdf_file,
577 metafile_path, 580 metafile_path,
578 settings, 581 settings,
579 page_ranges, 582 &page_ranges,
580 &highest_rendered_page_number, 583 &highest_rendered_page_number,
581 &scale_factor); 584 &scale_factor);
582 if (succeeded) { 585 if (succeeded) {
583 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( 586 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded(
Vitaly Buka (NO REVIEWS) 2014/05/15 19:23:34 So how about sending ChromeUtilityHostMsg_RenderPD
scottmg 2014/05/15 19:43:04 That should help for PdfToEmfConverter I think. It
584 highest_rendered_page_number, scale_factor)); 587 page_ranges, scale_factor));
585 } 588 }
586 #endif // defined(OS_WIN) 589 #endif // defined(OS_WIN)
587 if (!succeeded) { 590 if (!succeeded) {
588 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); 591 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed());
589 } 592 }
590 ReleaseProcessIfNeeded(); 593 ReleaseProcessIfNeeded();
591 } 594 }
592 595
593 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster( 596 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster(
594 IPC::PlatformFileForTransit pdf_transit, 597 IPC::PlatformFileForTransit pdf_transit,
(...skipping 10 matching lines...) Expand all
605 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); 608 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed());
606 } 609 }
607 ReleaseProcessIfNeeded(); 610 ReleaseProcessIfNeeded();
608 } 611 }
609 612
610 #if defined(OS_WIN) 613 #if defined(OS_WIN)
611 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( 614 bool ChromeContentUtilityClient::RenderPDFToWinMetafile(
612 base::PlatformFile pdf_file, 615 base::PlatformFile pdf_file,
613 const base::FilePath& metafile_path, 616 const base::FilePath& metafile_path,
614 const printing::PdfRenderSettings& settings, 617 const printing::PdfRenderSettings& settings,
615 const std::vector<printing::PageRange>& page_ranges, 618 std::vector<printing::PageRange>* page_ranges,
616 int* highest_rendered_page_number, 619 int* highest_rendered_page_number,
617 double* scale_factor) { 620 double* scale_factor) {
618 *highest_rendered_page_number = -1; 621 *highest_rendered_page_number = -1;
619 *scale_factor = 1.0; 622 *scale_factor = 1.0;
623 CHECK(page_ranges);
620 base::win::ScopedHandle file(pdf_file); 624 base::win::ScopedHandle file(pdf_file);
621 625
622 if (!g_pdf_lib.Get().IsValid()) 626 if (!g_pdf_lib.Get().IsValid())
623 return false; 627 return false;
624 628
625 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle 629 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle
626 // and a page range array. That way we don't need to read the entire PDF into 630 // and a page range array. That way we don't need to read the entire PDF into
627 // memory. 631 // memory.
628 DWORD length = ::GetFileSize(file, NULL); 632 DWORD length = ::GetFileSize(file, NULL);
629 if (length == INVALID_FILE_SIZE) 633 if (length == INVALID_FILE_SIZE)
630 return false; 634 return false;
631 635
632 std::vector<uint8> buffer; 636 std::vector<uint8> buffer;
633 buffer.resize(length); 637 buffer.resize(length);
634 DWORD bytes_read = 0; 638 DWORD bytes_read = 0;
635 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || 639 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) ||
636 (bytes_read != length)) { 640 (bytes_read != length)) {
637 return false; 641 return false;
638 } 642 }
639 643
640 int total_page_count = 0; 644 int total_page_count = 0;
641 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(), 645 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(),
642 &total_page_count, NULL)) { 646 &total_page_count, NULL)) {
643 return false; 647 return false;
644 } 648 }
645 649
646 printing::Emf metafile; 650 // If no range supplied, do all pages.
647 metafile.InitToFile(metafile_path); 651 if (page_ranges->empty()) {
648 // We need to scale down DC to fit an entire page into DC available area. 652 printing::PageRange page_range_all;
649 // Current metafile is based on screen DC and have current screen size. 653 page_range_all.from = 0;
650 // Writing outside of those boundaries will result in the cut-off output. 654 page_range_all.to = total_page_count - 1;
651 // On metafiles (this is the case here), scaling down will still record 655 page_ranges->push_back(page_range_all);
652 // original coordinates and we'll be able to print in full resolution. 656 }
653 // Before playback we'll need to counter the scaling up that will happen
654 // in the service (print_system_win.cc).
655 *scale_factor = gfx::CalculatePageScale(metafile.context(),
656 settings.area().right(),
657 settings.area().bottom());
658 gfx::ScaleDC(metafile.context(), *scale_factor);
659 657
660 bool ret = false; 658 bool ret = false;
661 std::vector<printing::PageRange>::const_iterator iter; 659 std::vector<printing::PageRange>::const_iterator iter;
662 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) { 660 for (iter = page_ranges->begin(); iter != page_ranges->end(); ++iter) {
663 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { 661 for (int page_number = iter->from; page_number <= iter->to; ++page_number) {
664 if (page_number >= total_page_count) 662 if (page_number >= total_page_count)
665 break; 663 break;
664
665 printing::Emf metafile;
666 metafile.InitToFile(metafile_path.InsertBeforeExtensionASCII(
667 base::StringPrintf(".%d", page_number)));
668
669 // We need to scale down DC to fit an entire page into DC available area.
670 // Current metafile is based on screen DC and have current screen size.
671 // Writing outside of those boundaries will result in the cut-off output.
672 // On metafiles (this is the case here), scaling down will still record
673 // original coordinates and we'll be able to print in full resolution.
674 // Before playback we'll need to counter the scaling up that will happen
675 // in the service (print_system_win.cc).
676 *scale_factor = gfx::CalculatePageScale(metafile.context(),
677 settings.area().right(),
678 settings.area().bottom());
679 gfx::ScaleDC(metafile.context(), *scale_factor);
680
666 // The underlying metafile is of type Emf and ignores the arguments passed 681 // The underlying metafile is of type Emf and ignores the arguments passed
667 // to StartPage. 682 // to StartPage.
668 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); 683 metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
669 if (g_pdf_lib.Get().RenderPDFPageToDC( 684 if (g_pdf_lib.Get().RenderPDFPageToDC(
670 &buffer.front(), buffer.size(), page_number, metafile.context(), 685 &buffer.front(), buffer.size(), page_number, metafile.context(),
671 settings.dpi(), settings.dpi(), settings.area().x(), 686 settings.dpi(), settings.dpi(), settings.area().x(),
672 settings.area().y(), settings.area().width(), 687 settings.area().y(), settings.area().width(),
673 settings.area().height(), true, false, true, true, 688 settings.area().height(), true, false, true, true,
674 settings.autorotate())) { 689 settings.autorotate())) {
675 if (*highest_rendered_page_number < page_number) 690 if (*highest_rendered_page_number < page_number)
676 *highest_rendered_page_number = page_number; 691 *highest_rendered_page_number = page_number;
677 ret = true; 692 ret = true;
678 } 693 }
679 metafile.FinishPage(); 694 metafile.FinishPage();
695 metafile.FinishDocument();
680 } 696 }
681 } 697 }
682 metafile.FinishDocument();
683 return ret; 698 return ret;
684 } 699 }
685 #endif // defined(OS_WIN) 700 #endif // defined(OS_WIN)
686 701
687 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster( 702 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster(
688 base::PlatformFile pdf_file, 703 base::PlatformFile pdf_file,
689 const printing::PdfRenderSettings& settings, 704 const printing::PdfRenderSettings& settings,
690 const printing::PwgRasterSettings& bitmap_settings, 705 const printing::PwgRasterSettings& bitmap_settings,
691 base::PlatformFile bitmap_file) { 706 base::PlatformFile bitmap_file) {
692 bool autoupdate = true; 707 bool autoupdate = true;
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 NetworkingPrivateCrypto crypto; 1033 NetworkingPrivateCrypto crypto;
1019 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); 1034 success = crypto.EncryptByteString(public_key, key_data, &ciphertext);
1020 } 1035 }
1021 1036
1022 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, 1037 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext,
1023 success)); 1038 success));
1024 } 1039 }
1025 #endif // defined(OS_WIN) 1040 #endif // defined(OS_WIN)
1026 1041
1027 } // namespace chrome 1042 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/utility/chrome_content_utility_client.h ('k') | content/renderer/pepper/pepper_plugin_instance_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698