| 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 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview); | 433 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview); |
| 434 #endif | 434 #endif |
| 435 | 435 |
| 436 if (!open_pdf_in_preview) { | 436 if (!open_pdf_in_preview) { |
| 437 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 437 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 438 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog); | 438 settings->GetBoolean(printing::kSettingCloudPrintDialog, &is_cloud_dialog); |
| 439 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); | 439 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); |
| 440 } | 440 } |
| 441 | 441 |
| 442 if (is_cloud_printer) { | 442 if (is_cloud_printer) { |
| 443 std::string print_ticket; | 443 SendCloudPrintJob(); |
| 444 bool res = args->GetString(1, &print_ticket); | |
| 445 DCHECK(res); | |
| 446 SendCloudPrintJob(*settings, print_ticket); | |
| 447 } else if (print_to_pdf) { | 444 } else if (print_to_pdf) { |
| 448 HandlePrintToPdf(*settings); | 445 HandlePrintToPdf(*settings); |
| 449 } else if (is_cloud_dialog) { | 446 } else if (is_cloud_dialog) { |
| 450 HandlePrintWithCloudPrint(NULL); | 447 HandlePrintWithCloudPrint(NULL); |
| 451 } else { | 448 } else { |
| 452 ReportPrintSettingsStats(*settings); | 449 ReportPrintSettingsStats(*settings); |
| 453 ReportUserActionHistogram(PRINT_TO_PRINTER); | 450 ReportUserActionHistogram(PRINT_TO_PRINTER); |
| 454 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", | 451 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPrinter", |
| 455 GetPageCountFromSettingsDictionary(*settings)); | 452 GetPageCountFromSettingsDictionary(*settings)); |
| 456 | 453 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 Profile* profile = Profile::FromBrowserContext( | 785 Profile* profile = Profile::FromBrowserContext( |
| 789 preview_web_contents()->GetBrowserContext()); | 786 preview_web_contents()->GetBrowserContext()); |
| 790 PrefService* prefs = profile->GetPrefs(); | 787 PrefService* prefs = profile->GetPrefs(); |
| 791 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { | 788 if (prefs->GetBoolean(prefs::kCloudPrintSubmitEnabled)) { |
| 792 GURL gcp_url(CloudPrintURL(profile).GetCloudPrintServiceURL()); | 789 GURL gcp_url(CloudPrintURL(profile).GetCloudPrintServiceURL()); |
| 793 base::StringValue gcp_url_value(gcp_url.spec()); | 790 base::StringValue gcp_url_value(gcp_url.spec()); |
| 794 web_ui()->CallJavascriptFunction("setUseCloudPrint", gcp_url_value); | 791 web_ui()->CallJavascriptFunction("setUseCloudPrint", gcp_url_value); |
| 795 } | 792 } |
| 796 } | 793 } |
| 797 | 794 |
| 798 void PrintPreviewHandler::SendCloudPrintJob(const DictionaryValue& settings, | 795 void PrintPreviewHandler::SendCloudPrintJob() { |
| 799 std::string print_ticket) { | |
| 800 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT); | 796 ReportUserActionHistogram(PRINT_WITH_CLOUD_PRINT); |
| 801 scoped_refptr<base::RefCountedBytes> data; | 797 scoped_refptr<base::RefCountedBytes> data; |
| 802 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 798 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 803 web_ui()->GetController()); | 799 web_ui()->GetController()); |
| 804 print_preview_ui->GetPrintPreviewDataForIndex( | 800 print_preview_ui->GetPrintPreviewDataForIndex( |
| 805 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); | 801 printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, &data); |
| 806 if (data.get() && data->size() > 0U && data->front()) { | 802 if (data.get() && data->size() > 0U && data->front()) { |
| 807 printing::PrintViewManager* print_view_manager = | |
| 808 printing::PrintViewManager::FromWebContents( | |
| 809 preview_tab_contents()->web_contents()); | |
| 810 string16 print_job_title_utf16 = print_view_manager->RenderSourceName(); | |
| 811 std::string print_job_title = UTF16ToUTF8(print_job_title_utf16); | |
| 812 std::string printer_id; | |
| 813 settings.GetString(printing::kSettingCloudPrintId, &printer_id); | |
| 814 // BASE64 encode the job data. | 803 // BASE64 encode the job data. |
| 815 std::string raw_data(reinterpret_cast<const char*>(data->front()), | 804 std::string raw_data(reinterpret_cast<const char*>(data->front()), |
| 816 data->size()); | 805 data->size()); |
| 817 std::string base64_data; | 806 std::string base64_data; |
| 818 if (!base::Base64Encode(raw_data, &base64_data)) { | 807 if (!base::Base64Encode(raw_data, &base64_data)) { |
| 819 NOTREACHED() << "Base64 encoding PDF data."; | 808 NOTREACHED() << "Base64 encoding PDF data."; |
| 820 } | 809 } |
| 821 | 810 StringValue data_value(base64_data); |
| 822 const char boundary[] = "----CloudPrintFormBoundaryjc9wuprokl8i"; | |
| 823 const char prolog[] = "--%s\r\n" | |
| 824 "Content-Disposition: form-data; name=\"capabilities\"\r\n\r\n%s\r\n" | |
| 825 "--%s\r\n" | |
| 826 "Content-Disposition: form-data; name=\"contentType\"\r\n\r\ndataUrl\r\n" | |
| 827 "--%s\r\n" | |
| 828 "Content-Disposition: form-data; name=\"title\"\r\n\r\n%s\r\n" | |
| 829 "--%s\r\n" | |
| 830 "Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%s\r\n" | |
| 831 "--%s\r\n" | |
| 832 "Content-Disposition: form-data; name=\"content\"\r\n\r\n" | |
| 833 "data:application/pdf;base64,%s\r\n" | |
| 834 "--%s\r\n"; | |
| 835 | |
| 836 // TODO(abodenha@chromium.org) This implies a large copy operation. | |
| 837 // Profile this and optimize if necessary. | |
| 838 std::string final_data; | |
| 839 base::SStringPrintf(&final_data, | |
| 840 prolog, | |
| 841 boundary, | |
| 842 print_ticket.c_str(), | |
| 843 boundary, | |
| 844 boundary, | |
| 845 print_job_title.c_str(), | |
| 846 boundary, | |
| 847 printer_id.c_str(), | |
| 848 boundary, | |
| 849 base64_data.c_str(), | |
| 850 boundary); | |
| 851 | |
| 852 StringValue data_value(final_data); | |
| 853 | 811 |
| 854 web_ui()->CallJavascriptFunction("printToCloud", data_value); | 812 web_ui()->CallJavascriptFunction("printToCloud", data_value); |
| 855 } else { | 813 } else { |
| 856 NOTREACHED(); | 814 NOTREACHED(); |
| 857 } | 815 } |
| 858 } | 816 } |
| 859 | 817 |
| 860 TabContents* PrintPreviewHandler::GetInitiatorTab() const { | 818 TabContents* PrintPreviewHandler::GetInitiatorTab() const { |
| 861 printing::PrintPreviewTabController* tab_controller = | 819 printing::PrintPreviewTabController* tab_controller = |
| 862 printing::PrintPreviewTabController::GetInstance(); | 820 printing::PrintPreviewTabController::GetInstance(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 return; | 932 return; |
| 975 | 933 |
| 976 // We no longer require the initiator tab details. Remove those details | 934 // We no longer require the initiator tab details. Remove those details |
| 977 // associated with the preview tab to allow the initiator tab to create | 935 // associated with the preview tab to allow the initiator tab to create |
| 978 // another preview tab. | 936 // another preview tab. |
| 979 printing::PrintPreviewTabController* tab_controller = | 937 printing::PrintPreviewTabController* tab_controller = |
| 980 printing::PrintPreviewTabController::GetInstance(); | 938 printing::PrintPreviewTabController::GetInstance(); |
| 981 if (tab_controller) | 939 if (tab_controller) |
| 982 tab_controller->EraseInitiatorTabInfo(preview_tab_contents()); | 940 tab_controller->EraseInitiatorTabInfo(preview_tab_contents()); |
| 983 } | 941 } |
| OLD | NEW |