Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 2948723002: Print Preview: Change print to cr.sendWithPromise (Closed)
Patch Set: Address comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 |args|. The caller takes
Lei Zhang 2017/06/23 08:30:01 Still referring to |args|.
rbpotter 2017/06/23 17:34:29 Done.
206 // ownership of the returned DictionaryValue. Returns NULL on failure. 206 // ownership of the returned DictionaryValue. Returns NULL on failure.
Lei Zhang 2017/06/23 08:30:01 BTW, no need to mention ownership since unique_ptr
rbpotter 2017/06/23 17:34:29 Done.
207 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( 207 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary(
208 const base::ListValue* args) { 208 std::string json_str) {
Lei Zhang 2017/06/23 08:30:01 Pass by const-ref, yet again. Does "git cl lint" w
rbpotter 2017/06/23 17:34:29 Done. No, git cl lint doesn't warn about this.
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
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
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
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) && !callback_id.empty());
Lei Zhang 2017/06/23 08:30:01 CHECK() the 2 checks separately, so if this ever f
rbpotter 2017/06/23 17:34:29 Done.
869 std::string json_str;
870 if (!args->GetString(1, &json_str))
871 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1));
872
873 std::unique_ptr<base::DictionaryValue> settings =
874 GetSettingsDictionary(json_str);
866 if (!settings) 875 if (!settings)
867 return; 876 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1));
868 877
869 ReportPrintSettingsStats(*settings); 878 ReportPrintSettingsStats(*settings);
870 879
871 // Report whether the user printed a PDF or an HTML document. 880 // Report whether the user printed a PDF or an HTML document.
872 ReportPrintDocumentTypeHistogram(print_preview_ui()->source_is_modifiable() ? 881 ReportPrintDocumentTypeHistogram(print_preview_ui()->source_is_modifiable() ?
873 HTML_DOCUMENT : PDF_DOCUMENT); 882 HTML_DOCUMENT : PDF_DOCUMENT);
874 883
875 bool print_to_pdf = false; 884 bool print_to_pdf = false;
876 bool is_cloud_printer = false; 885 bool is_cloud_printer = false;
877 bool print_with_privet = false; 886 bool print_with_privet = false;
(...skipping 11 matching lines...) Expand all
889 &print_with_extension); 898 &print_with_extension);
890 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); 899 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId);
891 } 900 }
892 901
893 int page_count = 0; 902 int page_count = 0;
894 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count); 903 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count);
895 904
896 if (print_to_pdf) { 905 if (print_to_pdf) {
897 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count); 906 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count);
898 ReportUserActionHistogram(PRINT_TO_PDF); 907 ReportUserActionHistogram(PRINT_TO_PDF);
908 DCHECK(pdf_callback_id_.empty());
909 pdf_callback_id_ = callback_id;
899 PrintToPdf(); 910 PrintToPdf();
900 return; 911 return;
901 } 912 }
902 913
903 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) 914 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
904 if (print_with_privet && PrivetPrintingEnabled()) { 915 if (print_with_privet && PrivetPrintingEnabled()) {
905 std::string printer_name; 916 std::string printer_name;
906 std::string print_ticket; 917 std::string print_ticket;
907 std::string capabilities; 918 std::string capabilities;
908 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithPrivet", page_count); 919 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithPrivet", page_count);
909 ReportUserActionHistogram(PRINT_WITH_PRIVET); 920 ReportUserActionHistogram(PRINT_WITH_PRIVET);
910 921
911 int width = 0; 922 int width = 0;
912 int height = 0; 923 int height = 0;
913 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) || 924 if (!settings->GetString(printing::kSettingDeviceName, &printer_name) ||
914 !settings->GetString(printing::kSettingTicket, &print_ticket) || 925 !settings->GetString(printing::kSettingTicket, &print_ticket) ||
915 !settings->GetString(printing::kSettingCapabilities, &capabilities) || 926 !settings->GetString(printing::kSettingCapabilities, &capabilities) ||
916 !settings->GetInteger(printing::kSettingPageWidth, &width) || 927 !settings->GetInteger(printing::kSettingPageWidth, &width) ||
917 !settings->GetInteger(printing::kSettingPageHeight, &height) || 928 !settings->GetInteger(printing::kSettingPageHeight, &height) ||
918 width <= 0 || height <= 0) { 929 width <= 0 || height <= 0) {
919 NOTREACHED(); 930 NOTREACHED();
920 FireWebUIListener("print-failed", base::Value(-1)); 931 RejectJavascriptCallback(base::Value(callback_id), base::Value(-1));
921 return; 932 return;
922 } 933 }
923 934
924 PrintToPrivetPrinter( 935 DCHECK(privet_print_callback_id_.empty());
925 printer_name, print_ticket, capabilities, gfx::Size(width, height)); 936 privet_print_callback_id_ = callback_id;
937 PrintToPrivetPrinter(callback_id, printer_name, print_ticket, capabilities,
938 gfx::Size(width, height));
926 return; 939 return;
927 } 940 }
928 #endif 941 #endif
929 942
930 if (print_with_extension) { 943 if (print_with_extension) {
931 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithExtension", 944 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithExtension",
932 page_count); 945 page_count);
933 ReportUserActionHistogram(PRINT_WITH_EXTENSION); 946 ReportUserActionHistogram(PRINT_WITH_EXTENSION);
934 947
935 std::string destination_id; 948 std::string destination_id;
936 std::string print_ticket; 949 std::string print_ticket;
937 std::string capabilities; 950 std::string capabilities;
938 int width = 0; 951 int width = 0;
939 int height = 0; 952 int height = 0;
940 if (!settings->GetString(printing::kSettingDeviceName, &destination_id) || 953 if (!settings->GetString(printing::kSettingDeviceName, &destination_id) ||
941 !settings->GetString(printing::kSettingTicket, &print_ticket) || 954 !settings->GetString(printing::kSettingTicket, &print_ticket) ||
942 !settings->GetString(printing::kSettingCapabilities, &capabilities) || 955 !settings->GetString(printing::kSettingCapabilities, &capabilities) ||
943 !settings->GetInteger(printing::kSettingPageWidth, &width) || 956 !settings->GetInteger(printing::kSettingPageWidth, &width) ||
944 !settings->GetInteger(printing::kSettingPageHeight, &height) || 957 !settings->GetInteger(printing::kSettingPageHeight, &height) ||
945 width <= 0 || height <= 0) { 958 width <= 0 || height <= 0) {
946 NOTREACHED(); 959 NOTREACHED();
947 OnExtensionPrintResult(false, "FAILED"); 960 RejectJavascriptCallback(base::Value(callback_id), base::Value("FAILED"));
948 return; 961 return;
949 } 962 }
950 963
951 base::string16 title; 964 base::string16 title;
952 scoped_refptr<base::RefCountedBytes> data; 965 scoped_refptr<base::RefCountedBytes> data;
953 if (!GetPreviewDataAndTitle(&data, &title)) { 966 if (!GetPreviewDataAndTitle(&data, &title)) {
954 LOG(ERROR) << "Nothing to print; no preview available."; 967 LOG(ERROR) << "Nothing to print; no preview available.";
955 OnExtensionPrintResult(false, "NO_DATA"); 968 RejectJavascriptCallback(base::Value(callback_id),
969 base::Value("NO_DATA"));
956 return; 970 return;
957 } 971 }
958 972
959 EnsureExtensionPrinterHandlerSet(); 973 EnsureExtensionPrinterHandlerSet();
960 extension_printer_handler_->StartPrint( 974 extension_printer_handler_->StartPrint(
961 destination_id, capabilities, title, print_ticket, 975 destination_id, capabilities, title, print_ticket,
962 gfx::Size(width, height), data, 976 gfx::Size(width, height), data,
963 base::Bind(&PrintPreviewHandler::OnExtensionPrintResult, 977 base::Bind(&PrintPreviewHandler::OnExtensionPrintResult,
964 weak_factory_.GetWeakPtr())); 978 weak_factory_.GetWeakPtr(), callback_id));
965 return; 979 return;
966 } 980 }
967 981
968 scoped_refptr<base::RefCountedBytes> data; 982 scoped_refptr<base::RefCountedBytes> data;
969 base::string16 title; 983 base::string16 title;
970 if (!GetPreviewDataAndTitle(&data, &title)) { 984 if (!GetPreviewDataAndTitle(&data, &title)) {
971 // Nothing to print, no preview available. 985 // Nothing to print, no preview available.
972 return; 986 RejectJavascriptCallback(base::Value(callback_id), base::Value());
Lei Zhang 2017/06/23 08:30:01 Is this missing a return?
rbpotter 2017/06/23 17:34:29 Done.
973 } 987 }
974 988
975 if (is_cloud_printer) { 989 if (is_cloud_printer) {
976 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint", 990 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint",
977 page_count); 991 page_count);
978 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT); 992 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT);
979 SendCloudPrintJob(data.get()); 993 SendCloudPrintJob(callback_id, data.get());
980 return; 994 return;
981 } 995 }
982 996
983 #if BUILDFLAG(ENABLE_BASIC_PRINTING) 997 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
984 bool system_dialog = false; 998 bool system_dialog = false;
985 settings->GetBoolean(printing::kSettingShowSystemDialog, &system_dialog); 999 settings->GetBoolean(printing::kSettingShowSystemDialog, &system_dialog);
986 if (system_dialog) { 1000 if (system_dialog) {
987 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", page_count); 1001 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", page_count);
988 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG); 1002 ReportUserActionHistogram(FALLBACK_TO_ADVANCED_SETTINGS_DIALOG);
989 } else { 1003 } else {
990 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", page_count); 1004 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", page_count);
991 ReportUserActionHistogram(PRINT_TO_PRINTER); 1005 ReportUserActionHistogram(PRINT_TO_PRINTER);
992 } 1006 }
993 1007
994 // This tries to activate the initiator as well, so do not clear the 1008 // This tries to activate the initiator as well, so do not clear the
Lei Zhang 2017/06/23 08:30:01 Is this comment still valid?
rbpotter 2017/06/23 17:34:29 Yes, because OnHidePreviewDialog will get called a
995 // association with the initiator yet. 1009 // association with the initiator yet.
996 print_preview_ui()->OnHidePreviewDialog(); 1010 ResolveJavascriptCallback(base::Value(callback_id), base::Value());
997 1011
998 // Grab the current initiator before calling ClearInitiatorDetails() below. 1012 // Grab the current initiator before calling ClearInitiatorDetails() below.
999 // Otherwise calling GetInitiator() later will return the wrong WebContents. 1013 // Otherwise calling GetInitiator() later will return the wrong WebContents.
1000 // https://crbug.com/407080 1014 // https://crbug.com/407080
1001 WebContents* initiator = GetInitiator(); 1015 WebContents* initiator = GetInitiator();
1002 if (initiator) { 1016 if (initiator) {
1003 // Save initiator IDs. PrintMsg_PrintForPrintPreview below should cause 1017 // Save initiator IDs. PrintMsg_PrintForPrintPreview below should cause
1004 // the renderer to send PrintHostMsg_UpdatePrintSettings and trigger 1018 // the renderer to send PrintHostMsg_UpdatePrintSettings and trigger
1005 // PrintingMessageFilter::OnUpdatePrintSettings(), which needs this info. 1019 // PrintingMessageFilter::OnUpdatePrintSettings(), which needs this info.
1006 auto* main_render_frame = initiator->GetMainFrame(); 1020 auto* main_render_frame = initiator->GetMainFrame();
(...skipping 22 matching lines...) Expand all
1029 print_view_manager->PrintPreviewDone(); 1043 print_view_manager->PrintPreviewDone();
1030 } 1044 }
1031 #else 1045 #else
1032 NOTREACHED(); 1046 NOTREACHED();
1033 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) 1047 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1034 } 1048 }
1035 1049
1036 void PrintPreviewHandler::PrintToPdf() { 1050 void PrintPreviewHandler::PrintToPdf() {
1037 if (!print_to_pdf_path_.empty()) { 1051 if (!print_to_pdf_path_.empty()) {
1038 // User has already selected a path, no need to show the dialog again. 1052 // User has already selected a path, no need to show the dialog again.
1053 ResolveJavascriptCallback(base::Value(pdf_callback_id_), base::Value());
1054 pdf_callback_id_.clear();
1039 PostPrintToPdfTask(); 1055 PostPrintToPdfTask();
1040 } else if (!select_file_dialog_.get() || 1056 } else if (!select_file_dialog_.get() ||
1041 !select_file_dialog_->IsRunning(platform_util::GetTopLevel( 1057 !select_file_dialog_->IsRunning(platform_util::GetTopLevel(
1042 preview_web_contents()->GetNativeView()))) { 1058 preview_web_contents()->GetNativeView()))) {
1043 // Pre-populating select file dialog with print job title. 1059 // Pre-populating select file dialog with print job title.
1044 const base::string16& print_job_title_utf16 = 1060 const base::string16& print_job_title_utf16 =
1045 print_preview_ui()->initiator_title(); 1061 print_preview_ui()->initiator_title();
1046 1062
1047 #if defined(OS_WIN) 1063 #if defined(OS_WIN)
1048 base::FilePath::StringType print_job_title(print_job_title_utf16); 1064 base::FilePath::StringType print_job_title(print_job_title_utf16);
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1387 PrefService* prefs = profile->GetPrefs(); 1403 PrefService* prefs = profile->GetPrefs();
1388 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { 1404 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) {
1389 base::DictionaryValue settings; 1405 base::DictionaryValue settings;
1390 settings.SetString(kCloudPrintUrl, 1406 settings.SetString(kCloudPrintUrl,
1391 GURL(cloud_devices::GetCloudPrintURL()).spec()); 1407 GURL(cloud_devices::GetCloudPrintURL()).spec());
1392 settings.SetBoolean(kAppKioskMode, chrome::IsRunningInForcedAppMode()); 1408 settings.SetBoolean(kAppKioskMode, chrome::IsRunningInForcedAppMode());
1393 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings); 1409 web_ui()->CallJavascriptFunctionUnsafe("setUseCloudPrint", settings);
1394 } 1410 }
1395 } 1411 }
1396 1412
1397 void PrintPreviewHandler::SendCloudPrintJob(const base::RefCountedBytes* data) { 1413 void PrintPreviewHandler::SendCloudPrintJob(const std::string& callback_id,
1414 const base::RefCountedBytes* data) {
1398 // BASE64 encode the job data. 1415 // BASE64 encode the job data.
1399 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()), 1416 const base::StringPiece raw_data(reinterpret_cast<const char*>(data->front()),
1400 data->size()); 1417 data->size());
1401 std::string base64_data; 1418 std::string base64_data;
1402 base::Base64Encode(raw_data, &base64_data); 1419 base::Base64Encode(raw_data, &base64_data);
1403 base::Value data_value(base64_data);
1404 1420
1405 web_ui()->CallJavascriptFunctionUnsafe("printToCloud", data_value); 1421 ResolveJavascriptCallback(base::Value(callback_id), base::Value(base64_data));
1406 } 1422 }
1407 1423
1408 WebContents* PrintPreviewHandler::GetInitiator() const { 1424 WebContents* PrintPreviewHandler::GetInitiator() const {
1409 printing::PrintPreviewDialogController* dialog_controller = 1425 printing::PrintPreviewDialogController* dialog_controller =
1410 printing::PrintPreviewDialogController::GetInstance(); 1426 printing::PrintPreviewDialogController::GetInstance();
1411 if (!dialog_controller) 1427 if (!dialog_controller)
1412 return NULL; 1428 return NULL;
1413 return dialog_controller->GetInitiator(preview_web_contents()); 1429 return dialog_controller->GetInitiator(preview_web_contents());
1414 } 1430 }
1415 1431
1416 void PrintPreviewHandler::OnAddAccountToCookieCompleted( 1432 void PrintPreviewHandler::OnAddAccountToCookieCompleted(
1417 const std::string& account_id, 1433 const std::string& account_id,
1418 const GoogleServiceAuthError& error) { 1434 const GoogleServiceAuthError& error) {
1419 OnSigninComplete(); 1435 OnSigninComplete();
1420 } 1436 }
1421 1437
1422 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename, 1438 void PrintPreviewHandler::SelectFile(const base::FilePath& default_filename,
1423 bool prompt_user) { 1439 bool prompt_user) {
1424 if (prompt_user) { 1440 if (prompt_user) {
1425 ChromeSelectFilePolicy policy(GetInitiator()); 1441 ChromeSelectFilePolicy policy(GetInitiator());
1426 if (!policy.CanOpenSelectFileDialog()) { 1442 if (!policy.CanOpenSelectFileDialog()) {
1427 policy.SelectFileDenied(); 1443 policy.SelectFileDenied();
1428 return ClosePreviewDialog(); 1444 RejectJavascriptCallback(base::Value(pdf_callback_id_), base::Value());
1445 pdf_callback_id_.clear();
Lei Zhang 2017/06/23 08:30:01 Is this suppose to return?
rbpotter 2017/06/23 17:34:30 Return type is void. ClosePreviewDialog() does not
Lei Zhang 2017/06/23 20:39:57 But now execution continues onwards, whereas befor
1429 } 1446 }
1430 } 1447 }
1431 1448
1432 // Get save location from Download Preferences. 1449 // Get save location from Download Preferences.
1433 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( 1450 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
1434 preview_web_contents()->GetBrowserContext()); 1451 preview_web_contents()->GetBrowserContext());
1435 base::FilePath file_path = download_prefs->SaveFilePath(); 1452 base::FilePath file_path = download_prefs->SaveFilePath();
1436 printing::StickySettings* sticky_settings = GetStickySettings(); 1453 printing::StickySettings* sticky_settings = GetStickySettings();
1437 sticky_settings->SaveInPrefs(Profile::FromBrowserContext( 1454 sticky_settings->SaveInPrefs(Profile::FromBrowserContext(
1438 preview_web_contents()->GetBrowserContext())->GetPrefs()); 1455 preview_web_contents()->GetBrowserContext())->GetPrefs());
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1487 int /* index */, 1504 int /* index */,
1488 void* /* params */) { 1505 void* /* params */) {
1489 // Update downloads location and save sticky settings. 1506 // Update downloads location and save sticky settings.
1490 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( 1507 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
1491 preview_web_contents()->GetBrowserContext()); 1508 preview_web_contents()->GetBrowserContext());
1492 download_prefs->SetSaveFilePath(path.DirName()); 1509 download_prefs->SetSaveFilePath(path.DirName());
1493 printing::StickySettings* sticky_settings = GetStickySettings(); 1510 printing::StickySettings* sticky_settings = GetStickySettings();
1494 sticky_settings->SaveInPrefs( 1511 sticky_settings->SaveInPrefs(
1495 Profile::FromBrowserContext(preview_web_contents()->GetBrowserContext()) 1512 Profile::FromBrowserContext(preview_web_contents()->GetBrowserContext())
1496 ->GetPrefs()); 1513 ->GetPrefs());
1497 web_ui()->CallJavascriptFunctionUnsafe("fileSelectionCompleted"); 1514 ResolveJavascriptCallback(base::Value(pdf_callback_id_), base::Value());
1515 pdf_callback_id_.clear();
1498 print_to_pdf_path_ = path; 1516 print_to_pdf_path_ = path;
1499 PostPrintToPdfTask(); 1517 PostPrintToPdfTask();
1500 } 1518 }
1501 1519
1502 void PrintPreviewHandler::PostPrintToPdfTask() { 1520 void PrintPreviewHandler::PostPrintToPdfTask() {
1503 scoped_refptr<base::RefCountedBytes> data; 1521 scoped_refptr<base::RefCountedBytes> data;
1504 base::string16 title; 1522 base::string16 title;
1505 if (!GetPreviewDataAndTitle(&data, &title)) { 1523 if (!GetPreviewDataAndTitle(&data, &title)) {
1506 NOTREACHED() << "Preview data was checked before file dialog."; 1524 NOTREACHED() << "Preview data was checked before file dialog.";
1507 return; 1525 return;
1508 } 1526 }
1509 1527
1510 base::PostTaskWithTraits( 1528 base::PostTaskWithTraits(
1511 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, 1529 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
1512 base::BindOnce(&PrintToPdfCallback, data, print_to_pdf_path_, 1530 base::BindOnce(&PrintToPdfCallback, data, print_to_pdf_path_,
1513 pdf_file_saved_closure_)); 1531 pdf_file_saved_closure_));
1514 print_to_pdf_path_.clear(); 1532 print_to_pdf_path_.clear();
1515 ClosePreviewDialog(); 1533 ClosePreviewDialog();
1516 } 1534 }
1517 1535
1518 void PrintPreviewHandler::FileSelectionCanceled(void* params) { 1536 void PrintPreviewHandler::FileSelectionCanceled(void* params) {
1519 print_preview_ui()->OnFileSelectionCancelled(); 1537 RejectJavascriptCallback(base::Value(pdf_callback_id_), base::Value());
1538 pdf_callback_id_.clear();
1520 } 1539 }
1521 1540
1522 void PrintPreviewHandler::ClearInitiatorDetails() { 1541 void PrintPreviewHandler::ClearInitiatorDetails() {
1523 WebContents* initiator = GetInitiator(); 1542 WebContents* initiator = GetInitiator();
1524 if (!initiator) 1543 if (!initiator)
1525 return; 1544 return;
1526 1545
1527 // We no longer require the initiator details. Remove those details associated 1546 // We no longer require the initiator details. Remove those details associated
1528 // with the preview dialog to allow the initiator to create another preview 1547 // with the preview dialog to allow the initiator to create another preview
1529 // dialog. 1548 // dialog.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1597 privet_http_client_->CreateCapabilitiesOperation( 1616 privet_http_client_->CreateCapabilitiesOperation(
1598 base::Bind(&PrintPreviewHandler::OnPrivetCapabilities, 1617 base::Bind(&PrintPreviewHandler::OnPrivetCapabilities,
1599 weak_factory_.GetWeakPtr(), callback_id)); 1618 weak_factory_.GetWeakPtr(), callback_id));
1600 privet_capabilities_operation_->Start(); 1619 privet_capabilities_operation_->Start();
1601 } 1620 }
1602 1621
1603 bool PrintPreviewHandler::PrivetUpdateClient( 1622 bool PrintPreviewHandler::PrivetUpdateClient(
1604 const std::string& callback_id, 1623 const std::string& callback_id,
1605 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { 1624 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) {
1606 if (!http_client) { 1625 if (!http_client) {
1607 if (callback_id.empty()) { 1626 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(); 1627 privet_http_resolution_.reset();
1628 if (callback_id == privet_print_callback_id_)
1629 privet_print_callback_id_.clear();
1614 return false; 1630 return false;
1615 } 1631 }
1616 1632
1617 privet_local_print_operation_.reset(); 1633 privet_local_print_operation_.reset();
1618 privet_capabilities_operation_.reset(); 1634 privet_capabilities_operation_.reset();
1619 privet_http_client_ = cloud_print::PrivetV1HTTPClient::CreateDefault( 1635 privet_http_client_ = cloud_print::PrivetV1HTTPClient::CreateDefault(
1620 std::move(http_client)); 1636 std::move(http_client));
1621 1637
1622 privet_http_resolution_.reset(); 1638 privet_http_resolution_.reset();
1623 1639
1624 return true; 1640 return true;
1625 } 1641 }
1626 1642
1627 void PrintPreviewHandler::PrivetLocalPrintUpdateClient( 1643 void PrintPreviewHandler::PrivetLocalPrintUpdateClient(
1644 const std::string& callback_id,
1628 std::string print_ticket, 1645 std::string print_ticket,
1629 std::string capabilities, 1646 std::string capabilities,
1630 gfx::Size page_size, 1647 gfx::Size page_size,
1631 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) { 1648 std::unique_ptr<cloud_print::PrivetHTTPClient> http_client) {
1632 if (!PrivetUpdateClient("", std::move(http_client))) 1649 if (!PrivetUpdateClient(callback_id, std::move(http_client)))
1633 return; 1650 return;
1634 1651
1635 StartPrivetLocalPrint(print_ticket, capabilities, page_size); 1652 StartPrivetLocalPrint(print_ticket, capabilities, page_size);
1636 } 1653 }
1637 1654
1638 void PrintPreviewHandler::StartPrivetLocalPrint(const std::string& print_ticket, 1655 void PrintPreviewHandler::StartPrivetLocalPrint(const std::string& print_ticket,
1639 const std::string& capabilities, 1656 const std::string& capabilities,
1640 const gfx::Size& page_size) { 1657 const gfx::Size& page_size) {
1641 privet_local_print_operation_ = 1658 privet_local_print_operation_ =
1642 privet_http_client_->CreateLocalPrintOperation(this); 1659 privet_http_client_->CreateLocalPrintOperation(this);
1643 1660
1644 privet_local_print_operation_->SetTicket(print_ticket); 1661 privet_local_print_operation_->SetTicket(print_ticket);
1645 privet_local_print_operation_->SetCapabilities(capabilities); 1662 privet_local_print_operation_->SetCapabilities(capabilities);
1646 1663
1647 scoped_refptr<base::RefCountedBytes> data; 1664 scoped_refptr<base::RefCountedBytes> data;
1648 base::string16 title; 1665 base::string16 title;
1649 1666
1650 if (!GetPreviewDataAndTitle(&data, &title)) { 1667 if (!GetPreviewDataAndTitle(&data, &title)) {
1651 FireWebUIListener("print-failed", base::Value(-1)); 1668 RejectJavascriptCallback(base::Value(privet_print_callback_id_),
1669 base::Value(-1));
1670 privet_print_callback_id_.clear();
1652 return; 1671 return;
1653 } 1672 }
1654 1673
1655 privet_local_print_operation_->SetJobname(base::UTF16ToUTF8(title)); 1674 privet_local_print_operation_->SetJobname(base::UTF16ToUTF8(title));
1656 privet_local_print_operation_->SetPageSize(page_size); 1675 privet_local_print_operation_->SetPageSize(page_size);
1657 privet_local_print_operation_->SetData(data.get()); 1676 privet_local_print_operation_->SetData(data.get());
1658 1677
1659 Profile* profile = Profile::FromWebUI(web_ui()); 1678 Profile* profile = Profile::FromWebUI(web_ui());
1660 SigninManagerBase* signin_manager = 1679 SigninManagerBase* signin_manager =
1661 SigninManagerFactory::GetForProfileIfExists(profile); 1680 SigninManagerFactory::GetForProfileIfExists(profile);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1694 printer_info_and_caps.SetDictionary("printer", std::move(printer_info)); 1713 printer_info_and_caps.SetDictionary("printer", std::move(printer_info));
1695 std::unique_ptr<base::DictionaryValue> capabilities_copy = 1714 std::unique_ptr<base::DictionaryValue> capabilities_copy =
1696 capabilities->CreateDeepCopy(); 1715 capabilities->CreateDeepCopy();
1697 printer_info_and_caps.SetDictionary("capabilities", 1716 printer_info_and_caps.SetDictionary("capabilities",
1698 std::move(capabilities_copy)); 1717 std::move(capabilities_copy));
1699 ResolveJavascriptCallback(base::Value(callback_id), printer_info_and_caps); 1718 ResolveJavascriptCallback(base::Value(callback_id), printer_info_and_caps);
1700 1719
1701 privet_capabilities_operation_.reset(); 1720 privet_capabilities_operation_.reset();
1702 } 1721 }
1703 1722
1704 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& device_name, 1723 void PrintPreviewHandler::PrintToPrivetPrinter(const std::string& callback_id,
1724 const std::string& device_name,
1705 const std::string& ticket, 1725 const std::string& ticket,
1706 const std::string& capabilities, 1726 const std::string& capabilities,
1707 const gfx::Size& page_size) { 1727 const gfx::Size& page_size) {
1708 if (!CreatePrivetHTTP( 1728 if (!CreatePrivetHTTP(
1709 device_name, 1729 device_name,
1710 base::Bind(&PrintPreviewHandler::PrivetLocalPrintUpdateClient, 1730 base::Bind(&PrintPreviewHandler::PrivetLocalPrintUpdateClient,
1711 weak_factory_.GetWeakPtr(), ticket, capabilities, 1731 weak_factory_.GetWeakPtr(), callback_id, ticket,
1712 page_size))) { 1732 capabilities, page_size))) {
1713 FireWebUIListener("print-failed", base::Value(-1)); 1733 RejectJavascriptCallback(base::Value(privet_print_callback_id_),
1734 base::Value(-1));
1735 privet_print_callback_id_.clear();
1714 } 1736 }
1715 } 1737 }
1716 1738
1717 bool PrintPreviewHandler::CreatePrivetHTTP( 1739 bool PrintPreviewHandler::CreatePrivetHTTP(
1718 const std::string& name, 1740 const std::string& name,
1719 const cloud_print::PrivetHTTPAsynchronousFactory::ResultCallback& 1741 const cloud_print::PrivetHTTPAsynchronousFactory::ResultCallback&
1720 callback) { 1742 callback) {
1721 const cloud_print::DeviceDescription* device_description = 1743 const cloud_print::DeviceDescription* device_description =
1722 printer_lister_ ? printer_lister_->GetDeviceDescription(name) : NULL; 1744 printer_lister_ ? printer_lister_->GetDeviceDescription(name) : NULL;
1723 1745
1724 if (!device_description) 1746 if (!device_description)
1725 return false; 1747 return false;
1726 1748
1727 privet_http_factory_ = 1749 privet_http_factory_ =
1728 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance( 1750 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance(
1729 Profile::FromWebUI(web_ui())->GetRequestContext()); 1751 Profile::FromWebUI(web_ui())->GetRequestContext());
1730 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP(name); 1752 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP(name);
1731 privet_http_resolution_->Start(device_description->address, callback); 1753 privet_http_resolution_->Start(device_description->address, callback);
1732 1754
1733 return true; 1755 return true;
1734 } 1756 }
1735 1757
1736 void PrintPreviewHandler::OnPrivetPrintingDone( 1758 void PrintPreviewHandler::OnPrivetPrintingDone(
1737 const cloud_print::PrivetLocalPrintOperation* print_operation) { 1759 const cloud_print::PrivetLocalPrintOperation* print_operation) {
1738 ClosePreviewDialog(); 1760 ResolveJavascriptCallback(base::Value(privet_print_callback_id_),
1761 base::Value());
1762 privet_print_callback_id_.clear();
1739 } 1763 }
1740 1764
1741 void PrintPreviewHandler::OnPrivetPrintingError( 1765 void PrintPreviewHandler::OnPrivetPrintingError(
1742 const cloud_print::PrivetLocalPrintOperation* print_operation, 1766 const cloud_print::PrivetLocalPrintOperation* print_operation,
1743 int http_code) { 1767 int http_code) {
1744 FireWebUIListener("print-failed", base::Value(http_code)); 1768 RejectJavascriptCallback(base::Value(privet_print_callback_id_),
1769 base::Value(http_code));
1770 privet_print_callback_id_.clear();
1745 } 1771 }
1746 1772
1747 void PrintPreviewHandler::FillPrinterDescription( 1773 void PrintPreviewHandler::FillPrinterDescription(
1748 const std::string& name, 1774 const std::string& name,
1749 const cloud_print::DeviceDescription& description, 1775 const cloud_print::DeviceDescription& description,
1750 bool has_local_printing, 1776 bool has_local_printing,
1751 base::DictionaryValue* printer_value) { 1777 base::DictionaryValue* printer_value) {
1752 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 1778 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
1753 1779
1754 printer_value->SetString("serviceName", name); 1780 printer_value->SetString("serviceName", name);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( 1824 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities(
1799 const std::string& callback_id, 1825 const std::string& callback_id,
1800 const base::DictionaryValue& capabilities) { 1826 const base::DictionaryValue& capabilities) {
1801 if (capabilities.empty()) { 1827 if (capabilities.empty()) {
1802 RejectJavascriptCallback(base::Value(callback_id), base::Value()); 1828 RejectJavascriptCallback(base::Value(callback_id), base::Value());
1803 return; 1829 return;
1804 } 1830 }
1805 ResolveJavascriptCallback(base::Value(callback_id), capabilities); 1831 ResolveJavascriptCallback(base::Value(callback_id), capabilities);
1806 } 1832 }
1807 1833
1808 void PrintPreviewHandler::OnExtensionPrintResult(bool success, 1834 void PrintPreviewHandler::OnExtensionPrintResult(const std::string& callback_id,
1835 bool success,
1809 const std::string& status) { 1836 const std::string& status) {
1810 if (success) { 1837 if (success) {
1811 ClosePreviewDialog(); 1838 ResolveJavascriptCallback(base::Value(callback_id), base::Value());
Lei Zhang 2017/06/23 08:30:01 We are calling: ResolveJavascriptCallback(base::Va
rbpotter 2017/06/23 17:34:29 Below is RejectJavascriptCallback, not Resolve, so
1812 return; 1839 return;
1813 } 1840 }
1814 FireWebUIListener("print-failed", base::Value(status)); 1841 RejectJavascriptCallback(base::Value(callback_id), base::Value(status));
1815 } 1842 }
1816 1843
1817 void PrintPreviewHandler::RegisterForGaiaCookieChanges() { 1844 void PrintPreviewHandler::RegisterForGaiaCookieChanges() {
1818 DCHECK(!gaia_cookie_manager_service_); 1845 DCHECK(!gaia_cookie_manager_service_);
1819 Profile* profile = Profile::FromWebUI(web_ui()); 1846 Profile* profile = Profile::FromWebUI(web_ui());
1820 if (switches::IsAccountConsistencyMirrorEnabled() && 1847 if (switches::IsAccountConsistencyMirrorEnabled() &&
1821 !profile->IsOffTheRecord()) { 1848 !profile->IsOffTheRecord()) {
1822 gaia_cookie_manager_service_ = 1849 gaia_cookie_manager_service_ =
1823 GaiaCookieManagerServiceFactory::GetForProfile(profile); 1850 GaiaCookieManagerServiceFactory::GetForProfile(profile);
1824 if (gaia_cookie_manager_service_) 1851 if (gaia_cookie_manager_service_)
1825 gaia_cookie_manager_service_->AddObserver(this); 1852 gaia_cookie_manager_service_->AddObserver(this);
1826 } 1853 }
1827 } 1854 }
1828 1855
1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { 1856 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
1830 if (gaia_cookie_manager_service_) 1857 if (gaia_cookie_manager_service_)
1831 gaia_cookie_manager_service_->RemoveObserver(this); 1858 gaia_cookie_manager_service_->RemoveObserver(this);
1832 } 1859 }
1833 1860
1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( 1861 void PrintPreviewHandler::SetPdfSavedClosureForTesting(
1835 const base::Closure& closure) { 1862 const base::Closure& closure) {
1836 pdf_file_saved_closure_ = closure; 1863 pdf_file_saved_closure_ = closure;
1837 } 1864 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698