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

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

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Addresses Wez's #7 comments. Created 3 years, 12 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 <sys/select.h> 8 #include <sys/select.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #define XK_MISCELLANY 10 #define XK_MISCELLANY
(...skipping 23 matching lines...) Expand all
34 34
35 namespace media { 35 namespace media {
36 namespace { 36 namespace {
37 37
38 // This is the actual implementation of event monitoring. It's separated from 38 // This is the actual implementation of event monitoring. It's separated from
39 // UserInputMonitorLinux since it needs to be deleted on the IO thread. 39 // UserInputMonitorLinux since it needs to be deleted on the IO thread.
40 class UserInputMonitorLinuxCore 40 class UserInputMonitorLinuxCore
41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>, 41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>,
42 public base::MessageLoop::DestructionObserver { 42 public base::MessageLoop::DestructionObserver {
43 public: 43 public:
44 enum EventType {
45 MOUSE_EVENT,
46 KEYBOARD_EVENT
47 };
48
49 explicit UserInputMonitorLinuxCore( 44 explicit UserInputMonitorLinuxCore(
50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 45 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
51 const scoped_refptr<UserInputMonitor::MouseListenerList>&
52 mouse_listeners);
53 ~UserInputMonitorLinuxCore() override; 46 ~UserInputMonitorLinuxCore() override;
54 47
55 // DestructionObserver overrides. 48 // DestructionObserver overrides.
56 void WillDestroyCurrentMessageLoop() override; 49 void WillDestroyCurrentMessageLoop() override;
57 50
58 size_t GetKeyPressCount() const; 51 size_t GetKeyPressCount() const;
59 void StartMonitor(EventType type); 52 void StartMonitor();
60 void StopMonitor(EventType type); 53 void StopMonitor();
61 54
62 private: 55 private:
63 void OnXEvent(); 56 void OnXEvent();
64 57
65 // Processes key and mouse events. 58 // Processes key events.
66 void ProcessXEvent(xEvent* event); 59 void ProcessXEvent(xEvent* event);
67 static void ProcessReply(XPointer self, XRecordInterceptData* data); 60 static void ProcessReply(XPointer self, XRecordInterceptData* data);
68 61
69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 62 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
70 scoped_refptr<base::ObserverListThreadSafe<
71 UserInputMonitor::MouseEventListener>> mouse_listeners_;
72 63
73 // 64 //
74 // The following members should only be accessed on the IO thread. 65 // The following members should only be accessed on the IO thread.
75 // 66 //
76 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_; 67 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_;
77 Display* x_control_display_; 68 Display* x_control_display_;
78 Display* x_record_display_; 69 Display* x_record_display_;
79 XRecordRange* x_record_range_[2]; 70 XRecordRange* x_record_range_;
80 XRecordContext x_record_context_; 71 XRecordContext x_record_context_;
81 KeyboardEventCounter counter_; 72 KeyboardEventCounter counter_;
82 73
83 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore); 74 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore);
84 }; 75 };
85 76
86 class UserInputMonitorLinux : public UserInputMonitor { 77 class UserInputMonitorLinux : public UserInputMonitor {
87 public: 78 public:
88 explicit UserInputMonitorLinux( 79 explicit UserInputMonitorLinux(
89 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); 80 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
90 ~UserInputMonitorLinux() override; 81 ~UserInputMonitorLinux() override;
91 82
92 // Public UserInputMonitor overrides. 83 // Public UserInputMonitor overrides.
93 size_t GetKeyPressCount() const override; 84 size_t GetKeyPressCount() const override;
94 85
95 private: 86 private:
96 // Private UserInputMonitor overrides. 87 // Private UserInputMonitor overrides.
97 void StartKeyboardMonitoring() override; 88 void StartKeyboardMonitoring() override;
98 void StopKeyboardMonitoring() override; 89 void StopKeyboardMonitoring() override;
99 void StartMouseMonitoring() override;
100 void StopMouseMonitoring() override;
101 90
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 91 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
103 UserInputMonitorLinuxCore* core_; 92 UserInputMonitorLinuxCore* core_;
104 93
105 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); 94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
106 }; 95 };
107 96
108 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore( 97 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore(
109 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 98 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
110 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners)
111 : io_task_runner_(io_task_runner), 99 : io_task_runner_(io_task_runner),
112 mouse_listeners_(mouse_listeners),
113 x_control_display_(NULL), 100 x_control_display_(NULL),
114 x_record_display_(NULL), 101 x_record_display_(NULL),
115 x_record_context_(0) { 102 x_record_range_(NULL),
116 x_record_range_[0] = NULL; 103 x_record_context_(0) {}
117 x_record_range_[1] = NULL;
118 }
119 104
120 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() { 105 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() {
121 DCHECK(!x_control_display_); 106 DCHECK(!x_control_display_);
122 DCHECK(!x_record_display_); 107 DCHECK(!x_record_display_);
123 DCHECK(!x_record_range_[0]); 108 DCHECK(!x_record_range_);
124 DCHECK(!x_record_range_[1]);
125 DCHECK(!x_record_context_); 109 DCHECK(!x_record_context_);
126 } 110 }
127 111
128 void UserInputMonitorLinuxCore::WillDestroyCurrentMessageLoop() { 112 void UserInputMonitorLinuxCore::WillDestroyCurrentMessageLoop() {
129 DCHECK(io_task_runner_->BelongsToCurrentThread()); 113 DCHECK(io_task_runner_->BelongsToCurrentThread());
130 StopMonitor(MOUSE_EVENT); 114 StopMonitor();
131 StopMonitor(KEYBOARD_EVENT); 115 StopMonitor();
132 } 116 }
133 117
134 size_t UserInputMonitorLinuxCore::GetKeyPressCount() const { 118 size_t UserInputMonitorLinuxCore::GetKeyPressCount() const {
135 return counter_.GetKeyPressCount(); 119 return counter_.GetKeyPressCount();
136 } 120 }
137 121
138 void UserInputMonitorLinuxCore::StartMonitor(EventType type) { 122 void UserInputMonitorLinuxCore::StartMonitor() {
139 DCHECK(io_task_runner_->BelongsToCurrentThread()); 123 DCHECK(io_task_runner_->BelongsToCurrentThread());
140 124
141 if (type == KEYBOARD_EVENT) 125 counter_.Reset();
142 counter_.Reset();
143 126
144 // TODO(jamiewalch): We should pass the display in. At that point, since 127 // TODO(jamiewalch): We should pass the display in. At that point, since
145 // XRecord needs a private connection to the X Server for its data channel 128 // XRecord needs a private connection to the X Server for its data channel
146 // and both channels are used from a separate thread, we'll need to duplicate 129 // and both channels are used from a separate thread, we'll need to duplicate
147 // them with something like the following: 130 // them with something like the following:
148 // XOpenDisplay(DisplayString(display)); 131 // XOpenDisplay(DisplayString(display));
149 if (!x_control_display_) 132 if (!x_control_display_)
150 x_control_display_ = gfx::OpenNewXDisplay(); 133 x_control_display_ = gfx::OpenNewXDisplay();
151 134
152 if (!x_record_display_) 135 if (!x_record_display_)
153 x_record_display_ = gfx::OpenNewXDisplay(); 136 x_record_display_ = gfx::OpenNewXDisplay();
154 137
155 if (!x_control_display_ || !x_record_display_) { 138 if (!x_control_display_ || !x_record_display_) {
156 LOG(ERROR) << "Couldn't open X display"; 139 LOG(ERROR) << "Couldn't open X display";
157 StopMonitor(type); 140 StopMonitor();
158 return; 141 return;
159 } 142 }
160 143
161 int xr_opcode, xr_event, xr_error; 144 int xr_opcode, xr_event, xr_error;
162 if (!XQueryExtension( 145 if (!XQueryExtension(
163 x_control_display_, "RECORD", &xr_opcode, &xr_event, &xr_error)) { 146 x_control_display_, "RECORD", &xr_opcode, &xr_event, &xr_error)) {
164 LOG(ERROR) << "X Record extension not available."; 147 LOG(ERROR) << "X Record extension not available.";
165 StopMonitor(type); 148 StopMonitor();
166 return; 149 return;
167 } 150 }
168 151
169 if (!x_record_range_[type]) 152 if (!x_record_range_)
170 x_record_range_[type] = XRecordAllocRange(); 153 x_record_range_ = XRecordAllocRange();
171 154
172 if (!x_record_range_[type]) { 155 if (!x_record_range_) {
173 LOG(ERROR) << "XRecordAllocRange failed."; 156 LOG(ERROR) << "XRecordAllocRange failed.";
174 StopMonitor(type); 157 StopMonitor();
175 return; 158 return;
176 } 159 }
177 160
178 if (type == MOUSE_EVENT) { 161 x_record_range_->device_events.first = KeyPress;
179 x_record_range_[type]->device_events.first = MotionNotify; 162 x_record_range_->device_events.last = KeyRelease;
180 x_record_range_[type]->device_events.last = MotionNotify;
181 } else {
182 DCHECK_EQ(KEYBOARD_EVENT, type);
183 x_record_range_[type]->device_events.first = KeyPress;
184 x_record_range_[type]->device_events.last = KeyRelease;
185 }
186 163
187 if (x_record_context_) { 164 if (x_record_context_) {
188 XRecordDisableContext(x_control_display_, x_record_context_); 165 XRecordDisableContext(x_control_display_, x_record_context_);
189 XFlush(x_control_display_); 166 XFlush(x_control_display_);
190 XRecordFreeContext(x_record_display_, x_record_context_); 167 XRecordFreeContext(x_record_display_, x_record_context_);
191 x_record_context_ = 0; 168 x_record_context_ = 0;
192 } 169 }
193 XRecordRange** record_range_to_use = 170 XRecordRange** record_range_to_use = &x_record_range_;
Wez 2017/01/03 23:59:40 nit: You can just use &x_record_range_ directly in
CJ 2017/01/07 00:12:35 Done.
194 (x_record_range_[0] && x_record_range_[1]) ? x_record_range_ 171 int number_of_ranges = 1;
195 : &x_record_range_[type];
196 int number_of_ranges = (x_record_range_[0] && x_record_range_[1]) ? 2 : 1;
197 172
198 XRecordClientSpec client_spec = XRecordAllClients; 173 XRecordClientSpec client_spec = XRecordAllClients;
199 x_record_context_ = XRecordCreateContext(x_record_display_, 174 x_record_context_ = XRecordCreateContext(x_record_display_,
200 0, 175 0,
201 &client_spec, 176 &client_spec,
202 1, 177 1,
203 record_range_to_use, 178 record_range_to_use,
204 number_of_ranges); 179 number_of_ranges);
205 if (!x_record_context_) { 180 if (!x_record_context_) {
206 LOG(ERROR) << "XRecordCreateContext failed."; 181 LOG(ERROR) << "XRecordCreateContext failed.";
207 StopMonitor(type); 182 StopMonitor();
208 return; 183 return;
209 } 184 }
210 185
211 if (!XRecordEnableContextAsync(x_record_display_, 186 if (!XRecordEnableContextAsync(x_record_display_,
212 x_record_context_, 187 x_record_context_,
213 &UserInputMonitorLinuxCore::ProcessReply, 188 &UserInputMonitorLinuxCore::ProcessReply,
214 reinterpret_cast<XPointer>(this))) { 189 reinterpret_cast<XPointer>(this))) {
215 LOG(ERROR) << "XRecordEnableContextAsync failed."; 190 LOG(ERROR) << "XRecordEnableContextAsync failed.";
216 StopMonitor(type); 191 StopMonitor();
217 return; 192 return;
218 } 193 }
219 194
220 if (!x_record_range_[0] || !x_record_range_[1]) { 195 if (!x_record_range_) {
Wez 2017/01/03 23:59:40 This is the wrong way around, and by the time you
CJ 2017/01/07 00:12:35 Done.
221 // Register OnXEvent() to be called every time there is something to read 196 // Register OnXEvent() to be called every time there is something to read
222 // from |x_record_display_|. 197 // from |x_record_display_|.
223 watch_controller_ = base::FileDescriptorWatcher::WatchReadable( 198 watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
224 ConnectionNumber(x_record_display_), 199 ConnectionNumber(x_record_display_),
225 base::Bind(&UserInputMonitorLinuxCore::OnXEvent, 200 base::Bind(&UserInputMonitorLinuxCore::OnXEvent,
226 base::Unretained(this))); 201 base::Unretained(this)));
227 202
228 // Start observing message loop destruction if we start monitoring the first 203 // Start observing message loop destruction if we start monitoring the first
229 // event. 204 // event.
230 base::MessageLoop::current()->AddDestructionObserver(this); 205 base::MessageLoop::current()->AddDestructionObserver(this);
231 } 206 }
232 207
233 // Fetch pending events if any. 208 // Fetch pending events if any.
234 OnXEvent(); 209 OnXEvent();
235 } 210 }
236 211
237 void UserInputMonitorLinuxCore::StopMonitor(EventType type) { 212 void UserInputMonitorLinuxCore::StopMonitor() {
238 DCHECK(io_task_runner_->BelongsToCurrentThread()); 213 DCHECK(io_task_runner_->BelongsToCurrentThread());
239 214
240 if (x_record_range_[type]) { 215 if (x_record_range_) {
241 XFree(x_record_range_[type]); 216 XFree(x_record_range_);
242 x_record_range_[type] = NULL; 217 x_record_range_ = NULL;
243 } 218 }
244 if (x_record_range_[0] || x_record_range_[1]) 219 if (x_record_range_)
245 return; 220 return;
246 221
247 // Context must be disabled via the control channel because we can't send 222 // Context must be disabled via the control channel because we can't send
248 // any X protocol traffic over the data channel while it's recording. 223 // any X protocol traffic over the data channel while it's recording.
249 if (x_record_context_) { 224 if (x_record_context_) {
250 XRecordDisableContext(x_control_display_, x_record_context_); 225 XRecordDisableContext(x_control_display_, x_record_context_);
251 XFlush(x_control_display_); 226 XFlush(x_control_display_);
252 XRecordFreeContext(x_record_display_, x_record_context_); 227 XRecordFreeContext(x_record_display_, x_record_context_);
253 x_record_context_ = 0; 228 x_record_context_ = 0;
254 229
(...skipping 15 matching lines...) Expand all
270 DCHECK(io_task_runner_->BelongsToCurrentThread()); 245 DCHECK(io_task_runner_->BelongsToCurrentThread());
271 XEvent event; 246 XEvent event;
272 // Fetch pending events if any. 247 // Fetch pending events if any.
273 while (XPending(x_record_display_)) { 248 while (XPending(x_record_display_)) {
274 XNextEvent(x_record_display_, &event); 249 XNextEvent(x_record_display_, &event);
275 } 250 }
276 } 251 }
277 252
278 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) { 253 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) {
279 DCHECK(io_task_runner_->BelongsToCurrentThread()); 254 DCHECK(io_task_runner_->BelongsToCurrentThread());
280 if (event->u.u.type == MotionNotify) { 255
281 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, 256 ui::EventType type;
Wez 2017/01/03 23:59:40 nit: We only need this (and the corresponding code
CJ 2017/01/07 00:12:35 Done.
Wez 2017/01/09 22:38:43 nit: Consider leaving a DCHECK in place to verify
CJ 2017/01/09 23:35:49 Done.
282 event->u.keyButtonPointer.rootY)); 257 if (event->u.u.type == KeyPress) {
283 mouse_listeners_->Notify( 258 type = ui::ET_KEY_PRESSED;
284 FROM_HERE, &UserInputMonitor::MouseEventListener::OnMouseMoved, 259 } else if (event->u.u.type == KeyRelease) {
285 position); 260 type = ui::ET_KEY_RELEASED;
286 } else { 261 } else {
287 ui::EventType type; 262 NOTREACHED();
288 if (event->u.u.type == KeyPress) { 263 return;
289 type = ui::ET_KEY_PRESSED; 264 }
290 } else if (event->u.u.type == KeyRelease) {
291 type = ui::ET_KEY_RELEASED;
292 } else {
293 NOTREACHED();
294 return;
295 }
296 265
297 KeySym key_sym = 266 KeySym key_sym =
298 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0); 267 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0);
299 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); 268 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
300 counter_.OnKeyboardEvent(type, key_code); 269 counter_.OnKeyboardEvent(type, key_code);
301 }
302 } 270 }
303 271
304 // static 272 // static
305 void UserInputMonitorLinuxCore::ProcessReply(XPointer self, 273 void UserInputMonitorLinuxCore::ProcessReply(XPointer self,
306 XRecordInterceptData* data) { 274 XRecordInterceptData* data) {
307 if (data->category == XRecordFromServer) { 275 if (data->category == XRecordFromServer) {
308 xEvent* event = reinterpret_cast<xEvent*>(data->data); 276 xEvent* event = reinterpret_cast<xEvent*>(data->data);
309 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event); 277 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event);
310 } 278 }
311 XRecordFreeData(data); 279 XRecordFreeData(data);
312 } 280 }
313 281
314 // 282 //
315 // Implementation of UserInputMonitorLinux. 283 // Implementation of UserInputMonitorLinux.
316 // 284 //
317 285
318 UserInputMonitorLinux::UserInputMonitorLinux( 286 UserInputMonitorLinux::UserInputMonitorLinux(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 287 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
320 : io_task_runner_(io_task_runner), 288 : io_task_runner_(io_task_runner),
321 core_(new UserInputMonitorLinuxCore(io_task_runner, mouse_listeners())) {} 289 core_(new UserInputMonitorLinuxCore(io_task_runner)) {}
322 290
323 UserInputMonitorLinux::~UserInputMonitorLinux() { 291 UserInputMonitorLinux::~UserInputMonitorLinux() {
324 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_)) 292 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_))
325 delete core_; 293 delete core_;
326 } 294 }
327 295
328 size_t UserInputMonitorLinux::GetKeyPressCount() const { 296 size_t UserInputMonitorLinux::GetKeyPressCount() const {
329 return core_->GetKeyPressCount(); 297 return core_->GetKeyPressCount();
330 } 298 }
331 299
332 void UserInputMonitorLinux::StartKeyboardMonitoring() { 300 void UserInputMonitorLinux::StartKeyboardMonitoring() {
333 io_task_runner_->PostTask( 301 io_task_runner_->PostTask(
334 FROM_HERE, 302 FROM_HERE,
335 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, 303 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, core_->AsWeakPtr()));
336 core_->AsWeakPtr(),
337 UserInputMonitorLinuxCore::KEYBOARD_EVENT));
338 } 304 }
339 305
340 void UserInputMonitorLinux::StopKeyboardMonitoring() { 306 void UserInputMonitorLinux::StopKeyboardMonitoring() {
341 io_task_runner_->PostTask( 307 io_task_runner_->PostTask(
342 FROM_HERE, 308 FROM_HERE,
343 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, 309 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, core_->AsWeakPtr()));
344 core_->AsWeakPtr(),
345 UserInputMonitorLinuxCore::KEYBOARD_EVENT));
346 }
347
348 void UserInputMonitorLinux::StartMouseMonitoring() {
349 io_task_runner_->PostTask(FROM_HERE,
350 base::Bind(&UserInputMonitorLinuxCore::StartMonitor,
351 core_->AsWeakPtr(),
352 UserInputMonitorLinuxCore::MOUSE_EVENT));
353 }
354
355 void UserInputMonitorLinux::StopMouseMonitoring() {
356 io_task_runner_->PostTask(FROM_HERE,
357 base::Bind(&UserInputMonitorLinuxCore::StopMonitor,
358 core_->AsWeakPtr(),
359 UserInputMonitorLinuxCore::MOUSE_EVENT));
360 } 310 }
361 311
362 } // namespace 312 } // namespace
363 313
364 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 314 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
365 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 315 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
366 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 316 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
367 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner)); 317 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner));
368 } 318 }
369 319
370 } // namespace media 320 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698