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

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

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Addresses Wez's #48 comment. 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 <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 int number_of_ranges = 1;
194 (x_record_range_[0] && x_record_range_[1]) ? x_record_range_
195 : &x_record_range_[type];
196 int number_of_ranges = (x_record_range_[0] && x_record_range_[1]) ? 2 : 1;
197 171
198 XRecordClientSpec client_spec = XRecordAllClients; 172 XRecordClientSpec client_spec = XRecordAllClients;
199 x_record_context_ = XRecordCreateContext(x_record_display_, 173 x_record_context_ =
200 0, 174 XRecordCreateContext(x_record_display_, 0, &client_spec, 1,
201 &client_spec, 175 &x_record_range_, number_of_ranges);
202 1,
203 record_range_to_use,
204 number_of_ranges);
205 if (!x_record_context_) { 176 if (!x_record_context_) {
206 LOG(ERROR) << "XRecordCreateContext failed."; 177 LOG(ERROR) << "XRecordCreateContext failed.";
207 StopMonitor(type); 178 StopMonitor();
208 return; 179 return;
209 } 180 }
210 181
211 if (!XRecordEnableContextAsync(x_record_display_, 182 if (!XRecordEnableContextAsync(x_record_display_,
212 x_record_context_, 183 x_record_context_,
213 &UserInputMonitorLinuxCore::ProcessReply, 184 &UserInputMonitorLinuxCore::ProcessReply,
214 reinterpret_cast<XPointer>(this))) { 185 reinterpret_cast<XPointer>(this))) {
215 LOG(ERROR) << "XRecordEnableContextAsync failed."; 186 LOG(ERROR) << "XRecordEnableContextAsync failed.";
216 StopMonitor(type); 187 StopMonitor();
217 return; 188 return;
218 } 189 }
219 190
220 if (!x_record_range_[0] || !x_record_range_[1]) { 191 // Register OnXEvent() to be called every time there is something to read
221 // Register OnXEvent() to be called every time there is something to read 192 // from |x_record_display_|.
222 // from |x_record_display_|. 193 watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
223 watch_controller_ = base::FileDescriptorWatcher::WatchReadable( 194 ConnectionNumber(x_record_display_),
224 ConnectionNumber(x_record_display_), 195 base::Bind(&UserInputMonitorLinuxCore::OnXEvent, base::Unretained(this)));
225 base::Bind(&UserInputMonitorLinuxCore::OnXEvent,
226 base::Unretained(this)));
227 196
228 // Start observing message loop destruction if we start monitoring the first 197 // Start observing message loop destruction if we start monitoring the first
229 // event. 198 // event.
230 base::MessageLoop::current()->AddDestructionObserver(this); 199 base::MessageLoop::current()->AddDestructionObserver(this);
231 }
232 200
233 // Fetch pending events if any. 201 // Fetch pending events if any.
234 OnXEvent(); 202 OnXEvent();
235 } 203 }
236 204
237 void UserInputMonitorLinuxCore::StopMonitor(EventType type) { 205 void UserInputMonitorLinuxCore::StopMonitor() {
238 DCHECK(io_task_runner_->BelongsToCurrentThread()); 206 DCHECK(io_task_runner_->BelongsToCurrentThread());
239 207
240 if (x_record_range_[type]) { 208 if (x_record_range_) {
241 XFree(x_record_range_[type]); 209 XFree(x_record_range_);
242 x_record_range_[type] = NULL; 210 x_record_range_ = NULL;
243 } 211 }
244 if (x_record_range_[0] || x_record_range_[1]) 212 if (x_record_range_)
245 return; 213 return;
246 214
247 // Context must be disabled via the control channel because we can't send 215 // 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. 216 // any X protocol traffic over the data channel while it's recording.
249 if (x_record_context_) { 217 if (x_record_context_) {
250 XRecordDisableContext(x_control_display_, x_record_context_); 218 XRecordDisableContext(x_control_display_, x_record_context_);
251 XFlush(x_control_display_); 219 XFlush(x_control_display_);
252 XRecordFreeContext(x_record_display_, x_record_context_); 220 XRecordFreeContext(x_record_display_, x_record_context_);
253 x_record_context_ = 0; 221 x_record_context_ = 0;
254 222
(...skipping 15 matching lines...) Expand all
270 DCHECK(io_task_runner_->BelongsToCurrentThread()); 238 DCHECK(io_task_runner_->BelongsToCurrentThread());
271 XEvent event; 239 XEvent event;
272 // Fetch pending events if any. 240 // Fetch pending events if any.
273 while (XPending(x_record_display_)) { 241 while (XPending(x_record_display_)) {
274 XNextEvent(x_record_display_, &event); 242 XNextEvent(x_record_display_, &event);
275 } 243 }
276 } 244 }
277 245
278 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) { 246 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) {
279 DCHECK(io_task_runner_->BelongsToCurrentThread()); 247 DCHECK(io_task_runner_->BelongsToCurrentThread());
280 if (event->u.u.type == MotionNotify) { 248 DCHECK(event->u.u.type == KeyRelease || event->u.u.type == KeyPress);
281 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX,
282 event->u.keyButtonPointer.rootY));
283 mouse_listeners_->Notify(
284 FROM_HERE, &UserInputMonitor::MouseEventListener::OnMouseMoved,
285 position);
286 } else {
287 ui::EventType type;
288 if (event->u.u.type == KeyPress) {
289 type = ui::ET_KEY_PRESSED;
290 } else if (event->u.u.type == KeyRelease) {
291 type = ui::ET_KEY_RELEASED;
292 } else {
293 NOTREACHED();
294 return;
295 }
296 249
297 KeySym key_sym = 250 ui::EventType type =
298 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0); 251 (event->u.u.type == KeyPress) ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
299 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); 252
300 counter_.OnKeyboardEvent(type, key_code); 253 KeySym key_sym =
301 } 254 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0);
255 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
256 counter_.OnKeyboardEvent(type, key_code);
302 } 257 }
303 258
304 // static 259 // static
305 void UserInputMonitorLinuxCore::ProcessReply(XPointer self, 260 void UserInputMonitorLinuxCore::ProcessReply(XPointer self,
306 XRecordInterceptData* data) { 261 XRecordInterceptData* data) {
307 if (data->category == XRecordFromServer) { 262 if (data->category == XRecordFromServer) {
308 xEvent* event = reinterpret_cast<xEvent*>(data->data); 263 xEvent* event = reinterpret_cast<xEvent*>(data->data);
309 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event); 264 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event);
310 } 265 }
311 XRecordFreeData(data); 266 XRecordFreeData(data);
312 } 267 }
313 268
314 // 269 //
315 // Implementation of UserInputMonitorLinux. 270 // Implementation of UserInputMonitorLinux.
316 // 271 //
317 272
318 UserInputMonitorLinux::UserInputMonitorLinux( 273 UserInputMonitorLinux::UserInputMonitorLinux(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 274 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
320 : io_task_runner_(io_task_runner), 275 : io_task_runner_(io_task_runner),
321 core_(new UserInputMonitorLinuxCore(io_task_runner, mouse_listeners())) {} 276 core_(new UserInputMonitorLinuxCore(io_task_runner)) {}
322 277
323 UserInputMonitorLinux::~UserInputMonitorLinux() { 278 UserInputMonitorLinux::~UserInputMonitorLinux() {
324 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_)) 279 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_))
325 delete core_; 280 delete core_;
326 } 281 }
327 282
328 size_t UserInputMonitorLinux::GetKeyPressCount() const { 283 size_t UserInputMonitorLinux::GetKeyPressCount() const {
329 return core_->GetKeyPressCount(); 284 return core_->GetKeyPressCount();
330 } 285 }
331 286
332 void UserInputMonitorLinux::StartKeyboardMonitoring() { 287 void UserInputMonitorLinux::StartKeyboardMonitoring() {
333 io_task_runner_->PostTask( 288 io_task_runner_->PostTask(
334 FROM_HERE, 289 FROM_HERE,
335 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, 290 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, core_->AsWeakPtr()));
336 core_->AsWeakPtr(),
337 UserInputMonitorLinuxCore::KEYBOARD_EVENT));
338 } 291 }
339 292
340 void UserInputMonitorLinux::StopKeyboardMonitoring() { 293 void UserInputMonitorLinux::StopKeyboardMonitoring() {
341 io_task_runner_->PostTask( 294 io_task_runner_->PostTask(
342 FROM_HERE, 295 FROM_HERE,
343 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, 296 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 } 297 }
361 298
362 } // namespace 299 } // namespace
363 300
364 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 301 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
365 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 302 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
366 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 303 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
367 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner)); 304 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner));
Wez 2017/01/11 23:19:19 nit: Consider migrating these WrapUnique calls (he
CJ 2017/01/11 23:41:55 Done.
368 } 305 }
369 306
370 } // namespace media 307 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698