| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/webui/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #if !defined(OS_CHROMEOS) | 10 #if !defined(OS_CHROMEOS) |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 622 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 623 | 623 |
| 624 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 624 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
| 625 | 625 |
| 626 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); | 626 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); |
| 627 if (print_to_cloud) { | 627 if (print_to_cloud) { |
| 628 std::string print_ticket; | 628 std::string print_ticket; |
| 629 args->GetString(1, &print_ticket); | 629 args->GetString(1, &print_ticket); |
| 630 SendCloudPrintJob(*settings, print_ticket); | 630 SendCloudPrintJob(*settings, print_ticket); |
| 631 } else if (print_to_pdf) { | 631 } else if (print_to_pdf) { |
| 632 HandlePrintToPdf(*settings); |
| 633 } else { |
| 634 ReportPrintSettingsStats(*settings); |
| 635 ReportUserActionHistogram(PRINT_TO_PRINTER); |
| 636 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", |
| 637 GetPageCountFromSettingsDictionary(*settings)); |
| 638 |
| 639 // This tries to activate the initiator tab as well, so do not clear the |
| 640 // association with the initiator tab yet. |
| 641 HidePreviewTab(); |
| 642 |
| 643 // Do this so the initiator tab can open a new print preview tab. |
| 644 ClearInitiatorTabDetails(); |
| 645 |
| 646 // The PDF being printed contains only the pages that the user selected, |
| 647 // so ignore the page range and print all pages. |
| 648 settings->Remove(printing::kSettingPageRange, NULL); |
| 649 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
| 650 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 651 } |
| 652 } |
| 653 |
| 654 void PrintPreviewHandler::HandlePrintToPdf( |
| 655 const base::DictionaryValue& settings) { |
| 656 if (print_to_pdf_path_.get()) { |
| 657 // User has already selected a path, no need to show the dialog again. |
| 658 PostPrintToPdfTask(); |
| 659 } else if (!select_file_dialog_.get() || !select_file_dialog_->IsRunning( |
| 660 platform_util::GetTopLevel(preview_tab()->GetNativeView()))) { |
| 632 ReportUserActionHistogram(PRINT_TO_PDF); | 661 ReportUserActionHistogram(PRINT_TO_PDF); |
| 633 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", | 662 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", |
| 634 GetPageCountFromSettingsDictionary(*settings)); | 663 GetPageCountFromSettingsDictionary(settings)); |
| 635 | 664 |
| 636 // Pre-populating select file dialog with print job title. | 665 // Pre-populating select file dialog with print job title. |
| 637 string16 print_job_title_utf16 = | 666 string16 print_job_title_utf16 = |
| 638 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); | 667 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); |
| 639 | 668 |
| 640 #if defined(OS_WIN) | 669 #if defined(OS_WIN) |
| 641 FilePath::StringType print_job_title(print_job_title_utf16); | 670 FilePath::StringType print_job_title(print_job_title_utf16); |
| 642 #elif defined(OS_POSIX) | 671 #elif defined(OS_POSIX) |
| 643 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); | 672 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); |
| 644 #endif | 673 #endif |
| 645 | 674 |
| 646 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | 675 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
| 647 FilePath default_filename(print_job_title); | 676 FilePath default_filename(print_job_title); |
| 648 default_filename = | 677 default_filename = |
| 649 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | 678 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
| 650 | 679 |
| 651 SelectFile(default_filename); | 680 SelectFile(default_filename); |
| 652 } else { | |
| 653 ReportPrintSettingsStats(*settings); | |
| 654 ReportUserActionHistogram(PRINT_TO_PRINTER); | |
| 655 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | |
| 656 GetPageCountFromSettingsDictionary(*settings)); | |
| 657 | |
| 658 // This tries to activate the initiator tab as well, so do not clear the | |
| 659 // association with the initiator tab yet. | |
| 660 HidePreviewTab(); | |
| 661 | |
| 662 // Do this so the initiator tab can open a new print preview tab. | |
| 663 ClearInitiatorTabDetails(); | |
| 664 | |
| 665 // The PDF being printed contains only the pages that the user selected, | |
| 666 // so ignore the page range and print all pages. | |
| 667 settings->Remove(printing::kSettingPageRange, NULL); | |
| 668 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); | |
| 669 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | |
| 670 } | 681 } |
| 671 } | 682 } |
| 672 | 683 |
| 673 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { | 684 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { |
| 674 HidePreviewTab(); | 685 HidePreviewTab(); |
| 675 } | 686 } |
| 676 | 687 |
| 677 void PrintPreviewHandler::HandleCancelPendingPrintRequest( | 688 void PrintPreviewHandler::HandleCancelPendingPrintRequest( |
| 678 const ListValue* /*args*/) { | 689 const ListValue* /*args*/) { |
| 679 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 690 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 reported_failed_preview_ = true; | 985 reported_failed_preview_ = true; |
| 975 ReportUserActionHistogram(PREVIEW_FAILED); | 986 ReportUserActionHistogram(PREVIEW_FAILED); |
| 976 } | 987 } |
| 977 | 988 |
| 978 void PrintPreviewHandler::ShowSystemDialog() { | 989 void PrintPreviewHandler::ShowSystemDialog() { |
| 979 HandleShowSystemDialog(NULL); | 990 HandleShowSystemDialog(NULL); |
| 980 } | 991 } |
| 981 | 992 |
| 982 void PrintPreviewHandler::FileSelected(const FilePath& path, | 993 void PrintPreviewHandler::FileSelected(const FilePath& path, |
| 983 int index, void* params) { | 994 int index, void* params) { |
| 995 // Updating last_saved_path_ to the newly selected folder. |
| 996 *last_saved_path_ = path.DirName(); |
| 997 |
| 998 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 999 print_preview_ui->CallJavascriptFunction("fileSelectionCompleted"); |
| 1000 scoped_refptr<RefCountedBytes> data; |
| 1001 print_preview_ui->GetPrintPreviewDataForIndex( |
| 1002 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); |
| 1003 print_to_pdf_path_.reset(new FilePath(path)); |
| 1004 if (data.get()) |
| 1005 PostPrintToPdfTask(); |
| 1006 } |
| 1007 |
| 1008 void PrintPreviewHandler::PostPrintToPdfTask() { |
| 984 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 1009 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 985 scoped_refptr<RefCountedBytes> data; | 1010 scoped_refptr<RefCountedBytes> data; |
| 986 print_preview_ui->GetPrintPreviewDataForIndex( | 1011 print_preview_ui->GetPrintPreviewDataForIndex( |
| 987 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | 1012 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); |
| 988 if (!data.get()) { | 1013 DCHECK(data.get()); |
| 989 NOTREACHED(); | |
| 990 return; | |
| 991 } | |
| 992 | |
| 993 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 1014 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
| 994 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); | 1015 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); |
| 995 | 1016 PrintToPdfTask* task = new PrintToPdfTask(metafile, |
| 996 // Updating last_saved_path_ to the newly selected folder. | 1017 *print_to_pdf_path_); |
| 997 *last_saved_path_ = path.DirName(); | |
| 998 | |
| 999 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | |
| 1000 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 1018 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 1001 | 1019 print_to_pdf_path_.reset(); |
| 1002 ActivateInitiatorTabAndClosePreviewTab(); | 1020 ActivateInitiatorTabAndClosePreviewTab(); |
| 1003 } | 1021 } |
| 1004 | 1022 |
| 1005 void PrintPreviewHandler::FileSelectionCanceled(void* params) { | 1023 void PrintPreviewHandler::FileSelectionCanceled(void* params) { |
| 1006 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 1024 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
| 1007 print_preview_ui->OnFileSelectionCancelled(); | 1025 print_preview_ui->OnFileSelectionCancelled(); |
| 1008 } | 1026 } |
| 1009 | 1027 |
| 1010 void PrintPreviewHandler::HidePreviewTab() { | 1028 void PrintPreviewHandler::HidePreviewTab() { |
| 1011 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) | 1029 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) |
| 1012 return; | 1030 return; |
| 1013 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); | 1031 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); |
| 1014 } | 1032 } |
| 1015 | 1033 |
| 1016 void PrintPreviewHandler::ClearInitiatorTabDetails() { | 1034 void PrintPreviewHandler::ClearInitiatorTabDetails() { |
| 1017 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 1035 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 1018 if (!initiator_tab) | 1036 if (!initiator_tab) |
| 1019 return; | 1037 return; |
| 1020 | 1038 |
| 1021 // We no longer require the initiator tab details. Remove those details | 1039 // We no longer require the initiator tab details. Remove those details |
| 1022 // associated with the preview tab to allow the initiator tab to create | 1040 // associated with the preview tab to allow the initiator tab to create |
| 1023 // another preview tab. | 1041 // another preview tab. |
| 1024 printing::PrintPreviewTabController* tab_controller = | 1042 printing::PrintPreviewTabController* tab_controller = |
| 1025 printing::PrintPreviewTabController::GetInstance(); | 1043 printing::PrintPreviewTabController::GetInstance(); |
| 1026 if (tab_controller) | 1044 if (tab_controller) |
| 1027 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 1045 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
| 1028 } | 1046 } |
| OLD | NEW |