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

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

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Addresses Wez's #17,18 comments. 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_;
74 KeyboardEventCounter counter_; 68 KeyboardEventCounter counter_;
75 69
76 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); 70 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore);
77 }; 71 };
78 72
79 class UserInputMonitorWin : public UserInputMonitor { 73 class UserInputMonitorWin : public UserInputMonitor {
80 public: 74 public:
81 explicit UserInputMonitorWin( 75 explicit UserInputMonitorWin(
82 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); 76 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
83 ~UserInputMonitorWin() override; 77 ~UserInputMonitorWin() override;
84 78
85 // Public UserInputMonitor overrides. 79 // Public UserInputMonitor overrides.
86 size_t GetKeyPressCount() const override; 80 size_t GetKeyPressCount() const override;
87 81
88 private: 82 private:
89 // Private UserInputMonitor overrides. 83 // Private UserInputMonitor overrides.
90 void StartKeyboardMonitoring() override; 84 void StartKeyboardMonitoring() override;
91 void StopKeyboardMonitoring() override; 85 void StopKeyboardMonitoring() override;
92 void StartMouseMonitoring() override;
93 void StopMouseMonitoring() override;
94 86
95 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 87 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
96 UserInputMonitorWinCore* core_; 88 UserInputMonitorWinCore* core_;
97 89
98 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin); 90 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin);
99 }; 91 };
100 92
101 UserInputMonitorWinCore::UserInputMonitorWinCore( 93 UserInputMonitorWinCore::UserInputMonitorWinCore(
102 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 94 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
103 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners) 95 : ui_task_runner_(ui_task_runner) {}
104 : ui_task_runner_(ui_task_runner),
105 mouse_listeners_(mouse_listeners),
106 events_monitored_(0) {}
107 96
108 UserInputMonitorWinCore::~UserInputMonitorWinCore() { 97 UserInputMonitorWinCore::~UserInputMonitorWinCore() {
109 DCHECK(!window_); 98 DCHECK(!window_);
110 DCHECK(!events_monitored_);
111 } 99 }
112 100
113 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() { 101 void UserInputMonitorWinCore::WillDestroyCurrentMessageLoop() {
114 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 102 DCHECK(ui_task_runner_->BelongsToCurrentThread());
115 StopMonitor(MOUSE_EVENT_MASK); 103 StopMonitor();
116 StopMonitor(KEYBOARD_EVENT_MASK);
117 } 104 }
118 105
119 size_t UserInputMonitorWinCore::GetKeyPressCount() const { 106 size_t UserInputMonitorWinCore::GetKeyPressCount() const {
120 return counter_.GetKeyPressCount(); 107 return counter_.GetKeyPressCount();
121 } 108 }
122 109
123 void UserInputMonitorWinCore::StartMonitor(EventBitMask type) { 110 void UserInputMonitorWinCore::StartMonitor() {
124 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 111 DCHECK(ui_task_runner_->BelongsToCurrentThread());
125 112
126 if (events_monitored_ & type) 113 if (window_)
127 return; 114 return;
128 115
129 if (type == KEYBOARD_EVENT_MASK) 116 counter_.Reset();
130 counter_.Reset();
131 117
132 if (!window_) { 118 std::unqiue_ptr<base::win::MessageWindow> window =
133 window_.reset(new base::win::MessageWindow()); 119 base::MakeUnique<base::win::MessageWindow>();
134 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, 120 if (!window->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage,
135 base::Unretained(this)))) { 121 base::Unretained(this)))) {
136 PLOG(ERROR) << "Failed to create the raw input window"; 122 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; 123 return;
149 } 124 }
150 125
126 // Register to receive raw keyboard input.
127 std::unique_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(RIDEV_INPUTSINK));
128 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
129 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK";
130 return;
131 }
132
133 window_(std::move(window));
Wez 2017/01/10 02:45:10 window_ = std::move(window) - I don't think this s
CJ 2017/01/10 02:55:02 Done.
151 // Start observing message loop destruction if we start monitoring the first 134 // Start observing message loop destruction if we start monitoring the first
152 // event. 135 // event.
153 if (!events_monitored_) 136 base::MessageLoop::current()->AddDestructionObserver(this);
154 base::MessageLoop::current()->AddDestructionObserver(this);
155
156 events_monitored_ |= type;
157 } 137 }
158 138
159 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { 139 void UserInputMonitorWinCore::StopMonitor() {
160 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 140 DCHECK(ui_task_runner_->BelongsToCurrentThread());
161 141
162 if (!(events_monitored_ & type)) 142 if (!window_)
163 return; 143 return;
164 144
165 // Stop receiving raw input. 145 // Stop receiving raw input.
166 DCHECK(window_); 146 std::unique_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(RIDEV_REMOVE));
167 std::unique_ptr<RAWINPUTDEVICE> device(
168 GetRawInputDevices(type, RIDEV_REMOVE));
169 147
170 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { 148 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) {
171 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; 149 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE";
172 } 150 }
173 151
174 events_monitored_ &= ~type; 152 window_ = nullptr;
175 if (events_monitored_ == 0) {
176 window_.reset();
177 153
178 // Stop observing message loop destruction if no event is being monitored. 154 // Stop observing message loop destruction if no event is being monitored.
179 base::MessageLoop::current()->RemoveDestructionObserver(this); 155 base::MessageLoop::current()->RemoveDestructionObserver(this);
180 }
181 } 156 }
182 157
158
183 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { 159 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) {
184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 160 DCHECK(ui_task_runner_->BelongsToCurrentThread());
185 161
186 // Get the size of the input record. 162 // Get the size of the input record.
187 UINT size = 0; 163 UINT size = 0;
188 UINT result = GetRawInputData( 164 UINT result = GetRawInputData(
189 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); 165 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
190 if (result == static_cast<UINT>(-1)) { 166 if (result == static_cast<UINT>(-1)) {
191 PLOG(ERROR) << "GetRawInputData() failed"; 167 PLOG(ERROR) << "GetRawInputData() failed";
192 return 0; 168 return 0;
193 } 169 }
194 DCHECK_EQ(0u, result); 170 DCHECK_EQ(0u, result);
195 171
196 // Retrieve the input record itself. 172 // Retrieve the input record itself.
197 std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]); 173 std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]);
198 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); 174 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get());
199 result = GetRawInputData( 175 result = GetRawInputData(
200 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); 176 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER));
201 if (result == static_cast<UINT>(-1)) { 177 if (result == static_cast<UINT>(-1)) {
202 PLOG(ERROR) << "GetRawInputData() failed"; 178 PLOG(ERROR) << "GetRawInputData() failed";
203 return 0; 179 return 0;
204 } 180 }
205 DCHECK_EQ(size, result); 181 DCHECK_EQ(size, result);
206 182
207 // Notify the observer about events generated locally. 183 // Notify the observer about events generated locally.
208 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { 184 if (input->header.dwType == RIM_TYPEKEYBOARD &&
209 POINT position; 185 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 = 186 ui::KeyboardCode key_code =
223 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey); 187 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey);
224 counter_.OnKeyboardEvent(event, key_code); 188 counter_.OnKeyboardEvent(!(input->data.keyboard.Flags & RI_KEY_BREAK),
189 key_code);
225 } 190 }
226 191
227 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER)); 192 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER));
228 } 193 }
229 194
230 bool UserInputMonitorWinCore::HandleMessage(UINT message, 195 bool UserInputMonitorWinCore::HandleMessage(UINT message,
231 WPARAM wparam, 196 WPARAM wparam,
232 LPARAM lparam, 197 LPARAM lparam,
233 LRESULT* result) { 198 LRESULT* result) {
234 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 199 DCHECK(ui_task_runner_->BelongsToCurrentThread());
235 200
236 switch (message) { 201 switch (message) {
237 case WM_INPUT: 202 case WM_INPUT:
238 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam)); 203 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam));
239 return true; 204 return true;
240 205
241 default: 206 default:
242 return false; 207 return false;
243 } 208 }
244 } 209 }
245 210
246 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event, 211 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(DWORD flags) {
247 DWORD flags) {
248 DCHECK(ui_task_runner_->BelongsToCurrentThread()); 212 DCHECK(ui_task_runner_->BelongsToCurrentThread());
249 213
250 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE()); 214 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE());
251 if (event == MOUSE_EVENT_MASK) { 215 device->dwFlags = flags;
252 device->dwFlags = flags; 216 device->usUsagePage = kGenericDesktopPage;
253 device->usUsagePage = kGenericDesktopPage; 217 device->usUsage = kKeyboardUsage;
254 device->usUsage = kMouseUsage; 218 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(); 219 return device.release();
264 } 220 }
265 221
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,
288 core_->AsWeakPtr(), 244 core_->AsWeakPtr()));
289 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK));
290 } 245 }
291 246
292 void UserInputMonitorWin::StopKeyboardMonitoring() { 247 void UserInputMonitorWin::StopKeyboardMonitoring() {
293 ui_task_runner_->PostTask( 248 ui_task_runner_->PostTask(
294 FROM_HERE, 249 FROM_HERE,
295 base::Bind(&UserInputMonitorWinCore::StopMonitor, 250 base::Bind(&UserInputMonitorWinCore::StopMonitor,
296 core_->AsWeakPtr(), 251 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 } 252 }
315 253
316 } // namespace 254 } // namespace
317 255
318 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 256 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 257 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
320 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 258 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
321 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner)); 259 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner));
322 } 260 }
323 261
324 } // namespace media 262 } // 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