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

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

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

Powered by Google App Engine
This is Rietveld 408576698