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

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

Issue 79363002: Convert PDF to PWG Raster using utility process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/scoped_native_library.h" 16 #include "base/scoped_native_library.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/common/chrome_paths.h" 18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_utility_messages.h" 19 #include "chrome/common/chrome_utility_messages.h"
20 #include "chrome/common/extensions/chrome_extensions_client.h" 20 #include "chrome/common/extensions/chrome_extensions_client.h"
21 #include "chrome/common/extensions/extension_l10n_util.h" 21 #include "chrome/common/extensions/extension_l10n_util.h"
22 #include "chrome/common/extensions/update_manifest.h" 22 #include "chrome/common/extensions/update_manifest.h"
23 #include "chrome/common/safe_browsing/zip_analyzer.h" 23 #include "chrome/common/safe_browsing/zip_analyzer.h"
24 #include "chrome/utility/cloud_print/bitmap_image.h"
25 #include "chrome/utility/cloud_print/pwg_encoder.h"
24 #include "chrome/utility/extensions/unpacker.h" 26 #include "chrome/utility/extensions/unpacker.h"
25 #include "chrome/utility/profile_import_handler.h" 27 #include "chrome/utility/profile_import_handler.h"
26 #include "chrome/utility/web_resource_unpacker.h" 28 #include "chrome/utility/web_resource_unpacker.h"
27 #include "content/public/child/image_decoder_utils.h" 29 #include "content/public/child/image_decoder_utils.h"
28 #include "content/public/common/content_paths.h" 30 #include "content/public/common/content_paths.h"
29 #include "content/public/utility/utility_thread.h" 31 #include "content/public/utility/utility_thread.h"
30 #include "extensions/common/extension.h" 32 #include "extensions/common/extension.h"
31 #include "extensions/common/manifest.h" 33 #include "extensions/common/manifest.h"
32 #include "media/base/media.h" 34 #include "media/base/media.h"
33 #include "media/base/media_file_checker.h" 35 #include "media/base/media_file_checker.h"
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message) 322 IPC_BEGIN_MESSAGE_MAP(ChromeContentUtilityClient, message)
321 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension) 323 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension, OnUnpackExtension)
322 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource, 324 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackWebResource,
323 OnUnpackWebResource) 325 OnUnpackWebResource)
324 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest, 326 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseUpdateManifest,
325 OnParseUpdateManifest) 327 OnParseUpdateManifest)
326 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage) 328 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImage, OnDecodeImage)
327 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64) 329 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DecodeImageBase64, OnDecodeImageBase64)
328 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile, 330 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafile,
329 OnRenderPDFPagesToMetafile) 331 OnRenderPDFPagesToMetafile)
332 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToPWGRaster,
333 OnRenderPDFPagesToPWGRaster)
330 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage, 334 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RobustJPEGDecodeImage,
331 OnRobustJPEGDecodeImage) 335 OnRobustJPEGDecodeImage)
332 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON) 336 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ParseJSON, OnParseJSON)
333 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults, 337 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_GetPrinterCapsAndDefaults,
334 OnGetPrinterCapsAndDefaults) 338 OnGetPrinterCapsAndDefaults)
335 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing, OnStartupPing) 339 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_StartupPing, OnStartupPing)
336 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection, 340 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_AnalyzeZipFileForDownloadProtection,
337 OnAnalyzeZipFileForDownloadProtection) 341 OnAnalyzeZipFileForDownloadProtection)
338 342
339 #if !defined(OS_ANDROID) && !defined(OS_IOS) 343 #if !defined(OS_ANDROID) && !defined(OS_IOS)
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded( 526 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded(
523 highest_rendered_page_number, scale_factor)); 527 highest_rendered_page_number, scale_factor));
524 } 528 }
525 #endif // defined(OS_WIN) 529 #endif // defined(OS_WIN)
526 if (!succeeded) { 530 if (!succeeded) {
527 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed()); 531 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed());
528 } 532 }
529 ReleaseProcessIfNeeded(); 533 ReleaseProcessIfNeeded();
530 } 534 }
531 535
536 void ChromeContentUtilityClient::OnRenderPDFPagesToPWGRaster(
537 IPC::PlatformFileForTransit pdf_transit,
538 const printing::PdfRenderSettings& settings,
539 IPC::PlatformFileForTransit bitmap_transit) {
540 base::PlatformFile pdf =
541 IPC::PlatformFileForTransitToPlatformFile(pdf_transit);
542 base::PlatformFile bitmap =
543 IPC::PlatformFileForTransitToPlatformFile(bitmap_transit);
544 if (RenderPDFPagesToPWGRaster(pdf, settings, bitmap)) {
545 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded());
546 } else {
547 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed());
548 }
549 ReleaseProcessIfNeeded();
550 }
551
532 #if defined(OS_WIN) 552 #if defined(OS_WIN)
533 bool ChromeContentUtilityClient::RenderPDFToWinMetafile( 553 bool ChromeContentUtilityClient::RenderPDFToWinMetafile(
534 base::PlatformFile pdf_file, 554 base::PlatformFile pdf_file,
535 const base::FilePath& metafile_path, 555 const base::FilePath& metafile_path,
536 const printing::PdfRenderSettings& settings, 556 const printing::PdfRenderSettings& settings,
537 const std::vector<printing::PageRange>& page_ranges, 557 const std::vector<printing::PageRange>& page_ranges,
538 int* highest_rendered_page_number, 558 int* highest_rendered_page_number,
539 double* scale_factor) { 559 double* scale_factor) {
540 *highest_rendered_page_number = -1; 560 *highest_rendered_page_number = -1;
541 *scale_factor = 1.0; 561 *scale_factor = 1.0;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 ret = true; 619 ret = true;
600 } 620 }
601 metafile.FinishPage(); 621 metafile.FinishPage();
602 } 622 }
603 } 623 }
604 metafile.FinishDocument(); 624 metafile.FinishDocument();
605 return ret; 625 return ret;
606 } 626 }
607 #endif // defined(OS_WIN) 627 #endif // defined(OS_WIN)
608 628
629 bool ChromeContentUtilityClient::RenderPDFPagesToPWGRaster(
630 base::PlatformFile pdf_file,
631 const printing::PdfRenderSettings& settings,
632 base::PlatformFile bitmap_file) {
633 bool autoupdate = true;
634 if (!g_pdf_lib.Get().IsValid())
635 return false;
636
637 base::PlatformFileInfo info;
638 if (!base::GetPlatformFileInfo(pdf_file, &info) || info.size <= 0)
639 return false;
640
641 std::vector<char> data(info.size, 0);
642 data.resize(base::ReadPlatformFile(pdf_file, 0, &data[0], data.size()));
Lei Zhang 2013/11/21 20:51:04 Can't ReadPlatformFile() fail for some reason and
Vitaly Buka (NO REVIEWS) 2013/11/22 00:13:00 Done.
643
644 int total_page_count = 0;
645 if (!g_pdf_lib.Get().GetPDFDocInfo(&data.front(), data.size(),
646 &total_page_count, NULL)) {
647 return false;
648 }
649
650 cloud_print::PwgEncoder encoder;
651 std::string pwg_header;
652 encoder.EncodeDocumentHeader(&pwg_header);
653 if (!base::WritePlatformFile(bitmap_file, 0, &pwg_header.front(),
654 pwg_header.size())) {
655 return false;
656 }
657
658 cloud_print::BitmapImage image(settings.area().size(),
659 cloud_print::BitmapImage::BGRA);
660 for (int i = 0; i < total_page_count; ++i) {
661 if (!g_pdf_lib.Get().RenderPDFPageToBitmap(
662 &data.front(), data.size(), i, image.pixel_data(),
663 image.size().width(), image.size().height(), settings.dpi(),
664 settings.dpi(), autoupdate)) {
665 return false;
666 }
667 std::string pwg_page;
668 if (!encoder.EncodePage(image, settings.dpi(), total_page_count,
669 &pwg_page) ||
670 !base::WritePlatformFile(bitmap_file, 0, &pwg_page.front(),
Lei Zhang 2013/11/21 20:51:04 Doesn't this overwrite |bitmap_file| on every loop
Vitaly Buka (NO REVIEWS) 2013/11/22 00:13:00 File is opened in APPEND mode, so offset is ignore
671 pwg_page.size())) {
672 return false;
673 }
674 }
675 return true;
676 }
677
609 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage( 678 void ChromeContentUtilityClient::OnRobustJPEGDecodeImage(
610 const std::vector<unsigned char>& encoded_data) { 679 const std::vector<unsigned char>& encoded_data) {
611 // Our robust jpeg decoding is using IJG libjpeg. 680 // Our robust jpeg decoding is using IJG libjpeg.
612 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG) { 681 if (gfx::JPEGCodec::JpegLibraryVariant() == gfx::JPEGCodec::IJG_LIBJPEG) {
613 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodec::Decode( 682 scoped_ptr<SkBitmap> decoded_image(gfx::JPEGCodec::Decode(
614 &encoded_data[0], encoded_data.size())); 683 &encoded_data[0], encoded_data.size()));
615 if (!decoded_image.get() || decoded_image->empty()) { 684 if (!decoded_image.get() || decoded_image->empty()) {
616 Send(new ChromeUtilityHostMsg_DecodeImage_Failed()); 685 Send(new ChromeUtilityHostMsg_DecodeImage_Failed());
617 } else { 686 } else {
618 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image)); 687 Send(new ChromeUtilityHostMsg_DecodeImage_Succeeded(*decoded_image));
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 picasa::PicasaAlbumsIndexer indexer(album_uids); 823 picasa::PicasaAlbumsIndexer indexer(album_uids);
755 indexer.ParseFolderINI(folders_inis); 824 indexer.ParseFolderINI(folders_inis);
756 825
757 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished( 826 Send(new ChromeUtilityHostMsg_IndexPicasaAlbumsContents_Finished(
758 indexer.albums_images())); 827 indexer.albums_images()));
759 ReleaseProcessIfNeeded(); 828 ReleaseProcessIfNeeded();
760 } 829 }
761 #endif // defined(OS_WIN) || defined(OS_MACOSX) 830 #endif // defined(OS_WIN) || defined(OS_MACOSX)
762 831
763 } // namespace chrome 832 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698