Chromium Code Reviews| Index: device/serial/serial_io_handler_win.cc |
| diff --git a/device/serial/serial_io_handler_win.cc b/device/serial/serial_io_handler_win.cc |
| index cd1045107fe746e285d1bf422297236228339638..db414a06ba43b60579fcc4dba4861dc246169f8e 100644 |
| --- a/device/serial/serial_io_handler_win.cc |
| +++ b/device/serial/serial_io_handler_win.cc |
| @@ -2,9 +2,17 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "device/serial/serial_io_handler_win.h" |
| + |
| #include <windows.h> |
| +#include <setupapi.h> |
| -#include "device/serial/serial_io_handler_win.h" |
| +#include "base/bind.h" |
| +#include "base/scoped_observer.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "base/win/scoped_device_info_object.h" |
| +#include "device/core/device_monitor_win.h" |
| +#include "third_party/re2/re2/re2.h" |
| namespace device { |
| @@ -130,6 +138,12 @@ serial::StopBits StopBitsConstantToEnum(int stop_bits) { |
| } |
| } |
| +// Searches for the COM port in the device's friendly name, assigns its value to |
| +// com_port, and returns whether the operation was successful. |
| +bool GetCOMPort(const std::string friendly_name, std::string* com_port) { |
| + return RE2::PartialMatch(friendly_name, ".* \\((COM[0-9]+)\\)", com_port); |
| +} |
| + |
| } // namespace |
| // static |
| @@ -139,6 +153,85 @@ scoped_refptr<SerialIoHandler> SerialIoHandler::Create( |
| return new SerialIoHandlerWin(file_thread_task_runner, ui_thread_task_runner); |
| } |
| +class SerialIoHandlerWin::UiThreadHelper : public DeviceMonitorWin::Observer { |
| + public: |
| + UiThreadHelper( |
| + base::WeakPtr<SerialIoHandlerWin> io_handler, |
| + scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) |
| + : device_observer_(this), |
| + io_handler_(io_handler), |
| + io_thread_task_runner_(io_thread_task_runner) {} |
| + |
| + ~UiThreadHelper() { DCHECK(thread_checker_.CalledOnValidThread()); } |
| + |
| + static void Start(UiThreadHelper* self) { |
| + self->thread_checker_.DetachFromThread(); |
| + DeviceMonitorWin* device_monitor = DeviceMonitorWin::GetForAllInterfaces(); |
| + if (device_monitor) { |
|
Reilly Grant (use Gerrit)
2015/11/12 19:30:02
nit: no braces around single-line if
juncai
2015/11/12 23:21:39
Done.
|
| + self->device_observer_.Add(device_monitor); |
| + } |
| + } |
| + |
| + private: |
| + // DeviceMonitorWin::Observer |
| + void OnDeviceRemoved(const GUID& class_guid, |
| + const std::string& device_path) override { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + io_thread_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&SerialIoHandlerWin::OnDeviceRemoved, io_handler_, |
| + device_path)); |
| + } |
| + |
| + base::ThreadChecker thread_checker_; |
| + ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; |
| + |
| + // This weak pointer is only valid when checked on this task runner. |
| + base::WeakPtr<SerialIoHandlerWin> io_handler_; |
| + scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UiThreadHelper); |
| +}; |
| + |
| +void SerialIoHandlerWin::OnDeviceRemoved(const std::string& device_path) { |
| + DCHECK(CalledOnValidThread()); |
| + base::win::ScopedDeviceInfoList dev_info_list( |
| + SetupDiCreateDeviceInfoList(NULL, NULL)); |
| + if (!dev_info_list.IsValid()) { |
| + VPLOG(1) << "Failed to create a device information set"; |
| + return; |
| + } |
| + |
| + // This will add the device to |dev_info_list| so we can query driver info. |
| + if (!SetupDiOpenDeviceInterfaceA(dev_info_list.Get(), device_path.c_str(), 0, |
| + NULL)) { |
| + VPLOG(1) << "Failed to get device interface data for " << device_path; |
| + return; |
| + } |
| + |
| + base::win::ScopedDeviceInfo dev_info; |
| + if (!SetupDiEnumDeviceInfo(dev_info_list.Get(), 0, dev_info.get())) { |
| + VPLOG(1) << "Failed to get device info for " << device_path; |
| + return; |
| + } |
| + |
| + dev_info.set_valid(dev_info_list.Get()); |
| + |
| + BYTE friendly_name[512]; |
| + if (!SetupDiGetDeviceRegistryPropertyA( |
| + dev_info_list.Get(), dev_info.get(), SPDRP_FRIENDLYNAME, NULL, |
| + friendly_name, sizeof(friendly_name), NULL)) { |
| + VPLOG(1) << "Failed to get device service property"; |
| + return; |
| + } |
| + |
| + std::string port; |
| + if (!GetCOMPort(reinterpret_cast<const char*>(friendly_name), &port)) |
|
Reilly Grant (use Gerrit)
2015/11/12 19:30:02
VPLOG(1) << "Failed to get port name from \"" << f
juncai
2015/11/12 23:21:39
Done.
|
| + return; |
| + |
| + if (port_ == port) |
| + CancelRead(serial::RECEIVE_ERROR_DISCONNECTED); |
| +} |
| + |
| bool SerialIoHandlerWin::PostOpen() { |
| DCHECK(!comm_context_); |
| DCHECK(!read_context_); |
| @@ -159,6 +252,13 @@ bool SerialIoHandlerWin::PostOpen() { |
| write_context_->handler = this; |
| memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); |
| + scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner = |
| + base::ThreadTaskRunnerHandle::Get(); |
| + helper_ = |
| + new UiThreadHelper(weak_factory_.GetWeakPtr(), io_thread_task_runner); |
| + ui_thread_task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&UiThreadHelper::Start, helper_)); |
| + |
| // A ReadIntervalTimeout of MAXDWORD will cause async reads to complete |
| // immediately with any data that's available, even if there is none. |
| // This is OK because we never issue a read request until WaitCommEvent |
| @@ -271,10 +371,11 @@ SerialIoHandlerWin::SerialIoHandlerWin( |
| scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
| : SerialIoHandler(file_thread_task_runner, ui_thread_task_runner), |
| event_mask_(0), |
| - is_comm_pending_(false) { |
| -} |
| + is_comm_pending_(false), |
| + weak_factory_(this) {} |
| SerialIoHandlerWin::~SerialIoHandlerWin() { |
| + ui_thread_task_runner_->DeleteSoon(FROM_HERE, helper_); |
| } |
| void SerialIoHandlerWin::OnIOCompleted( |