| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 "device/hid/hid_connection_win.h" | 5 #include "device/hid/hid_connection_win.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 16 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
| 17 #include "components/device_event_log/device_event_log.h" | 16 #include "components/device_event_log/device_event_log.h" |
| 18 | 17 |
| 19 #define INITGUID | 18 #define INITGUID |
| 20 | 19 |
| 21 #include <windows.h> | 20 #include <windows.h> |
| 22 #include <hidclass.h> | 21 #include <hidclass.h> |
| 23 | 22 |
| 24 extern "C" { | 23 extern "C" { |
| 25 #include <hidsdi.h> | 24 #include <hidsdi.h> |
| 26 } | 25 } |
| 27 | 26 |
| 28 #include <setupapi.h> | 27 #include <setupapi.h> |
| 29 #include <winioctl.h> | 28 #include <winioctl.h> |
| 30 | 29 |
| 31 namespace device { | 30 namespace device { |
| 32 | 31 |
| 33 class PendingHidTransfer : public base::win::ObjectWatcher::Delegate, | 32 class PendingHidTransfer : public base::win::ObjectWatcher::Delegate { |
| 34 public base::MessageLoop::DestructionObserver { | |
| 35 public: | 33 public: |
| 36 typedef base::OnceCallback<void(PendingHidTransfer*, bool)> Callback; | 34 typedef base::OnceCallback<void(PendingHidTransfer*, bool)> Callback; |
| 37 | 35 |
| 38 PendingHidTransfer(scoped_refptr<net::IOBuffer> buffer, Callback callback); | 36 PendingHidTransfer(scoped_refptr<net::IOBuffer> buffer, Callback callback); |
| 39 ~PendingHidTransfer() override; | 37 ~PendingHidTransfer() override; |
| 40 | 38 |
| 41 void TakeResultFromWindowsAPI(BOOL result); | 39 void TakeResultFromWindowsAPI(BOOL result); |
| 42 | 40 |
| 43 OVERLAPPED* GetOverlapped() { return &overlapped_; } | 41 OVERLAPPED* GetOverlapped() { return &overlapped_; } |
| 44 | 42 |
| 45 // Implements base::win::ObjectWatcher::Delegate. | 43 // Implements base::win::ObjectWatcher::Delegate. |
| 46 void OnObjectSignaled(HANDLE object) override; | 44 void OnObjectSignaled(HANDLE object) override; |
| 47 | 45 |
| 48 // Implements base::MessageLoop::DestructionObserver | |
| 49 void WillDestroyCurrentMessageLoop() override; | |
| 50 | |
| 51 private: | 46 private: |
| 52 // The buffer isn't used by this object but it's important that a reference | 47 // The buffer isn't used by this object but it's important that a reference |
| 53 // to it is held until the transfer completes. | 48 // to it is held until the transfer completes. |
| 54 scoped_refptr<net::IOBuffer> buffer_; | 49 scoped_refptr<net::IOBuffer> buffer_; |
| 55 Callback callback_; | 50 Callback callback_; |
| 56 OVERLAPPED overlapped_; | 51 OVERLAPPED overlapped_; |
| 57 base::win::ScopedHandle event_; | 52 base::win::ScopedHandle event_; |
| 58 base::win::ObjectWatcher watcher_; | 53 base::win::ObjectWatcher watcher_; |
| 59 | 54 |
| 60 DISALLOW_COPY_AND_ASSIGN(PendingHidTransfer); | 55 DISALLOW_COPY_AND_ASSIGN(PendingHidTransfer); |
| 61 }; | 56 }; |
| 62 | 57 |
| 63 PendingHidTransfer::PendingHidTransfer(scoped_refptr<net::IOBuffer> buffer, | 58 PendingHidTransfer::PendingHidTransfer(scoped_refptr<net::IOBuffer> buffer, |
| 64 PendingHidTransfer::Callback callback) | 59 PendingHidTransfer::Callback callback) |
| 65 : buffer_(buffer), | 60 : buffer_(buffer), |
| 66 callback_(std::move(callback)), | 61 callback_(std::move(callback)), |
| 67 event_(CreateEvent(NULL, FALSE, FALSE, NULL)) { | 62 event_(CreateEvent(NULL, FALSE, FALSE, NULL)) { |
| 68 memset(&overlapped_, 0, sizeof(OVERLAPPED)); | 63 memset(&overlapped_, 0, sizeof(OVERLAPPED)); |
| 69 overlapped_.hEvent = event_.Get(); | 64 overlapped_.hEvent = event_.Get(); |
| 70 } | 65 } |
| 71 | 66 |
| 72 PendingHidTransfer::~PendingHidTransfer() { | 67 PendingHidTransfer::~PendingHidTransfer() { |
| 73 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
| 74 if (callback_) | 68 if (callback_) |
| 75 std::move(callback_).Run(this, false); | 69 std::move(callback_).Run(this, false); |
| 76 } | 70 } |
| 77 | 71 |
| 78 void PendingHidTransfer::TakeResultFromWindowsAPI(BOOL result) { | 72 void PendingHidTransfer::TakeResultFromWindowsAPI(BOOL result) { |
| 79 if (result) { | 73 if (result) { |
| 80 std::move(callback_).Run(this, true); | 74 std::move(callback_).Run(this, true); |
| 81 } else if (GetLastError() == ERROR_IO_PENDING) { | 75 } else if (GetLastError() == ERROR_IO_PENDING) { |
| 82 base::MessageLoop::current()->AddDestructionObserver(this); | |
| 83 watcher_.StartWatchingOnce(event_.Get(), this); | 76 watcher_.StartWatchingOnce(event_.Get(), this); |
| 84 } else { | 77 } else { |
| 85 HID_PLOG(EVENT) << "HID transfer failed"; | 78 HID_PLOG(EVENT) << "HID transfer failed"; |
| 86 std::move(callback_).Run(this, false); | 79 std::move(callback_).Run(this, false); |
| 87 } | 80 } |
| 88 } | 81 } |
| 89 | 82 |
| 90 void PendingHidTransfer::OnObjectSignaled(HANDLE event_handle) { | 83 void PendingHidTransfer::OnObjectSignaled(HANDLE event_handle) { |
| 91 std::move(callback_).Run(this, true); | 84 std::move(callback_).Run(this, true); |
| 92 } | 85 } |
| 93 | 86 |
| 94 void PendingHidTransfer::WillDestroyCurrentMessageLoop() { | |
| 95 watcher_.StopWatching(); | |
| 96 std::move(callback_).Run(this, false); | |
| 97 } | |
| 98 | |
| 99 HidConnectionWin::HidConnectionWin(scoped_refptr<HidDeviceInfo> device_info, | 87 HidConnectionWin::HidConnectionWin(scoped_refptr<HidDeviceInfo> device_info, |
| 100 base::win::ScopedHandle file) | 88 base::win::ScopedHandle file) |
| 101 : HidConnection(device_info), file_(std::move(file)) {} | 89 : HidConnection(std::move(device_info)), file_(std::move(file)) {} |
| 102 | 90 |
| 103 HidConnectionWin::~HidConnectionWin() { | 91 HidConnectionWin::~HidConnectionWin() { |
| 104 DCHECK(!file_.IsValid()); | 92 DCHECK(!file_.IsValid()); |
| 105 DCHECK(transfers_.empty()); | 93 DCHECK(transfers_.empty()); |
| 106 } | 94 } |
| 107 | 95 |
| 108 void HidConnectionWin::PlatformClose() { | 96 void HidConnectionWin::PlatformClose() { |
| 109 CancelIo(file_.Get()); | 97 CancelIo(file_.Get()); |
| 110 file_.Close(); | 98 file_.Close(); |
| 111 transfers_.clear(); | 99 transfers_.clear(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 [transfer](const std::unique_ptr<PendingHidTransfer>& this_transfer) { | 235 [transfer](const std::unique_ptr<PendingHidTransfer>& this_transfer) { |
| 248 return transfer == this_transfer.get(); | 236 return transfer == this_transfer.get(); |
| 249 }); | 237 }); |
| 250 DCHECK(it != transfers_.end()); | 238 DCHECK(it != transfers_.end()); |
| 251 std::unique_ptr<PendingHidTransfer> saved_transfer = std::move(*it); | 239 std::unique_ptr<PendingHidTransfer> saved_transfer = std::move(*it); |
| 252 transfers_.erase(it); | 240 transfers_.erase(it); |
| 253 return saved_transfer; | 241 return saved_transfer; |
| 254 } | 242 } |
| 255 | 243 |
| 256 } // namespace device | 244 } // namespace device |
| OLD | NEW |