OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/printing/usb_util.h" |
| 6 |
| 7 #include <ctype.h> |
| 8 #include <stdint.h> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chromeos/printing/printer_configuration.h" |
| 14 #include "device/usb/usb_device.h" |
| 15 #include "device/usb/usb_device_filter.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 |
| 18 namespace chromeos { |
| 19 namespace { |
| 20 |
| 21 // Base class used for printer USB interfaces |
| 22 // (https://www.usb.org/developers/defined_class). |
| 23 constexpr uint8_t kPrinterInterfaceClass = 7; |
| 24 |
| 25 // Escape URI strings the same way cups does it, so we end up with a URI cups |
| 26 // recognizes. Cups hex-encodes '%', ' ', and anything not in the standard |
| 27 // ASCII range. CUPS lets everything else through unchanged. |
| 28 // |
| 29 // TODO(justincarlson): Determine whether we should apply escaping at the |
| 30 // outgoing edge, when we send Printer information to CUPS, instead of |
| 31 // pre-escaping at the point the field is filled in. |
| 32 // |
| 33 // https://crbug.com/701606 |
| 34 std::string CupsURIEscape(const std::string& in) { |
| 35 static const char kHexDigits[] = "0123456789ABCDEF"; |
| 36 std::vector<char> buf; |
| 37 buf.reserve(in.size()); |
| 38 for (char c : in) { |
| 39 if (c == ' ' || c == '%' || (c & 0x80)) { |
| 40 buf.push_back('%'); |
| 41 buf.push_back(kHexDigits[(c >> 4) & 0xf]); |
| 42 buf.push_back(kHexDigits[c & 0xf]); |
| 43 } else { |
| 44 buf.push_back(c); |
| 45 } |
| 46 } |
| 47 return std::string(buf.data(), buf.size()); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 bool UsbDeviceIsPrinter(scoped_refptr<device::UsbDevice> usb_device) { |
| 53 device::UsbDeviceFilter printer_filter; |
| 54 printer_filter.interface_class = kPrinterInterfaceClass; |
| 55 return printer_filter.Matches(usb_device); |
| 56 } |
| 57 |
| 58 std::string UsbPrinterDeviceDetailsAsString(const device::UsbDevice& device) { |
| 59 return base::StringPrintf( |
| 60 " guid: %s\n" |
| 61 " usb version: %d\n" |
| 62 " device class: %d\n" |
| 63 " device subclass: %d\n" |
| 64 " device protocol: %d\n" |
| 65 " vendor id: %04x\n" |
| 66 " product id: %04x\n" |
| 67 " device version: %d\n" |
| 68 " manufacturer string: %s\n" |
| 69 " product string: %s\n" |
| 70 " serial number: %s", |
| 71 device.guid().c_str(), device.usb_version(), device.device_class(), |
| 72 device.device_subclass(), device.device_protocol(), device.vendor_id(), |
| 73 device.product_id(), device.device_version(), |
| 74 base::UTF16ToUTF8(device.manufacturer_string()).c_str(), |
| 75 base::UTF16ToUTF8(device.product_string()).c_str(), |
| 76 base::UTF16ToUTF8(device.serial_number()).c_str()); |
| 77 } |
| 78 |
| 79 // Attempt to gather all the information we need to work with this printer by |
| 80 // querying the USB device. This should only be called using devices we believe |
| 81 // are printers, not arbitrary USB devices, as we may get weird partial results |
| 82 // from arbitrary devices. |
| 83 bool UsbDeviceToPrinter(const device::UsbDevice& device, |
| 84 chromeos::Printer* printer) { |
| 85 // Preflight all required fields and log errors if we find something wrong. |
| 86 if (device.vendor_id() == 0 || device.product_id() == 0 || |
| 87 device.manufacturer_string().empty() || device.product_string().empty()) { |
| 88 LOG(ERROR) << "Failed to convert USB device to printer. Fields were:\n" |
| 89 << UsbPrinterDeviceDetailsAsString(device); |
| 90 return false; |
| 91 } |
| 92 |
| 93 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); |
| 94 printer->set_model(base::UTF16ToUTF8(device.product_string())); |
| 95 printer->set_display_name(base::StringPrintf("%s %s (USB)", |
| 96 printer->manufacturer().c_str(), |
| 97 printer->model().c_str())); |
| 98 printer->set_description(printer->display_name()); |
| 99 |
| 100 // Note that serial may, for some devices, be empty or bogus (all zeros, non |
| 101 // unique, or otherwise not really a serial number), but having a non-unique |
| 102 // or empty serial field in the URI still lets us print, it just means we |
| 103 // don't have a way to uniquely identify a printer if there are multiple ones |
| 104 // plugged in with the same VID/PID, so we may print to the *wrong* printer. |
| 105 // There doesn't seem to be a robust solution to this problem; if printers |
| 106 // don't supply a serial number, we don't have any reliable way to do that |
| 107 // differentiation. |
| 108 std::string serial = base::UTF16ToUTF8(device.serial_number()); |
| 109 printer->set_uri(CupsURIEscape( |
| 110 base::StringPrintf("usb://%04x/%04x?serial=%s", device.vendor_id(), |
| 111 device.product_id(), serial.c_str()))); |
| 112 return true; |
| 113 } |
| 114 |
| 115 } // namespace chromeos |
OLD | NEW |