Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/printing/usb_printer_util.h" | 5 #include "chrome/browser/chromeos/printing/usb_printer_util.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 base::UTF16ToUTF8(device.product_string()).c_str(), | 139 base::UTF16ToUTF8(device.product_string()).c_str(), |
| 140 base::UTF16ToUTF8(device.serial_number()).c_str()); | 140 base::UTF16ToUTF8(device.serial_number()).c_str()); |
| 141 } | 141 } |
| 142 | 142 |
| 143 // Attempt to gather all the information we need to work with this printer by | 143 // Attempt to gather all the information we need to work with this printer by |
| 144 // querying the USB device. This should only be called using devices we believe | 144 // querying the USB device. This should only be called using devices we believe |
| 145 // are printers, not arbitrary USB devices, as we may get weird partial results | 145 // are printers, not arbitrary USB devices, as we may get weird partial results |
| 146 // from arbitrary devices. | 146 // from arbitrary devices. |
| 147 std::unique_ptr<Printer> UsbDeviceToPrinter(const device::UsbDevice& device) { | 147 std::unique_ptr<Printer> UsbDeviceToPrinter(const device::UsbDevice& device) { |
| 148 // Preflight all required fields and log errors if we find something wrong. | 148 // Preflight all required fields and log errors if we find something wrong. |
| 149 if (device.vendor_id() == 0 || device.product_id() == 0 || | 149 if (device.vendor_id() == 0 || device.product_id() == 0) { |
| 150 device.manufacturer_string().empty() || device.product_string().empty()) { | |
| 151 LOG(ERROR) << "Failed to convert USB device to printer. Fields were:\n" | 150 LOG(ERROR) << "Failed to convert USB device to printer. Fields were:\n" |
| 152 << UsbPrinterDeviceDetailsAsString(device); | 151 << UsbPrinterDeviceDetailsAsString(device); |
| 153 return nullptr; | 152 return nullptr; |
| 154 } | 153 } |
| 154 if (device.manufacturer_string().empty() || device.product_string().empty()) { | |
| 155 LOG(WARNING) | |
| 156 << "Usb printer device missing make/model metadata. Fields were:\n" | |
| 157 << UsbPrinterDeviceDetailsAsString(device); | |
|
skau
2017/07/10 21:24:18
I would just skip the log. We should be pretty to
Carlson
2017/07/10 21:32:01
Added some logic to make the display name less cry
| |
| 158 } | |
| 155 | 159 |
| 156 auto printer = base::MakeUnique<Printer>(); | 160 auto printer = base::MakeUnique<Printer>(); |
| 157 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); | 161 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); |
| 158 printer->set_model(base::UTF16ToUTF8(device.product_string())); | 162 printer->set_model(base::UTF16ToUTF8(device.product_string())); |
| 159 printer->set_display_name(base::StringPrintf("%s %s (USB)", | 163 printer->set_display_name(base::StringPrintf("%s %s (USB)", |
| 160 printer->manufacturer().c_str(), | 164 printer->manufacturer().c_str(), |
| 161 printer->model().c_str())); | 165 printer->model().c_str())); |
| 162 printer->set_description(printer->display_name()); | 166 printer->set_description(printer->display_name()); |
| 163 printer->set_uri(UsbPrinterUri(device)); | 167 printer->set_uri(UsbPrinterUri(device)); |
| 164 printer->set_id(UsbPrinterId(device)); | 168 printer->set_id(UsbPrinterId(device)); |
| 165 return printer; | 169 return printer; |
| 166 } | 170 } |
| 167 | 171 |
| 168 std::string UsbPrinterUri(const device::UsbDevice& device) { | 172 std::string UsbPrinterUri(const device::UsbDevice& device) { |
| 169 // Note that serial may, for some devices, be empty or bogus (all zeros, non | 173 // Note that serial may, for some devices, be empty or bogus (all zeros, non |
| 170 // unique, or otherwise not really a serial number), but having a non-unique | 174 // unique, or otherwise not really a serial number), but having a non-unique |
| 171 // or empty serial field in the URI still lets us print, it just means we | 175 // or empty serial field in the URI still lets us print, it just means we |
| 172 // don't have a way to uniquely identify a printer if there are multiple ones | 176 // don't have a way to uniquely identify a printer if there are multiple ones |
| 173 // plugged in with the same VID/PID, so we may print to the *wrong* printer. | 177 // plugged in with the same VID/PID, so we may print to the *wrong* printer. |
| 174 // There doesn't seem to be a robust solution to this problem; if printers | 178 // There doesn't seem to be a robust solution to this problem; if printers |
| 175 // don't supply a serial number, we don't have any reliable way to do that | 179 // don't supply a serial number, we don't have any reliable way to do that |
| 176 // differentiation. | 180 // differentiation. |
| 177 std::string serial = base::UTF16ToUTF8(device.serial_number()); | 181 std::string serial = base::UTF16ToUTF8(device.serial_number()); |
| 178 return CupsURIEscape(base::StringPrintf("usb://%04x/%04x?serial=%s", | 182 return CupsURIEscape(base::StringPrintf("usb://%04x/%04x?serial=%s", |
| 179 device.vendor_id(), | 183 device.vendor_id(), |
| 180 device.product_id(), serial.c_str())); | 184 device.product_id(), serial.c_str())); |
| 181 } | 185 } |
| 182 | 186 |
| 183 } // namespace chromeos | 187 } // namespace chromeos |
| OLD | NEW |