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

Side by Side Diff: chrome/browser/automation/ui_controls_mac.mm

Issue 8212006: base::Bind: Cleanup in automation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Linux test fixes. Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/automation/ui_controls.h" 5 #include "chrome/browser/automation/ui_controls.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 #include <mach/mach_time.h> 8 #include <mach/mach_time.h>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h"
11 #include "base/message_loop.h" 12 #include "base/message_loop.h"
12 #include "chrome/browser/automation/ui_controls_internal.h" 13 #include "chrome/browser/automation/ui_controls_internal.h"
13 #include "content/browser/browser_thread.h" 14 #include "content/browser/browser_thread.h"
14 #include "ui/base/keycodes/keyboard_code_conversion_mac.h" 15 #include "ui/base/keycodes/keyboard_code_conversion_mac.h"
15 16
16 // Implementation details: We use [NSApplication sendEvent:] instead 17 // Implementation details: We use [NSApplication sendEvent:] instead
17 // of [NSApplication postEvent:atStart:] so that the event gets sent 18 // of [NSApplication postEvent:atStart:] so that the event gets sent
18 // immediately. This lets us run the post-event task right 19 // immediately. This lets us run the post-event task right
19 // immediately as well. Unfortunately I cannot subclass NSEvent (it's 20 // immediately as well. Unfortunately I cannot subclass NSEvent (it's
20 // probably a class cluster) to allow other easy answers. For 21 // probably a class cluster) to allow other easy answers. For
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 if (control) { 183 if (control) {
183 flags &= ~NSControlKeyMask; 184 flags &= ~NSControlKeyMask;
184 event = SynthesizeKeyEvent(window, false, ui::VKEY_CONTROL, flags); 185 event = SynthesizeKeyEvent(window, false, ui::VKEY_CONTROL, flags);
185 DCHECK(event); 186 DCHECK(event);
186 events->push_back(event); 187 events->push_back(event);
187 } 188 }
188 } 189 }
189 190
190 // A task class to watch for the event queue. The specific task will be fired 191 // A task class to watch for the event queue. The specific task will be fired
191 // when there is no more event in the queue. 192 // when there is no more event in the queue.
192 class EventQueueWatcher : public Task { 193 class EventQueueWatcher : public base::Closure {
awong 2011/10/11 01:18:04 Eek. bad.
James Hawkins 2011/10/11 21:11:22 Yeah, forgot to fix this. Done.
193 public: 194 public:
194 EventQueueWatcher(Task* task) : task_(task) {} 195 EventQueueWatcher(const base::Closure& task) : task_(task) {}
195
196 virtual ~EventQueueWatcher() {} 196 virtual ~EventQueueWatcher() {}
197 197
198 virtual void Run() { 198 virtual void Run() {
199 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask 199 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
200 untilDate:nil 200 untilDate:nil
201 inMode:NSDefaultRunLoopMode 201 inMode:NSDefaultRunLoopMode
202 dequeue:NO]; 202 dequeue:NO];
203 // If there is still event in the queue, then we need to check again. 203 // If there is still event in the queue, then we need to check again.
204 if (event) 204 if (event)
205 MessageLoop::current()->PostTask(FROM_HERE, new EventQueueWatcher(task_)); 205 MessageLoop::current()->PostTask(FROM_HERE, EventQueueWatcher(task_));
206 else 206 else
207 MessageLoop::current()->PostTask(FROM_HERE, task_); 207 MessageLoop::current()->PostTask(FROM_HERE, task_);
208 } 208 }
209 209
210 private: 210 private:
211 Task* task_; 211 base::Closure task_;
212 }; 212 };
213 213
214 // Stores the current mouse location on the screen. So that we can use it 214 // Stores the current mouse location on the screen. So that we can use it
215 // when firing keyboard and mouse click events. 215 // when firing keyboard and mouse click events.
216 NSPoint g_mouse_location = { 0, 0 }; 216 NSPoint g_mouse_location = { 0, 0 };
217 217
218 } // anonymous namespace 218 } // anonymous namespace
219 219
220
221 namespace ui_controls { 220 namespace ui_controls {
222 221
223 bool SendKeyPress(gfx::NativeWindow window, 222 bool SendKeyPress(gfx::NativeWindow window,
224 ui::KeyboardCode key, 223 ui::KeyboardCode key,
225 bool control, 224 bool control,
226 bool shift, 225 bool shift,
227 bool alt, 226 bool alt,
228 bool command) { 227 bool command) {
229 return SendKeyPressNotifyWhenDone(window, key, 228 return SendKeyPressNotifyWhenDone(window, key,
230 control, shift, alt, command, 229 control, shift, alt, command,
231 NULL); 230 MessageLoop::QuitClosure());
232 } 231 }
233 232
234 // Win and Linux implement a SendKeyPress() this as a 233 // Win and Linux implement a SendKeyPress() this as a
235 // SendKeyPressAndRelease(), so we should as well (despite the name). 234 // SendKeyPressAndRelease(), so we should as well (despite the name).
236 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, 235 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
237 ui::KeyboardCode key, 236 ui::KeyboardCode key,
238 bool control, 237 bool control,
239 bool shift, 238 bool shift,
240 bool alt, 239 bool alt,
241 bool command, 240 bool command,
242 Task* task) { 241 const base::Closure& task) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 243
245 std::vector<NSEvent*> events; 244 std::vector<NSEvent*> events;
246 SynthesizeKeyEventsSequence( 245 SynthesizeKeyEventsSequence(
247 window, key, control, shift, alt, command, &events); 246 window, key, control, shift, alt, command, &events);
248 247
249 // TODO(suzhe): Using [NSApplication postEvent:atStart:] here causes 248 // TODO(suzhe): Using [NSApplication postEvent:atStart:] here causes
250 // BrowserKeyEventsTest.CommandKeyEvents to fail. See http://crbug.com/49270 249 // BrowserKeyEventsTest.CommandKeyEvents to fail. See http://crbug.com/49270
251 // But using [NSApplication sendEvent:] should be safe for keyboard events, 250 // But using [NSApplication sendEvent:] should be safe for keyboard events,
252 // because until now, no code wants to retrieve the next event when handling 251 // because until now, no code wants to retrieve the next event when handling
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 } 353 }
355 354
356 bool SendMouseClick(MouseButton type) { 355 bool SendMouseClick(MouseButton type) {
357 return SendMouseEventsNotifyWhenDone(type, UP|DOWN, NULL); 356 return SendMouseEventsNotifyWhenDone(type, UP|DOWN, NULL);
358 } 357 }
359 358
360 void MoveMouseToCenterAndPress( 359 void MoveMouseToCenterAndPress(
361 NSView* view, 360 NSView* view,
362 MouseButton button, 361 MouseButton button,
363 int state, 362 int state,
364 Task* task) { 363 const base::Closure& task) {
365 DCHECK(view); 364 DCHECK(view);
366 NSWindow* window = [view window]; 365 NSWindow* window = [view window];
367 DCHECK(window); 366 DCHECK(window);
368 NSScreen* screen = [window screen]; 367 NSScreen* screen = [window screen];
369 DCHECK(screen); 368 DCHECK(screen);
370 369
371 // Converts the center position of the view into the coordinates accepted 370 // Converts the center position of the view into the coordinates accepted
372 // by SendMouseMoveNotifyWhenDone() method. 371 // by SendMouseMoveNotifyWhenDone() method.
373 NSRect bounds = [view bounds]; 372 NSRect bounds = [view bounds];
374 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); 373 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
375 center = [view convertPoint:center toView:nil]; 374 center = [view convertPoint:center toView:nil];
376 center = [window convertBaseToScreen:center]; 375 center = [window convertBaseToScreen:center];
377 center = NSMakePoint(center.x, [screen frame].size.height - center.y); 376 center = NSMakePoint(center.x, [screen frame].size.height - center.y);
378 377
379 SendMouseMoveNotifyWhenDone(center.x, center.y, 378 SendMouseMoveNotifyWhenDone(center.x, center.y,
380 new ClickTask(button, state, task)); 379 ClickTask(button, state, task));
381 } 380 }
382 381
383 } // ui_controls 382 } // ui_controls
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698