Commit 06198969 authored by juncai's avatar juncai Committed by Commit bot

Fix blank names issue for navigator.usb.requestDevice

Sometimes UsbDevice::product_string() returns empty string, and the
current WebUSB chooser uses it as the device name to be displayed.
This CL fixes the blank name issue by displaying, in decreasing order
of preference:
1. UsbDevice::product_string()
2. UsbIds::GetProductName()
3. "Unknown device from " + UsbIds::GetVendorName()
4. "Unknown device [$VID:$PID]"

BUG=665599

Review-Url: https://codereview.chromium.org/2539593002
Cr-Commit-Position: refs/heads/master@{#435019}
parent bf33256a
......@@ -15213,6 +15213,12 @@ Please check your email at <ph name="ACCOUNT_EMAIL">$2<ex>jane.doe@gmail.com</ex
<message name="IDS_DEVICE_CHOOSER_DEVICE_NAME_AND_PAIRED_STATUS_TEXT" desc="Text of the device name and paired status.">
<ph name="DEVICE_NAME">$1<ex>device name</ex></ph> - Paired
</message>
<message name="IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_NAME" desc="String describing an unknown device by its vendor name.">
Unknown device from <ph name="VENDOR_NAME">$1<ex>vendor name</ex></ph>
</message>
<message name="IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_ID_AND_PRODUCT_ID" desc="String describing a device when only numeric vendor and product IDs are available.">
Unknown device [<ph name="VENDOR_ID">$1<ex>123</ex></ph>:<ph name="PRODUCT_ID">$2<ex>123</ex></ph>]
</message>
</if>
<if expr="is_android">
......@@ -9,6 +9,8 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/net/referrer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
......@@ -27,6 +29,7 @@
#include "device/usb/mojo/type_converters.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_device_filter.h"
#include "device/usb/usb_ids.h"
#include "device/usb/webusb_descriptors.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
......@@ -40,6 +43,30 @@ Browser* GetBrowser() {
return browser_displayer.browser();
}
base::string16 GetDeviceName(scoped_refptr<device::UsbDevice> device) {
base::string16 device_name = device->product_string();
if (device_name.empty()) {
uint16_t vendor_id = device->vendor_id();
uint16_t product_id = device->product_id();
if (const char* product_name =
device::UsbIds::GetProductName(vendor_id, product_id)) {
device_name = base::UTF8ToUTF16(product_name);
} else if (const char* vendor_name =
device::UsbIds::GetVendorName(vendor_id)) {
device_name = l10n_util::GetStringFUTF16(
IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_NAME,
base::UTF8ToUTF16(vendor_name));
} else {
device_name = l10n_util::GetStringFUTF16(
IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_ID_AND_PRODUCT_ID,
base::ASCIIToUTF16(base::StringPrintf("%04x", vendor_id)),
base::ASCIIToUTF16(base::StringPrintf("%04x", product_id)));
}
}
return device_name;
}
} // namespace
UsbChooserController::UsbChooserController(
......@@ -154,7 +181,7 @@ void UsbChooserController::OpenHelpCenterUrl() const {
void UsbChooserController::OnDeviceAdded(
scoped_refptr<device::UsbDevice> device) {
if (DisplayDevice(device)) {
const base::string16& device_name = device->product_string();
base::string16 device_name = GetDeviceName(device);
devices_.push_back(std::make_pair(device, device_name));
++device_name_map_[device_name];
if (view())
......@@ -184,7 +211,7 @@ void UsbChooserController::GotUsbDeviceList(
const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
for (const auto& device : devices) {
if (DisplayDevice(device)) {
const base::string16& device_name = device->product_string();
base::string16 device_name = GetDeviceName(device);
devices_.push_back(std::make_pair(device, device_name));
++device_name_map_[device_name];
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment