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 |
11 #include "base/big_endian.h" | 11 #include "base/big_endian.h" |
12 #include "base/md5.h" | 12 #include "base/md5.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "chromeos/printing/printer_configuration.h" | 19 #include "chromeos/printing/printer_configuration.h" |
19 #include "device/usb/public/cpp/filter_utils.h" | 20 #include "device/usb/public/cpp/filter_utils.h" |
20 #include "device/usb/public/interfaces/device_manager.mojom.h" | 21 #include "device/usb/public/interfaces/device_manager.mojom.h" |
21 #include "device/usb/usb_device.h" | 22 #include "device/usb/usb_device.h" |
22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
23 | 24 |
24 namespace chromeos { | 25 namespace chromeos { |
25 namespace { | 26 namespace { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 base::UTF16ToUTF8(device.product_string()).c_str(), | 140 base::UTF16ToUTF8(device.product_string()).c_str(), |
140 base::UTF16ToUTF8(device.serial_number()).c_str()); | 141 base::UTF16ToUTF8(device.serial_number()).c_str()); |
141 } | 142 } |
142 | 143 |
143 // Attempt to gather all the information we need to work with this printer by | 144 // 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 | 145 // 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 | 146 // are printers, not arbitrary USB devices, as we may get weird partial results |
146 // from arbitrary devices. | 147 // from arbitrary devices. |
147 std::unique_ptr<Printer> UsbDeviceToPrinter(const device::UsbDevice& device) { | 148 std::unique_ptr<Printer> UsbDeviceToPrinter(const device::UsbDevice& device) { |
148 // Preflight all required fields and log errors if we find something wrong. | 149 // Preflight all required fields and log errors if we find something wrong. |
149 if (device.vendor_id() == 0 || device.product_id() == 0 || | 150 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" | 151 LOG(ERROR) << "Failed to convert USB device to printer. Fields were:\n" |
152 << UsbPrinterDeviceDetailsAsString(device); | 152 << UsbPrinterDeviceDetailsAsString(device); |
153 return nullptr; | 153 return nullptr; |
154 } | 154 } |
155 | 155 |
156 auto printer = base::MakeUnique<Printer>(); | 156 auto printer = base::MakeUnique<Printer>(); |
157 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); | 157 printer->set_manufacturer(base::UTF16ToUTF8(device.manufacturer_string())); |
158 printer->set_model(base::UTF16ToUTF8(device.product_string())); | 158 printer->set_model(base::UTF16ToUTF8(device.product_string())); |
159 printer->set_display_name(base::StringPrintf("%s %s (USB)", | 159 |
160 printer->manufacturer().c_str(), | 160 // Construct the display name by however much of the manufacturer/model |
161 printer->model().c_str())); | 161 // information that we have available. |
| 162 std::vector<std::string> display_name_parts; |
| 163 if (!printer->manufacturer().empty()) { |
| 164 display_name_parts.push_back(printer->manufacturer()); |
| 165 } |
| 166 if (!printer->model().empty()) { |
| 167 display_name_parts.push_back(printer->model()); |
| 168 } |
| 169 if (display_name_parts.empty()) { |
| 170 // If we have neither manufacturer nor model, just display the name as |
| 171 // unknown. |
| 172 display_name_parts.push_back("Unknown Printer"); |
| 173 } |
| 174 display_name_parts.push_back("(USB)"); |
| 175 printer->set_display_name(base::JoinString(display_name_parts, " ")); |
162 printer->set_description(printer->display_name()); | 176 printer->set_description(printer->display_name()); |
163 printer->set_uri(UsbPrinterUri(device)); | 177 printer->set_uri(UsbPrinterUri(device)); |
164 printer->set_id(UsbPrinterId(device)); | 178 printer->set_id(UsbPrinterId(device)); |
165 return printer; | 179 return printer; |
166 } | 180 } |
167 | 181 |
168 std::string UsbPrinterUri(const device::UsbDevice& device) { | 182 std::string UsbPrinterUri(const device::UsbDevice& device) { |
169 // Note that serial may, for some devices, be empty or bogus (all zeros, non | 183 // 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 | 184 // 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 | 185 // 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 | 186 // 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. | 187 // 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 | 188 // 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 | 189 // don't supply a serial number, we don't have any reliable way to do that |
176 // differentiation. | 190 // differentiation. |
177 std::string serial = base::UTF16ToUTF8(device.serial_number()); | 191 std::string serial = base::UTF16ToUTF8(device.serial_number()); |
178 return CupsURIEscape(base::StringPrintf("usb://%04x/%04x?serial=%s", | 192 return CupsURIEscape(base::StringPrintf("usb://%04x/%04x?serial=%s", |
179 device.vendor_id(), | 193 device.vendor_id(), |
180 device.product_id(), serial.c_str())); | 194 device.product_id(), serial.c_str())); |
181 } | 195 } |
182 | 196 |
183 } // namespace chromeos | 197 } // namespace chromeos |
OLD | NEW |