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

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

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