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) |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #endif | 12 #endif |
13 #include "base/i18n/file_util_icu.h" | 13 #include "base/i18n/file_util_icu.h" |
14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
21 #include "base/values.h" | |
22 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/platform_util.h" | 22 #include "chrome/browser/platform_util.h" |
24 #include "chrome/browser/printing/background_printing_manager.h" | 23 #include "chrome/browser/printing/background_printing_manager.h" |
25 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 24 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
26 #include "chrome/browser/printing/printer_manager_dialog.h" | 25 #include "chrome/browser/printing/printer_manager_dialog.h" |
27 #include "chrome/browser/printing/print_preview_tab_controller.h" | 26 #include "chrome/browser/printing/print_preview_tab_controller.h" |
28 #include "chrome/browser/printing/print_view_manager.h" | 27 #include "chrome/browser/printing/print_view_manager.h" |
29 #include "chrome/browser/sessions/restore_tab_helper.h" | 28 #include "chrome/browser/sessions/restore_tab_helper.h" |
30 #include "chrome/browser/tabs/tab_strip_model.h" | 29 #include "chrome/browser/tabs/tab_strip_model.h" |
31 #include "chrome/browser/ui/browser_list.h" | 30 #include "chrome/browser/ui/browser_list.h" |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 600 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
602 | 601 |
603 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 602 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
604 | 603 |
605 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); | 604 bool print_to_cloud = settings->HasKey(printing::kSettingCloudPrintId); |
606 if (print_to_cloud) { | 605 if (print_to_cloud) { |
607 std::string print_ticket; | 606 std::string print_ticket; |
608 args->GetString(1, &print_ticket); | 607 args->GetString(1, &print_ticket); |
609 SendCloudPrintJob(*settings, print_ticket); | 608 SendCloudPrintJob(*settings, print_ticket); |
610 } else if (print_to_pdf) { | 609 } else if (print_to_pdf) { |
611 ReportUserActionHistogram(PRINT_TO_PDF); | 610 HandlePrintToPdf(settings.get()); |
612 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", | |
613 GetPageCountFromSettingsDictionary(*settings)); | |
614 | |
615 // Pre-populating select file dialog with print job title. | |
616 string16 print_job_title_utf16 = | |
617 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); | |
618 | |
619 #if defined(OS_WIN) | |
620 FilePath::StringType print_job_title(print_job_title_utf16); | |
621 #elif defined(OS_POSIX) | |
622 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); | |
623 #endif | |
624 | |
625 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | |
626 FilePath default_filename(print_job_title); | |
627 default_filename = | |
628 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | |
629 | |
630 SelectFile(default_filename); | |
631 } else { | 611 } else { |
632 ReportPrintSettingsStats(*settings); | 612 ReportPrintSettingsStats(*settings); |
633 ReportUserActionHistogram(PRINT_TO_PRINTER); | 613 ReportUserActionHistogram(PRINT_TO_PRINTER); |
634 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | 614 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", |
635 GetPageCountFromSettingsDictionary(*settings)); | 615 GetPageCountFromSettingsDictionary(*settings)); |
636 | 616 |
637 // This tries to activate the initiator tab as well, so do not clear the | 617 // This tries to activate the initiator tab as well, so do not clear the |
638 // association with the initiator tab yet. | 618 // association with the initiator tab yet. |
639 HidePreviewTab(); | 619 HidePreviewTab(); |
640 | 620 |
641 // Do this so the initiator tab can open a new print preview tab. | 621 // Do this so the initiator tab can open a new print preview tab. |
642 ClearInitiatorTabDetails(); | 622 ClearInitiatorTabDetails(); |
643 | 623 |
644 // The PDF being printed contains only the pages that the user selected, | 624 // The PDF being printed contains only the pages that the user selected, |
645 // so ignore the page range and print all pages. | 625 // so ignore the page range and print all pages. |
646 settings->Remove(printing::kSettingPageRange, NULL); | 626 settings->Remove(printing::kSettingPageRange, NULL); |
647 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); | 627 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
648 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 628 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
649 } | 629 } |
650 } | 630 } |
651 | 631 |
632 void PrintPreviewHandler::HandlePrintToPdf(const DictionaryValue* settings) { | |
vandebo (ex-Chrome)
2011/09/07 21:38:19
Will this method be called exactly once? If not,
dpapad
2011/09/07 23:42:39
Good catch. This function will be called twice in
| |
633 ReportUserActionHistogram(PRINT_TO_PDF); | |
634 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", | |
635 GetPageCountFromSettingsDictionary(*settings)); | |
636 | |
637 if (pending_print_to_pdf_path_.get()) { | |
638 // User has already selected a path, no need to show the dialog again. | |
639 PostPrintToPdfTask(*pending_print_to_pdf_path_.get()); | |
vandebo (ex-Chrome)
2011/09/07 21:38:19
Can you explain how we would get to this point, I
dpapad
2011/09/07 23:42:39
In chrome/browser/resources/print_preview/print_he
| |
640 } else if (!select_file_dialog_.get() || !select_file_dialog_->IsRunning( | |
641 platform_util::GetTopLevel(preview_tab()->GetNativeView()))) { | |
642 // Pre-populating select file dialog with print job title. | |
643 string16 print_job_title_utf16 = | |
644 preview_tab_wrapper()->print_view_manager()->RenderSourceName(); | |
645 | |
646 #if defined(OS_WIN) | |
647 FilePath::StringType print_job_title(print_job_title_utf16); | |
648 #elif defined(OS_POSIX) | |
649 FilePath::StringType print_job_title = UTF16ToUTF8(print_job_title_utf16); | |
650 #endif | |
651 | |
652 file_util::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | |
653 FilePath default_filename(print_job_title); | |
654 default_filename = | |
655 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | |
656 | |
657 SelectFile(default_filename); | |
658 } | |
659 } | |
660 | |
652 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { | 661 void PrintPreviewHandler::HandleHidePreview(const ListValue* /*args*/) { |
653 HidePreviewTab(); | 662 HidePreviewTab(); |
654 } | 663 } |
655 | 664 |
656 void PrintPreviewHandler::HandleCancelPendingPrintRequest( | 665 void PrintPreviewHandler::HandleCancelPendingPrintRequest( |
657 const ListValue* /*args*/) { | 666 const ListValue* /*args*/) { |
658 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 667 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
659 if (initiator_tab) { | 668 if (initiator_tab) { |
660 ClearInitiatorTabDetails(); | 669 ClearInitiatorTabDetails(); |
661 } else { | 670 } else { |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 reported_failed_preview_ = true; | 962 reported_failed_preview_ = true; |
954 ReportUserActionHistogram(PREVIEW_FAILED); | 963 ReportUserActionHistogram(PREVIEW_FAILED); |
955 } | 964 } |
956 | 965 |
957 void PrintPreviewHandler::ShowSystemDialog() { | 966 void PrintPreviewHandler::ShowSystemDialog() { |
958 HandleShowSystemDialog(NULL); | 967 HandleShowSystemDialog(NULL); |
959 } | 968 } |
960 | 969 |
961 void PrintPreviewHandler::FileSelected(const FilePath& path, | 970 void PrintPreviewHandler::FileSelected(const FilePath& path, |
962 int index, void* params) { | 971 int index, void* params) { |
972 // Updating last_saved_path_ to the newly selected folder. | |
973 *last_saved_path_ = path.DirName(); | |
kmadhusu
2011/09/07 20:42:57
I think you need the save the path name only after
dpapad
2011/09/07 23:42:39
At this point the user has demonstrated that he wa
kmadhusu
2011/09/08 00:04:33
ok.. leave the code as it is.
| |
974 | |
975 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | |
976 print_preview_ui->OnFileSelectionCompleted(); | |
kmadhusu
2011/09/07 20:42:57
You can remove the changes in print_preview_ui.h a
dpapad
2011/09/07 23:42:39
Done.
| |
977 scoped_refptr<RefCountedBytes> data; | |
978 print_preview_ui->GetPrintPreviewDataForIndex( | |
979 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | |
980 if (!data.get()) { | |
981 pending_print_to_pdf_path_.reset(new FilePath(path)); | |
kmadhusu
2011/09/07 20:42:57
How about renaming pending_print_to_pdf_path_ => p
vandebo (ex-Chrome)
2011/09/07 21:38:19
Then you could also free print_to_pdf_path_ in Pos
dpapad
2011/09/07 23:42:39
Done.
dpapad
2011/09/07 23:42:39
Done. print_to_pdf_path_ is a scoped_ptr already.
| |
982 } else { | |
983 PostPrintToPdfTask(path); | |
984 } | |
985 } | |
986 | |
987 void PrintPreviewHandler::PostPrintToPdfTask(const FilePath& path) { | |
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); |
kmadhusu
2011/09/07 20:42:57
Add a dcheck to validate data.
dpapad
2011/09/07 23:42:39
Done.
| |
967 if (!data.get()) { | |
968 NOTREACHED(); | |
969 return; | |
970 } | |
971 | |
972 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; | 992 printing::PreviewMetafile* metafile = new printing::PreviewMetafile; |
973 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); | 993 metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); |
974 | |
975 // Updating last_saved_path_ to the newly selected folder. | |
976 *last_saved_path_ = path.DirName(); | |
977 | |
978 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); | 994 PrintToPdfTask* task = new PrintToPdfTask(metafile, path); |
979 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 995 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
980 | |
981 ActivateInitiatorTabAndClosePreviewTab(); | 996 ActivateInitiatorTabAndClosePreviewTab(); |
982 } | 997 } |
983 | 998 |
984 void PrintPreviewHandler::FileSelectionCanceled(void* params) { | 999 void PrintPreviewHandler::FileSelectionCanceled(void* params) { |
985 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); | 1000 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); |
986 print_preview_ui->OnFileSelectionCancelled(); | 1001 print_preview_ui->OnFileSelectionCancelled(); |
987 } | 1002 } |
988 | 1003 |
989 void PrintPreviewHandler::HidePreviewTab() { | 1004 void PrintPreviewHandler::HidePreviewTab() { |
990 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) | 1005 if (GetBackgroundPrintingManager()->HasPrintPreviewTab(preview_tab_wrapper())) |
991 return; | 1006 return; |
992 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); | 1007 GetBackgroundPrintingManager()->OwnPrintPreviewTab(preview_tab_wrapper()); |
993 } | 1008 } |
994 | 1009 |
995 void PrintPreviewHandler::ClearInitiatorTabDetails() { | 1010 void PrintPreviewHandler::ClearInitiatorTabDetails() { |
996 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 1011 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
997 if (!initiator_tab) | 1012 if (!initiator_tab) |
998 return; | 1013 return; |
999 | 1014 |
1000 // We no longer require the initiator tab details. Remove those details | 1015 // We no longer require the initiator tab details. Remove those details |
1001 // associated with the preview tab to allow the initiator tab to create | 1016 // associated with the preview tab to allow the initiator tab to create |
1002 // another preview tab. | 1017 // another preview tab. |
1003 printing::PrintPreviewTabController* tab_controller = | 1018 printing::PrintPreviewTabController* tab_controller = |
1004 printing::PrintPreviewTabController::GetInstance(); | 1019 printing::PrintPreviewTabController::GetInstance(); |
1005 if (tab_controller) | 1020 if (tab_controller) |
1006 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 1021 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
1007 } | 1022 } |
OLD | NEW |