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 "device/usb/usb_device_filter.h" | |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 namespace { | |
| 16 | |
| 17 // Base class used for printer USB interfaces | |
| 18 // (https://www.usb.org/developers/defined_class). | |
| 19 constexpr uint8_t kPrinterInterfaceClass = 7; | |
| 20 | |
| 21 // Escape URI strings the same way cups does it, so we end up with a URI cups | |
| 22 // recognizes. Cups hex-encodes '%', ' ', and anything with the MSB set, and | |
| 23 // lets everything else through unchanged. | |
| 24 std::string CupsURIEscape(const std::string& in) { | |
| 25 static const char kHexDigits[] = "0123456789ABCDEF"; | |
|
tbarzic
2017/03/14 01:43:32
I wonder if this should be done before sending req
Carlson
2017/03/15 00:31:16
Hmm, that's a good point. I think we don't actual
| |
| 26 std::vector<char> buf; | |
| 27 buf.reserve(in.size()); | |
| 28 for (char c : in) { | |
| 29 if (c == ' ' || c == '%' || (c & 0x80)) { | |
| 30 buf.push_back('%'); | |
| 31 buf.push_back(kHexDigits[(c >> 4) & 0xf]); | |
| 32 buf.push_back(kHexDigits[c & 0xf]); | |
| 33 } else { | |
| 34 buf.push_back(c); | |
| 35 } | |
| 36 } | |
| 37 return std::string(buf.data(), buf.size()); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 bool UsbDeviceIsPrinter(scoped_refptr<device::UsbDevice> usb_device) { | |
| 43 device::UsbDeviceFilter printer_filter; | |
| 44 printer_filter.interface_class = kPrinterInterfaceClass; | |
| 45 return printer_filter.Matches(usb_device); | |
| 46 } | |
| 47 | |
| 48 std::string UsbPrinterDeviceDetailsAsString(const device::UsbDevice& device) { | |
| 49 return base::StringPrintf( | |
| 50 " guid: %s\n" | |
| 51 " usb version: %d\n" | |
| 52 " device class: %d\n" | |
| 53 " device subclass: %d\n" | |
| 54 " device protocol: %d\n" | |
| 55 " vendor id: %04x\n" | |
| 56 " product id: %04x\n" | |
| 57 " device version: %d\n" | |
| 58 " manufacturer string: %s\n" | |
| 59 " product string: %s\n" | |
| 60 " serial number: %s", | |
| 61 device.guid().c_str(), device.usb_version(), device.device_class(), | |
| 62 device.device_subclass(), device.device_protocol(), device.vendor_id(), | |
| 63 device.product_id(), device.device_version(), | |
| 64 base::UTF16ToUTF8(device.manufacturer_string()).c_str(), | |
| 65 base::UTF16ToUTF8(device.product_string()).c_str(), | |
| 66 base::UTF16ToUTF8(device.serial_number()).c_str()); | |
| 67 } | |
| 68 | |
| 69 // Attempt to gather all the information we need to work with this printer by | |
| 70 // querying the USB device. This should only be called using devices we believe | |
| 71 // are printers, not arbitrary USB devices, as we may get weird partial results | |
| 72 // from arbitrary devices. | |
| 73 // | |
|
tbarzic
2017/03/14 01:43:32
nit: rm this line
Carlson
2017/03/15 00:31:15
Done.
| |
| 74 bool UsbDeviceToPrinter(const device::UsbDevice& device, | |
| 75 chromeos::Printer* printer) { | |
| 76 // Preflight all required fields and log errors if we find something wrong. | |
| 77 if (device.vendor_id() == 0 || device.product_id() == 0 || | |
| 78 device.manufacturer_string().empty() || device.product_string().empty()) { | |
|
tbarzic
2017/03/14 01:43:32
Are these so important that we want to reject the
Carlson
2017/03/15 00:31:16
I think so; they are really the basic fields we ne
| |
| 79 LOG(ERROR) << "Failed to convert USB device to printer. Fields were:\n" | |
| 80 << UsbPrinterDeviceDetailsAsString(device); | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); | |
| 85 printer->set_model(base::UTF16ToUTF8(device.product_string())); | |
| 86 printer->set_display_name(base::StringPrintf("%s %s (USB)", | |
| 87 printer->manufacturer().c_str(), | |
| 88 printer->model().c_str())); | |
| 89 printer->set_description(printer->display_name()); | |
| 90 | |
| 91 // Note that serial may, for some devices, be empty or bogus (all zeros, non | |
| 92 // unique, or otherwise not really a serial number), but having a non-unique | |
| 93 // or empty serial field in the URI still lets us print, it just means we | |
| 94 // don't have a way to uniquely identify a printer if there are multiple ones | |
| 95 // plugged in with the same VID/PID, so we may print to the *wrong* printer. | |
| 96 // There doesn't seem to be a robust solution to this problem; if printers | |
| 97 // don't supply a serial number, we don't have any reliable way to do that | |
| 98 // differentiation. | |
| 99 std::string serial = base::UTF16ToUTF8(device.serial_number()); | |
| 100 printer->set_uri(CupsURIEscape( | |
| 101 base::StringPrintf("usb://%04x/%04x?serial=%s", device.vendor_id(), | |
| 102 device.product_id(), serial.c_str()))); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 } // namespace chromeos | |
| OLD | NEW |