Commit 4a93af85 authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Use base::string16 for device paths in DeviceMonitorWin

This is based on a comment from robliao@ in https://crrev.com/c/2099238.
This change avoids converting the UTF-16 device path to a UTF-8 path and
therefore makes it possible to call the W versions of Win32 APIs.

Change-Id: I833c2cc7806cd0a61cbad236ebd9fd1985654635
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2106752Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Commit-Queue: Robert Liao <robliao@chromium.org>
Auto-Submit: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751190}
parent cf2d38fa
...@@ -26,9 +26,9 @@ DeviceInfoQueryWin::~DeviceInfoQueryWin() { ...@@ -26,9 +26,9 @@ DeviceInfoQueryWin::~DeviceInfoQueryWin() {
} }
} }
bool DeviceInfoQueryWin::AddDevice(const std::string& device_path) { bool DeviceInfoQueryWin::AddDevice(const base::string16& device_path) {
return SetupDiOpenDeviceInterfaceA(device_info_list_, device_path.c_str(), 0, return SetupDiOpenDeviceInterface(device_info_list_, device_path.c_str(), 0,
nullptr) != FALSE; nullptr) != FALSE;
} }
bool DeviceInfoQueryWin::GetDeviceInfo() { bool DeviceInfoQueryWin::GetDeviceInfo() {
...@@ -53,15 +53,15 @@ bool DeviceInfoQueryWin::GetDeviceStringProperty(const DEVPROPKEY& property, ...@@ -53,15 +53,15 @@ bool DeviceInfoQueryWin::GetDeviceStringProperty(const DEVPROPKEY& property,
return false; return false;
} }
base::string16 buffer16; base::string16 buffer;
if (!SetupDiGetDeviceProperty( if (!SetupDiGetDeviceProperty(
device_info_list_, &device_info_data_, &property, &property_type, device_info_list_, &device_info_data_, &property, &property_type,
reinterpret_cast<PBYTE>(base::WriteInto(&buffer16, required_size)), reinterpret_cast<PBYTE>(base::WriteInto(&buffer, required_size)),
required_size, nullptr, 0)) { required_size, nullptr, 0)) {
return false; return false;
} }
*property_buffer = base::UTF16ToUTF8(buffer16); *property_buffer = base::UTF16ToUTF8(buffer);
return true; return true;
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string16.h"
#include "device/base/device_base_export.h" #include "device/base/device_base_export.h"
namespace device { namespace device {
...@@ -25,7 +26,7 @@ class DEVICE_BASE_EXPORT DeviceInfoQueryWin { ...@@ -25,7 +26,7 @@ class DEVICE_BASE_EXPORT DeviceInfoQueryWin {
// Add a device to |device_info_list_| using its |device_path| so that // Add a device to |device_info_list_| using its |device_path| so that
// its device info can be retrieved. // its device info can be retrieved.
bool AddDevice(const std::string& device_path); bool AddDevice(const base::string16& device_path);
// Get the device info and store it into |device_info_data_|, this function // Get the device info and store it into |device_info_data_|, this function
// should be called at most once. // should be called at most once.
bool GetDeviceInfo(); bool GetDeviceInfo();
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
#include "device/base/device_monitor_win.h" #include "device/base/device_monitor_win.h"
#include <dbt.h> // windows.h must be included before dbt.h.
#include <windows.h> #include <windows.h>
#include <dbt.h>
#include <map> #include <map>
#include <memory> #include <memory>
...@@ -121,7 +123,7 @@ class DeviceMonitorMessageWindow { ...@@ -121,7 +123,7 @@ class DeviceMonitorMessageWindow {
if (map_entry != device_monitors_.end()) if (map_entry != device_monitors_.end())
device_monitor = map_entry->second.get(); device_monitor = map_entry->second.get();
std::string device_path(base::SysWideToUTF8(db->dbcc_name)); base::string16 device_path(db->dbcc_name);
DCHECK(base::IsStringASCII(device_path)); DCHECK(base::IsStringASCII(device_path));
device_path = base::ToLowerASCII(device_path); device_path = base::ToLowerASCII(device_path);
...@@ -152,13 +154,13 @@ class DeviceMonitorMessageWindow { ...@@ -152,13 +154,13 @@ class DeviceMonitorMessageWindow {
DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMessageWindow); DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMessageWindow);
}; };
void DeviceMonitorWin::Observer::OnDeviceAdded(const GUID& class_guid, void DeviceMonitorWin::Observer::OnDeviceAdded(
const std::string& device_path) { const GUID& class_guid,
} const base::string16& device_path) {}
void DeviceMonitorWin::Observer::OnDeviceRemoved( void DeviceMonitorWin::Observer::OnDeviceRemoved(
const GUID& class_guid, const GUID& class_guid,
const std::string& device_path) {} const base::string16& device_path) {}
// static // static
DeviceMonitorWin* DeviceMonitorWin::GetForDeviceInterface( DeviceMonitorWin* DeviceMonitorWin::GetForDeviceInterface(
...@@ -194,13 +196,13 @@ void DeviceMonitorWin::RemoveObserver(Observer* observer) { ...@@ -194,13 +196,13 @@ void DeviceMonitorWin::RemoveObserver(Observer* observer) {
DeviceMonitorWin::DeviceMonitorWin() {} DeviceMonitorWin::DeviceMonitorWin() {}
void DeviceMonitorWin::NotifyDeviceAdded(const GUID& class_guid, void DeviceMonitorWin::NotifyDeviceAdded(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnDeviceAdded(class_guid, device_path); observer.OnDeviceAdded(class_guid, device_path);
} }
void DeviceMonitorWin::NotifyDeviceRemoved(const GUID& class_guid, void DeviceMonitorWin::NotifyDeviceRemoved(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnDeviceRemoved(class_guid, device_path); observer.OnDeviceRemoved(class_guid, device_path);
} }
......
...@@ -5,9 +5,11 @@ ...@@ -5,9 +5,11 @@
#ifndef DEVICE_BASE_DEVICE_MONITOR_WIN_H_ #ifndef DEVICE_BASE_DEVICE_MONITOR_WIN_H_
#define DEVICE_BASE_DEVICE_MONITOR_WIN_H_ #define DEVICE_BASE_DEVICE_MONITOR_WIN_H_
#include <windows.h> #include <string>
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/strings/string16.h"
#include "base/win/windows_types.h"
#include "device/base/device_base_export.h" #include "device/base/device_base_export.h"
namespace device { namespace device {
...@@ -19,9 +21,9 @@ class DEVICE_BASE_EXPORT DeviceMonitorWin { ...@@ -19,9 +21,9 @@ class DEVICE_BASE_EXPORT DeviceMonitorWin {
class DEVICE_BASE_EXPORT Observer { class DEVICE_BASE_EXPORT Observer {
public: public:
virtual void OnDeviceAdded(const GUID& class_guid, virtual void OnDeviceAdded(const GUID& class_guid,
const std::string& device_path); const base::string16& device_path);
virtual void OnDeviceRemoved(const GUID& class_guid, virtual void OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path); const base::string16& device_path);
}; };
~DeviceMonitorWin(); ~DeviceMonitorWin();
...@@ -38,9 +40,9 @@ class DEVICE_BASE_EXPORT DeviceMonitorWin { ...@@ -38,9 +40,9 @@ class DEVICE_BASE_EXPORT DeviceMonitorWin {
DeviceMonitorWin(); DeviceMonitorWin();
void NotifyDeviceAdded(const GUID& class_guid, void NotifyDeviceAdded(const GUID& class_guid,
const std::string& device_path); const base::string16& device_path);
void NotifyDeviceRemoved(const GUID& class_guid, void NotifyDeviceRemoved(const GUID& class_guid,
const std::string& device_path); const base::string16& device_path);
base::ObserverList<Observer>::Unchecked observer_list_; base::ObserverList<Observer>::Unchecked observer_list_;
}; };
......
...@@ -24,8 +24,10 @@ namespace { ...@@ -24,8 +24,10 @@ namespace {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
const uint64_t kTestDeviceId = 123; const uint64_t kTestDeviceId = 123;
#elif defined(OS_WIN)
const wchar_t kTestDeviceId[] = L"123";
#else #else
const char* kTestDeviceId = "123"; const char kTestDeviceId[] = "123";
#endif #endif
const char kPhysicalDeviceId[] = "1"; const char kPhysicalDeviceId[] = "1";
......
...@@ -20,6 +20,8 @@ namespace { ...@@ -20,6 +20,8 @@ namespace {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
const uint64_t kTestDeviceId = 123; const uint64_t kTestDeviceId = 123;
#elif defined(OS_WIN)
const wchar_t* kTestDeviceId = L"123";
#else #else
const char* kTestDeviceId = "123"; const char* kTestDeviceId = "123";
#endif #endif
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "services/device/public/mojom/hid.mojom.h" #include "services/device/public/mojom/hid.mojom.h"
...@@ -20,6 +21,8 @@ namespace device { ...@@ -20,6 +21,8 @@ namespace device {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
typedef uint64_t HidPlatformDeviceId; typedef uint64_t HidPlatformDeviceId;
#elif defined(OS_WIN)
typedef base::string16 HidPlatformDeviceId;
#else #else
typedef std::string HidPlatformDeviceId; typedef std::string HidPlatformDeviceId;
#endif #endif
......
...@@ -24,6 +24,8 @@ namespace { ...@@ -24,6 +24,8 @@ namespace {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
const uint64_t kTestDeviceIds[] = {0, 1, 2}; const uint64_t kTestDeviceIds[] = {0, 1, 2};
#elif defined(OS_WIN)
const wchar_t* const kTestDeviceIds[] = {L"0", L"1", L"2"};
#else #else
const char* const kTestDeviceIds[] = {"0", "1", "2"}; const char* const kTestDeviceIds[] = {"0", "1", "2"};
#endif #endif
......
...@@ -113,8 +113,7 @@ void HidServiceWin::EnumerateBlocking( ...@@ -113,8 +113,7 @@ void HidServiceWin::EnumerateBlocking(
continue; continue;
} }
std::string device_path( base::string16 device_path(device_interface_detail_data->DevicePath);
base::SysWideToUTF8(device_interface_detail_data->DevicePath));
DCHECK(base::IsStringASCII(device_path)); DCHECK(base::IsStringASCII(device_path));
AddDeviceBlocking(service, task_runner, base::ToLowerASCII(device_path)); AddDeviceBlocking(service, task_runner, base::ToLowerASCII(device_path));
} }
...@@ -171,7 +170,7 @@ void HidServiceWin::CollectInfoFromValueCaps( ...@@ -171,7 +170,7 @@ void HidServiceWin::CollectInfoFromValueCaps(
void HidServiceWin::AddDeviceBlocking( void HidServiceWin::AddDeviceBlocking(
base::WeakPtr<HidServiceWin> service, base::WeakPtr<HidServiceWin> service,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner,
const std::string& device_path) { const base::string16& device_path) {
base::win::ScopedHandle device_handle(OpenDevice(device_path)); base::win::ScopedHandle device_handle(OpenDevice(device_path));
if (!device_handle.IsValid()) { if (!device_handle.IsValid()) {
return; return;
...@@ -267,7 +266,7 @@ void HidServiceWin::AddDeviceBlocking( ...@@ -267,7 +266,7 @@ void HidServiceWin::AddDeviceBlocking(
} }
void HidServiceWin::OnDeviceAdded(const GUID& class_guid, void HidServiceWin::OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
blocking_task_runner_->PostTask( blocking_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&HidServiceWin::AddDeviceBlocking, base::BindOnce(&HidServiceWin::AddDeviceBlocking,
...@@ -275,7 +274,7 @@ void HidServiceWin::OnDeviceAdded(const GUID& class_guid, ...@@ -275,7 +274,7 @@ void HidServiceWin::OnDeviceAdded(const GUID& class_guid,
} }
void HidServiceWin::OnDeviceRemoved(const GUID& class_guid, void HidServiceWin::OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
// Execute a no-op closure on the file task runner to synchronize with any // Execute a no-op closure on the file task runner to synchronize with any
// devices that are still being enumerated. // devices that are still being enumerated.
blocking_task_runner_->PostTaskAndReply( blocking_task_runner_->PostTaskAndReply(
...@@ -286,14 +285,14 @@ void HidServiceWin::OnDeviceRemoved(const GUID& class_guid, ...@@ -286,14 +285,14 @@ void HidServiceWin::OnDeviceRemoved(const GUID& class_guid,
// static // static
base::win::ScopedHandle HidServiceWin::OpenDevice( base::win::ScopedHandle HidServiceWin::OpenDevice(
const std::string& device_path) { const base::string16& device_path) {
base::win::ScopedHandle file( base::win::ScopedHandle file(
CreateFileA(device_path.c_str(), GENERIC_WRITE | GENERIC_READ, CreateFile(device_path.c_str(), GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL)); FILE_FLAG_OVERLAPPED, nullptr));
if (!file.IsValid() && GetLastError() == ERROR_ACCESS_DENIED) { if (!file.IsValid() && GetLastError() == ERROR_ACCESS_DENIED) {
file.Set(CreateFileA(device_path.c_str(), GENERIC_READ, FILE_SHARE_READ, file.Set(CreateFile(device_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)); nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr));
} }
return file; return file;
} }
......
...@@ -22,6 +22,7 @@ extern "C" { ...@@ -22,6 +22,7 @@ extern "C" {
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "base/win/scoped_handle.h" #include "base/win/scoped_handle.h"
#include "device/base/device_monitor_win.h" #include "device/base/device_monitor_win.h"
#include "services/device/hid/hid_device_info.h" #include "services/device/hid/hid_device_info.h"
...@@ -58,16 +59,16 @@ class HidServiceWin : public HidService, public DeviceMonitorWin::Observer { ...@@ -58,16 +59,16 @@ class HidServiceWin : public HidService, public DeviceMonitorWin::Observer {
static void AddDeviceBlocking( static void AddDeviceBlocking(
base::WeakPtr<HidServiceWin> service, base::WeakPtr<HidServiceWin> service,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner,
const std::string& device_path); const base::string16& device_path);
// DeviceMonitorWin::Observer implementation: // DeviceMonitorWin::Observer implementation:
void OnDeviceAdded(const GUID& class_guid, void OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
void OnDeviceRemoved(const GUID& class_guid, void OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
// Tries to open the device read-write and falls back to read-only. // Tries to open the device read-write and falls back to read-only.
static base::win::ScopedHandle OpenDevice(const std::string& device_path); static base::win::ScopedHandle OpenDevice(const base::string16& device_path);
const scoped_refptr<base::SequencedTaskRunner> task_runner_; const scoped_refptr<base::SequencedTaskRunner> task_runner_;
const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
......
...@@ -17,6 +17,8 @@ namespace { ...@@ -17,6 +17,8 @@ namespace {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
const uint64_t kTestDeviceId = 42; const uint64_t kTestDeviceId = 42;
#elif defined(OS_WIN)
const wchar_t* kTestDeviceId = L"device1";
#else #else
const char* kTestDeviceId = "device1"; const char* kTestDeviceId = "device1";
#endif #endif
......
...@@ -175,7 +175,7 @@ class SerialIoHandlerWin::UiThreadHelper final ...@@ -175,7 +175,7 @@ class SerialIoHandlerWin::UiThreadHelper final
private: private:
// DeviceMonitorWin::Observer // DeviceMonitorWin::Observer
void OnDeviceRemoved(const GUID& class_guid, void OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) override { const base::string16& device_path) override {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
io_thread_task_runner_->PostTask( io_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&SerialIoHandlerWin::OnDeviceRemoved, FROM_HERE, base::BindOnce(&SerialIoHandlerWin::OnDeviceRemoved,
...@@ -192,7 +192,7 @@ class SerialIoHandlerWin::UiThreadHelper final ...@@ -192,7 +192,7 @@ class SerialIoHandlerWin::UiThreadHelper final
DISALLOW_COPY_AND_ASSIGN(UiThreadHelper); DISALLOW_COPY_AND_ASSIGN(UiThreadHelper);
}; };
void SerialIoHandlerWin::OnDeviceRemoved(const std::string& device_path) { void SerialIoHandlerWin::OnDeviceRemoved(const base::string16& device_path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DeviceInfoQueryWin device_info_query; DeviceInfoQueryWin device_info_query;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_pump_for_io.h" #include "base/message_loop/message_pump_for_io.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string16.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "services/device/serial/serial_io_handler.h" #include "services/device/serial/serial_io_handler.h"
...@@ -46,7 +47,7 @@ class SerialIoHandlerWin : public SerialIoHandler, ...@@ -46,7 +47,7 @@ class SerialIoHandlerWin : public SerialIoHandler,
DWORD bytes_transfered, DWORD bytes_transfered,
DWORD error) override; DWORD error) override;
void OnDeviceRemoved(const std::string& device_path); void OnDeviceRemoved(const base::string16& device_path);
// Context used for asynchronous WaitCommEvent calls. // Context used for asynchronous WaitCommEvent calls.
std::unique_ptr<base::MessagePumpForIO::IOContext> comm_context_; std::unique_ptr<base::MessagePumpForIO::IOContext> comm_context_;
......
...@@ -523,7 +523,7 @@ bool UsbDeviceHandleWin::OpenInterfaceHandle(Interface* interface) { ...@@ -523,7 +523,7 @@ bool UsbDeviceHandleWin::OpenInterfaceHandle(Interface* interface) {
WINUSB_INTERFACE_HANDLE handle; WINUSB_INTERFACE_HANDLE handle;
if (interface->first_interface == interface->interface_number) { if (interface->first_interface == interface->interface_number) {
if (!function_handle_.IsValid()) { if (!function_handle_.IsValid()) {
function_handle_.Set(CreateFileA( function_handle_.Set(CreateFile(
device_->device_path().c_str(), GENERIC_READ | GENERIC_WRITE, device_->device_path().c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, nullptr)); FILE_FLAG_OVERLAPPED, nullptr));
......
...@@ -24,11 +24,11 @@ namespace { ...@@ -24,11 +24,11 @@ namespace {
const uint16_t kUsbVersion2_1 = 0x0210; const uint16_t kUsbVersion2_1 = 0x0210;
} // namespace } // namespace
UsbDeviceWin::UsbDeviceWin(const std::string& device_path, UsbDeviceWin::UsbDeviceWin(const base::string16& device_path,
const std::string& hub_path, const base::string16& hub_path,
uint32_t bus_number, uint32_t bus_number,
uint32_t port_number, uint32_t port_number,
const std::string& driver_name) const base::string16& driver_name)
: UsbDevice(bus_number, port_number), : UsbDevice(bus_number, port_number),
device_path_(device_path), device_path_(device_path),
hub_path_(hub_path), hub_path_(hub_path),
...@@ -40,7 +40,7 @@ void UsbDeviceWin::Open(OpenCallback callback) { ...@@ -40,7 +40,7 @@ void UsbDeviceWin::Open(OpenCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scoped_refptr<UsbDeviceHandle> device_handle; scoped_refptr<UsbDeviceHandle> device_handle;
if (base::EqualsCaseInsensitiveASCII(driver_name_, "winusb")) if (base::EqualsCaseInsensitiveASCII(driver_name_, L"winusb"))
device_handle = new UsbDeviceHandleWin(this, false); device_handle = new UsbDeviceHandleWin(this, false);
// TODO: Support composite devices. // TODO: Support composite devices.
// else if (base::EqualsCaseInsensitiveASCII(driver_name_, "usbccgp")) // else if (base::EqualsCaseInsensitiveASCII(driver_name_, "usbccgp"))
...@@ -55,8 +55,8 @@ void UsbDeviceWin::ReadDescriptors(base::OnceCallback<void(bool)> callback) { ...@@ -55,8 +55,8 @@ void UsbDeviceWin::ReadDescriptors(base::OnceCallback<void(bool)> callback) {
scoped_refptr<UsbDeviceHandle> device_handle; scoped_refptr<UsbDeviceHandle> device_handle;
base::win::ScopedHandle handle( base::win::ScopedHandle handle(
CreateFileA(hub_path_.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CreateFile(hub_path_.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr)); OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr));
if (handle.IsValid()) { if (handle.IsValid()) {
device_handle = new UsbDeviceHandleWin(this, std::move(handle)); device_handle = new UsbDeviceHandleWin(this, std::move(handle));
} else { } else {
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/strings/string16.h"
#include "services/device/usb/usb_device.h" #include "services/device/usb/usb_device.h"
namespace device { namespace device {
...@@ -27,16 +28,16 @@ class UsbDeviceWin : public UsbDevice { ...@@ -27,16 +28,16 @@ class UsbDeviceWin : public UsbDevice {
friend class UsbDeviceHandleWin; friend class UsbDeviceHandleWin;
// Called by UsbServiceWin only. // Called by UsbServiceWin only.
UsbDeviceWin(const std::string& device_path, UsbDeviceWin(const base::string16& device_path,
const std::string& hub_path, const base::string16& hub_path,
uint32_t bus_number, uint32_t bus_number,
uint32_t port_number, uint32_t port_number,
const std::string& driver_name); const base::string16& driver_name);
~UsbDeviceWin() override; ~UsbDeviceWin() override;
const std::string& device_path() const { return device_path_; } const base::string16& device_path() const { return device_path_; }
const std::string& driver_name() const { return driver_name_; } const base::string16& driver_name() const { return driver_name_; }
// Opens the device's parent hub in order to read the device, configuration // Opens the device's parent hub in order to read the device, configuration
// and string descriptors. // and string descriptors.
...@@ -63,9 +64,9 @@ class UsbDeviceWin : public UsbDevice { ...@@ -63,9 +64,9 @@ class UsbDeviceWin : public UsbDevice {
private: private:
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
const std::string device_path_; const base::string16 device_path_;
const std::string hub_path_; const base::string16 hub_path_;
const std::string driver_name_; const base::string16 driver_name_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceWin); DISALLOW_COPY_AND_ASSIGN(UsbDeviceWin);
}; };
......
...@@ -50,7 +50,7 @@ const uint16_t kUsbVersion2_1 = 0x0210; ...@@ -50,7 +50,7 @@ const uint16_t kUsbVersion2_1 = 0x0210;
#if defined(OS_WIN) #if defined(OS_WIN)
bool IsWinUsbInterface(const std::string& device_path) { bool IsWinUsbInterface(const base::string16& device_path) {
DeviceInfoQueryWin device_info_query; DeviceInfoQueryWin device_info_query;
if (!device_info_query.device_info_list_valid()) { if (!device_info_query.device_info_list_valid()) {
USB_PLOG(ERROR) << "Failed to create a device information set"; USB_PLOG(ERROR) << "Failed to create a device information set";
...@@ -97,7 +97,7 @@ scoped_refptr<UsbContext> InitializeUsbContextBlocking() { ...@@ -97,7 +97,7 @@ scoped_refptr<UsbContext> InitializeUsbContextBlocking() {
} }
base::Optional<std::vector<ScopedLibusbDeviceRef>> GetDeviceListBlocking( base::Optional<std::vector<ScopedLibusbDeviceRef>> GetDeviceListBlocking(
const std::string& new_device_path, const base::string16& new_device_path,
scoped_refptr<UsbContext> usb_context) { scoped_refptr<UsbContext> usb_context) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK); base::BlockingType::MAY_BLOCK);
...@@ -267,20 +267,20 @@ void UsbServiceImpl::GetDevices(GetDevicesCallback callback) { ...@@ -267,20 +267,20 @@ void UsbServiceImpl::GetDevices(GetDevicesCallback callback) {
#if defined(OS_WIN) #if defined(OS_WIN)
void UsbServiceImpl::OnDeviceAdded(const GUID& class_guid, void UsbServiceImpl::OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
// Only the root node of a composite USB device has the class GUID // Only the root node of a composite USB device has the class GUID
// GUID_DEVINTERFACE_USB_DEVICE but we want to wait until WinUSB is loaded. // GUID_DEVINTERFACE_USB_DEVICE but we want to wait until WinUSB is loaded.
// This first pass filter will catch anything that's sitting on the USB bus // This first pass filter will catch anything that's sitting on the USB bus
// (including devices on 3rd party USB controllers) to avoid the more // (including devices on 3rd party USB controllers) to avoid the more
// expensive driver check that needs to be done on the FILE thread. // expensive driver check that needs to be done on the FILE thread.
if (device_path.find("usb") != std::string::npos) { if (device_path.find(L"usb") != base::string16::npos) {
pending_path_enumerations_.push(device_path); pending_path_enumerations_.push(device_path);
RefreshDevices(); RefreshDevices();
} }
} }
void UsbServiceImpl::OnDeviceRemoved(const GUID& class_guid, void UsbServiceImpl::OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
// The root USB device node is removed last. // The root USB device node is removed last.
if (class_guid == GUID_DEVINTERFACE_USB_DEVICE) { if (class_guid == GUID_DEVINTERFACE_USB_DEVICE) {
RefreshDevices(); RefreshDevices();
...@@ -326,7 +326,7 @@ void UsbServiceImpl::RefreshDevices() { ...@@ -326,7 +326,7 @@ void UsbServiceImpl::RefreshDevices() {
enumeration_in_progress_ = true; enumeration_in_progress_ = true;
DCHECK(devices_being_enumerated_.empty()); DCHECK(devices_being_enumerated_.empty());
std::string device_path; base::string16 device_path;
if (!pending_path_enumerations_.empty()) { if (!pending_path_enumerations_.empty()) {
device_path = pending_path_enumerations_.front(); device_path = pending_path_enumerations_.front();
pending_path_enumerations_.pop(); pending_path_enumerations_.pop();
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "services/device/usb/scoped_libusb_device_ref.h" #include "services/device/usb/scoped_libusb_device_ref.h"
#include "services/device/usb/usb_context.h" #include "services/device/usb/usb_context.h"
...@@ -52,9 +53,9 @@ class UsbServiceImpl final : ...@@ -52,9 +53,9 @@ class UsbServiceImpl final :
#if defined(OS_WIN) #if defined(OS_WIN)
// device::DeviceMonitorWin::Observer implementation // device::DeviceMonitorWin::Observer implementation
void OnDeviceAdded(const GUID& class_guid, void OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
void OnDeviceRemoved(const GUID& class_guid, void OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
#endif // OS_WIN #endif // OS_WIN
void OnUsbContext(scoped_refptr<UsbContext> context); void OnUsbContext(scoped_refptr<UsbContext> context);
...@@ -100,7 +101,7 @@ class UsbServiceImpl final : ...@@ -100,7 +101,7 @@ class UsbServiceImpl final :
// Enumeration callbacks are queued until an enumeration completes. // Enumeration callbacks are queued until an enumeration completes.
bool enumeration_ready_ = false; bool enumeration_ready_ = false;
bool enumeration_in_progress_ = false; bool enumeration_in_progress_ = false;
base::queue<std::string> pending_path_enumerations_; base::queue<base::string16> pending_path_enumerations_;
std::vector<GetDevicesCallback> pending_enumeration_callbacks_; std::vector<GetDevicesCallback> pending_enumeration_callbacks_;
// The map from libusb_device to UsbDeviceImpl. The key is a weak pointer to // The map from libusb_device to UsbDeviceImpl. The key is a weak pointer to
......
...@@ -58,7 +58,7 @@ bool GetDeviceUint32Property(HDEVINFO dev_info, ...@@ -58,7 +58,7 @@ bool GetDeviceUint32Property(HDEVINFO dev_info,
bool GetDeviceStringProperty(HDEVINFO dev_info, bool GetDeviceStringProperty(HDEVINFO dev_info,
SP_DEVINFO_DATA* dev_info_data, SP_DEVINFO_DATA* dev_info_data,
const DEVPROPKEY& property, const DEVPROPKEY& property,
std::string* property_buffer) { base::string16* buffer) {
DEVPROPTYPE property_type; DEVPROPTYPE property_type;
DWORD required_size; DWORD required_size;
if (SetupDiGetDeviceProperty(dev_info, dev_info_data, &property, if (SetupDiGetDeviceProperty(dev_info, dev_info_data, &property,
...@@ -68,25 +68,23 @@ bool GetDeviceStringProperty(HDEVINFO dev_info, ...@@ -68,25 +68,23 @@ bool GetDeviceStringProperty(HDEVINFO dev_info,
return false; return false;
} }
base::string16 buffer16;
if (!SetupDiGetDeviceProperty( if (!SetupDiGetDeviceProperty(
dev_info, dev_info_data, &property, &property_type, dev_info, dev_info_data, &property, &property_type,
reinterpret_cast<PBYTE>(base::WriteInto(&buffer16, required_size)), reinterpret_cast<PBYTE>(base::WriteInto(buffer, required_size)),
required_size, nullptr, 0)) { required_size, nullptr, 0)) {
return false; return false;
} }
*property_buffer = base::UTF16ToUTF8(buffer16);
return true; return true;
} }
bool GetDeviceInterfaceDetails(HDEVINFO dev_info, bool GetDeviceInterfaceDetails(HDEVINFO dev_info,
SP_DEVICE_INTERFACE_DATA* device_interface_data, SP_DEVICE_INTERFACE_DATA* device_interface_data,
std::string* device_path, base::string16* device_path,
uint32_t* bus_number, uint32_t* bus_number,
uint32_t* port_number, uint32_t* port_number,
std::string* parent_instance_id, base::string16* parent_instance_id,
std::string* service_name) { base::string16* service_name) {
DWORD required_size = 0; DWORD required_size = 0;
if (SetupDiGetDeviceInterfaceDetail(dev_info, device_interface_data, nullptr, if (SetupDiGetDeviceInterfaceDetail(dev_info, device_interface_data, nullptr,
0, &required_size, nullptr) || 0, &required_size, nullptr) ||
...@@ -110,8 +108,7 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info, ...@@ -110,8 +108,7 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info,
} }
if (device_path) { if (device_path) {
*device_path = *device_path = base::string16(device_interface_detail_data->DevicePath);
base::SysWideToUTF8(device_interface_detail_data->DevicePath);
} }
if (bus_number) { if (bus_number) {
...@@ -147,7 +144,7 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info, ...@@ -147,7 +144,7 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info,
// Windows pads this string with a variable number of NUL bytes for no // Windows pads this string with a variable number of NUL bytes for no
// discernible reason. // discernible reason.
size_t end = service_name->find_last_not_of('\0'); size_t end = service_name->find_last_not_of(L'\0');
if (end != std::string::npos) if (end != std::string::npos)
service_name->erase(end + 1); service_name->erase(end + 1);
} }
...@@ -155,11 +152,11 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info, ...@@ -155,11 +152,11 @@ bool GetDeviceInterfaceDetails(HDEVINFO dev_info,
return true; return true;
} }
bool GetHubDevicePath(const std::string& instance_id, bool GetHubDevicePath(const base::string16& instance_id,
std::string* device_path) { base::string16* device_path) {
ScopedDevInfo dev_info( ScopedDevInfo dev_info(
SetupDiGetClassDevsA(&GUID_DEVINTERFACE_USB_HUB, instance_id.c_str(), 0, SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_HUB, instance_id.c_str(), 0,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); DIGCF_DEVICEINTERFACE | DIGCF_PRESENT));
if (!dev_info.is_valid()) { if (!dev_info.is_valid()) {
USB_PLOG(ERROR) << "SetupDiGetClassDevs"; USB_PLOG(ERROR) << "SetupDiGetClassDevs";
return false; return false;
...@@ -205,20 +202,20 @@ class UsbServiceWin::BlockingTaskRunnerHelper { ...@@ -205,20 +202,20 @@ class UsbServiceWin::BlockingTaskRunnerHelper {
&GUID_DEVINTERFACE_USB_DEVICE, &GUID_DEVINTERFACE_USB_DEVICE,
i, &device_interface_data); i, &device_interface_data);
++i) { ++i) {
std::string device_path; base::string16 device_path;
uint32_t bus_number; uint32_t bus_number;
uint32_t port_number; uint32_t port_number;
std::string parent_instance_id; base::string16 parent_instance_id;
std::string service_name; base::string16 service_name;
if (!GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data, if (!GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data,
&device_path, &bus_number, &port_number, &device_path, &bus_number, &port_number,
&parent_instance_id, &service_name)) { &parent_instance_id, &service_name)) {
continue; continue;
} }
std::string& hub_path = hub_paths_[parent_instance_id]; base::string16& hub_path = hub_paths_[parent_instance_id];
if (hub_path.empty()) { if (hub_path.empty()) {
std::string parent_path; base::string16 parent_path;
if (!GetHubDevicePath(parent_instance_id, &parent_path)) if (!GetHubDevicePath(parent_instance_id, &parent_path))
continue; continue;
...@@ -238,7 +235,7 @@ class UsbServiceWin::BlockingTaskRunnerHelper { ...@@ -238,7 +235,7 @@ class UsbServiceWin::BlockingTaskRunnerHelper {
FROM_HERE, base::BindOnce(&UsbServiceWin::HelperStarted, service_)); FROM_HERE, base::BindOnce(&UsbServiceWin::HelperStarted, service_));
} }
void EnumerateDevicePath(const std::string& device_path) { void EnumerateDevicePath(const base::string16& device_path) {
ScopedDevInfo dev_info( ScopedDevInfo dev_info(
SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, nullptr, 0, SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, nullptr, 0,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT)); DIGCF_DEVICEINTERFACE | DIGCF_PRESENT));
...@@ -249,25 +246,25 @@ class UsbServiceWin::BlockingTaskRunnerHelper { ...@@ -249,25 +246,25 @@ class UsbServiceWin::BlockingTaskRunnerHelper {
SP_DEVICE_INTERFACE_DATA device_interface_data; SP_DEVICE_INTERFACE_DATA device_interface_data;
device_interface_data.cbSize = sizeof(device_interface_data); device_interface_data.cbSize = sizeof(device_interface_data);
if (!SetupDiOpenDeviceInterfaceA(dev_info.get(), device_path.c_str(), 0, if (!SetupDiOpenDeviceInterface(dev_info.get(), device_path.c_str(), 0,
&device_interface_data)) { &device_interface_data)) {
USB_PLOG(ERROR) << "Failed to add device interface: " << device_path; USB_PLOG(ERROR) << "Failed to add device interface: " << device_path;
return; return;
} }
uint32_t bus_number; uint32_t bus_number;
uint32_t port_number; uint32_t port_number;
std::string parent_instance_id; base::string16 parent_instance_id;
std::string service_name; base::string16 service_name;
if (!GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data, if (!GetDeviceInterfaceDetails(dev_info.get(), &device_interface_data,
nullptr, &bus_number, &port_number, nullptr, &bus_number, &port_number,
&parent_instance_id, &service_name)) { &parent_instance_id, &service_name)) {
return; return;
} }
std::string& hub_path = hub_paths_[parent_instance_id]; base::string16& hub_path = hub_paths_[parent_instance_id];
if (hub_path.empty()) { if (hub_path.empty()) {
std::string parent_path; base::string16 parent_path;
if (!GetHubDevicePath(parent_instance_id, &parent_path)) if (!GetHubDevicePath(parent_instance_id, &parent_path))
return; return;
...@@ -281,7 +278,7 @@ class UsbServiceWin::BlockingTaskRunnerHelper { ...@@ -281,7 +278,7 @@ class UsbServiceWin::BlockingTaskRunnerHelper {
} }
private: private:
std::unordered_map<std::string, std::string> hub_paths_; std::unordered_map<base::string16, base::string16> hub_paths_;
// Calls back to |service_| must be posted to |service_task_runner_|, which // Calls back to |service_| must be posted to |service_task_runner_|, which
// runs tasks on the thread where that object lives. // runs tasks on the thread where that object lives.
...@@ -318,14 +315,14 @@ void UsbServiceWin::GetDevices(GetDevicesCallback callback) { ...@@ -318,14 +315,14 @@ void UsbServiceWin::GetDevices(GetDevicesCallback callback) {
} }
void UsbServiceWin::OnDeviceAdded(const GUID& class_guid, void UsbServiceWin::OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
blocking_task_runner_->PostTask( blocking_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&BlockingTaskRunnerHelper::EnumerateDevicePath, FROM_HERE, base::BindOnce(&BlockingTaskRunnerHelper::EnumerateDevicePath,
base::Unretained(helper_.get()), device_path)); base::Unretained(helper_.get()), device_path));
} }
void UsbServiceWin::OnDeviceRemoved(const GUID& class_guid, void UsbServiceWin::OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) { const base::string16& device_path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto by_path_it = devices_by_path_.find(device_path); auto by_path_it = devices_by_path_.find(device_path);
if (by_path_it == devices_by_path_.end()) if (by_path_it == devices_by_path_.end())
...@@ -359,11 +356,11 @@ void UsbServiceWin::HelperStarted() { ...@@ -359,11 +356,11 @@ void UsbServiceWin::HelperStarted() {
} }
} }
void UsbServiceWin::CreateDeviceObject(const std::string& device_path, void UsbServiceWin::CreateDeviceObject(const base::string16& device_path,
const std::string& hub_path, const base::string16& hub_path,
uint32_t bus_number, uint32_t bus_number,
uint32_t port_number, uint32_t port_number,
const std::string& driver_name) { const base::string16& driver_name) {
// Devices that appear during initial enumeration are gathered into the first // Devices that appear during initial enumeration are gathered into the first
// result returned by GetDevices() and prevent device add/remove notifications // result returned by GetDevices() and prevent device add/remove notifications
// from being sent. // from being sent.
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/strings/string16.h"
#include "device/base/device_monitor_win.h" #include "device/base/device_monitor_win.h"
#include "services/device/usb/usb_device_win.h" #include "services/device/usb/usb_device_win.h"
...@@ -33,17 +34,17 @@ class UsbServiceWin final : public DeviceMonitorWin::Observer, ...@@ -33,17 +34,17 @@ class UsbServiceWin final : public DeviceMonitorWin::Observer,
// device::DeviceMonitorWin::Observer implementation // device::DeviceMonitorWin::Observer implementation
void OnDeviceAdded(const GUID& class_guid, void OnDeviceAdded(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
void OnDeviceRemoved(const GUID& class_guid, void OnDeviceRemoved(const GUID& class_guid,
const std::string& device_path) override; const base::string16& device_path) override;
// Methods called by BlockingThreadHelper // Methods called by BlockingThreadHelper
void HelperStarted(); void HelperStarted();
void CreateDeviceObject(const std::string& device_path, void CreateDeviceObject(const base::string16& device_path,
const std::string& hub_path, const base::string16& hub_path,
uint32_t bus_number, uint32_t bus_number,
uint32_t port_number, uint32_t port_number,
const std::string& driver_name); const base::string16& driver_name);
void DeviceReady(scoped_refptr<UsbDeviceWin> device, bool success); void DeviceReady(scoped_refptr<UsbDeviceWin> device, bool success);
...@@ -58,7 +59,8 @@ class UsbServiceWin final : public DeviceMonitorWin::Observer, ...@@ -58,7 +59,8 @@ class UsbServiceWin final : public DeviceMonitorWin::Observer,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
std::unique_ptr<BlockingTaskRunnerHelper, base::OnTaskRunnerDeleter> helper_; std::unique_ptr<BlockingTaskRunnerHelper, base::OnTaskRunnerDeleter> helper_;
std::unordered_map<std::string, scoped_refptr<UsbDeviceWin>> devices_by_path_; std::unordered_map<base::string16, scoped_refptr<UsbDeviceWin>>
devices_by_path_;
ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_;
......
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