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