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

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

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Updates win's Start/Stop Monitor. 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
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
32 // This is the actual implementation of event monitoring. It's separated from 31 // This is the actual implementation of event monitoring. It's separated from
33 // UserInputMonitorWin since it needs to be deleted on the UI thread. 32 // UserInputMonitorWin since it needs to be deleted on the UI thread.
34 class UserInputMonitorWinCore 33 class UserInputMonitorWinCore
35 : public base::SupportsWeakPtr<UserInputMonitorWinCore>, 34 : public base::SupportsWeakPtr<UserInputMonitorWinCore>,
36 public base::MessageLoop::DestructionObserver { 35 public base::MessageLoop::DestructionObserver {
37 public: 36 public:
38 enum EventBitMask { 37 enum EventBitMask {
39 MOUSE_EVENT_MASK = 1, 38 MOUSE_EVENT_MASK = 1,
40 KEYBOARD_EVENT_MASK = 2, 39 KEYBOARD_EVENT_MASK = 2,
41 }; 40 };
42 41
43 explicit UserInputMonitorWinCore( 42 explicit UserInputMonitorWinCore(
44 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
45 const scoped_refptr<UserInputMonitor::MouseListenerList>&
46 mouse_listeners);
47 ~UserInputMonitorWinCore() override; 44 ~UserInputMonitorWinCore() override;
48 45
49 // DestructionObserver overrides. 46 // DestructionObserver overrides.
50 void WillDestroyCurrentMessageLoop() override; 47 void WillDestroyCurrentMessageLoop() override;
51 48
52 size_t GetKeyPressCount() const; 49 size_t GetKeyPressCount() const;
53 void StartMonitor(EventBitMask type); 50 void StartMonitor();
54 void StopMonitor(EventBitMask type); 51 void StopMonitor();
55 52
56 private: 53 private:
57 // Handles WM_INPUT messages. 54 // Handles WM_INPUT messages.
58 LRESULT OnInput(HRAWINPUT input_handle); 55 LRESULT OnInput(HRAWINPUT input_handle);
59 // Handles messages received by |window_|. 56 // Handles messages received by |window_|.
60 bool HandleMessage(UINT message, 57 bool HandleMessage(UINT message,
61 WPARAM wparam, 58 WPARAM wparam,
62 LPARAM lparam, 59 LPARAM lparam,
63 LRESULT* result); 60 LRESULT* result);
64 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags); 61 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags);
65 62
66 // Task runner on which |window_| is created. 63 // Task runner on which |window_| is created.
67 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 64 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
68 scoped_refptr<base::ObserverListThreadSafe<
69 UserInputMonitor::MouseEventListener>> mouse_listeners_;
70 65
71 // These members are only accessed on the UI thread. 66 // These members are only accessed on the UI thread.
72 std::unique_ptr<base::win::MessageWindow> window_; 67 std::unique_ptr<base::win::MessageWindow> window_;
73 uint8_t events_monitored_; 68 bool events_monitored_;
74 KeyboardEventCounter counter_; 69 KeyboardEventCounter counter_;
75 70
76 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); 71 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore);
77 }; 72 };
78 73
79 class UserInputMonitorWin : public UserInputMonitor { 74 class UserInputMonitorWin : public UserInputMonitor {
80 public: 75 public:
81 explicit UserInputMonitorWin( 76 explicit UserInputMonitorWin(
82 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); 77 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
83 ~UserInputMonitorWin() override; 78 ~UserInputMonitorWin() override;
84 79
85 // Public UserInputMonitor overrides. 80 // Public UserInputMonitor overrides.
86 size_t GetKeyPressCount() const override; 81 size_t GetKeyPressCount() const override;
87 82
88 private: 83 private:
89 // Private UserInputMonitor overrides. 84 // Private UserInputMonitor overrides.
90 void StartKeyboardMonitoring() override; 85 void StartKeyboardMonitoring() override;
91 void StopKeyboardMonitoring() override; 86 void StopKeyboardMonitoring() override;
92 void StartMouseMonitoring() override;
93 void StopMouseMonitoring() override;
94 87
95 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 88 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
96 UserInputMonitorWinCore* core_; 89 UserInputMonitorWinCore* core_;
97 90
98 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin); 91 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin);
99 }; 92 };
100 93
101 UserInputMonitorWinCore::UserInputMonitorWinCore( 94 UserInputMonitorWinCore::UserInputMonitorWinCore(
102 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 95 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
103 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners) 96 : ui_task_runner_(ui_task_runner), events_monitored_(0) {}
104 : ui_task_runner_(ui_task_runner),
105 mouse_listeners_(mouse_listeners),
106 events_monitored_(0) {}
107 97
108 UserInputMonitorWinCore::~UserInputMonitorWinCore() { 98 UserInputMonitorWinCore::~UserInputMonitorWinCore() {
109 DCHECK(!window_); 99 DCHECK(!window_);
110 DCHECK(!events_monitored_); 100 DCHECK(!events_monitored_);
111 } 101 }
112 102
113 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() { 103 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() {
114 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 104 DCHECK(ui_task_runner_->BelongsToCurrentThread());
115 StopMonitor(MOUSE_EVENT_MASK); 105 StopMonitor();
116 StopMonitor(KEYBOARD_EVENT_MASK);
117 } 106 }
118 107
119 size_t UserInputMonitorWinCore::GetKeyPressCount() const { 108 size_t UserInputMonitorWinCore::GetKeyPressCount() const {
120 return counter_.GetKeyPressCount(); 109 return counter_.GetKeyPressCount();
121 } 110 }
122 111
123 void UserInputMonitorWinCore::StartMonitor(EventBitMask type) { 112 void UserInputMonitorWinCore::StartMonitor() {
124 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 113 DCHECK(ui_task_runner_->BelongsToCurrentThread());
125 114
126 if (events_monitored_ & type) 115 if (events_monitored_)
127 return; 116 return;
128 117
129 if (type == KEYBOARD_EVENT_MASK) 118 counter_.Reset();
130 counter_.Reset();
131 119
132 if (!window_) { 120 if (!window_) {
133 window_.reset(new base::win::MessageWindow()); 121 window_.reset(new base::win::MessageWindow());
134 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, 122 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage,
135 base::Unretained(this)))) { 123 base::Unretained(this)))) {
136 PLOG(ERROR) << "Failed to create the raw input window"; 124 PLOG(ERROR) << "Failed to create the raw input window";
137 window_.reset(); 125 window_.reset();
138 return; 126 return;
139 } 127 }
140 } 128 }
141 129
142 // Register to receive raw mouse and/or keyboard input. 130 // Register to receive raw keyboard input.
143 std::unique_ptr<RAWINPUTDEVICE> device( 131 std::unique_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(RIDEV_INPUTSINK));
144 GetRawInputDevices(type, RIDEV_INPUTSINK));
145 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { 132 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
146 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; 133 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK";
147 window_.reset(); 134 window_.reset();
148 return; 135 return;
149 } 136 }
150 137
151 // Start observing message loop destruction if we start monitoring the first 138 // Start observing message loop destruction if we start monitoring the first
152 // event. 139 // event.
153 if (!events_monitored_) 140 if (!events_monitored_)
154 base::MessageLoop::current()->AddDestructionObserver(this); 141 base::MessageLoop::current()->AddDestructionObserver(this);
155 142
156 events_monitored_ |= type; 143 events_monitored_ = true;
157 } 144 }
158 145
159 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { 146 void UserInputMonitorWinCore::StopMonitor() {
160 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 147 DCHECK(ui_task_runner_->BelongsToCurrentThread());
161 148
162 if (!(events_monitored_ & type)) 149 if (!events_monitored_)
163 return; 150 return;
164 151
165 // Stop receiving raw input. 152 // Stop receiving raw input.
166 DCHECK(window_); 153 DCHECK(window_);
167 std::unique_ptr<RAWINPUTDEVICE> device( 154 std::unique_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(RIDEV_REMOVE));
168 GetRawInputDevices(type, RIDEV_REMOVE));
169 155
170 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { 156 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
171 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; 157 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE";
172 } 158 }
173 159
174 events_monitored_ &= ~type; 160 events_monitored_ = false;
175 if (events_monitored_ == 0) { 161 if (events_monitored_ == 0) {
Wez 2017/01/09 22:38:44 No need for this check, any more. However, note t
CJ 2017/01/09 23:35:49 Done.
176 window_.reset(); 162 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 } 166 }
181 } 167 }
182 168
183 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { 169 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) {
184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 170 DCHECK(ui_task_runner_->BelongsToCurrentThread());
185 171
(...skipping 12 matching lines...) Expand all
198 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); 184 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get());
199 result = GetRawInputData( 185 result = GetRawInputData(
200 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); 186 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER));
201 if (result == static_cast<UINT>(-1)) { 187 if (result == static_cast<UINT>(-1)) {
202 PLOG(ERROR) << "GetRawInputData() failed"; 188 PLOG(ERROR) << "GetRawInputData() failed";
203 return 0; 189 return 0;
204 } 190 }
205 DCHECK_EQ(size, result); 191 DCHECK_EQ(size, result);
206 192
207 // Notify the observer about events generated locally. 193 // Notify the observer about events generated locally.
208 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { 194 if (input->header.dwType == RIM_TYPEKEYBOARD &&
209 POINT position; 195 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)
220 ? ui::ET_KEY_RELEASED
221 : ui::ET_KEY_PRESSED;
222 ui::KeyboardCode key_code = 196 ui::KeyboardCode key_code =
223 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey); 197 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey);
224 counter_.OnKeyboardEvent(event, key_code); 198 counter_.OnKeyboardEvent(!(input->data.keyboard.Flags & RI_KEY_BREAK),
199 key_code);
225 } 200 }
226 201
227 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER)); 202 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER));
228 } 203 }
229 204
230 bool UserInputMonitorWinCore::HandleMessage(UINT message, 205 bool UserInputMonitorWinCore::HandleMessage(UINT message,
231 WPARAM wparam, 206 WPARAM wparam,
232 LPARAM lparam, 207 LPARAM lparam,
233 LRESULT* result) { 208 LRESULT* result) {
234 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 209 DCHECK(ui_task_runner_->BelongsToCurrentThread());
235 210
236 switch (message) { 211 switch (message) {
237 case WM_INPUT: 212 case WM_INPUT:
238 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam)); 213 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam));
239 return true; 214 return true;
240 215
241 default: 216 default:
242 return false; 217 return false;
243 } 218 }
244 } 219 }
245 220
246 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event, 221 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(DWORD flags) {
247 DWORD flags) {
248 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 222 DCHECK(ui_task_runner_->BelongsToCurrentThread());
249 223
250 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE()); 224 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE());
251 if (event == MOUSE_EVENT_MASK) { 225 device->dwFlags = flags;
252 device->dwFlags = flags; 226 device->usUsagePage = kGenericDesktopPage;
253 device->usUsagePage = kGenericDesktopPage; 227 device->usUsage = kKeyboardUsage;
254 device->usUsage = kMouseUsage; 228 device->hwndTarget = window_->hwnd();
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(); 229 return device.release();
264 } 230 }
265 231
266 // 232 //
267 // Implementation of UserInputMonitorWin. 233 // Implementation of UserInputMonitorWin.
268 // 234 //
269 235
270 UserInputMonitorWin::UserInputMonitorWin( 236 UserInputMonitorWin::UserInputMonitorWin(
271 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) 237 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
272 : ui_task_runner_(ui_task_runner), 238 : ui_task_runner_(ui_task_runner),
273 core_(new UserInputMonitorWinCore(ui_task_runner, mouse_listeners())) {} 239 core_(new UserInputMonitorWinCore(ui_task_runner)) {}
274 240
275 UserInputMonitorWin::~UserInputMonitorWin() { 241 UserInputMonitorWin::~UserInputMonitorWin() {
276 if (!ui_task_runner_->DeleteSoon(FROM_HERE, core_)) 242 if (!ui_task_runner_->DeleteSoon(FROM_HERE, core_))
277 delete core_; 243 delete core_;
278 } 244 }
279 245
280 size_t UserInputMonitorWin::GetKeyPressCount() const { 246 size_t UserInputMonitorWin::GetKeyPressCount() const {
281 return core_->GetKeyPressCount(); 247 return core_->GetKeyPressCount();
282 } 248 }
283 249
284 void UserInputMonitorWin::StartKeyboardMonitoring() { 250 void UserInputMonitorWin::StartKeyboardMonitoring() {
285 ui_task_runner_->PostTask( 251 ui_task_runner_->PostTask(
286 FROM_HERE, 252 FROM_HERE,
287 base::Bind(&UserInputMonitorWinCore::StartMonitor, 253 base::Bind(&UserInputMonitorWinCore::StartMonitor,
288 core_->AsWeakPtr(), 254 core_->AsWeakPtr()));
289 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK));
290 } 255 }
291 256
292 void UserInputMonitorWin::StopKeyboardMonitoring() { 257 void UserInputMonitorWin::StopKeyboardMonitoring() {
293 ui_task_runner_->PostTask( 258 ui_task_runner_->PostTask(
294 FROM_HERE, 259 FROM_HERE,
295 base::Bind(&UserInputMonitorWinCore::StopMonitor, 260 base::Bind(&UserInputMonitorWinCore::StopMonitor,
296 core_->AsWeakPtr(), 261 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 } 262 }
315 263
316 } // namespace 264 } // namespace
317 265
318 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 266 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 267 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
320 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 268 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
321 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner)); 269 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner));
322 } 270 }
323 271
324 } // namespace media 272 } // namespace media
OLDNEW
« media/base/user_input_monitor_unittest.cc ('K') | « 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