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/browser/ui/webui/print_preview/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 #include "components/prefs/pref_service.h" | 63 #include "components/prefs/pref_service.h" |
64 #include "components/printing/common/print_messages.h" | 64 #include "components/printing/common/print_messages.h" |
65 #include "components/signin/core/browser/gaia_cookie_manager_service.h" | 65 #include "components/signin/core/browser/gaia_cookie_manager_service.h" |
66 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 66 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
67 #include "components/signin/core/browser/signin_manager.h" | 67 #include "components/signin/core/browser/signin_manager.h" |
68 #include "components/signin/core/common/profile_management_switches.h" | 68 #include "components/signin/core/common/profile_management_switches.h" |
69 #include "content/public/browser/browser_context.h" | 69 #include "content/public/browser/browser_context.h" |
70 #include "content/public/browser/browser_thread.h" | 70 #include "content/public/browser/browser_thread.h" |
71 #include "content/public/browser/navigation_controller.h" | 71 #include "content/public/browser/navigation_controller.h" |
72 #include "content/public/browser/navigation_entry.h" | 72 #include "content/public/browser/navigation_entry.h" |
| 73 #include "content/public/browser/render_frame_host.h" |
73 #include "content/public/browser/render_process_host.h" | 74 #include "content/public/browser/render_process_host.h" |
74 #include "content/public/browser/render_view_host.h" | |
75 #include "content/public/browser/web_contents.h" | 75 #include "content/public/browser/web_contents.h" |
76 #include "content/public/browser/web_ui.h" | 76 #include "content/public/browser/web_ui.h" |
77 #include "google_apis/gaia/oauth2_token_service.h" | 77 #include "google_apis/gaia/oauth2_token_service.h" |
78 #include "net/base/url_util.h" | 78 #include "net/base/url_util.h" |
79 #include "printing/backend/print_backend.h" | 79 #include "printing/backend/print_backend.h" |
80 #include "printing/backend/print_backend_consts.h" | 80 #include "printing/backend/print_backend_consts.h" |
81 #include "printing/pdf_render_settings.h" | 81 #include "printing/pdf_render_settings.h" |
82 #include "printing/print_settings.h" | 82 #include "printing/print_settings.h" |
83 #include "printing/printing_context.h" | 83 #include "printing/printing_context.h" |
84 #include "printing/units.h" | 84 #include "printing/units.h" |
85 #include "third_party/icu/source/i18n/unicode/ulocdata.h" | 85 #include "third_party/icu/source/i18n/unicode/ulocdata.h" |
86 | 86 |
87 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
88 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 88 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
89 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" | 89 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" |
90 #include "chrome/common/url_constants.h" | 90 #include "chrome/common/url_constants.h" |
91 #endif | 91 #endif |
92 | 92 |
93 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 93 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
94 #include "chrome/browser/printing/cloud_print/privet_constants.h" | 94 #include "chrome/browser/printing/cloud_print/privet_constants.h" |
95 #endif | 95 #endif |
96 | 96 |
97 using content::BrowserThread; | 97 using content::BrowserThread; |
98 using content::RenderViewHost; | 98 using content::RenderFrameHost; |
99 using content::WebContents; | 99 using content::WebContents; |
| 100 using printing::PrintViewManager; |
100 | 101 |
101 namespace { | 102 namespace { |
102 | 103 |
103 enum UserActionBuckets { | 104 enum UserActionBuckets { |
104 PRINT_TO_PRINTER, | 105 PRINT_TO_PRINTER, |
105 PRINT_TO_PDF, | 106 PRINT_TO_PDF, |
106 CANCEL, | 107 CANCEL, |
107 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, | 108 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, |
108 PREVIEW_FAILED, | 109 PREVIEW_FAILED, |
109 PREVIEW_STARTED, | 110 PREVIEW_STARTED, |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 403 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
403 | 404 |
404 scoped_refptr<printing::PrintBackend> print_backend( | 405 scoped_refptr<printing::PrintBackend> print_backend( |
405 printing::PrintBackend::CreateInstance(nullptr)); | 406 printing::PrintBackend::CreateInstance(nullptr)); |
406 | 407 |
407 VLOG(1) << "Enumerate printers start"; | 408 VLOG(1) << "Enumerate printers start"; |
408 printing::PrinterList printer_list; | 409 printing::PrinterList printer_list; |
409 print_backend->EnumeratePrinters(&printer_list); | 410 print_backend->EnumeratePrinters(&printer_list); |
410 | 411 |
411 for (const printing::PrinterBasicInfo& printer : printer_list) { | 412 for (const printing::PrinterBasicInfo& printer : printer_list) { |
412 std::unique_ptr<base::DictionaryValue> printer_info( | 413 auto printer_info = base::MakeUnique<base::DictionaryValue>(); |
413 new base::DictionaryValue); | |
414 const auto printer_name_description = GetPrinterNameAndDescription(printer); | 414 const auto printer_name_description = GetPrinterNameAndDescription(printer); |
415 const std::string& printer_name = printer_name_description.first; | 415 const std::string& printer_name = printer_name_description.first; |
416 const std::string& printer_description = printer_name_description.second; | 416 const std::string& printer_description = printer_name_description.second; |
417 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); | 417 printer_info->SetString(printing::kSettingDeviceName, printer.printer_name); |
418 printer_info->SetString(printing::kSettingPrinterName, printer_name); | 418 printer_info->SetString(printing::kSettingPrinterName, printer_name); |
419 printer_info->SetString(printing::kSettingPrinterDescription, | 419 printer_info->SetString(printing::kSettingPrinterDescription, |
420 printer_description); | 420 printer_description); |
421 | 421 |
422 base::DictionaryValue* options = new base::DictionaryValue; | 422 base::DictionaryValue* options = new base::DictionaryValue; |
423 printer_info->Set(printing::kSettingPrinterOptions, options); | 423 printer_info->Set(printing::kSettingPrinterOptions, options); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 466 |
467 printing::PrinterBasicInfo basic_info; | 467 printing::PrinterBasicInfo basic_info; |
468 if (!print_backend->GetPrinterBasicInfo(device_name, &basic_info)) | 468 if (!print_backend->GetPrinterBasicInfo(device_name, &basic_info)) |
469 return printer_info; | 469 return printer_info; |
470 | 470 |
471 const auto printer_name_description = | 471 const auto printer_name_description = |
472 GetPrinterNameAndDescription(basic_info); | 472 GetPrinterNameAndDescription(basic_info); |
473 const std::string& printer_name = printer_name_description.first; | 473 const std::string& printer_name = printer_name_description.first; |
474 const std::string& printer_description = printer_name_description.second; | 474 const std::string& printer_description = printer_name_description.second; |
475 | 475 |
476 printer_info.reset(new base::DictionaryValue); | 476 printer_info = base::MakeUnique<base::DictionaryValue>(); |
477 printer_info->SetString(kPrinterId, device_name); | 477 printer_info->SetString(kPrinterId, device_name); |
478 printer_info->SetString(printing::kSettingPrinterName, printer_name); | 478 printer_info->SetString(printing::kSettingPrinterName, printer_name); |
479 printer_info->SetString(printing::kSettingPrinterDescription, | 479 printer_info->SetString(printing::kSettingPrinterDescription, |
480 printer_description); | 480 printer_description); |
481 printer_info->Set(kPrinterCapabilities, printer_capabilities.release()); | 481 printer_info->Set(kPrinterCapabilities, printer_capabilities.release()); |
482 return printer_info; | 482 return printer_info; |
483 } | 483 } |
484 | 484 |
485 base::LazyInstance<printing::StickySettings> g_sticky_settings = | 485 base::LazyInstance<printing::StickySettings> g_sticky_settings = |
486 LAZY_INSTANCE_INITIALIZER; | 486 LAZY_INSTANCE_INITIALIZER; |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 // Add an additional key in order to identify |print_preview_ui| later on | 776 // Add an additional key in order to identify |print_preview_ui| later on |
777 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO | 777 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO |
778 // thread. | 778 // thread. |
779 settings->SetInteger(printing::kPreviewUIID, | 779 settings->SetInteger(printing::kPreviewUIID, |
780 print_preview_ui()->GetIDForPrintPreviewUI()); | 780 print_preview_ui()->GetIDForPrintPreviewUI()); |
781 | 781 |
782 // Increment request count. | 782 // Increment request count. |
783 ++regenerate_preview_request_count_; | 783 ++regenerate_preview_request_count_; |
784 | 784 |
785 WebContents* initiator = GetInitiator(); | 785 WebContents* initiator = GetInitiator(); |
786 if (!initiator) { | 786 content::RenderFrameHost* rfh = |
| 787 initiator |
| 788 ? PrintViewManager::FromWebContents(initiator)->print_preview_rfh() |
| 789 : nullptr; |
| 790 if (!rfh) { |
787 ReportUserActionHistogram(INITIATOR_CLOSED); | 791 ReportUserActionHistogram(INITIATOR_CLOSED); |
788 print_preview_ui()->OnClosePrintPreviewDialog(); | 792 print_preview_ui()->OnClosePrintPreviewDialog(); |
789 return; | 793 return; |
790 } | 794 } |
791 | 795 |
792 // Retrieve the page title and url and send it to the renderer process if | 796 // Retrieve the page title and url and send it to the renderer process if |
793 // headers and footers are to be displayed. | 797 // headers and footers are to be displayed. |
794 bool display_header_footer = false; | 798 bool display_header_footer = false; |
795 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, | 799 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, |
796 &display_header_footer)) { | 800 &display_header_footer)) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 } | 838 } |
835 | 839 |
836 VLOG(1) << "Print preview request start"; | 840 VLOG(1) << "Print preview request start"; |
837 | 841 |
838 bool selection_only = false; | 842 bool selection_only = false; |
839 if (!settings->GetBoolean(printing::kSettingShouldPrintSelectionOnly, | 843 if (!settings->GetBoolean(printing::kSettingShouldPrintSelectionOnly, |
840 &selection_only)) { | 844 &selection_only)) { |
841 NOTREACHED(); | 845 NOTREACHED(); |
842 } | 846 } |
843 | 847 |
844 RenderViewHost* rvh = initiator->GetRenderViewHost(); | 848 rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), *settings)); |
845 rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings)); | |
846 } | 849 } |
847 | 850 |
848 void PrintPreviewHandler::HandlePrint(const base::ListValue* args) { | 851 void PrintPreviewHandler::HandlePrint(const base::ListValue* args) { |
849 ReportStats(); | 852 ReportStats(); |
850 | 853 |
851 // Record the number of times the user requests to regenerate preview data | 854 // Record the number of times the user requests to regenerate preview data |
852 // before printing. | 855 // before printing. |
853 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", | 856 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", |
854 regenerate_preview_request_count_); | 857 regenerate_preview_request_count_); |
855 | 858 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1001 | 1004 |
1002 // The PDF being printed contains only the pages that the user selected, | 1005 // The PDF being printed contains only the pages that the user selected, |
1003 // so ignore the page range and print all pages. | 1006 // so ignore the page range and print all pages. |
1004 settings->Remove(printing::kSettingPageRange, NULL); | 1007 settings->Remove(printing::kSettingPageRange, NULL); |
1005 // Reset selection only flag for the same reason. | 1008 // Reset selection only flag for the same reason. |
1006 settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, false); | 1009 settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, false); |
1007 | 1010 |
1008 // Set ID to know whether printing is for preview. | 1011 // Set ID to know whether printing is for preview. |
1009 settings->SetInteger(printing::kPreviewUIID, | 1012 settings->SetInteger(printing::kPreviewUIID, |
1010 print_preview_ui()->GetIDForPrintPreviewUI()); | 1013 print_preview_ui()->GetIDForPrintPreviewUI()); |
1011 RenderViewHost* rvh = preview_web_contents()->GetRenderViewHost(); | 1014 RenderFrameHost* rfh = preview_web_contents()->GetMainFrame(); |
1012 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->GetRoutingID(), | 1015 rfh->Send( |
1013 *settings)); | 1016 new PrintMsg_PrintForPrintPreview(rfh->GetRoutingID(), *settings)); |
1014 | 1017 |
1015 // For all other cases above, the preview dialog will stay open until the | 1018 // For all other cases above, the preview dialog will stay open until the |
1016 // printing has finished. Then the dialog closes and PrintPreviewDone() gets | 1019 // printing has finished. Then the dialog closes and PrintPreviewDone() gets |
1017 // called. In the case below, since the preview dialog will be hidden and | 1020 // called. In the case below, since the preview dialog will be hidden and |
1018 // not closed, we need to make this call. | 1021 // not closed, we need to make this call. |
1019 if (initiator) { | 1022 if (initiator) { |
1020 printing::PrintViewManager* print_view_manager = | 1023 auto* print_view_manager = PrintViewManager::FromWebContents(initiator); |
1021 printing::PrintViewManager::FromWebContents(initiator); | |
1022 print_view_manager->PrintPreviewDone(); | 1024 print_view_manager->PrintPreviewDone(); |
1023 } | 1025 } |
1024 #else | 1026 #else |
1025 NOTREACHED(); | 1027 NOTREACHED(); |
1026 #endif // defined(ENABLE_BASIC_PRINTING) | 1028 #endif // defined(ENABLE_BASIC_PRINTING) |
1027 } | 1029 } |
1028 } | 1030 } |
1029 | 1031 |
1030 void PrintPreviewHandler::PrintToPdf() { | 1032 void PrintPreviewHandler::PrintToPdf() { |
1031 if (!print_to_pdf_path_.empty()) { | 1033 if (!print_to_pdf_path_.empty()) { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 #if BUILDFLAG(ENABLE_BASIC_PRINT_DIALOG) | 1151 #if BUILDFLAG(ENABLE_BASIC_PRINT_DIALOG) |
1150 void PrintPreviewHandler::HandleShowSystemDialog( | 1152 void PrintPreviewHandler::HandleShowSystemDialog( |
1151 const base::ListValue* /*args*/) { | 1153 const base::ListValue* /*args*/) { |
1152 ReportStats(); | 1154 ReportStats(); |
1153 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); | 1155 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); |
1154 | 1156 |
1155 WebContents* initiator = GetInitiator(); | 1157 WebContents* initiator = GetInitiator(); |
1156 if (!initiator) | 1158 if (!initiator) |
1157 return; | 1159 return; |
1158 | 1160 |
1159 printing::PrintViewManager* print_view_manager = | 1161 auto* print_view_manager = PrintViewManager::FromWebContents(initiator); |
1160 printing::PrintViewManager::FromWebContents(initiator); | |
1161 print_view_manager->PrintForSystemDialogNow( | 1162 print_view_manager->PrintForSystemDialogNow( |
1162 base::Bind(&PrintPreviewHandler::ClosePreviewDialog, | 1163 base::Bind(&PrintPreviewHandler::ClosePreviewDialog, |
1163 weak_factory_.GetWeakPtr())); | 1164 weak_factory_.GetWeakPtr())); |
1164 | 1165 |
1165 // Cancel the pending preview request if exists. | 1166 // Cancel the pending preview request if exists. |
1166 print_preview_ui()->OnCancelPendingPreviewRequest(); | 1167 print_preview_ui()->OnCancelPendingPreviewRequest(); |
1167 } | 1168 } |
1168 #endif | 1169 #endif |
1169 | 1170 |
1170 void PrintPreviewHandler::HandleManagePrinters( | 1171 void PrintPreviewHandler::HandleManagePrinters( |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1765 | 1766 |
1766 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1767 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
1767 if (gaia_cookie_manager_service_) | 1768 if (gaia_cookie_manager_service_) |
1768 gaia_cookie_manager_service_->RemoveObserver(this); | 1769 gaia_cookie_manager_service_->RemoveObserver(this); |
1769 } | 1770 } |
1770 | 1771 |
1771 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1772 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
1772 const base::Closure& closure) { | 1773 const base::Closure& closure) { |
1773 pdf_file_saved_closure_ = closure; | 1774 pdf_file_saved_closure_ = closure; |
1774 } | 1775 } |
OLD | NEW |