OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/settings/chromeos/cups_printers_handler.h" | 5 #include "chrome/browser/ui/webui/settings/chromeos/cups_printers_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 void OnRemovedPrinter(bool success) {} | 47 void OnRemovedPrinter(bool success) {} |
48 | 48 |
49 std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { | 49 std::unique_ptr<base::DictionaryValue> GetPrinterInfo(const Printer& printer) { |
50 std::unique_ptr<base::DictionaryValue> printer_info = | 50 std::unique_ptr<base::DictionaryValue> printer_info = |
51 base::MakeUnique<base::DictionaryValue>(); | 51 base::MakeUnique<base::DictionaryValue>(); |
52 printer_info->SetString("printerId", printer.id()); | 52 printer_info->SetString("printerId", printer.id()); |
53 printer_info->SetString("printerName", printer.display_name()); | 53 printer_info->SetString("printerName", printer.display_name()); |
54 printer_info->SetString("printerDescription", printer.description()); | 54 printer_info->SetString("printerDescription", printer.description()); |
55 printer_info->SetString("printerManufacturer", printer.manufacturer()); | 55 printer_info->SetString("printerManufacturer", printer.manufacturer()); |
56 printer_info->SetString("printerModel", printer.model()); | 56 printer_info->SetString("printerModel", printer.model()); |
57 | |
58 // Get protocol, ip address and queue from the printer's URI. | 57 // Get protocol, ip address and queue from the printer's URI. |
59 const std::string printer_uri = printer.uri(); | 58 const std::string printer_uri = printer.uri(); |
60 url::Parsed parsed; | 59 url::Parsed parsed; |
61 url::ParseStandardURL(printer_uri.c_str(), printer_uri.length(), &parsed); | 60 url::ParseStandardURL(printer_uri.c_str(), printer_uri.length(), &parsed); |
62 | 61 |
63 std::string scheme; | 62 std::string scheme; |
64 std::string host; | 63 std::string host; |
65 std::string path; | 64 std::string path; |
66 if (parsed.scheme.len > 0) | 65 if (parsed.scheme.len > 0) |
67 scheme = std::string(printer_uri, parsed.scheme.begin, parsed.scheme.len); | 66 scheme = std::string(printer_uri, parsed.scheme.begin, parsed.scheme.len); |
68 if (parsed.host.len > 0) | 67 if (parsed.host.len > 0) |
69 host = std::string(printer_uri, parsed.host.begin, parsed.host.len); | 68 host = std::string(printer_uri, parsed.host.begin, parsed.host.len); |
70 if (parsed.path.len > 0) | 69 if (parsed.path.len > 0) |
71 path = std::string(printer_uri, parsed.path.begin, parsed.path.len); | 70 path = std::string(printer_uri, parsed.path.begin, parsed.path.len); |
72 | 71 if (base::ToLowerASCII(scheme) == "usb") { |
73 printer_info->SetString("printerAddress", host); | 72 // USB has URI path (and, maybe, query) components that aren't really |
| 73 // associated with a queue -- the mapping between printing semantics and URI |
| 74 // semantics breaks down a bit here. From the user's point of view, the |
| 75 // entire host/path/query block is the printer address for USB. |
| 76 printer_info->SetString("printerAddress", |
| 77 printer_uri.substr(parsed.host.begin)); |
| 78 } else { |
| 79 printer_info->SetString("printerAddress", host); |
| 80 if (!path.empty()) { |
| 81 printer_info->SetString("printerQueue", path.substr(1)); |
| 82 } |
| 83 } |
74 printer_info->SetString("printerProtocol", base::ToLowerASCII(scheme)); | 84 printer_info->SetString("printerProtocol", base::ToLowerASCII(scheme)); |
75 if (!path.empty()) | |
76 printer_info->SetString("printerQueue", path.substr(1)); | |
77 | 85 |
78 return printer_info; | 86 return printer_info; |
79 } | 87 } |
80 | 88 |
81 } // namespace | 89 } // namespace |
82 | 90 |
83 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) | 91 CupsPrintersHandler::CupsPrintersHandler(content::WebUI* webui) |
84 : printer_discoverer_(nullptr), | 92 : printer_discoverer_(nullptr), |
85 profile_(Profile::FromWebUI(webui)), | 93 profile_(Profile::FromWebUI(webui)), |
86 weak_factory_(this) { | 94 weak_factory_(this) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 CHECK(printer_dict->GetString("printerDescription", &printer_description)); | 204 CHECK(printer_dict->GetString("printerDescription", &printer_description)); |
197 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); | 205 CHECK(printer_dict->GetString("printerManufacturer", &printer_manufacturer)); |
198 CHECK(printer_dict->GetString("printerModel", &printer_model)); | 206 CHECK(printer_dict->GetString("printerModel", &printer_model)); |
199 CHECK(printer_dict->GetString("printerAddress", &printer_address)); | 207 CHECK(printer_dict->GetString("printerAddress", &printer_address)); |
200 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); | 208 CHECK(printer_dict->GetString("printerProtocol", &printer_protocol)); |
201 // printerQueue might be null for a printer whose protocol is not 'LPD'. | 209 // printerQueue might be null for a printer whose protocol is not 'LPD'. |
202 printer_dict->GetString("printerQueue", &printer_queue); | 210 printer_dict->GetString("printerQueue", &printer_queue); |
203 | 211 |
204 // printerPPDPath might be null for an auto-discovered printer. | 212 // printerPPDPath might be null for an auto-discovered printer. |
205 printer_dict->GetString("printerPPDPath", &printer_ppd_path); | 213 printer_dict->GetString("printerPPDPath", &printer_ppd_path); |
206 std::string printer_uri = | 214 std::string printer_uri = printer_protocol + "://" + printer_address; |
207 printer_protocol + "://" + printer_address + "/" + printer_queue; | 215 if (!printer_queue.empty()) { |
| 216 printer_uri += "/" + printer_queue; |
| 217 } |
208 | 218 |
209 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); | 219 std::unique_ptr<Printer> printer = base::MakeUnique<Printer>(printer_id); |
210 printer->set_display_name(printer_name); | 220 printer->set_display_name(printer_name); |
211 printer->set_description(printer_description); | 221 printer->set_description(printer_description); |
212 printer->set_manufacturer(printer_manufacturer); | 222 printer->set_manufacturer(printer_manufacturer); |
213 printer->set_model(printer_model); | 223 printer->set_model(printer_model); |
214 printer->set_uri(printer_uri); | 224 printer->set_uri(printer_uri); |
215 | 225 |
216 // Verify a valid ppd path is present. | 226 // Verify a valid ppd path is present. |
217 if (!printer_ppd_path.empty()) { | 227 if (!printer_ppd_path.empty()) { |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 base::Value("on-printer-discovered"), *printers_list); | 411 base::Value("on-printer-discovered"), *printers_list); |
402 } | 412 } |
403 | 413 |
404 void CupsPrintersHandler::OnDiscoveryInitialScanDone() { | 414 void CupsPrintersHandler::OnDiscoveryInitialScanDone() { |
405 CallJavascriptFunction("cr.webUIListenerCallback", | 415 CallJavascriptFunction("cr.webUIListenerCallback", |
406 base::Value("on-printer-discovery-done")); | 416 base::Value("on-printer-discovery-done")); |
407 } | 417 } |
408 | 418 |
409 } // namespace settings | 419 } // namespace settings |
410 } // namespace chromeos | 420 } // namespace chromeos |
OLD | NEW |