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 19 matching lines...) Expand all Loading... |
30 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 30 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
31 #include "chrome/browser/printing/print_dialog_cloud.h" | 31 #include "chrome/browser/printing/print_dialog_cloud.h" |
32 #include "chrome/browser/printing/print_error_dialog.h" | 32 #include "chrome/browser/printing/print_error_dialog.h" |
33 #include "chrome/browser/printing/print_job_manager.h" | 33 #include "chrome/browser/printing/print_job_manager.h" |
34 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 34 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
35 #include "chrome/browser/printing/print_view_manager.h" | 35 #include "chrome/browser/printing/print_view_manager.h" |
36 #include "chrome/browser/printing/printer_manager_dialog.h" | 36 #include "chrome/browser/printing/printer_manager_dialog.h" |
37 #include "chrome/browser/profiles/profile.h" | 37 #include "chrome/browser/profiles/profile.h" |
38 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 38 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
39 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 39 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 40 #include "chrome/browser/signin/signin_manager.h" |
| 41 #include "chrome/browser/signin/signin_manager_base.h" |
| 42 #include "chrome/browser/signin/signin_manager_factory.h" |
40 #include "chrome/browser/ui/browser_finder.h" | 43 #include "chrome/browser/ui/browser_finder.h" |
41 #include "chrome/browser/ui/browser_tabstrip.h" | 44 #include "chrome/browser/ui/browser_tabstrip.h" |
42 #include "chrome/browser/ui/chrome_select_file_policy.h" | 45 #include "chrome/browser/ui/chrome_select_file_policy.h" |
43 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 46 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
44 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 47 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
45 #include "chrome/common/chrome_paths.h" | 48 #include "chrome/common/chrome_paths.h" |
46 #include "chrome/common/chrome_switches.h" | 49 #include "chrome/common/chrome_switches.h" |
47 #include "chrome/common/cloud_print/cloud_print_constants.h" | 50 #include "chrome/common/cloud_print/cloud_print_constants.h" |
48 #include "chrome/common/crash_keys.h" | 51 #include "chrome/common/crash_keys.h" |
49 #include "chrome/common/pref_names.h" | 52 #include "chrome/common/pref_names.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 enum UserActionBuckets { | 84 enum UserActionBuckets { |
82 PRINT_TO_PRINTER, | 85 PRINT_TO_PRINTER, |
83 PRINT_TO_PDF, | 86 PRINT_TO_PDF, |
84 CANCEL, | 87 CANCEL, |
85 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, | 88 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, |
86 PREVIEW_FAILED, | 89 PREVIEW_FAILED, |
87 PREVIEW_STARTED, | 90 PREVIEW_STARTED, |
88 INITIATOR_CRASHED, // UNUSED | 91 INITIATOR_CRASHED, // UNUSED |
89 INITIATOR_CLOSED, | 92 INITIATOR_CLOSED, |
90 PRINT_WITH_CLOUD_PRINT, | 93 PRINT_WITH_CLOUD_PRINT, |
| 94 PRINT_WITH_PRIVET, |
91 USERACTION_BUCKET_BOUNDARY | 95 USERACTION_BUCKET_BOUNDARY |
92 }; | 96 }; |
93 | 97 |
94 enum PrintSettingsBuckets { | 98 enum PrintSettingsBuckets { |
95 LANDSCAPE = 0, | 99 LANDSCAPE = 0, |
96 PORTRAIT, | 100 PORTRAIT, |
97 COLOR, | 101 COLOR, |
98 BLACK_AND_WHITE, | 102 BLACK_AND_WHITE, |
99 COLLATE, | 103 COLLATE, |
100 SIMPLEX, | 104 SIMPLEX, |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } | 599 } |
596 } | 600 } |
597 | 601 |
598 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( | 602 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( |
599 const base::ListValue* args) { | 603 const base::ListValue* args) { |
600 #if defined(ENABLE_MDNS) | 604 #if defined(ENABLE_MDNS) |
601 std::string name; | 605 std::string name; |
602 bool success = args->GetString(0, &name); | 606 bool success = args->GetString(0, &name); |
603 DCHECK(success); | 607 DCHECK(success); |
604 | 608 |
605 const local_discovery::DeviceDescription* device_description = | 609 CreatePrivetHTTP( |
606 printer_lister_->GetDeviceDescription(name); | |
607 | |
608 if (!device_description) { | |
609 SendPrivetCapabilitiesError(name); | |
610 return; | |
611 } | |
612 | |
613 privet_http_factory_ = | |
614 local_discovery::PrivetHTTPAsynchronousFactory::CreateInstance( | |
615 service_discovery_client_, | |
616 Profile::FromWebUI(web_ui())->GetRequestContext()); | |
617 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP( | |
618 name, | 610 name, |
619 device_description->address, | 611 base::Bind(&PrintPreviewHandler::PrivetCapabilitiesUpdateClient, |
620 base::Bind(&PrintPreviewHandler::StartPrivetCapabilities, | |
621 base::Unretained(this))); | 612 base::Unretained(this))); |
622 privet_http_resolution_->Start(); | |
623 #endif | 613 #endif |
624 } | 614 } |
625 | 615 |
626 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { | 616 void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { |
627 DCHECK_EQ(3U, args->GetSize()); | 617 DCHECK_EQ(3U, args->GetSize()); |
628 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 618 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
629 if (!settings.get()) | 619 if (!settings.get()) |
630 return; | 620 return; |
631 int request_id = -1; | 621 int request_id = -1; |
632 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) | 622 if (!settings->GetInteger(printing::kPreviewRequestID, &request_id)) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 | 701 |
712 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 702 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
713 if (!settings.get()) | 703 if (!settings.get()) |
714 return; | 704 return; |
715 | 705 |
716 // Never try to add headers/footers here. It's already in the generated PDF. | 706 // Never try to add headers/footers here. It's already in the generated PDF. |
717 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); | 707 settings->SetBoolean(printing::kSettingHeaderFooterEnabled, false); |
718 | 708 |
719 bool print_to_pdf = false; | 709 bool print_to_pdf = false; |
720 bool is_cloud_printer = false; | 710 bool is_cloud_printer = false; |
| 711 bool print_with_privet = false; |
721 | 712 |
722 bool open_pdf_in_preview = false; | 713 bool open_pdf_in_preview = false; |
723 #if defined(OS_MACOSX) | 714 #if defined(OS_MACOSX) |
724 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview); | 715 open_pdf_in_preview = settings->HasKey(printing::kSettingOpenPDFInPreview); |
725 #endif | 716 #endif |
726 | 717 |
727 if (!open_pdf_in_preview) { | 718 if (!open_pdf_in_preview) { |
728 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); | 719 settings->GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf); |
| 720 settings->GetBoolean(printing::kSettingPrintWithPrivet, &print_with_privet); |
729 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); | 721 is_cloud_printer = settings->HasKey(printing::kSettingCloudPrintId); |
730 } | 722 } |
731 | 723 |
732 int page_count = 0; | 724 int page_count = 0; |
733 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count); | 725 settings->GetInteger(printing::kSettingPreviewPageCount, &page_count); |
734 | 726 |
735 if (print_to_pdf) { | 727 if (print_to_pdf) { |
736 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count); | 728 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToPDF", page_count); |
737 ReportUserActionHistogram(PRINT_TO_PDF); | 729 ReportUserActionHistogram(PRINT_TO_PDF); |
738 PrintToPdf(); | 730 PrintToPdf(); |
739 return; | 731 return; |
740 } | 732 } |
741 | 733 |
| 734 #if defined(ENABLE_MDNS) |
| 735 if (print_with_privet && PrivetPrintingEnabled()) { |
| 736 std::string printer_name; |
| 737 std::string print_ticket; |
| 738 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintWithPrivet", |
| 739 page_count); |
| 740 ReportUserActionHistogram(PRINT_WITH_PRIVET); |
| 741 |
| 742 bool success = settings->GetString(printing::kSettingDeviceName, |
| 743 &printer_name); |
| 744 DCHECK(success); |
| 745 success = settings->GetString(printing::kSettingTicket, |
| 746 &print_ticket); |
| 747 DCHECK(success); |
| 748 |
| 749 PrintToPrivetPrinter(printer_name, print_ticket); |
| 750 return; |
| 751 } |
| 752 #endif |
| 753 |
742 scoped_refptr<base::RefCountedBytes> data; | 754 scoped_refptr<base::RefCountedBytes> data; |
743 string16 title; | 755 string16 title; |
744 if (!GetPreviewDataAndTitle(&data, &title)) { | 756 if (!GetPreviewDataAndTitle(&data, &title)) { |
745 // Nothing to print, no preview available. | 757 // Nothing to print, no preview available. |
746 return; | 758 return; |
747 } | 759 } |
748 | 760 |
749 if (is_cloud_printer) { | 761 if (is_cloud_printer) { |
750 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint", | 762 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.PrintToCloudPrint", |
751 page_count); | 763 page_count); |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1359 } | 1371 } |
1360 | 1372 |
1361 void PrintPreviewHandler::LocalPrinterCacheFlushed() { | 1373 void PrintPreviewHandler::LocalPrinterCacheFlushed() { |
1362 } | 1374 } |
1363 | 1375 |
1364 void PrintPreviewHandler::StopPrivetPrinterSearch() { | 1376 void PrintPreviewHandler::StopPrivetPrinterSearch() { |
1365 printer_lister_->Stop(); | 1377 printer_lister_->Stop(); |
1366 web_ui()->CallJavascriptFunction("onPrivetPrinterSearchDone"); | 1378 web_ui()->CallJavascriptFunction("onPrivetPrinterSearchDone"); |
1367 } | 1379 } |
1368 | 1380 |
1369 void PrintPreviewHandler::StartPrivetCapabilities( | 1381 void PrintPreviewHandler::PrivetCapabilitiesUpdateClient( |
| 1382 scoped_ptr<local_discovery::PrivetHTTPClient> http_client) { |
| 1383 if (!PrivetUpdateClient(http_client.Pass())) |
| 1384 return; |
| 1385 |
| 1386 privet_capabilities_operation_ = |
| 1387 privet_http_client_->CreateCapabilitiesOperation( |
| 1388 this); |
| 1389 privet_capabilities_operation_->Start(); |
| 1390 } |
| 1391 |
| 1392 bool PrintPreviewHandler::PrivetUpdateClient( |
1370 scoped_ptr<local_discovery::PrivetHTTPClient> http_client) { | 1393 scoped_ptr<local_discovery::PrivetHTTPClient> http_client) { |
1371 if (!http_client) { | 1394 if (!http_client) { |
1372 SendPrivetCapabilitiesError(privet_http_resolution_->GetName()); | 1395 SendPrivetCapabilitiesError(privet_http_resolution_->GetName()); |
1373 privet_http_resolution_.reset(); | 1396 privet_http_resolution_.reset(); |
1374 return; | 1397 return false; |
1375 } | 1398 } |
1376 | 1399 |
| 1400 privet_local_print_operation_.reset(); |
| 1401 privet_capabilities_operation_.reset(); |
1377 privet_http_client_ = http_client.Pass(); | 1402 privet_http_client_ = http_client.Pass(); |
1378 privet_capabilities_operation_ = | 1403 |
1379 privet_http_client_->CreateCapabilitiesOperation( | |
1380 this); | |
1381 privet_http_resolution_.reset(); | 1404 privet_http_resolution_.reset(); |
1382 privet_capabilities_operation_->Start(); | 1405 |
| 1406 return true; |
1383 } | 1407 } |
1384 | 1408 |
| 1409 void PrintPreviewHandler::PrivetLocalPrintUpdateClient( |
| 1410 std::string print_ticket, |
| 1411 scoped_ptr<local_discovery::PrivetHTTPClient> http_client) { |
| 1412 if (!PrivetUpdateClient(http_client.Pass())) |
| 1413 return; |
| 1414 |
| 1415 StartPrivetLocalPrint(print_ticket); |
| 1416 } |
| 1417 |
| 1418 void PrintPreviewHandler::StartPrivetLocalPrint( |
| 1419 const std::string& print_ticket) { |
| 1420 privet_local_print_operation_ = |
| 1421 privet_http_client_->CreateLocalPrintOperation(this); |
| 1422 |
| 1423 privet_local_print_operation_->SetTicket(print_ticket); |
| 1424 |
| 1425 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 1426 web_ui()->GetController()); |
| 1427 privet_local_print_operation_->SetJobname( |
| 1428 base::UTF16ToUTF8(print_preview_ui->initiator_title())); |
| 1429 |
| 1430 Profile* profile = Profile::FromWebUI(web_ui()); |
| 1431 SigninManagerBase* signin_manager = |
| 1432 SigninManagerFactory::GetForProfileIfExists(profile); |
| 1433 |
| 1434 if (signin_manager) { |
| 1435 privet_local_print_operation_->SetUsername( |
| 1436 signin_manager->GetAuthenticatedUsername()); |
| 1437 } |
| 1438 |
| 1439 privet_local_print_operation_->Start(); |
| 1440 } |
| 1441 |
| 1442 |
1385 void PrintPreviewHandler::OnPrivetCapabilities( | 1443 void PrintPreviewHandler::OnPrivetCapabilities( |
1386 local_discovery::PrivetCapabilitiesOperation* capabilities_operation, | 1444 local_discovery::PrivetCapabilitiesOperation* capabilities_operation, |
1387 int http_error, | 1445 int http_error, |
1388 const base::DictionaryValue* capabilities) { | 1446 const base::DictionaryValue* capabilities) { |
1389 std::string name = capabilities_operation->GetHTTPClient()->GetName(); | 1447 std::string name = capabilities_operation->GetHTTPClient()->GetName(); |
1390 | 1448 |
1391 if (!capabilities || capabilities->HasKey(local_discovery::kPrivetKeyError)) { | 1449 if (!capabilities || capabilities->HasKey(local_discovery::kPrivetKeyError)) { |
1392 SendPrivetCapabilitiesError(name); | 1450 SendPrivetCapabilitiesError(name); |
1393 return; | 1451 return; |
1394 } | 1452 } |
(...skipping 18 matching lines...) Expand all Loading... |
1413 } | 1471 } |
1414 | 1472 |
1415 void PrintPreviewHandler::SendPrivetCapabilitiesError( | 1473 void PrintPreviewHandler::SendPrivetCapabilitiesError( |
1416 const std::string& device_name) { | 1474 const std::string& device_name) { |
1417 base::StringValue name_value(device_name); | 1475 base::StringValue name_value(device_name); |
1418 web_ui()->CallJavascriptFunction( | 1476 web_ui()->CallJavascriptFunction( |
1419 "failedToGetPrivetPrinterCapabilities", | 1477 "failedToGetPrivetPrinterCapabilities", |
1420 name_value); | 1478 name_value); |
1421 } | 1479 } |
1422 | 1480 |
| 1481 void PrintPreviewHandler::PrintToPrivetPrinter( |
| 1482 const std::string& device_name, |
| 1483 const std::string& ticket) { |
| 1484 CreatePrivetHTTP( |
| 1485 device_name, |
| 1486 base::Bind(&PrintPreviewHandler::PrivetLocalPrintUpdateClient, |
| 1487 base::Unretained(this), |
| 1488 ticket)); |
| 1489 } |
| 1490 |
| 1491 bool PrintPreviewHandler::CreatePrivetHTTP( |
| 1492 const std::string& name, |
| 1493 const local_discovery::PrivetHTTPAsynchronousFactory::ResultCallback& |
| 1494 callback) { |
| 1495 const local_discovery::DeviceDescription* device_description = |
| 1496 printer_lister_->GetDeviceDescription(name); |
| 1497 |
| 1498 if (!device_description) { |
| 1499 SendPrivetCapabilitiesError(name); |
| 1500 return false; |
| 1501 } |
| 1502 |
| 1503 privet_http_factory_ = |
| 1504 local_discovery::PrivetHTTPAsynchronousFactory::CreateInstance( |
| 1505 service_discovery_client_, |
| 1506 Profile::FromWebUI(web_ui())->GetRequestContext()); |
| 1507 privet_http_resolution_ = privet_http_factory_->CreatePrivetHTTP( |
| 1508 name, |
| 1509 device_description->address, |
| 1510 callback); |
| 1511 privet_http_resolution_->Start(); |
| 1512 |
| 1513 return true; |
| 1514 } |
| 1515 |
| 1516 void PrintPreviewHandler::OnPrivetPrintingRequestPDF( |
| 1517 const local_discovery::PrivetLocalPrintOperation* print_operation) { |
| 1518 scoped_refptr<base::RefCountedBytes> data; |
| 1519 string16 title; |
| 1520 |
| 1521 if (!GetPreviewDataAndTitle(&data, &title)) { |
| 1522 base::FundamentalValue http_code_value(-1); |
| 1523 web_ui()->CallJavascriptFunction("onPrivetPrintFailed", http_code_value); |
| 1524 return; |
| 1525 } |
| 1526 |
| 1527 // TODO(noamsml): Move data into request without copying it? |
| 1528 std::string data_str((const char*)data->front(), data->size()); |
| 1529 |
| 1530 privet_local_print_operation_->SendData(data_str); |
| 1531 } |
| 1532 |
| 1533 void PrintPreviewHandler::OnPrivetPrintingRequestPWGRaster( |
| 1534 const local_discovery::PrivetLocalPrintOperation* print_operation) { |
| 1535 NOTIMPLEMENTED(); |
| 1536 } |
| 1537 |
| 1538 void PrintPreviewHandler::OnPrivetPrintingDone( |
| 1539 const local_discovery::PrivetLocalPrintOperation* print_operation) { |
| 1540 ClosePreviewDialog(); |
| 1541 } |
| 1542 |
| 1543 void PrintPreviewHandler::OnPrivetPrintingError( |
| 1544 const local_discovery::PrivetLocalPrintOperation* print_operation, |
| 1545 int http_code) { |
| 1546 base::FundamentalValue http_code_value(http_code); |
| 1547 web_ui()->CallJavascriptFunction("onPrivetPrintFailed", http_code_value); |
| 1548 } |
| 1549 |
1423 void PrintPreviewHandler::FillPrinterDescription( | 1550 void PrintPreviewHandler::FillPrinterDescription( |
1424 const std::string& name, | 1551 const std::string& name, |
1425 const local_discovery::DeviceDescription& description, | 1552 const local_discovery::DeviceDescription& description, |
1426 base::DictionaryValue* printer_value) { | 1553 base::DictionaryValue* printer_value) { |
1427 printer_value->SetString("serviceName", name); | 1554 printer_value->SetString("serviceName", name); |
1428 printer_value->SetString("name", description.name); | 1555 printer_value->SetString("name", description.name); |
1429 } | 1556 } |
1430 | 1557 |
1431 #endif | 1558 #endif |
OLD | NEW |