Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: media/base/user_input_monitor_win.cc

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Fixes GetRawDevice. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/user_input_monitor_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "media/base/user_input_monitor.h" 5 #include "media/base/user_input_monitor.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/win/message_window.h" 19 #include "base/win/message_window.h"
20 #include "media/base/keyboard_event_counter.h" 20 #include "media/base/keyboard_event_counter.h"
21 #include "third_party/skia/include/core/SkPoint.h" 21 #include "third_party/skia/include/core/SkPoint.h"
22 #include "ui/events/keycodes/keyboard_code_conversion_win.h" 22 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
23 23
24 namespace media { 24 namespace media {
25 namespace { 25 namespace {
26 26
27 // From the HID Usage Tables specification. 27 // From the HID Usage Tables specification.
28 const USHORT kGenericDesktopPage = 1; 28 const USHORT kGenericDesktopPage = 1;
29 const USHORT kMouseUsage = 2;
30 const USHORT kKeyboardUsage = 6; 29 const USHORT kKeyboardUsage = 6;
31 30
31 std::unique_ptr<RAWINPUTDEVICE> GetRawInputDevices(HWND hwnd, DWORD flags) {
32 DCHECK(ui_task_runner_->BelongsToCurrentThread());
Wez 2017/01/19 01:59:56 Remove this - you're no longer in a class member,
CJ 2017/01/20 23:34:46 Done.
33
34 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE());
35 device->dwFlags = flags;
36 device->usUsagePage = kGenericDesktopPage;
37 device->usUsage = kKeyboardUsage;
38 device->hwndTarget = hwnd;
39 return std::move(device);
40 }
41
32 // This is the actual implementation of event monitoring. It's separated from 42 // This is the actual implementation of event monitoring. It's separated from
33 // UserInputMonitorWin since it needs to be deleted on the UI thread. 43 // UserInputMonitorWin since it needs to be deleted on the UI thread.
34 class UserInputMonitorWinCore 44 class UserInputMonitorWinCore
35 : public base::SupportsWeakPtr<UserInputMonitorWinCore>, 45 : public base::SupportsWeakPtr<UserInputMonitorWinCore>,
36 public base::MessageLoop::DestructionObserver { 46 public base::MessageLoop::DestructionObserver {
37 public: 47 public:
38 enum EventBitMask { 48 enum EventBitMask {
39 MOUSE_EVENT_MASK = 1, 49 MOUSE_EVENT_MASK = 1,
40 KEYBOARD_EVENT_MASK = 2, 50 KEYBOARD_EVENT_MASK = 2,
41 }; 51 };
42 52
43 explicit UserInputMonitorWinCore( 53 explicit UserInputMonitorWinCore(
44 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 54 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
45 const scoped_refptr<UserInputMonitor::MouseListenerList>&
46 mouse_listeners);
47 ~UserInputMonitorWinCore() override; 55 ~UserInputMonitorWinCore() override;
48 56
49 // DestructionObserver overrides. 57 // DestructionObserver overrides.
50 void WillDestroyCurrentMessageLoop() override; 58 void WillDestroyCurrentMessageLoop() override;
51 59
52 size_t GetKeyPressCount() const; 60 size_t GetKeyPressCount() const;
53 void StartMonitor(EventBitMask type); 61 void StartMonitor();
54 void StopMonitor(EventBitMask type); 62 void StopMonitor();
55 63
56 private: 64 private:
57 // Handles WM_INPUT messages. 65 // Handles WM_INPUT messages.
58 LRESULT OnInput(HRAWINPUT input_handle); 66 LRESULT OnInput(HRAWINPUT input_handle);
59 // Handles messages received by |window_|. 67 // Handles messages received by |window_|.
60 bool HandleMessage(UINT message, 68 bool HandleMessage(UINT message,
61 WPARAM wparam, 69 WPARAM wparam,
62 LPARAM lparam, 70 LPARAM lparam,
63 LRESULT* result); 71 LRESULT* result);
64 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags);
65 72
66 // Task runner on which |window_| is created. 73 // Task runner on which |window_| is created.
67 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 74 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
68 scoped_refptr<base::ObserverListThreadSafe<
69 UserInputMonitor::MouseEventListener>> mouse_listeners_;
70 75
71 // These members are only accessed on the UI thread. 76 // These members are only accessed on the UI thread.
72 std::unique_ptr<base::win::MessageWindow> window_; 77 std::unique_ptr<base::win::MessageWindow> window_;
73 uint8_t events_monitored_;
74 KeyboardEventCounter counter_; 78 KeyboardEventCounter counter_;
75 79
76 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); 80 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore);
77 }; 81 };
78 82
79 class UserInputMonitorWin : public UserInputMonitor { 83 class UserInputMonitorWin : public UserInputMonitor {
80 public: 84 public:
81 explicit UserInputMonitorWin( 85 explicit UserInputMonitorWin(
82 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); 86 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
83 ~UserInputMonitorWin() override; 87 ~UserInputMonitorWin() override;
84 88
85 // Public UserInputMonitor overrides. 89 // Public UserInputMonitor overrides.
86 size_t GetKeyPressCount() const override; 90 size_t GetKeyPressCount() const override;
87 91
88 private: 92 private:
89 // Private UserInputMonitor overrides. 93 // Private UserInputMonitor overrides.
90 void StartKeyboardMonitoring() override; 94 void StartKeyboardMonitoring() override;
91 void StopKeyboardMonitoring() override; 95 void StopKeyboardMonitoring() override;
92 void StartMouseMonitoring() override;
93 void StopMouseMonitoring() override;
94 96
95 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 97 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
96 UserInputMonitorWinCore* core_; 98 UserInputMonitorWinCore* core_;
97 99
98 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin); 100 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin);
99 }; 101 };
100 102
101 UserInputMonitorWinCore::UserInputMonitorWinCore( 103 UserInputMonitorWinCore::UserInputMonitorWinCore(
102 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 104 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
103 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners) 105 : ui_task_runner_(ui_task_runner) {}
104 : ui_task_runner_(ui_task_runner),
105 mouse_listeners_(mouse_listeners),
106 events_monitored_(0) {}
107 106
108 UserInputMonitorWinCore::~UserInputMonitorWinCore() { 107 UserInputMonitorWinCore::~UserInputMonitorWinCore() {
109 DCHECK(!window_); 108 DCHECK(!window_);
110 DCHECK(!events_monitored_);
111 } 109 }
112 110
113 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() { 111 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() {
114 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 112 DCHECK(ui_task_runner_->BelongsToCurrentThread());
115 StopMonitor(MOUSE_EVENT_MASK); 113 StopMonitor();
116 StopMonitor(KEYBOARD_EVENT_MASK);
117 } 114 }
118 115
119 size_t UserInputMonitorWinCore::GetKeyPressCount() const { 116 size_t UserInputMonitorWinCore::GetKeyPressCount() const {
120 return counter_.GetKeyPressCount(); 117 return counter_.GetKeyPressCount();
121 } 118 }
122 119
123 void UserInputMonitorWinCore::StartMonitor(EventBitMask type) { 120 void UserInputMonitorWinCore::StartMonitor() {
124 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 121 DCHECK(ui_task_runner_->BelongsToCurrentThread());
125 122
126 if (events_monitored_ & type) 123 if (window_)
127 return; 124 return;
128 125
129 if (type == KEYBOARD_EVENT_MASK) 126 std::unique_ptr<base::win::MessageWindow> window =
130 counter_.Reset(); 127 base::MakeUnique<base::win::MessageWindow>();
131 128 if (!window->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage,
132 if (!window_) { 129 base::Unretained(this)))) {
133 window_.reset(new base::win::MessageWindow()); 130 PLOG(ERROR) << "Failed to create the raw input window";
134 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage,
135 base::Unretained(this)))) {
136 PLOG(ERROR) << "Failed to create the raw input window";
137 window_.reset();
138 return;
139 }
140 }
141
142 // Register to receive raw mouse and/or keyboard input.
143 std::unique_ptr<RAWINPUTDEVICE> device(
144 GetRawInputDevices(type, RIDEV_INPUTSINK));
145 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
146 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK";
147 window_.reset();
148 return; 131 return;
149 } 132 }
150 133
134 // Register to receive raw keyboard input.
135 std::unique_ptr<RAWINPUTDEVICE> device(
136 GetRawInputDevices(window->hwnd(), RIDEV_INPUTSINK));
137 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
138 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK";
139 return;
140 }
141
142 window_ = std::move(window);
151 // Start observing message loop destruction if we start monitoring the first 143 // Start observing message loop destruction if we start monitoring the first
152 // event. 144 // event.
153 if (!events_monitored_) 145 base::MessageLoop::current()->AddDestructionObserver(this);
154 base::MessageLoop::current()->AddDestructionObserver(this);
155
156 events_monitored_ |= type;
157 } 146 }
158 147
159 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { 148 void UserInputMonitorWinCore::StopMonitor() {
160 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 149 DCHECK(ui_task_runner_->BelongsToCurrentThread());
161 150
162 if (!(events_monitored_ & type)) 151 if (!window_)
163 return; 152 return;
164 153
165 // Stop receiving raw input. 154 // Stop receiving raw input.
166 DCHECK(window_); 155 std::unique_ptr<RAWINPUTDEVICE> device(window_->hwnd(),
167 std::unique_ptr<RAWINPUTDEVICE> device( 156 GetRawInputDevices(RIDEV_REMOVE));
Wez 2017/01/19 01:59:56 You put the window_->hwnd() before GetRawInputDevi
CJ 2017/01/20 23:34:46 Done.
168 GetRawInputDevices(type, RIDEV_REMOVE));
169 157
170 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { 158 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
171 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; 159 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE";
172 } 160 }
173 161
174 events_monitored_ &= ~type; 162 window_ = nullptr;
175 if (events_monitored_ == 0) {
176 window_.reset();
177 163
178 // Stop observing message loop destruction if no event is being monitored. 164 // Stop observing message loop destruction if no event is being monitored.
179 base::MessageLoop::current()->RemoveDestructionObserver(this); 165 base::MessageLoop::current()->RemoveDestructionObserver(this);
180 }
181 } 166 }
182 167
183 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { 168 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) {
184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 169 DCHECK(ui_task_runner_->BelongsToCurrentThread());
185 170
186 // Get the size of the input record. 171 // Get the size of the input record.
187 UINT size = 0; 172 UINT size = 0;
188 UINT result = GetRawInputData( 173 UINT result = GetRawInputData(
189 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); 174 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
190 if (result == static_cast<UINT>(-1)) { 175 if (result == static_cast<UINT>(-1)) {
191 PLOG(ERROR) << "GetRawInputData() failed"; 176 PLOG(ERROR) << "GetRawInputData() failed";
192 return 0; 177 return 0;
193 } 178 }
194 DCHECK_EQ(0u, result); 179 DCHECK_EQ(0u, result);
195 180
196 // Retrieve the input record itself. 181 // Retrieve the input record itself.
197 std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]); 182 std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]);
198 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); 183 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get());
199 result = GetRawInputData( 184 result = GetRawInputData(
200 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); 185 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER));
201 if (result == static_cast<UINT>(-1)) { 186 if (result == static_cast<UINT>(-1)) {
202 PLOG(ERROR) << "GetRawInputData() failed"; 187 PLOG(ERROR) << "GetRawInputData() failed";
203 return 0; 188 return 0;
204 } 189 }
205 DCHECK_EQ(size, result); 190 DCHECK_EQ(size, result);
206 191
207 // Notify the observer about events generated locally. 192 // Notify the observer about events generated locally.
208 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { 193 if (input->header.dwType == RIM_TYPEKEYBOARD &&
209 POINT position; 194 input->header.hDevice != NULL) {
210 if (!GetCursorPos(&position)) {
211 position.x = 0;
212 position.y = 0;
213 }
214 mouse_listeners_->Notify(
215 FROM_HERE, &UserInputMonitor::MouseEventListener::OnMouseMoved,
216 SkIPoint::Make(position.x, position.y));
217 } else if (input->header.dwType == RIM_TYPEKEYBOARD &&
218 input->header.hDevice != NULL) {
219 ui::EventType event = (input->data.keyboard.Flags & RI_KEY_BREAK) 195 ui::EventType event = (input->data.keyboard.Flags & RI_KEY_BREAK)
220 ? ui::ET_KEY_RELEASED 196 ? ui::ET_KEY_RELEASED
221 : ui::ET_KEY_PRESSED; 197 : ui::ET_KEY_PRESSED;
222 ui::KeyboardCode key_code = 198 ui::KeyboardCode key_code =
223 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey); 199 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey);
224 counter_.OnKeyboardEvent(event, key_code); 200 counter_.OnKeyboardEvent(event, key_code);
225 } 201 }
226 202
227 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER)); 203 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER));
228 } 204 }
229 205
230 bool UserInputMonitorWinCore::HandleMessage(UINT message, 206 bool UserInputMonitorWinCore::HandleMessage(UINT message,
231 WPARAM wparam, 207 WPARAM wparam,
232 LPARAM lparam, 208 LPARAM lparam,
233 LRESULT* result) { 209 LRESULT* result) {
234 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 210 DCHECK(ui_task_runner_->BelongsToCurrentThread());
235 211
236 switch (message) { 212 switch (message) {
237 case WM_INPUT: 213 case WM_INPUT:
238 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam)); 214 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam));
239 return true; 215 return true;
240 216
241 default: 217 default:
242 return false; 218 return false;
243 } 219 }
244 } 220 }
245 221
246 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event,
247 DWORD flags) {
248 DCHECK(ui_task_runner_->BelongsToCurrentThread());
249
250 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE());
251 if (event == MOUSE_EVENT_MASK) {
252 device->dwFlags = flags;
253 device->usUsagePage = kGenericDesktopPage;
254 device->usUsage = kMouseUsage;
255 device->hwndTarget = window_->hwnd();
256 } else {
257 DCHECK_EQ(KEYBOARD_EVENT_MASK, event);
258 device->dwFlags = flags;
259 device->usUsagePage = kGenericDesktopPage;
260 device->usUsage = kKeyboardUsage;
261 device->hwndTarget = window_->hwnd();
262 }
263 return device.release();
264 }
265
266 // 222 //
267 // Implementation of UserInputMonitorWin. 223 // Implementation of UserInputMonitorWin.
268 // 224 //
269 225
270 UserInputMonitorWin::UserInputMonitorWin( 226 UserInputMonitorWin::UserInputMonitorWin(
271 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) 227 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
272 : ui_task_runner_(ui_task_runner), 228 : ui_task_runner_(ui_task_runner),
273 core_(new UserInputMonitorWinCore(ui_task_runner, mouse_listeners())) {} 229 core_(new UserInputMonitorWinCore(ui_task_runner)) {}
274 230
275 UserInputMonitorWin::~UserInputMonitorWin() { 231 UserInputMonitorWin::~UserInputMonitorWin() {
276 if (!ui_task_runner_->DeleteSoon(FROM_HERE, core_)) 232 if (!ui_task_runner_->DeleteSoon(FROM_HERE, core_))
277 delete core_; 233 delete core_;
278 } 234 }
279 235
280 size_t UserInputMonitorWin::GetKeyPressCount() const { 236 size_t UserInputMonitorWin::GetKeyPressCount() const {
281 return core_->GetKeyPressCount(); 237 return core_->GetKeyPressCount();
282 } 238 }
283 239
284 void UserInputMonitorWin::StartKeyboardMonitoring() { 240 void UserInputMonitorWin::StartKeyboardMonitoring() {
285 ui_task_runner_->PostTask( 241 ui_task_runner_->PostTask(
286 FROM_HERE, 242 FROM_HERE,
287 base::Bind(&UserInputMonitorWinCore::StartMonitor, 243 base::Bind(&UserInputMonitorWinCore::StartMonitor, core_->AsWeakPtr()));
288 core_->AsWeakPtr(),
289 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK));
290 } 244 }
291 245
292 void UserInputMonitorWin::StopKeyboardMonitoring() { 246 void UserInputMonitorWin::StopKeyboardMonitoring() {
293 ui_task_runner_->PostTask( 247 ui_task_runner_->PostTask(
294 FROM_HERE, 248 FROM_HERE,
295 base::Bind(&UserInputMonitorWinCore::StopMonitor, 249 base::Bind(&UserInputMonitorWinCore::StopMonitor, core_->AsWeakPtr()));
296 core_->AsWeakPtr(),
297 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK));
298 }
299
300 void UserInputMonitorWin::StartMouseMonitoring() {
301 ui_task_runner_->PostTask(
302 FROM_HERE,
303 base::Bind(&UserInputMonitorWinCore::StartMonitor,
304 core_->AsWeakPtr(),
305 UserInputMonitorWinCore::MOUSE_EVENT_MASK));
306 }
307
308 void UserInputMonitorWin::StopMouseMonitoring() {
309 ui_task_runner_->PostTask(
310 FROM_HERE,
311 base::Bind(&UserInputMonitorWinCore::StopMonitor,
312 core_->AsWeakPtr(),
313 UserInputMonitorWinCore::MOUSE_EVENT_MASK));
314 } 250 }
315 251
316 } // namespace 252 } // namespace
317 253
318 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 254 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 255 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
320 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 256 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
321 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner)); 257 return base::MakeUnique<UserInputMonitorWin>(ui_task_runner);
322 } 258 }
323 259
324 } // namespace media 260 } // namespace media
OLDNEW
« no previous file with comments | « media/base/user_input_monitor_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698