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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 const char kCloudPrintUrl[] = "cloudPrintUrl"; | 192 const char kCloudPrintUrl[] = "cloudPrintUrl"; |
| 193 // Name of a dictionary field holding the state of selection for document. | 193 // Name of a dictionary field holding the state of selection for document. |
| 194 const char kDocumentHasSelection[] = "documentHasSelection"; | 194 const char kDocumentHasSelection[] = "documentHasSelection"; |
| 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. | |
| 203 const int kPrivetTimeoutSec = 5; | |
| 204 | |
| 202 // Get the print job settings dictionary from |args|. The caller takes | 205 // Get the print job settings dictionary from |args|. The caller takes |
| 203 // ownership of the returned DictionaryValue. Returns NULL on failure. | 206 // ownership of the returned DictionaryValue. Returns NULL on failure. |
| 204 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( | 207 std::unique_ptr<base::DictionaryValue> GetSettingsDictionary( |
| 205 const base::ListValue* args) { | 208 const base::ListValue* args) { |
| 206 std::string json_str; | 209 std::string json_str; |
| 207 if (!args->GetString(0, &json_str)) { | 210 if (!args->GetString(0, &json_str)) { |
| 208 NOTREACHED() << "Could not read JSON argument"; | 211 NOTREACHED() << "Could not read JSON argument"; |
| 209 return NULL; | 212 return NULL; |
| 210 } | 213 } |
| 211 if (json_str.empty()) { | 214 if (json_str.empty()) { |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 608 base::Unretained(this))); | 611 base::Unretained(this))); |
| 609 web_ui()->RegisterMessageCallback("getInitialSettings", | 612 web_ui()->RegisterMessageCallback("getInitialSettings", |
| 610 base::Bind(&PrintPreviewHandler::HandleGetInitialSettings, | 613 base::Bind(&PrintPreviewHandler::HandleGetInitialSettings, |
| 611 base::Unretained(this))); | 614 base::Unretained(this))); |
| 612 web_ui()->RegisterMessageCallback("forceOpenNewTab", | 615 web_ui()->RegisterMessageCallback("forceOpenNewTab", |
| 613 base::Bind(&PrintPreviewHandler::HandleForceOpenNewTab, | 616 base::Bind(&PrintPreviewHandler::HandleForceOpenNewTab, |
| 614 base::Unretained(this))); | 617 base::Unretained(this))); |
| 615 web_ui()->RegisterMessageCallback("getPrivetPrinters", | 618 web_ui()->RegisterMessageCallback("getPrivetPrinters", |
| 616 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinters, | 619 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinters, |
| 617 base::Unretained(this))); | 620 base::Unretained(this))); |
| 618 web_ui()->RegisterMessageCallback("stopGetPrivetPrinters", | |
| 619 base::Bind(&PrintPreviewHandler::HandleStopGetPrivetPrinters, | |
| 620 base::Unretained(this))); | |
| 621 web_ui()->RegisterMessageCallback("getPrivetPrinterCapabilities", | 621 web_ui()->RegisterMessageCallback("getPrivetPrinterCapabilities", |
| 622 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinterCapabilities, | 622 base::Bind(&PrintPreviewHandler::HandleGetPrivetPrinterCapabilities, |
| 623 base::Unretained(this))); | 623 base::Unretained(this))); |
| 624 web_ui()->RegisterMessageCallback( | 624 web_ui()->RegisterMessageCallback( |
| 625 "getExtensionPrinters", | 625 "getExtensionPrinters", |
| 626 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinters, | 626 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinters, |
| 627 base::Unretained(this))); | 627 base::Unretained(this))); |
| 628 web_ui()->RegisterMessageCallback( | 628 web_ui()->RegisterMessageCallback( |
| 629 "getExtensionPrinterCapabilities", | 629 "getExtensionPrinterCapabilities", |
| 630 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinterCapabilities, | 630 base::Bind(&PrintPreviewHandler::HandleGetExtensionPrinterCapabilities, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 668 CHECK(!callback_id.empty()); | 668 CHECK(!callback_id.empty()); |
| 669 | 669 |
| 670 AllowJavascript(); | 670 AllowJavascript(); |
| 671 | 671 |
| 672 printer_backend_proxy()->EnumeratePrinters( | 672 printer_backend_proxy()->EnumeratePrinters( |
| 673 base::Bind(&PrintPreviewHandler::SetupPrinterList, | 673 base::Bind(&PrintPreviewHandler::SetupPrinterList, |
| 674 weak_factory_.GetWeakPtr(), callback_id)); | 674 weak_factory_.GetWeakPtr(), callback_id)); |
| 675 } | 675 } |
| 676 | 676 |
| 677 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { | 677 void PrintPreviewHandler::HandleGetPrivetPrinters(const base::ListValue* args) { |
| 678 if (!PrivetPrintingEnabled()) | 678 std::string callback_id; |
| 679 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); | 679 CHECK(args->GetString(0, &callback_id)); |
| 680 CHECK(!callback_id.empty()); | |
| 681 | |
| 682 AllowJavascript(); | |
| 683 | |
| 684 if (!PrivetPrintingEnabled()) { | |
| 685 RejectJavascriptCallback(base::Value(callback_id), base::Value(false)); | |
| 686 } | |
| 680 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 687 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 681 using local_discovery::ServiceDiscoverySharedClient; | 688 using local_discovery::ServiceDiscoverySharedClient; |
| 682 scoped_refptr<ServiceDiscoverySharedClient> service_discovery = | 689 scoped_refptr<ServiceDiscoverySharedClient> service_discovery = |
| 683 ServiceDiscoverySharedClient::GetInstance(); | 690 ServiceDiscoverySharedClient::GetInstance(); |
| 691 privet_callback_id_ = callback_id; | |
|
dpapad
2017/06/06 21:16:33
Should we assert that privet_callback_id_ is not a
rbpotter
2017/06/07 02:37:46
Done.
| |
| 684 StartPrivetLister(service_discovery); | 692 StartPrivetLister(service_discovery); |
| 685 #endif | 693 #endif |
| 686 } | 694 } |
| 687 | 695 |
| 688 void PrintPreviewHandler::HandleStopGetPrivetPrinters( | 696 void PrintPreviewHandler::StopPrivetLister() { |
| 689 const base::ListValue* args) { | |
| 690 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 697 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 698 privet_lister_timer_.reset(); | |
| 691 if (PrivetPrintingEnabled() && printer_lister_) { | 699 if (PrivetPrintingEnabled() && printer_lister_) { |
| 692 printer_lister_->Stop(); | 700 printer_lister_->Stop(); |
| 693 } | 701 } |
| 702 ResolveJavascriptCallback(base::Value(privet_callback_id_), | |
| 703 base::Value(true)); | |
| 694 #endif | 704 #endif |
| 695 } | 705 } |
| 696 | 706 |
| 697 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( | 707 void PrintPreviewHandler::HandleGetPrivetPrinterCapabilities( |
| 698 const base::ListValue* args) { | 708 const base::ListValue* args) { |
| 699 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 709 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 700 std::string name; | 710 std::string name; |
| 701 bool success = args->GetString(0, &name); | 711 bool success = args->GetString(0, &name); |
| 702 DCHECK(success); | 712 DCHECK(success); |
| 703 | 713 |
| (...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1520 DCHECK(tmp_data->front()); | 1530 DCHECK(tmp_data->front()); |
| 1521 | 1531 |
| 1522 *data = tmp_data; | 1532 *data = tmp_data; |
| 1523 *title = print_preview_ui()->initiator_title(); | 1533 *title = print_preview_ui()->initiator_title(); |
| 1524 return true; | 1534 return true; |
| 1525 } | 1535 } |
| 1526 | 1536 |
| 1527 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) | 1537 #if BUILDFLAG(ENABLE_SERVICE_DISCOVERY) |
| 1528 void PrintPreviewHandler::StartPrivetLister(const scoped_refptr< | 1538 void PrintPreviewHandler::StartPrivetLister(const scoped_refptr< |
| 1529 local_discovery::ServiceDiscoverySharedClient>& client) { | 1539 local_discovery::ServiceDiscoverySharedClient>& client) { |
| 1530 if (!PrivetPrintingEnabled()) | |
| 1531 return web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterSearchDone"); | |
| 1532 | |
| 1533 Profile* profile = Profile::FromWebUI(web_ui()); | 1540 Profile* profile = Profile::FromWebUI(web_ui()); |
| 1534 DCHECK(!service_discovery_client_.get() || | 1541 DCHECK(!service_discovery_client_.get() || |
| 1535 service_discovery_client_.get() == client.get()); | 1542 service_discovery_client_.get() == client.get()); |
| 1536 service_discovery_client_ = client; | 1543 service_discovery_client_ = client; |
| 1537 printer_lister_ = base::MakeUnique<cloud_print::PrivetLocalPrinterLister>( | 1544 printer_lister_ = base::MakeUnique<cloud_print::PrivetLocalPrinterLister>( |
| 1538 service_discovery_client_.get(), profile->GetRequestContext(), this); | 1545 service_discovery_client_.get(), profile->GetRequestContext(), this); |
| 1546 privet_lister_timer_.reset(new base::OneShotTimer()); | |
| 1547 privet_lister_timer_->Start(FROM_HERE, | |
| 1548 base::TimeDelta::FromSeconds(kPrivetTimeoutSec), | |
| 1549 this, &PrintPreviewHandler::StopPrivetLister); | |
| 1539 printer_lister_->Start(); | 1550 printer_lister_->Start(); |
| 1540 } | 1551 } |
| 1541 | 1552 |
| 1542 void PrintPreviewHandler::LocalPrinterChanged( | 1553 void PrintPreviewHandler::LocalPrinterChanged( |
| 1543 const std::string& name, | 1554 const std::string& name, |
| 1544 bool has_local_printing, | 1555 bool has_local_printing, |
| 1545 const cloud_print::DeviceDescription& description) { | 1556 const cloud_print::DeviceDescription& description) { |
| 1546 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 1557 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 1547 if (has_local_printing || | 1558 if (has_local_printing || |
| 1548 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) { | 1559 command_line->HasSwitch(switches::kEnablePrintPreviewRegisterPromos)) { |
| 1549 base::DictionaryValue info; | 1560 base::DictionaryValue info; |
| 1550 FillPrinterDescription(name, description, has_local_printing, &info); | 1561 FillPrinterDescription(name, description, has_local_printing, &info); |
| 1551 web_ui()->CallJavascriptFunctionUnsafe("onPrivetPrinterChanged", info); | 1562 FireWebUIListener("privet-printer-added", info); |
| 1552 } | 1563 } |
| 1553 } | 1564 } |
| 1554 | 1565 |
| 1555 void PrintPreviewHandler::LocalPrinterRemoved(const std::string& name) { | 1566 void PrintPreviewHandler::LocalPrinterRemoved(const std::string& name) { |
| 1556 } | 1567 } |
| 1557 | 1568 |
| 1558 void PrintPreviewHandler::LocalPrinterCacheFlushed() { | 1569 void PrintPreviewHandler::LocalPrinterCacheFlushed() { |
| 1559 } | 1570 } |
| 1560 | 1571 |
| 1561 void PrintPreviewHandler::PrivetCapabilitiesUpdateClient( | 1572 void PrintPreviewHandler::PrivetCapabilitiesUpdateClient( |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1804 | 1815 |
| 1805 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1816 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1806 if (gaia_cookie_manager_service_) | 1817 if (gaia_cookie_manager_service_) |
| 1807 gaia_cookie_manager_service_->RemoveObserver(this); | 1818 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1808 } | 1819 } |
| 1809 | 1820 |
| 1810 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1821 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1811 const base::Closure& closure) { | 1822 const base::Closure& closure) { |
| 1812 pdf_file_saved_closure_ = closure; | 1823 pdf_file_saved_closure_ = closure; |
| 1813 } | 1824 } |
| OLD | NEW |