Chromium Code Reviews| Index: chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
| diff --git a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
| index 2d1181a17ed7e5521625ae97f6e861f751441f56..d4f992aa0cb93b86abd8cb2c4bba6202ff9bef81 100644 |
| --- a/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
| +++ b/chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.cc |
| @@ -20,6 +20,7 @@ |
| #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" |
| #include "chrome/browser/chromeos/printing/printer_configurer.h" |
| #include "chrome/browser/chromeos/printing/printer_discoverer.h" |
| +#include "chrome/browser/chromeos/printing/printer_info.h" |
| #include "chrome/browser/chromeos/printing/printers_manager_factory.h" |
| #include "chrome/browser/download/download_prefs.h" |
| #include "chrome/browser/profiles/profile.h" |
| @@ -45,6 +46,9 @@ namespace settings { |
| namespace { |
| +const char kIppScheme[] = "ipp"; |
| +const char kIppsScheme[] = "ipps"; |
| + |
| void OnRemovedPrinter(bool success) {} |
| std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { |
| @@ -116,6 +120,9 @@ void CupsPrintersHandler::RegisterMessages() { |
| "addCupsPrinter", base::Bind(&CupsPrintersHandler::HandleAddCupsPrinter, |
| base::Unretained(this))); |
| web_ui()->RegisterMessageCallback( |
| + "getPrinterInfo", base::Bind(&CupsPrintersHandler::HandleGetPrinterInfo, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| "getCupsPrinterManufacturersList", |
| base::Bind(&CupsPrintersHandler::HandleGetCupsPrinterManufacturers, |
| base::Unretained(this))); |
| @@ -185,6 +192,71 @@ void CupsPrintersHandler::HandleRemoveCupsPrinter(const base::ListValue* args) { |
| base::Bind(&base::DoNothing)); |
| } |
| +void CupsPrintersHandler::HandleGetPrinterInfo(const base::ListValue* args) { |
| + DCHECK(args); |
| + std::string callback_id; |
| + if (!args->GetString(0, &callback_id)) { |
| + NOTREACHED() << "Expected request for a promise"; |
| + return; |
| + } |
| + |
| + const base::DictionaryValue* printer_dict = nullptr; |
| + if (!args->GetDictionary(1, &printer_dict)) { |
| + NOTREACHED() << "Dictionary missing"; |
| + return; |
| + } |
| + |
| + AllowJavascript(); |
| + |
| + std::string printer_address; |
| + if (!printer_dict->GetString("printerAddress", &printer_address)) { |
| + NOTREACHED() << "Address missing"; |
| + return; |
| + } |
| + |
| + if (printer_address.empty()) { |
| + // Run the failure callback. |
|
Carlson
2017/05/31 16:58:52
I'm confused about when you do and don't run the f
skau
2017/06/01 21:50:29
We run the failure callback for expected failures.
|
| + OnPrinterInfo(callback_id, false, "", "", false); |
| + return; |
| + } |
| + |
| + std::string printer_queue; |
| + printer_dict->GetString("printerQueue", &printer_queue); |
| + |
| + std::string printer_protocol; |
| + if (!printer_dict->GetString("printerProtocol", &printer_protocol)) { |
| + NOTREACHED() << "Protocol missing"; |
| + return; |
| + } |
| + |
| + std::string printer_uri = printer_protocol + url::kStandardSchemeSeparator + |
| + printer_address + "/" + printer_queue; |
|
Carlson
2017/05/31 16:58:52
I don't understand why you assemble a uri and then
skau
2017/06/01 21:50:29
It is just for the port so the whole uri doesn't n
skau
2017/06/07 23:55:39
So, I was wrong. If there's no scheme (aka protoc
|
| + const char* uri_ptr = printer_uri.c_str(); |
| + url::Parsed parsed; |
| + url::ParseStandardURL(uri_ptr, printer_uri.length(), &parsed); |
| + base::StringPiece host(&uri_ptr[parsed.host.begin], parsed.host.len); |
| + base::StringPiece path(&uri_ptr[parsed.path.begin], parsed.path.len); |
| + |
| + int port = ParsePort(uri_ptr, parsed.port); |
| + if (port == url::SpecialPort::PORT_UNSPECIFIED || |
| + port == url::SpecialPort::PORT_INVALID) { |
| + // imply port from protocol |
| + if (printer_protocol == kIppScheme) { |
| + port = 631; |
| + } else if (printer_protocol == kIppsScheme) { |
| + // ipps is ipp over https so it uses the https port. |
| + port = 443; |
| + } else { |
| + NOTREACHED() << "Unrecognized protocol. Port was not set."; |
| + } |
| + } |
| + |
| + ::chromeos::QueryIppPrinter( |
| + host.as_string(), port, path.as_string(), |
| + base::Bind(&CupsPrintersHandler::OnPrinterInfo, |
| + weak_factory_.GetWeakPtr(), callback_id)); |
| +} |
| + |
| void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| AllowJavascript(); |
| @@ -207,16 +279,19 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| CHECK(printer_dict->GetString("printerModel", &printer_model)); |
| CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
| CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
| + |
| // printerQueue might be null for a printer whose protocol is not 'LPD'. |
| printer_dict->GetString("printerQueue", &printer_queue); |
| - // printerPPDPath might be null for an auto-discovered printer. |
| - printer_dict->GetString("printerPPDPath", &printer_ppd_path); |
| - std::string printer_uri = printer_protocol + "://" + printer_address; |
| + std::string printer_uri = |
| + printer_protocol + url::kStandardSchemeSeparator + printer_address; |
| if (!printer_queue.empty()) { |
| printer_uri += "/" + printer_queue; |
| } |
| + // printerPPDPath might be null for an auto-discovered printer. |
| + printer_dict->GetString("printerPPDPath", &printer_ppd_path); |
| + |
| std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
| printer->set_display_name(printer_name); |
| printer->set_description(printer_description); |
| @@ -224,8 +299,13 @@ void CupsPrintersHandler::HandleAddCupsPrinter(const base::ListValue* args) { |
| printer->set_model(printer_model); |
| printer->set_uri(printer_uri); |
| - // Verify a valid ppd path is present. |
| - if (!printer_ppd_path.empty()) { |
| + bool autoconf = false; |
| + printer_dict->GetBoolean("autoconf", &autoconf); |
| + |
| + // Verify that the printer is autoconf or a valid ppd path is present. |
| + if (autoconf) { |
| + printer->mutable_ppd_reference()->autoconf = true; |
| + } else if (!printer_ppd_path.empty()) { |
| GURL tmp = net::FilePathToFileURL(base::FilePath(printer_ppd_path)); |
| if (!tmp.is_valid()) { |
| LOG(ERROR) << "Invalid ppd path: " << printer_ppd_path; |
| @@ -419,5 +499,24 @@ void CupsPrintersHandler::OnDiscoveryInitialScanDone(int printer_count) { |
| FireWebUIListener("on-printer-discovery-done"); |
| } |
| +void CupsPrintersHandler::OnPrinterInfo(const std::string& callback_id, |
| + bool success, |
| + const std::string& make, |
| + const std::string& model, |
| + bool ipp_everywhere) { |
| + if (!success) { |
| + base::DictionaryValue reject; |
| + reject.SetString("message", "Querying printer failed"); |
| + RejectJavascriptCallback(base::Value(callback_id), reject); |
| + return; |
| + } |
| + |
| + base::DictionaryValue info; |
| + info.SetString("manufacturer", make); |
| + info.SetString("model", model); |
| + info.SetBoolean("autoconf", ipp_everywhere); |
| + ResolveJavascriptCallback(base::Value(callback_id), info); |
| +} |
| + |
| } // namespace settings |
| } // namespace chromeos |