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 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 601 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
602 | 602 |
603 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 603 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
604 | 604 |
605 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); | 605 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); |
606 if (print_to_cloud) { | 606 if (print_to_cloud) { |
607 std::string print_ticket; | 607 std::string print_ticket; |
608 args->GetString(1, &print_ticket); | 608 args->GetString(1, &print_ticket); |
609 SendCloudPrintJob(*settings, print_ticket); | 609 SendCloudPrintJob(*settings, print_ticket); |
610 } else if (print_to_pdf) { | 610 } else if (print_to_pdf) { |
611 HandlePrintToPdf(*settings.get()); | |
kmadhusu
2011/09/08 18:01:47
HandlePrintToPdf(*settings);
dpapad
2011/09/08 19:11:33
Done.
| |
612 } else { | |
613 ReportPrintSettingsStats(*settings); | |
614 ReportUserActionHistogram(PRINT_TO_PRINTER); | |
615 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | |
616 GetPageCountFromSettingsDictionary(*settings)); | |
617 | |
618 // This tries to activate the initiator tab as well, so do not clear the | |
619 // association with the initiator tab yet. | |
620 HidePreviewTab(); | |
621 | |
622 // Do this so the initiator tab can open a new print preview tab. | |
623 ClearInitiatorTabDetails(); | |
624 | |
625 // The PDF being printed contains only the pages that the user selected, | |
626 // so ignore the page range and print all pages. | |
627 settings->Remove(printing::kSettingPageRange, NULL); | |
628 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); | |
629 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | |
630 } | |
631 } | |
632 | |
633 void PrintPreviewHandler::HandlePrintToPdf( | |
634 const base::DictionaryValue& settings) { | |
635 if (print_to_pdf_path_.get()) { | |
636 // User has already selected a path, no need to show the dialog again. | |
637 PostPrintToPdfTask(); | |
638 } else if (!select_file_dialog_.get() || !select_file_dialog_->IsRunning( | |
639 platform_util::GetTopLevel(preview_tab()->GetNativeView()))) { | |
611 ReportUserActionHistogram(PRINT_TO_PDF); | 640 ReportUserActionHistogram(PRINT_TO_PDF); |
612 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", | 641 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", |
613 GetPageCountFromSettingsDictionary(*settings)); | 642 GetPageCountFromSettingsDictionary(settings)); |
614 | 643 |
615 // Pre-populating select file dialog with print job title. | 644 // Pre-populating select file dialog with print job title. |
616 string16 print_job_title_utf16 = | 645 string16 print_job_title_utf16 = |
617 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); | 646 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); |
618 | 647 |
619 #if defined(OS_WIN) | 648 #if defined(OS_WIN) |
620 FilePath::StringType print_job_title(print_job_title_utf16); | 649 FilePath::StringType print_job_title(print_job_title_utf16); |
621 #elif defined(OS_POSIX) | 650 #elif defined(OS_POSIX) |
622 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); | 651 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); |
623 #endif | 652 #endif |
624 | 653 |
625 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | 654 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
626 FilePath default_filename(print_job_title); | 655 FilePath default_filename(print_job_title); |
627 default_filename = | 656 default_filename = |
628 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | 657 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
629 | 658 |
630 SelectFile(default_filename); | 659 SelectFile(default_filename); |
631 } else { | |
632 ReportPrintSettingsStats(*settings); | |
633 ReportUserActionHistogram(PRINT_TO_PRINTER); | |
634 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | |
635 GetPageCountFromSettingsDictionary(*settings)); | |
636 | |
637 // This tries to activate the initiator tab as well, so do not clear the | |
638 // association with the initiator tab yet. | |
639 HidePreviewTab(); | |
640 | |
641 // Do this so the initiator tab can open a new print preview tab. | |
642 ClearInitiatorTabDetails(); | |
643 | |
644 // The PDF being printed contains only the pages that the user selected, | |
645 // so ignore the page range and print all pages. | |
646 settings->Remove(printing::kSettingPageRange, NULL); | |
647 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); | |
648 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | |
649 } | 660 } |
650 } | 661 } |
651 | 662 |
652 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { | 663 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { |
653 HidePreviewTab(); | 664 HidePreviewTab(); |
654 } | 665 } |
655 | 666 |
656 void PrintPreviewHandler::HandleCancelPendingPrintRequest( | 667 void PrintPreviewHandler::HandleCancelPendingPrintRequest( |
657 const ListValue* /*args*/) { | 668 const ListValue* /*args*/) { |
658 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 669 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 reported_failed_preview_ = true; | 964 reported_failed_preview_ = true; |
954 ReportUserActionHistogram(PREVIEW_FAILED); | 965 ReportUserActionHistogram(PREVIEW_FAILED); |
955 } | 966 } |
956 | 967 |
957 void PrintPreviewHandler::ShowSystemDialog() { | 968 void PrintPreviewHandler::ShowSystemDialog() { |
958 HandleShowSystemDialog(NULL); | 969 HandleShowSystemDialog(NULL); |
959 } | 970 } |
960 | 971 |
961 void PrintPreviewHandler::FileSelected(const FilePath& path, | 972 void PrintPreviewHandler::FileSelected(const FilePath& path, |
962 int index, void* params) { | 973 int index, void* params) { |
974 // Updating last_saved_path_ to the newly selected folder. | |
975 *last_saved_path_ = path.DirName(); | |
976 | |
977 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | |
978 print_preview_ui->CallJavascriptFunction("fileSelectionCompleted"); | |
979 scoped_refptr<RefCountedBytes> data; | |
980 print_preview_ui->GetPrintPreviewDataForIndex( | |
981 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | |
982 print_to_pdf_path_.reset(new FilePath(path)); | |
983 if (data.get()) | |
984 PostPrintToPdfTask(); | |
985 } | |
986 | |
987 void PrintPreviewHandler::PostPrintToPdfTask() { | |
963 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 988 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
964 scoped_refptr<RefCountedBytes> data; | 989 scoped_refptr<RefCountedBytes> data; |
965 print_preview_ui->GetPrintPreviewDataForIndex( | 990 print_preview_ui->GetPrintPreviewDataForIndex( |
966 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | 991 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); |
967 if (!data.get()) { | 992 DCHECK(data.get()); |
968 NOTREACHED(); | |
969 return; | |
970 } | |
971 | |
972 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 993 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
973 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); | 994 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); |
974 | 995 PrintToPdfTask* task = new PrintToPdfTask(metafile, |
975 // Updating last_saved_path_ to the newly selected folder. | 996 *print_to_pdf_path_.get()); |
kmadhusu
2011/09/08 18:01:47
"*print_to_pdf_path_" is sufficient. ".get()" is n
dpapad
2011/09/08 19:11:33
Done.
| |
976 *last_saved_path_ = path.DirName(); | |
977 | |
978 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | |
979 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 997 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
980 | 998 print_to_pdf_path_.reset(); |
981 ActivateInitiatorTabAndClosePreviewTab(); | 999 ActivateInitiatorTabAndClosePreviewTab(); |
982 } | 1000 } |
983 | 1001 |
984 void PrintPreviewHandler::FileSelectionCanceled(void* params) { | 1002 void PrintPreviewHandler::FileSelectionCanceled(void* params) { |
985 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 1003 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
986 print_preview_ui->OnFileSelectionCancelled(); | 1004 print_preview_ui->OnFileSelectionCancelled(); |
987 } | 1005 } |
988 | 1006 |
989 void PrintPreviewHandler::HidePreviewTab() { | 1007 void PrintPreviewHandler::HidePreviewTab() { |
990 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) | 1008 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) |
991 return; | 1009 return; |
992 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); | 1010 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); |
993 } | 1011 } |
994 | 1012 |
995 void PrintPreviewHandler::ClearInitiatorTabDetails() { | 1013 void PrintPreviewHandler::ClearInitiatorTabDetails() { |
996 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 1014 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
997 if (!initiator_tab) | 1015 if (!initiator_tab) |
998 return; | 1016 return; |
999 | 1017 |
1000 // We no longer require the initiator tab details. Remove those details | 1018 // We no longer require the initiator tab details. Remove those details |
1001 // associated with the preview tab to allow the initiator tab to create | 1019 // associated with the preview tab to allow the initiator tab to create |
1002 // another preview tab. | 1020 // another preview tab. |
1003 printing::PrintPreviewTabController* tab_controller = | 1021 printing::PrintPreviewTabController* tab_controller = |
1004 printing::PrintPreviewTabController::GetInstance(); | 1022 printing::PrintPreviewTabController::GetInstance(); |
1005 if (tab_controller) | 1023 if (tab_controller) |
1006 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 1024 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
1007 } | 1025 } |
OLD | NEW |