Chromium Code Reviews| 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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 // Dictionary field holding the default destination selection rules. | 195 // Dictionary field holding the default destination selection rules. |
| 196 const char kDefaultDestinationSelectionRules[] = | 196 const char kDefaultDestinationSelectionRules[] = |
| 197 "defaultDestinationSelectionRules"; | 197 "defaultDestinationSelectionRules"; |
| 198 | 198 |
| 199 // Id of the predefined PDF printer. | 199 // Id of the predefined PDF printer. |
| 200 const char kLocalPdfPrinterId[] = "Save as PDF"; | 200 const char kLocalPdfPrinterId[] = "Save as PDF"; |
| 201 | 201 |
| 202 // Timeout for searching for privet printers, in seconds. | 202 // Timeout for searching for privet printers, in seconds. |
| 203 const int kPrivetTimeoutSec = 5; | 203 const int kPrivetTimeoutSec = 5; |
| 204 | 204 |
| 205 // Get the print job settings dictionary from |args|. The caller takes | 205 // Get the print job settings dictionary from |json_str|. Returns NULL on |
| 206 // ownership of the returned DictionaryValue. Returns NULL on failure. | 206 // failure. |
| 207 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( | 207 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( |
| 208 const base::ListValue* args) { | 208 const std::string& json_str) { |
| 209 std::string json_str; | |
| 210 if (!args->GetString(0, &json_str)) { | |
| 211 NOTREACHED() << "Could not read JSON argument"; | |
| 212 return NULL; | |
| 213 } | |
| 214 if (json_str.empty()) { | 209 if (json_str.empty()) { |
| 215 NOTREACHED() << "Empty print job settings"; | 210 NOTREACHED() << "Empty print job settings"; |
| 216 return NULL; | 211 return NULL; |
| 217 } | 212 } |
| 218 std::unique_ptr<base::DictionaryValue> settings = | 213 std::unique_ptr<base::DictionaryValue> settings = |
| 219 base::DictionaryValue::From(base::JSONReader::Read(json_str)); | 214 base::DictionaryValue::From(base::JSONReader::Read(json_str)); |
| 220 if (!settings) { | 215 if (!settings) { |
| 221 NOTREACHED() << "Print job settings must be a dictionary."; | 216 NOTREACHED() << "Print job settings must be a dictionary."; |
| 222 return NULL; | 217 return NULL; |
| 223 } | 218 } |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 681 | 676 |
| 682 AllowJavascript(); | 677 AllowJavascript(); |
| 683 | 678 |
| 684 if (!PrivetPrintingEnabled()) { | 679 if (!PrivetPrintingEnabled()) { |
| 685 RejectJavascriptCallback(base::Value(callback_id), base::Value()); | 680 RejectJavascriptCallback(base::Value(callback_id), base::Value()); |
| 686 } | 681 } |
| 687 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 682 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 688 using local_discovery::ServiceDiscoverySharedClient; | 683 using local_discovery::ServiceDiscoverySharedClient; |
| 689 scoped_refptr<ServiceDiscoverySharedClient> service_discovery = | 684 scoped_refptr<ServiceDiscoverySharedClient> service_discovery = |
| 690 ServiceDiscoverySharedClient::GetInstance(); | 685 ServiceDiscoverySharedClient::GetInstance(); |
| 691 DCHECK(privet_callback_id_.empty()); | 686 DCHECK(privet_search_callback_id_.empty()); |
| 692 privet_callback_id_ = callback_id; | 687 privet_search_callback_id_ = callback_id; |
| 693 StartPrivetLister(service_discovery); | 688 StartPrivetLister(service_discovery); |
| 694 #endif | 689 #endif |
| 695 } | 690 } |
| 696 | 691 |
| 697 void PrintPreviewHandler::StopPrivetLister() { | 692 void PrintPreviewHandler::StopPrivetLister() { |
| 698 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 693 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 699 privet_lister_timer_.reset(); | 694 privet_lister_timer_.reset(); |
| 700 if (PrivetPrintingEnabled() && printer_lister_) { | 695 if (PrivetPrintingEnabled() && printer_lister_) { |
| 701 printer_lister_->Stop(); | 696 printer_lister_->Stop(); |
| 702 } | 697 } |
| 703 ResolveJavascriptCallback(base::Value(privet_callback_id_), base::Value()); | 698 ResolveJavascriptCallback(base::Value(privet_search_callback_id_), |
| 704 privet_callback_id_ = ""; | 699 base::Value()); |
| 700 privet_search_callback_id_.clear(); | |
| 705 #endif | 701 #endif |
| 706 } | 702 } |
| 707 | 703 |
| 708 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( | 704 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( |
| 709 const base::ListValue* args) { | 705 const base::ListValue* args) { |
| 710 AllowJavascript(); | 706 AllowJavascript(); |
| 711 | 707 |
| 712 std::string callback_id; | 708 std::string callback_id; |
| 713 std::string printer_name; | 709 std::string printer_name; |
| 714 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || | 710 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 769 | 765 |
| 770 EnsureExtensionPrinterHandlerSet(); | 766 EnsureExtensionPrinterHandlerSet(); |
| 771 extension_printer_handler_->StartGetCapability( | 767 extension_printer_handler_->StartGetCapability( |
| 772 printer_name, | 768 printer_name, |
| 773 base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterCapabilities, | 769 base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterCapabilities, |
| 774 weak_factory_.GetWeakPtr(), callback_id)); | 770 weak_factory_.GetWeakPtr(), callback_id)); |
| 775 } | 771 } |
| 776 | 772 |
| 777 void PrintPreviewHandler::HandleGetPreview(const base::ListValue* args) { | 773 void PrintPreviewHandler::HandleGetPreview(const base::ListValue* args) { |
| 778 DCHECK_EQ(2U, args->GetSize()); | 774 DCHECK_EQ(2U, args->GetSize()); |
| 779 std::unique_ptr<base::DictionaryValue> settings = GetSettingsDictionary(args); | 775 std::string json_str; |
| 776 if (!args->GetString(0, &json_str)) | |
| 777 return; | |
| 778 std::unique_ptr<base::DictionaryValue> settings = | |
| 779 GetSettingsDictionary(json_str); | |
| 780 if (!settings) | 780 if (!settings) |
| 781 return; | 781 return; |
| 782 int request_id = -1; | 782 int request_id = -1; |
| 783 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) | 783 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) |
| 784 return; | 784 return; |
| 785 | 785 |
| 786 print_preview_ui()->OnPrintPreviewRequest(request_id); | 786 print_preview_ui()->OnPrintPreviewRequest(request_id); |
| 787 // Add an additional key in order to identify |print_preview_ui| later on | 787 // Add an additional key in order to identify |print_preview_ui| later on |
| 788 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO | 788 // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO |
| 789 // thread. | 789 // thread. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 855 } | 855 } |
| 856 | 856 |
| 857 void PrintPreviewHandler::HandlePrint(const base::ListValue* args) { | 857 void PrintPreviewHandler::HandlePrint(const base::ListValue* args) { |
| 858 ReportStats(); | 858 ReportStats(); |
| 859 | 859 |
| 860 // Record the number of times the user requests to regenerate preview data | 860 // Record the number of times the user requests to regenerate preview data |
| 861 // before printing. | 861 // before printing. |
| 862 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", | 862 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", |
| 863 regenerate_preview_request_count_); | 863 regenerate_preview_request_count_); |
| 864 | 864 |
| 865 std::unique_ptr<base::DictionaryValue> settings = GetSettingsDictionary(args); | 865 AllowJavascript(); |
| 866 | |
| 867 std::string callback_id; | |
| 868 CHECK(args->GetString(0, &callback_id)); | |
| 869 CHECK(!callback_id.empty()); | |
| 870 std::string json_str; | |
| 871 if (!args->GetString(1, &json_str)) | |
| 872 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1)); | |
|
Lei Zhang
2017/07/11 05:11:10
Wait, don't we need to return after a failure like
rbpotter
2017/07/11 16:35:50
Yes for the case below. Should probably just crash
| |
| 873 | |
| 874 std::unique_ptr<base::DictionaryValue> settings = | |
| 875 GetSettingsDictionary(json_str); | |
| 866 if (!settings) | 876 if (!settings) |
| 867 return; | 877 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1)); |
| 868 | 878 |
| 869 ReportPrintSettingsStats(*settings); | 879 ReportPrintSettingsStats(*settings); |
| 870 | 880 |
| 871 // Report whether the user printed a PDF or an HTML document. | 881 // Report whether the user printed a PDF or an HTML document. |
| 872 ReportPrintDocumentTypeHistogram(print_preview_ui()->source_is_modifiable() ? | 882 ReportPrintDocumentTypeHistogram(print_preview_ui()->source_is_modifiable() ? |
| 873 HTML_DOCUMENT : PDF_DOCUMENT); | 883 HTML_DOCUMENT : PDF_DOCUMENT); |
| 874 | 884 |
| 875 bool print_to_pdf = false; | 885 bool print_to_pdf = false; |
| 876 bool is_cloud_printer = false; | 886 bool is_cloud_printer = false; |
| 877 bool print_with_privet = false; | 887 bool print_with_privet = false; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 889 &print_with_extension); | 899 &print_with_extension); |
| 890 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); | 900 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); |
| 891 } | 901 } |
| 892 | 902 |
| 893 int page_count = 0; | 903 int page_count = 0; |
| 894 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count); | 904 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count); |
| 895 | 905 |
| 896 if (print_to_pdf) { | 906 if (print_to_pdf) { |
| 897 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count); | 907 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count); |
| 898 ReportUserActionHistogram(PRINT_TO_PDF); | 908 ReportUserActionHistogram(PRINT_TO_PDF); |
| 909 DCHECK(pdf_callback_id_.empty()); | |
| 910 pdf_callback_id_ = callback_id; | |
| 899 PrintToPdf(); | 911 PrintToPdf(); |
| 900 return; | 912 return; |
| 901 } | 913 } |
| 902 | 914 |
| 903 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 915 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 904 if (print_with_privet && PrivetPrintingEnabled()) { | 916 if (print_with_privet && PrivetPrintingEnabled()) { |
| 905 std::string printer_name; | 917 std::string printer_name; |
| 906 std::string print_ticket; | 918 std::string print_ticket; |
| 907 std::string capabilities; | 919 std::string capabilities; |
| 908 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithPrivet", page_count); | 920 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithPrivet", page_count); |
| 909 ReportUserActionHistogram(PRINT_WITH_PRIVET); | 921 ReportUserActionHistogram(PRINT_WITH_PRIVET); |
| 910 | 922 |
| 911 int width = 0; | 923 int width = 0; |
| 912 int height = 0; | 924 int height = 0; |
| 913 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) || | 925 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) || |
| 914 !settings->GetString(printing::kSettingTicket, &print_ticket) || | 926 !settings->GetString(printing::kSettingTicket, &print_ticket) || |
| 915 !settings->GetString(printing::kSettingCapabilities, &capabilities) || | 927 !settings->GetString(printing::kSettingCapabilities, &capabilities) || |
| 916 !settings->GetInteger(printing::kSettingPageWidth, &width) || | 928 !settings->GetInteger(printing::kSettingPageWidth, &width) || |
| 917 !settings->GetInteger(printing::kSettingPageHeight, &height) || | 929 !settings->GetInteger(printing::kSettingPageHeight, &height) || |
| 918 width <= 0 || height <= 0) { | 930 width <= 0 || height <= 0) { |
| 919 NOTREACHED(); | 931 NOTREACHED(); |
| 920 FireWebUIListener("print-failed", base::Value(-1)); | 932 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1)); |
| 921 return; | 933 return; |
| 922 } | 934 } |
| 923 | 935 |
| 924 PrintToPrivetPrinter( | 936 DCHECK(privet_print_callback_id_.empty()); |
| 925 printer_name, print_ticket, capabilities, gfx::Size(width, height)); | 937 privet_print_callback_id_ = callback_id; |
| 938 PrintToPrivetPrinter(callback_id, printer_name, print_ticket, capabilities, | |
| 939 gfx::Size(width, height)); | |
| 926 return; | 940 return; |
| 927 } | 941 } |
| 928 #endif | 942 #endif |
| 929 | 943 |
| 930 if (print_with_extension) { | 944 if (print_with_extension) { |
| 931 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithExtension", | 945 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithExtension", |
| 932 page_count); | 946 page_count); |
| 933 ReportUserActionHistogram(PRINT_WITH_EXTENSION); | 947 ReportUserActionHistogram(PRINT_WITH_EXTENSION); |
| 934 | 948 |
| 935 std::string destination_id; | 949 std::string destination_id; |
| 936 std::string print_ticket; | 950 std::string print_ticket; |
| 937 std::string capabilities; | 951 std::string capabilities; |
| 938 int width = 0; | 952 int width = 0; |
| 939 int height = 0; | 953 int height = 0; |
| 940 if (!settings->GetString(printing::kSettingDeviceName, &destination_id) || | 954 if (!settings->GetString(printing::kSettingDeviceName, &destination_id) || |
| 941 !settings->GetString(printing::kSettingTicket, &print_ticket) || | 955 !settings->GetString(printing::kSettingTicket, &print_ticket) || |
| 942 !settings->GetString(printing::kSettingCapabilities, &capabilities) || | 956 !settings->GetString(printing::kSettingCapabilities, &capabilities) || |
| 943 !settings->GetInteger(printing::kSettingPageWidth, &width) || | 957 !settings->GetInteger(printing::kSettingPageWidth, &width) || |
| 944 !settings->GetInteger(printing::kSettingPageHeight, &height) || | 958 !settings->GetInteger(printing::kSettingPageHeight, &height) || |
| 945 width <= 0 || height <= 0) { | 959 width <= 0 || height <= 0) { |
| 946 NOTREACHED(); | 960 NOTREACHED(); |
| 947 OnExtensionPrintResult(false, "FAILED"); | 961 RejectJavascriptCallback(base::Value(callback_id), base::Value("FAILED")); |
| 948 return; | 962 return; |
| 949 } | 963 } |
| 950 | 964 |
| 951 base::string16 title; | 965 base::string16 title; |
| 952 scoped_refptr<base::RefCountedBytes> data; | 966 scoped_refptr<base::RefCountedBytes> data; |
| 953 if (!GetPreviewDataAndTitle(&data, &title)) { | 967 if (!GetPreviewDataAndTitle(&data, &title)) { |
| 954 LOG(ERROR) << "Nothing to print; no preview available."; | 968 LOG(ERROR) << "Nothing to print; no preview available."; |
| 955 OnExtensionPrintResult(false, "NO_DATA"); | 969 RejectJavascriptCallback(base::Value(callback_id), |
| 970 base::Value("NO_DATA")); | |
| 956 return; | 971 return; |
| 957 } | 972 } |
| 958 | 973 |
| 959 EnsureExtensionPrinterHandlerSet(); | 974 EnsureExtensionPrinterHandlerSet(); |
| 960 extension_printer_handler_->StartPrint( | 975 extension_printer_handler_->StartPrint( |
| 961 destination_id, capabilities, title, print_ticket, | 976 destination_id, capabilities, title, print_ticket, |
| 962 gfx::Size(width, height), data, | 977 gfx::Size(width, height), data, |
| 963 base::Bind(&PrintPreviewHandler::OnExtensionPrintResult, | 978 base::Bind(&PrintPreviewHandler::OnExtensionPrintResult, |
| 964 weak_factory_.GetWeakPtr())); | 979 weak_factory_.GetWeakPtr(), callback_id)); |
| 965 return; | 980 return; |
| 966 } | 981 } |
| 967 | 982 |
| 968 scoped_refptr<base::RefCountedBytes> data; | 983 scoped_refptr<base::RefCountedBytes> data; |
| 969 base::string16 title; | 984 base::string16 title; |
| 970 if (!GetPreviewDataAndTitle(&data, &title)) { | 985 if (!GetPreviewDataAndTitle(&data, &title)) { |
| 971 // Nothing to print, no preview available. | 986 // Nothing to print, no preview available. |
| 987 RejectJavascriptCallback(base::Value(callback_id), base::Value()); | |
| 972 return; | 988 return; |
| 973 } | 989 } |
| 974 | 990 |
| 975 if (is_cloud_printer) { | 991 if (is_cloud_printer) { |
| 976 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint", | 992 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint", |
| 977 page_count); | 993 page_count); |
| 978 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT); | 994 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT); |
| 979 SendCloudPrintJob(data.get()); | 995 SendCloudPrintJob(callback_id, data.get()); |
| 980 return; | 996 return; |
| 981 } | 997 } |
| 982 | 998 |
| 983 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 999 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 984 bool system_dialog = false; | 1000 bool system_dialog = false; |
| 985 settings->GetBoolean(printing::kSettingShowSystemDialog, &system_dialog); | 1001 settings->GetBoolean(printing::kSettingShowSystemDialog, &system_dialog); |
| 986 if (system_dialog) { | 1002 if (system_dialog) { |
| 987 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", page_count); | 1003 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", page_count); |
| 988 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); | 1004 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); |
| 989 } else { | 1005 } else { |
| 990 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", page_count); | 1006 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", page_count); |
| 991 ReportUserActionHistogram(PRINT_TO_PRINTER); | 1007 ReportUserActionHistogram(PRINT_TO_PRINTER); |
| 992 } | 1008 } |
| 993 | 1009 |
| 994 // This tries to activate the initiator as well, so do not clear the | |
| 995 // association with the initiator yet. | |
| 996 print_preview_ui()->OnHidePreviewDialog(); | |
| 997 | |
| 998 // Grab the current initiator before calling ClearInitiatorDetails() below. | |
| 999 // Otherwise calling GetInitiator() later will return the wrong WebContents. | |
| 1000 // https://crbug.com/407080 | |
| 1001 WebContents* initiator = GetInitiator(); | 1010 WebContents* initiator = GetInitiator(); |
| 1002 if (initiator) { | 1011 if (initiator) { |
| 1003 // Save initiator IDs. PrintMsg_PrintForPrintPreview below should cause | 1012 // Save initiator IDs. PrintMsg_PrintForPrintPreview below should cause |
| 1004 // the renderer to send PrintHostMsg_UpdatePrintSettings and trigger | 1013 // the renderer to send PrintHostMsg_UpdatePrintSettings and trigger |
| 1005 // PrintingMessageFilter::OnUpdatePrintSettings(), which needs this info. | 1014 // PrintingMessageFilter::OnUpdatePrintSettings(), which needs this info. |
| 1006 auto* main_render_frame = initiator->GetMainFrame(); | 1015 auto* main_render_frame = initiator->GetMainFrame(); |
| 1007 settings->SetInteger(printing::kPreviewInitiatorHostId, | 1016 settings->SetInteger(printing::kPreviewInitiatorHostId, |
| 1008 main_render_frame->GetProcess()->GetID()); | 1017 main_render_frame->GetProcess()->GetID()); |
| 1009 settings->SetInteger(printing::kPreviewInitiatorRoutingId, | 1018 settings->SetInteger(printing::kPreviewInitiatorRoutingId, |
| 1010 main_render_frame->GetRoutingID()); | 1019 main_render_frame->GetRoutingID()); |
| 1011 } | 1020 } |
| 1012 | 1021 |
| 1013 // Do this so the initiator can open a new print preview dialog, while the | |
| 1014 // current print preview dialog is still handling its print job. | |
| 1015 ClearInitiatorDetails(); | |
| 1016 | |
| 1017 // Set ID to know whether printing is for preview. | 1022 // Set ID to know whether printing is for preview. |
| 1018 settings->SetInteger(printing::kPreviewUIID, | 1023 settings->SetInteger(printing::kPreviewUIID, |
| 1019 print_preview_ui()->GetIDForPrintPreviewUI()); | 1024 print_preview_ui()->GetIDForPrintPreviewUI()); |
| 1020 RenderFrameHost* rfh = preview_web_contents()->GetMainFrame(); | 1025 RenderFrameHost* rfh = preview_web_contents()->GetMainFrame(); |
| 1021 rfh->Send(new PrintMsg_PrintForPrintPreview(rfh->GetRoutingID(), *settings)); | 1026 rfh->Send(new PrintMsg_PrintForPrintPreview(rfh->GetRoutingID(), *settings)); |
| 1022 | 1027 |
| 1023 // For all other cases above, the preview dialog will stay open until the | 1028 // Set this so when print preview sends "hidePreviewDialog" we clear the |
| 1024 // printing has finished. Then the dialog closes and PrintPreviewDone() gets | 1029 // initiator and call PrintPreviewDone(). In the cases above, the preview |
| 1025 // called. In the case below, since the preview dialog will be hidden and | 1030 // dialog stays open until printing is finished and we do this when the |
| 1026 // not closed, we need to make this call. | 1031 // dialog is closed. In this case, we set this so that these tasks are |
| 1027 if (initiator) { | 1032 // done in HandleHidePreview(). |
| 1028 auto* print_view_manager = PrintViewManager::FromWebContents(initiator); | 1033 printing_started_ = true; |
| 1029 print_view_manager->PrintPreviewDone(); | 1034 |
| 1030 } | 1035 // This will ultimately try to activate the initiator as well, so do not |
| 1036 // clear the association with the initiator until "hidePreviewDialog" is | |
| 1037 // received from JS. | |
| 1038 ResolveJavascriptCallback(base::Value(callback_id), base::Value()); | |
| 1039 | |
| 1031 #else | 1040 #else |
| 1032 NOTREACHED(); | 1041 NOTREACHED(); |
| 1033 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 1042 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
| 1034 } | 1043 } |
| 1035 | 1044 |
| 1036 void PrintPreviewHandler::PrintToPdf() { | 1045 void PrintPreviewHandler::PrintToPdf() { |
| 1037 if (!print_to_pdf_path_.empty()) { | 1046 if (!print_to_pdf_path_.empty()) { |
| 1038 // User has already selected a path, no need to show the dialog again. | 1047 // User has already selected a path, no need to show the dialog again. |
| 1048 ResolveJavascriptCallback(base::Value(pdf_callback_id_), base::Value()); | |
| 1049 pdf_callback_id_.clear(); | |
| 1039 PostPrintToPdfTask(); | 1050 PostPrintToPdfTask(); |
| 1040 } else if (!select_file_dialog_.get() || | 1051 } else if (!select_file_dialog_.get() || |
| 1041 !select_file_dialog_->IsRunning(platform_util::GetTopLevel( | 1052 !select_file_dialog_->IsRunning(platform_util::GetTopLevel( |
| 1042 preview_web_contents()->GetNativeView()))) { | 1053 preview_web_contents()->GetNativeView()))) { |
| 1043 // Pre-populating select file dialog with print job title. | 1054 // Pre-populating select file dialog with print job title. |
| 1044 const base::string16& print_job_title_utf16 = | 1055 const base::string16& print_job_title_utf16 = |
| 1045 print_preview_ui()->initiator_title(); | 1056 print_preview_ui()->initiator_title(); |
| 1046 | 1057 |
| 1047 #if defined(OS_WIN) | 1058 #if defined(OS_WIN) |
| 1048 base::FilePath::StringType print_job_title(print_job_title_utf16); | 1059 base::FilePath::StringType print_job_title(print_job_title_utf16); |
| 1049 #elif defined(OS_POSIX) | 1060 #elif defined(OS_POSIX) |
| 1050 base::FilePath::StringType print_job_title = | 1061 base::FilePath::StringType print_job_title = |
| 1051 base::UTF16ToUTF8(print_job_title_utf16); | 1062 base::UTF16ToUTF8(print_job_title_utf16); |
| 1052 #endif | 1063 #endif |
| 1053 | 1064 |
| 1054 base::i18n::ReplaceIllegalCharactersInPath(&print_job_title, '_'); | 1065 base::i18n::ReplaceIllegalCharactersInPath(&print_job_title, '_'); |
| 1055 base::FilePath default_filename(print_job_title); | 1066 base::FilePath default_filename(print_job_title); |
| 1056 default_filename = | 1067 default_filename = |
| 1057 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); | 1068 default_filename.ReplaceExtension(FILE_PATH_LITERAL("pdf")); |
| 1058 | 1069 |
| 1059 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 1070 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| 1060 bool prompt_user = !cmdline->HasSwitch(switches::kKioskModePrinting); | 1071 bool prompt_user = !cmdline->HasSwitch(switches::kKioskModePrinting); |
| 1061 SelectFile(default_filename, prompt_user); | 1072 SelectFile(default_filename, prompt_user); |
| 1062 } | 1073 } |
| 1063 } | 1074 } |
| 1064 | 1075 |
| 1065 void PrintPreviewHandler::HandleHidePreview(const base::ListValue* /*args*/) { | 1076 void PrintPreviewHandler::HandleHidePreview(const base::ListValue* /*args*/) { |
| 1077 if (printing_started_) { | |
| 1078 // Printing has started, so clear the initiator so that it can open a new | |
| 1079 // print preview dialog, while the current print preview dialog is still | |
| 1080 // handling its print job. | |
| 1081 WebContents* initiator = GetInitiator(); | |
| 1082 ClearInitiatorDetails(); | |
| 1083 | |
| 1084 // Since the preview dialog will be hidden and not closed, we need to make | |
| 1085 // this call. | |
| 1086 if (initiator) { | |
| 1087 auto* print_view_manager = PrintViewManager::FromWebContents(initiator); | |
| 1088 print_view_manager->PrintPreviewDone(); | |
| 1089 } | |
| 1090 | |
| 1091 // Since the initiator is cleared, only want to do this once. | |
| 1092 printing_started_ = false; | |
| 1093 } | |
| 1094 | |
| 1066 print_preview_ui()->OnHidePreviewDialog(); | 1095 print_preview_ui()->OnHidePreviewDialog(); |
| 1067 } | 1096 } |
| 1068 | 1097 |
| 1069 void PrintPreviewHandler::HandleCancelPendingPrintRequest( | 1098 void PrintPreviewHandler::HandleCancelPendingPrintRequest( |
| 1070 const base::ListValue* /*args*/) { | 1099 const base::ListValue* /*args*/) { |
| 1071 WebContents* initiator = GetInitiator(); | 1100 WebContents* initiator = GetInitiator(); |
| 1072 if (initiator) | 1101 if (initiator) |
| 1073 ClearInitiatorDetails(); | 1102 ClearInitiatorDetails(); |
| 1074 chrome::ShowPrintErrorDialog(); | 1103 chrome::ShowPrintErrorDialog(); |
| 1075 } | 1104 } |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1387 PrefService* prefs = profile->GetPrefs(); | 1416 PrefService* prefs = profile->GetPrefs(); |
| 1388 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { | 1417 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { |
| 1389 base::DictionaryValue settings; | 1418 base::DictionaryValue settings; |
| 1390 settings.SetString(kCloudPrintUrl, | 1419 settings.SetString(kCloudPrintUrl, |
| 1391 GURL(cloud_devices::GetCloudPrintURL()).spec()); | 1420 GURL(cloud_devices::GetCloudPrintURL()).spec()); |
| 1392 settings.SetBoolean(kAppKioskMode, chrome::IsRunningInForcedAppMode()); | 1421 settings.SetBoolean(kAppKioskMode, chrome::IsRunningInForcedAppMode()); |
| 1393 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings); | 1422 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings); |
| 1394 } | 1423 } |
| 1395 } | 1424 } |
| 1396 | 1425 |
| 1397 void PrintPreviewHandler::SendCloudPrintJob(const base::RefCountedBytes* data) { | 1426 void PrintPreviewHandler::SendCloudPrintJob(const std::string& callback_id, |
| 1427 const base::RefCountedBytes* data) { | |
| 1398 // BASE64 encode the job data. | 1428 // BASE64 encode the job data. |
| 1399 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()), | 1429 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()), |
| 1400 data->size()); | 1430 data->size()); |
| 1401 std::string base64_data; | 1431 std::string base64_data; |
| 1402 base::Base64Encode(raw_data, &base64_data); | 1432 base::Base64Encode(raw_data, &base64_data); |
| 1403 base::Value data_value(base64_data); | |
| 1404 | 1433 |
| 1405 web_ui()->CallJavascriptFunctionUnsafe("printToCloud", data_value); | 1434 ResolveJavascriptCallback(base::Value(callback_id), base::Value(base64_data)); |
| 1406 } | 1435 } |
| 1407 | 1436 |
| 1408 WebContents* PrintPreviewHandler::GetInitiator() const { | 1437 WebContents* PrintPreviewHandler::GetInitiator() const { |
| 1409 printing::PrintPreviewDialogController* dialog_controller = | 1438 printing::PrintPreviewDialogController* dialog_controller = |
| 1410 printing::PrintPreviewDialogController::GetInstance(); | 1439 printing::PrintPreviewDialogController::GetInstance(); |
| 1411 if (!dialog_controller) | 1440 if (!dialog_controller) |
| 1412 return NULL; | 1441 return NULL; |
| 1413 return dialog_controller->GetInitiator(preview_web_contents()); | 1442 return dialog_controller->GetInitiator(preview_web_contents()); |
| 1414 } | 1443 } |
| 1415 | 1444 |
| 1416 void PrintPreviewHandler::OnAddAccountToCookieCompleted( | 1445 void PrintPreviewHandler::OnAddAccountToCookieCompleted( |
| 1417 const std::string& account_id, | 1446 const std::string& account_id, |
| 1418 const GoogleServiceAuthError& error) { | 1447 const GoogleServiceAuthError& error) { |
| 1419 OnSigninComplete(); | 1448 OnSigninComplete(); |
| 1420 } | 1449 } |
| 1421 | 1450 |
| 1422 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, | 1451 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, |
| 1423 bool prompt_user) { | 1452 bool prompt_user) { |
| 1424 if (prompt_user) { | 1453 if (prompt_user) { |
| 1425 ChromeSelectFilePolicy policy(GetInitiator()); | 1454 ChromeSelectFilePolicy policy(GetInitiator()); |
| 1426 if (!policy.CanOpenSelectFileDialog()) { | 1455 if (!policy.CanOpenSelectFileDialog()) { |
| 1427 policy.SelectFileDenied(); | 1456 policy.SelectFileDenied(); |
| 1428 return ClosePreviewDialog(); | 1457 RejectJavascriptCallback(base::Value(pdf_callback_id_), base::Value()); |
| 1458 pdf_callback_id_.clear(); | |
| 1459 return; | |
| 1429 } | 1460 } |
| 1430 } | 1461 } |
| 1431 | 1462 |
| 1432 // Get save location from Download Preferences. | 1463 // Get save location from Download Preferences. |
| 1433 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 1464 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 1434 preview_web_contents()->GetBrowserContext()); | 1465 preview_web_contents()->GetBrowserContext()); |
| 1435 base::FilePath file_path = download_prefs->SaveFilePath(); | 1466 base::FilePath file_path = download_prefs->SaveFilePath(); |
| 1436 printing::StickySettings* sticky_settings = GetStickySettings(); | 1467 printing::StickySettings* sticky_settings = GetStickySettings(); |
| 1437 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( | 1468 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( |
| 1438 preview_web_contents()->GetBrowserContext())->GetPrefs()); | 1469 preview_web_contents()->GetBrowserContext())->GetPrefs()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1487 int /* index */, | 1518 int /* index */, |
| 1488 void* /* params */) { | 1519 void* /* params */) { |
| 1489 // Update downloads location and save sticky settings. | 1520 // Update downloads location and save sticky settings. |
| 1490 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 1521 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 1491 preview_web_contents()->GetBrowserContext()); | 1522 preview_web_contents()->GetBrowserContext()); |
| 1492 download_prefs->SetSaveFilePath(path.DirName()); | 1523 download_prefs->SetSaveFilePath(path.DirName()); |
| 1493 printing::StickySettings* sticky_settings = GetStickySettings(); | 1524 printing::StickySettings* sticky_settings = GetStickySettings(); |
| 1494 sticky_settings->SaveInPrefs( | 1525 sticky_settings->SaveInPrefs( |
| 1495 Profile::FromBrowserContext(preview_web_contents()->GetBrowserContext()) | 1526 Profile::FromBrowserContext(preview_web_contents()->GetBrowserContext()) |
| 1496 ->GetPrefs()); | 1527 ->GetPrefs()); |
| 1497 web_ui()->CallJavascriptFunctionUnsafe("fileSelectionCompleted"); | 1528 ResolveJavascriptCallback(base::Value(pdf_callback_id_), base::Value()); |
| 1529 pdf_callback_id_.clear(); | |
| 1498 print_to_pdf_path_ = path; | 1530 print_to_pdf_path_ = path; |
| 1499 PostPrintToPdfTask(); | 1531 PostPrintToPdfTask(); |
| 1500 } | 1532 } |
| 1501 | 1533 |
| 1502 void PrintPreviewHandler::PostPrintToPdfTask() { | 1534 void PrintPreviewHandler::PostPrintToPdfTask() { |
| 1503 scoped_refptr<base::RefCountedBytes> data; | 1535 scoped_refptr<base::RefCountedBytes> data; |
| 1504 base::string16 title; | 1536 base::string16 title; |
| 1505 if (!GetPreviewDataAndTitle(&data, &title)) { | 1537 if (!GetPreviewDataAndTitle(&data, &title)) { |
| 1506 NOTREACHED() << "Preview data was checked before file dialog."; | 1538 NOTREACHED() << "Preview data was checked before file dialog."; |
| 1507 return; | 1539 return; |
| 1508 } | 1540 } |
| 1509 | 1541 |
| 1510 base::PostTaskWithTraits( | 1542 base::PostTaskWithTraits( |
| 1511 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, | 1543 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
| 1512 base::BindOnce(&PrintToPdfCallback, data, print_to_pdf_path_, | 1544 base::BindOnce(&PrintToPdfCallback, data, print_to_pdf_path_, |
| 1513 pdf_file_saved_closure_)); | 1545 pdf_file_saved_closure_)); |
| 1514 print_to_pdf_path_.clear(); | 1546 print_to_pdf_path_.clear(); |
| 1515 ClosePreviewDialog(); | 1547 ClosePreviewDialog(); |
| 1516 } | 1548 } |
| 1517 | 1549 |
| 1518 void PrintPreviewHandler::FileSelectionCanceled(void* params) { | 1550 void PrintPreviewHandler::FileSelectionCanceled(void* params) { |
| 1519 print_preview_ui()->OnFileSelectionCancelled(); | 1551 RejectJavascriptCallback(base::Value(pdf_callback_id_), base::Value()); |
| 1552 pdf_callback_id_.clear(); | |
| 1520 } | 1553 } |
| 1521 | 1554 |
| 1522 void PrintPreviewHandler::ClearInitiatorDetails() { | 1555 void PrintPreviewHandler::ClearInitiatorDetails() { |
| 1523 WebContents* initiator = GetInitiator(); | 1556 WebContents* initiator = GetInitiator(); |
| 1524 if (!initiator) | 1557 if (!initiator) |
| 1525 return; | 1558 return; |
| 1526 | 1559 |
| 1527 // We no longer require the initiator details. Remove those details associated | 1560 // We no longer require the initiator details. Remove those details associated |
| 1528 // with the preview dialog to allow the initiator to create another preview | 1561 // with the preview dialog to allow the initiator to create another preview |
| 1529 // dialog. | 1562 // dialog. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1597 privet_http_client_->CreateCapabilitiesOperation( | 1630 privet_http_client_->CreateCapabilitiesOperation( |
| 1598 base::Bind(&PrintPreviewHandler::OnPrivetCapabilities, | 1631 base::Bind(&PrintPreviewHandler::OnPrivetCapabilities, |
| 1599 weak_factory_.GetWeakPtr(), callback_id)); | 1632 weak_factory_.GetWeakPtr(), callback_id)); |
| 1600 privet_capabilities_operation_->Start(); | 1633 privet_capabilities_operation_->Start(); |
| 1601 } | 1634 } |
| 1602 | 1635 |
| 1603 bool PrintPreviewHandler::PrivetUpdateClient( | 1636 bool PrintPreviewHandler::PrivetUpdateClient( |
| 1604 const std::string& callback_id, | 1637 const std::string& callback_id, |
| 1605 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { | 1638 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { |
| 1606 if (!http_client) { | 1639 if (!http_client) { |
| 1607 if (callback_id.empty()) { | 1640 RejectJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1608 // This was an attempt to print to a privet printer and has failed. | |
| 1609 FireWebUIListener("print-failed", base::Value(-1)); | |
| 1610 } else { // Capabilities update failed | |
| 1611 RejectJavascriptCallback(base::Value(callback_id), base::Value()); | |
| 1612 } | |
| 1613 privet_http_resolution_.reset(); | 1641 privet_http_resolution_.reset(); |
| 1642 if (callback_id == privet_print_callback_id_) | |
| 1643 privet_print_callback_id_.clear(); | |
| 1614 return false; | 1644 return false; |
| 1615 } | 1645 } |
| 1616 | 1646 |
| 1617 privet_local_print_operation_.reset(); | 1647 privet_local_print_operation_.reset(); |
| 1618 privet_capabilities_operation_.reset(); | 1648 privet_capabilities_operation_.reset(); |
| 1619 privet_http_client_ = cloud_print::PrivetV1HTTPClient::CreateDefault( | 1649 privet_http_client_ = cloud_print::PrivetV1HTTPClient::CreateDefault( |
| 1620 std::move(http_client)); | 1650 std::move(http_client)); |
| 1621 | 1651 |
| 1622 privet_http_resolution_.reset(); | 1652 privet_http_resolution_.reset(); |
| 1623 | 1653 |
| 1624 return true; | 1654 return true; |
| 1625 } | 1655 } |
| 1626 | 1656 |
| 1627 void PrintPreviewHandler::PrivetLocalPrintUpdateClient( | 1657 void PrintPreviewHandler::PrivetLocalPrintUpdateClient( |
| 1658 const std::string& callback_id, | |
| 1628 std::string print_ticket, | 1659 std::string print_ticket, |
| 1629 std::string capabilities, | 1660 std::string capabilities, |
| 1630 gfx::Size page_size, | 1661 gfx::Size page_size, |
| 1631 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { | 1662 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { |
| 1632 if (!PrivetUpdateClient("", std::move(http_client))) | 1663 if (!PrivetUpdateClient(callback_id, std::move(http_client))) |
| 1633 return; | 1664 return; |
| 1634 | 1665 |
| 1635 StartPrivetLocalPrint(print_ticket, capabilities, page_size); | 1666 StartPrivetLocalPrint(print_ticket, capabilities, page_size); |
| 1636 } | 1667 } |
| 1637 | 1668 |
| 1638 void PrintPreviewHandler::StartPrivetLocalPrint(const std::string& print_ticket, | 1669 void PrintPreviewHandler::StartPrivetLocalPrint(const std::string& print_ticket, |
| 1639 const std::string& capabilities, | 1670 const std::string& capabilities, |
| 1640 const gfx::Size& page_size) { | 1671 const gfx::Size& page_size) { |
| 1641 privet_local_print_operation_ = | 1672 privet_local_print_operation_ = |
| 1642 privet_http_client_->CreateLocalPrintOperation(this); | 1673 privet_http_client_->CreateLocalPrintOperation(this); |
| 1643 | 1674 |
| 1644 privet_local_print_operation_->SetTicket(print_ticket); | 1675 privet_local_print_operation_->SetTicket(print_ticket); |
| 1645 privet_local_print_operation_->SetCapabilities(capabilities); | 1676 privet_local_print_operation_->SetCapabilities(capabilities); |
| 1646 | 1677 |
| 1647 scoped_refptr<base::RefCountedBytes> data; | 1678 scoped_refptr<base::RefCountedBytes> data; |
| 1648 base::string16 title; | 1679 base::string16 title; |
| 1649 | 1680 |
| 1650 if (!GetPreviewDataAndTitle(&data, &title)) { | 1681 if (!GetPreviewDataAndTitle(&data, &title)) { |
| 1651 FireWebUIListener("print-failed", base::Value(-1)); | 1682 RejectJavascriptCallback(base::Value(privet_print_callback_id_), |
| 1683 base::Value(-1)); | |
| 1684 privet_print_callback_id_.clear(); | |
| 1652 return; | 1685 return; |
| 1653 } | 1686 } |
| 1654 | 1687 |
| 1655 privet_local_print_operation_->SetJobname(base::UTF16ToUTF8(title)); | 1688 privet_local_print_operation_->SetJobname(base::UTF16ToUTF8(title)); |
| 1656 privet_local_print_operation_->SetPageSize(page_size); | 1689 privet_local_print_operation_->SetPageSize(page_size); |
| 1657 privet_local_print_operation_->SetData(data.get()); | 1690 privet_local_print_operation_->SetData(data.get()); |
| 1658 | 1691 |
| 1659 Profile* profile = Profile::FromWebUI(web_ui()); | 1692 Profile* profile = Profile::FromWebUI(web_ui()); |
| 1660 SigninManagerBase* signin_manager = | 1693 SigninManagerBase* signin_manager = |
| 1661 SigninManagerFactory::GetForProfileIfExists(profile); | 1694 SigninManagerFactory::GetForProfileIfExists(profile); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1694 printer_info_and_caps.SetDictionary("printer", std::move(printer_info)); | 1727 printer_info_and_caps.SetDictionary("printer", std::move(printer_info)); |
| 1695 std::unique_ptr<base::DictionaryValue> capabilities_copy = | 1728 std::unique_ptr<base::DictionaryValue> capabilities_copy = |
| 1696 capabilities->CreateDeepCopy(); | 1729 capabilities->CreateDeepCopy(); |
| 1697 printer_info_and_caps.SetDictionary("capabilities", | 1730 printer_info_and_caps.SetDictionary("capabilities", |
| 1698 std::move(capabilities_copy)); | 1731 std::move(capabilities_copy)); |
| 1699 ResolveJavascriptCallback(base::Value(callback_id), printer_info_and_caps); | 1732 ResolveJavascriptCallback(base::Value(callback_id), printer_info_and_caps); |
| 1700 | 1733 |
| 1701 privet_capabilities_operation_.reset(); | 1734 privet_capabilities_operation_.reset(); |
| 1702 } | 1735 } |
| 1703 | 1736 |
| 1704 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& device_name, | 1737 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& callback_id, |
| 1738 const std::string& device_name, | |
| 1705 const std::string& ticket, | 1739 const std::string& ticket, |
| 1706 const std::string& capabilities, | 1740 const std::string& capabilities, |
| 1707 const gfx::Size& page_size) { | 1741 const gfx::Size& page_size) { |
| 1708 if (!CreatePrivetHTTP( | 1742 if (!CreatePrivetHTTP( |
| 1709 device_name, | 1743 device_name, |
| 1710 base::Bind(&PrintPreviewHandler::PrivetLocalPrintUpdateClient, | 1744 base::Bind(&PrintPreviewHandler::PrivetLocalPrintUpdateClient, |
| 1711 weak_factory_.GetWeakPtr(), ticket, capabilities, | 1745 weak_factory_.GetWeakPtr(), callback_id, ticket, |
| 1712 page_size))) { | 1746 capabilities, page_size))) { |
| 1713 FireWebUIListener("print-failed", base::Value(-1)); | 1747 RejectJavascriptCallback(base::Value(privet_print_callback_id_), |
| 1748 base::Value(-1)); | |
| 1749 privet_print_callback_id_.clear(); | |
| 1714 } | 1750 } |
| 1715 } | 1751 } |
| 1716 | 1752 |
| 1717 bool PrintPreviewHandler::CreatePrivetHTTP( | 1753 bool PrintPreviewHandler::CreatePrivetHTTP( |
| 1718 const std::string& name, | 1754 const std::string& name, |
| 1719 const cloud_print::PrivetHTTPAsynchronousFactory::ResultCallback& | 1755 const cloud_print::PrivetHTTPAsynchronousFactory::ResultCallback& |
| 1720 callback) { | 1756 callback) { |
| 1721 const cloud_print::DeviceDescription* device_description = | 1757 const cloud_print::DeviceDescription* device_description = |
| 1722 printer_lister_ ? printer_lister_->GetDeviceDescription(name) : NULL; | 1758 printer_lister_ ? printer_lister_->GetDeviceDescription(name) : NULL; |
| 1723 | 1759 |
| 1724 if (!device_description) | 1760 if (!device_description) |
| 1725 return false; | 1761 return false; |
| 1726 | 1762 |
| 1727 privet_http_factory_ = | 1763 privet_http_factory_ = |
| 1728 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance( | 1764 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance( |
| 1729 Profile::FromWebUI(web_ui())->GetRequestContext()); | 1765 Profile::FromWebUI(web_ui())->GetRequestContext()); |
| 1730 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP(name); | 1766 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP(name); |
| 1731 privet_http_resolution_->Start(device_description->address, callback); | 1767 privet_http_resolution_->Start(device_description->address, callback); |
| 1732 | 1768 |
| 1733 return true; | 1769 return true; |
| 1734 } | 1770 } |
| 1735 | 1771 |
| 1736 void PrintPreviewHandler::OnPrivetPrintingDone( | 1772 void PrintPreviewHandler::OnPrivetPrintingDone( |
| 1737 const cloud_print::PrivetLocalPrintOperation* print_operation) { | 1773 const cloud_print::PrivetLocalPrintOperation* print_operation) { |
| 1738 ClosePreviewDialog(); | 1774 ResolveJavascriptCallback(base::Value(privet_print_callback_id_), |
| 1775 base::Value()); | |
| 1776 privet_print_callback_id_.clear(); | |
| 1739 } | 1777 } |
| 1740 | 1778 |
| 1741 void PrintPreviewHandler::OnPrivetPrintingError( | 1779 void PrintPreviewHandler::OnPrivetPrintingError( |
| 1742 const cloud_print::PrivetLocalPrintOperation* print_operation, | 1780 const cloud_print::PrivetLocalPrintOperation* print_operation, |
| 1743 int http_code) { | 1781 int http_code) { |
| 1744 FireWebUIListener("print-failed", base::Value(http_code)); | 1782 RejectJavascriptCallback(base::Value(privet_print_callback_id_), |
| 1783 base::Value(http_code)); | |
| 1784 privet_print_callback_id_.clear(); | |
| 1745 } | 1785 } |
| 1746 | 1786 |
| 1747 void PrintPreviewHandler::FillPrinterDescription( | 1787 void PrintPreviewHandler::FillPrinterDescription( |
| 1748 const std::string& name, | 1788 const std::string& name, |
| 1749 const cloud_print::DeviceDescription& description, | 1789 const cloud_print::DeviceDescription& description, |
| 1750 bool has_local_printing, | 1790 bool has_local_printing, |
| 1751 base::DictionaryValue* printer_value) { | 1791 base::DictionaryValue* printer_value) { |
| 1752 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 1792 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 1753 | 1793 |
| 1754 printer_value->SetString("serviceName", name); | 1794 printer_value->SetString("serviceName", name); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1798 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( | 1838 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( |
| 1799 const std::string& callback_id, | 1839 const std::string& callback_id, |
| 1800 const base::DictionaryValue& capabilities) { | 1840 const base::DictionaryValue& capabilities) { |
| 1801 if (capabilities.empty()) { | 1841 if (capabilities.empty()) { |
| 1802 RejectJavascriptCallback(base::Value(callback_id), base::Value()); | 1842 RejectJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1803 return; | 1843 return; |
| 1804 } | 1844 } |
| 1805 ResolveJavascriptCallback(base::Value(callback_id), capabilities); | 1845 ResolveJavascriptCallback(base::Value(callback_id), capabilities); |
| 1806 } | 1846 } |
| 1807 | 1847 |
| 1808 void PrintPreviewHandler::OnExtensionPrintResult(bool success, | 1848 void PrintPreviewHandler::OnExtensionPrintResult(const std::string& callback_id, |
| 1849 bool success, | |
| 1809 const std::string& status) { | 1850 const std::string& status) { |
| 1810 if (success) { | 1851 if (success) { |
| 1811 ClosePreviewDialog(); | 1852 ResolveJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1812 return; | 1853 return; |
| 1813 } | 1854 } |
| 1814 FireWebUIListener("print-failed", base::Value(status)); | 1855 RejectJavascriptCallback(base::Value(callback_id), base::Value(status)); |
| 1815 } | 1856 } |
| 1816 | 1857 |
| 1817 void PrintPreviewHandler::RegisterForGaiaCookieChanges() { | 1858 void PrintPreviewHandler::RegisterForGaiaCookieChanges() { |
| 1818 DCHECK(!gaia_cookie_manager_service_); | 1859 DCHECK(!gaia_cookie_manager_service_); |
| 1819 Profile* profile = Profile::FromWebUI(web_ui()); | 1860 Profile* profile = Profile::FromWebUI(web_ui()); |
| 1820 if (switches::IsAccountConsistencyMirrorEnabled() && | 1861 if (switches::IsAccountConsistencyMirrorEnabled() && |
| 1821 !profile->IsOffTheRecord()) { | 1862 !profile->IsOffTheRecord()) { |
| 1822 gaia_cookie_manager_service_ = | 1863 gaia_cookie_manager_service_ = |
| 1823 GaiaCookieManagerServiceFactory::GetForProfile(profile); | 1864 GaiaCookieManagerServiceFactory::GetForProfile(profile); |
| 1824 if (gaia_cookie_manager_service_) | 1865 if (gaia_cookie_manager_service_) |
| 1825 gaia_cookie_manager_service_->AddObserver(this); | 1866 gaia_cookie_manager_service_->AddObserver(this); |
| 1826 } | 1867 } |
| 1827 } | 1868 } |
| 1828 | 1869 |
| 1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1870 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1830 if (gaia_cookie_manager_service_) | 1871 if (gaia_cookie_manager_service_) |
| 1831 gaia_cookie_manager_service_->RemoveObserver(this); | 1872 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1832 } | 1873 } |
| 1833 | 1874 |
| 1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1875 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1835 const base::Closure& closure) { | 1876 const base::Closure& closure) { |
| 1836 pdf_file_saved_closure_ = closure; | 1877 pdf_file_saved_closure_ = closure; |
| 1837 } | 1878 } |
| OLD | NEW |