Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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_RenderPDFPagesToMetafile, |
| 367 OnRenderPDFPagesToMetafile) | 367 OnRenderPDFPagesToMetafile) |
| 368 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafileHandles, | |
| 369 OnRenderPDFPagesToMetafileHandles) | |
| 368 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, | 370 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster, |
| 369 OnRenderPDFPagesToPWGRaster) | 371 OnRenderPDFPagesToPWGRaster) |
| 370 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, | 372 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, |
| 371 OnRobustJPEGDecodeImage) | 373 OnRobustJPEGDecodeImage) |
| 372 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) | 374 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) |
| 373 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, | 375 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, |
| 374 OnGetPrinterCapsAndDefaults) | 376 OnGetPrinterCapsAndDefaults) |
| 375 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, | 377 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults, |
| 376 OnGetPrinterSemanticCapsAndDefaults) | 378 OnGetPrinterSemanticCapsAndDefaults) |
| 377 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff, | 379 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_PatchFileBsdiff, |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 583 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( | 585 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( |
| 584 highest_rendered_page_number, scale_factor)); | 586 highest_rendered_page_number, scale_factor)); |
| 585 } | 587 } |
| 586 #endif // defined(OS_WIN) | 588 #endif // defined(OS_WIN) |
| 587 if (!succeeded) { | 589 if (!succeeded) { |
| 588 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | 590 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); |
| 589 } | 591 } |
| 590 ReleaseProcessIfNeeded(); | 592 ReleaseProcessIfNeeded(); |
| 591 } | 593 } |
| 592 | 594 |
| 595 void ChromeContentUtilityClient::OnRenderPDFPagesToMetafileHandles( | |
| 596 IPC::PlatformFileForTransit pdf_transit, | |
| 597 IPC::PlatformFileForTransit metafile_transit, | |
| 598 const printing::PdfRenderSettings& settings, | |
| 599 const std::vector<printing::PageRange>& page_ranges) { | |
| 600 bool succeeded = false; | |
| 601 #if defined(OS_WIN) | |
| 602 int highest_rendered_page_number = 0; | |
| 603 double scale_factor = 1.0; | |
| 604 base::PlatformFile pdf = | |
| 605 IPC::PlatformFileForTransitToPlatformFile(pdf_transit); | |
| 606 base::PlatformFile metafile = | |
| 607 IPC::PlatformFileForTransitToPlatformFile(metafile_transit); | |
| 608 succeeded = RenderPDFToWinMetafileHandles(pdf, | |
| 609 metafile, | |
| 610 settings, | |
| 611 page_ranges, | |
| 612 &highest_rendered_page_number, | |
| 613 &scale_factor); | |
| 614 if (succeeded) { | |
| 615 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( | |
| 616 highest_rendered_page_number, scale_factor)); | |
| 617 } | |
| 618 #endif // defined(OS_WIN) | |
| 619 if (!succeeded) { | |
| 620 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); | |
| 621 } | |
| 622 ReleaseProcessIfNeeded(); | |
| 623 } | |
| 624 | |
| 593 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster( | 625 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster( |
| 594 IPC::PlatformFileForTransit pdf_transit, | 626 IPC::PlatformFileForTransit pdf_transit, |
| 595 const printing::PdfRenderSettings& settings, | 627 const printing::PdfRenderSettings& settings, |
| 596 const printing::PwgRasterSettings& bitmap_settings, | 628 const printing::PwgRasterSettings& bitmap_settings, |
| 597 IPC::PlatformFileForTransit bitmap_transit) { | 629 IPC::PlatformFileForTransit bitmap_transit) { |
| 598 base::PlatformFile pdf = | 630 base::PlatformFile pdf = |
| 599 IPC::PlatformFileForTransitToPlatformFile(pdf_transit); | 631 IPC::PlatformFileForTransitToPlatformFile(pdf_transit); |
| 600 base::PlatformFile bitmap = | 632 base::PlatformFile bitmap = |
| 601 IPC::PlatformFileForTransitToPlatformFile(bitmap_transit); | 633 IPC::PlatformFileForTransitToPlatformFile(bitmap_transit); |
| 602 if (RenderPDFPagesToPWGRaster(pdf, settings, bitmap_settings, bitmap)) { | 634 if (RenderPDFPagesToPWGRaster(pdf, settings, bitmap_settings, bitmap)) { |
| 603 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | 635 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); |
| 604 } else { | 636 } else { |
| 605 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | 637 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); |
| 606 } | 638 } |
| 607 ReleaseProcessIfNeeded(); | 639 ReleaseProcessIfNeeded(); |
| 608 } | 640 } |
| 609 | 641 |
| 610 #if defined(OS_WIN) | 642 #if defined(OS_WIN) |
| 611 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( | 643 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( |
| 612 base::PlatformFile pdf_file, | 644 base::PlatformFile pdf_file, |
| 613 const base::FilePath& metafile_path, | 645 const base::FilePath& metafile_path, |
| 614 const printing::PdfRenderSettings& settings, | 646 const printing::PdfRenderSettings& settings, |
| 615 const std::vector<printing::PageRange>& page_ranges, | 647 const std::vector<printing::PageRange>& page_ranges, |
| 616 int* highest_rendered_page_number, | 648 int* highest_rendered_page_number, |
| 617 double* scale_factor) { | 649 double* scale_factor) { |
| 650 base::File metafile; | |
| 651 metafile.Initialize(metafile_path, | |
| 652 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_APPEND); | |
| 653 return RenderPDFToWinMetafileHandles(pdf_file, | |
| 654 metafile.GetPlatformFile(), | |
| 655 settings, | |
| 656 page_ranges, | |
| 657 highest_rendered_page_number, | |
| 658 scale_factor); | |
| 659 } | |
| 660 | |
| 661 bool ChromeContentUtilityClient::RenderPDFToWinMetafileHandles( | |
| 662 base::PlatformFile pdf_file, | |
| 663 base::PlatformFile metafile_file, | |
| 664 const printing::PdfRenderSettings& settings, | |
| 665 const std::vector<printing::PageRange>& page_ranges, | |
| 666 int* highest_rendered_page_number, | |
| 667 double* scale_factor) { | |
| 618 *highest_rendered_page_number = -1; | 668 *highest_rendered_page_number = -1; |
| 619 *scale_factor = 1.0; | 669 *scale_factor = 1.0; |
| 620 base::win::ScopedHandle file(pdf_file); | 670 base::win::ScopedHandle file(pdf_file); |
| 621 | 671 |
| 622 if (!g_pdf_lib.Get().IsValid()) | 672 if (!g_pdf_lib.Get().IsValid()) |
| 623 return false; | 673 return false; |
| 624 | 674 |
| 625 // TODO(sanjeevr): Add a method to the PDF DLL that takes in a file handle | 675 // 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 | 676 // and a page range array. That way we don't need to read the entire PDF into |
| 627 // memory. | 677 // memory. |
| 628 DWORD length = ::GetFileSize(file, NULL); | 678 DWORD length = ::GetFileSize(file, NULL); |
| 629 if (length == INVALID_FILE_SIZE) | 679 if (length == INVALID_FILE_SIZE) |
| 630 return false; | 680 return false; |
| 631 | 681 |
| 632 std::vector<uint8> buffer; | 682 std::vector<uint8> buffer; |
| 633 buffer.resize(length); | 683 buffer.resize(length); |
| 634 DWORD bytes_read = 0; | 684 DWORD bytes_read = 0; |
| 635 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || | 685 if (!ReadFile(pdf_file, &buffer.front(), length, &bytes_read, NULL) || |
| 636 (bytes_read != length)) { | 686 (bytes_read != length)) { |
| 637 return false; | 687 return false; |
| 638 } | 688 } |
| 639 | 689 |
| 640 int total_page_count = 0; | 690 int total_page_count = 0; |
| 641 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(), | 691 if (!g_pdf_lib.Get().GetPDFDocInfo(&buffer.front(), buffer.size(), |
| 642 &total_page_count, NULL)) { | 692 &total_page_count, NULL)) { |
| 643 return false; | 693 return false; |
| 644 } | 694 } |
| 645 | 695 |
| 646 printing::Emf metafile; | 696 printing::Emf metafile; |
| 647 metafile.InitToFile(metafile_path); | 697 metafile.Init(); |
| 648 // We need to scale down DC to fit an entire page into DC available area. | 698 // We need to scale down DC to fit an entire page into DC available area. |
| 649 // Current metafile is based on screen DC and have current screen size. | 699 // Current metafile is based on screen DC and have current screen size. |
| 650 // Writing outside of those boundaries will result in the cut-off output. | 700 // Writing outside of those boundaries will result in the cut-off output. |
| 651 // On metafiles (this is the case here), scaling down will still record | 701 // On metafiles (this is the case here), scaling down will still record |
| 652 // original coordinates and we'll be able to print in full resolution. | 702 // original coordinates and we'll be able to print in full resolution. |
| 653 // Before playback we'll need to counter the scaling up that will happen | 703 // Before playback we'll need to counter the scaling up that will happen |
| 654 // in the service (print_system_win.cc). | 704 // in the service (print_system_win.cc). |
| 655 *scale_factor = gfx::CalculatePageScale(metafile.context(), | 705 *scale_factor = gfx::CalculatePageScale(metafile.context(), |
| 656 settings.area().right(), | 706 settings.area().right(), |
| 657 settings.area().bottom()); | 707 settings.area().bottom()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 673 settings.area().height(), true, false, true, true, | 723 settings.area().height(), true, false, true, true, |
| 674 settings.autorotate())) { | 724 settings.autorotate())) { |
| 675 if (*highest_rendered_page_number < page_number) | 725 if (*highest_rendered_page_number < page_number) |
| 676 *highest_rendered_page_number = page_number; | 726 *highest_rendered_page_number = page_number; |
| 677 ret = true; | 727 ret = true; |
| 678 } | 728 } |
| 679 metafile.FinishPage(); | 729 metafile.FinishPage(); |
| 680 } | 730 } |
| 681 } | 731 } |
| 682 metafile.FinishDocument(); | 732 metafile.FinishDocument(); |
| 733 | |
| 734 uint32 final_size = metafile.GetDataSize(); | |
| 735 scoped_ptr<char[]> final_buffer(new char[final_size]); | |
| 736 CHECK(metafile.GetData(final_buffer.get(), final_size)); | |
| 737 CHECK_EQ(static_cast<int>(final_size), | |
|
Vitaly Buka (NO REVIEWS)
2014/04/25 22:35:24
This could be huge, metafile keeps images uncompre
scottmg
2014/04/25 23:07:18
Sorry, I'm not sure what you mean here. You mean c
Vitaly Buka (NO REVIEWS)
2014/04/28 06:47:33
It's not about rasterize.
You don't need to try t
Vitaly Buka (NO REVIEWS)
2014/04/28 06:49:07
base::WritePlatformFile -> base::WritePlatformFile
| |
| 738 base::WritePlatformFile( | |
| 739 metafile_file, 0, final_buffer.get(), final_size)); | |
| 683 return ret; | 740 return ret; |
| 684 } | 741 } |
| 685 #endif // defined(OS_WIN) | 742 #endif // defined(OS_WIN) |
| 686 | 743 |
| 687 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster( | 744 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster( |
| 688 base::PlatformFile pdf_file, | 745 base::PlatformFile pdf_file, |
| 689 const printing::PdfRenderSettings& settings, | 746 const printing::PdfRenderSettings& settings, |
| 690 const printing::PwgRasterSettings& bitmap_settings, | 747 const printing::PwgRasterSettings& bitmap_settings, |
| 691 base::PlatformFile bitmap_file) { | 748 base::PlatformFile bitmap_file) { |
| 692 bool autoupdate = true; | 749 bool autoupdate = true; |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1020 NetworkingPrivateCrypto crypto; | 1077 NetworkingPrivateCrypto crypto; |
| 1021 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); | 1078 success = crypto.EncryptByteString(public_key, key_data, &ciphertext); |
| 1022 } | 1079 } |
| 1023 | 1080 |
| 1024 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, | 1081 Send(new ChromeUtilityHostMsg_GotEncryptedWiFiCredentials(ciphertext, |
| 1025 success)); | 1082 success)); |
| 1026 } | 1083 } |
| 1027 #endif // defined(OS_WIN) | 1084 #endif // defined(OS_WIN) |
| 1028 | 1085 |
| 1029 } // namespace chrome | 1086 } // namespace chrome |
| OLD | NEW |