| OLD | NEW |
| 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_internal.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/task.h" | 12 #include "base/task.h" |
| 13 #include "ui/base/keycodes/keyboard_codes.h" | 13 #include "ui/base/keycodes/keyboard_codes.h" |
| 14 #include "ui/base/keycodes/keyboard_code_conversion_win.h" | 14 #include "ui/base/keycodes/keyboard_code_conversion_win.h" |
| 15 #include "views/view.h" | |
| 16 | |
| 17 namespace ui_controls { | |
| 18 | 15 |
| 19 namespace { | 16 namespace { |
| 20 | 17 |
| 21 // InputDispatcher ------------------------------------------------------------ | 18 // InputDispatcher ------------------------------------------------------------ |
| 22 | 19 |
| 23 // InputDispatcher is used to listen for a mouse/keyboard event. When the | 20 // InputDispatcher is used to listen for a mouse/keyboard event. When the |
| 24 // appropriate event is received the task is notified. | 21 // appropriate event is received the task is notified. |
| 25 class InputDispatcher : public base::RefCounted<InputDispatcher> { | 22 class InputDispatcher : public base::RefCounted<InputDispatcher> { |
| 26 public: | 23 public: |
| 27 InputDispatcher(const base::Closure& task, WPARAM message_waiting_for); | 24 InputDispatcher(const base::Closure& task, WPARAM message_waiting_for); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 155 |
| 159 if (!FillKeyboardInput(key, &input, up)) | 156 if (!FillKeyboardInput(key, &input, up)) |
| 160 return false; | 157 return false; |
| 161 | 158 |
| 162 if (!::SendInput(1, &input, sizeof(INPUT))) | 159 if (!::SendInput(1, &input, sizeof(INPUT))) |
| 163 return false; | 160 return false; |
| 164 | 161 |
| 165 return true; | 162 return true; |
| 166 } | 163 } |
| 167 | 164 |
| 165 } // namespace |
| 166 |
| 167 namespace ui_controls { |
| 168 namespace internal { |
| 169 |
| 168 bool SendKeyPressImpl(ui::KeyboardCode key, | 170 bool SendKeyPressImpl(ui::KeyboardCode key, |
| 169 bool control, bool shift, bool alt, | 171 bool control, bool shift, bool alt, |
| 170 const base::Closure& task) { | 172 const base::Closure& task) { |
| 171 scoped_refptr<InputDispatcher> dispatcher( | 173 scoped_refptr<InputDispatcher> dispatcher( |
| 172 !task.is_null() ? new InputDispatcher(task, WM_KEYUP) : NULL); | 174 !task.is_null() ? new InputDispatcher(task, WM_KEYUP) : NULL); |
| 173 | 175 |
| 174 // If a pop-up menu is open, it won't receive events sent using SendInput. | 176 // If a pop-up menu is open, it won't receive events sent using SendInput. |
| 175 // Check for a pop-up menu using its window class (#32768) and if one | 177 // Check for a pop-up menu using its window class (#32768) and if one |
| 176 // exists, send the key event directly there. | 178 // exists, send the key event directly there. |
| 177 HWND popup_menu = ::FindWindow(L"#32768", 0); | 179 HWND popup_menu = ::FindWindow(L"#32768", 0); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 input.mi.dwFlags = up_flags; | 320 input.mi.dwFlags = up_flags; |
| 319 if ((state & UP) && !::SendInput(1, &input, sizeof(INPUT))) | 321 if ((state & UP) && !::SendInput(1, &input, sizeof(INPUT))) |
| 320 return false; | 322 return false; |
| 321 | 323 |
| 322 if (dispatcher.get()) | 324 if (dispatcher.get()) |
| 323 dispatcher->AddRef(); | 325 dispatcher->AddRef(); |
| 324 | 326 |
| 325 return true; | 327 return true; |
| 326 } | 328 } |
| 327 | 329 |
| 328 } // namespace | 330 } // namespace internal |
| 329 | 331 } // namespace ui_controls |
| 330 // public functions ----------------------------------------------------------- | |
| 331 | |
| 332 bool SendKeyPress(gfx::NativeWindow window, | |
| 333 ui::KeyboardCode key, | |
| 334 bool control, | |
| 335 bool shift, | |
| 336 bool alt, | |
| 337 bool command) { | |
| 338 DCHECK(!command); // No command key on Windows | |
| 339 return SendKeyPressImpl(key, control, shift, alt, base::Closure()); | |
| 340 } | |
| 341 | |
| 342 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, | |
| 343 ui::KeyboardCode key, | |
| 344 bool control, | |
| 345 bool shift, | |
| 346 bool alt, | |
| 347 bool command, | |
| 348 const base::Closure& task) { | |
| 349 DCHECK(!command); // No command key on Windows | |
| 350 return SendKeyPressImpl(key, control, shift, alt, task); | |
| 351 } | |
| 352 | |
| 353 bool SendMouseMove(long x, long y) { | |
| 354 return SendMouseMoveImpl(x, y, base::Closure()); | |
| 355 } | |
| 356 | |
| 357 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) { | |
| 358 return SendMouseMoveImpl(x, y, task); | |
| 359 } | |
| 360 | |
| 361 bool SendMouseEvents(MouseButton type, int state) { | |
| 362 return SendMouseEventsImpl(type, state, base::Closure()); | |
| 363 } | |
| 364 | |
| 365 bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, | |
| 366 const base::Closure& task) { | |
| 367 return SendMouseEventsImpl(type, state, task); | |
| 368 } | |
| 369 | |
| 370 bool SendMouseClick(MouseButton type) { | |
| 371 return SendMouseEventsImpl(type, UP | DOWN, base::Closure()); | |
| 372 } | |
| 373 | |
| 374 void MoveMouseToCenterAndPress(views::View* view, | |
| 375 MouseButton button, | |
| 376 int state, | |
| 377 const base::Closure& task) { | |
| 378 DCHECK(view); | |
| 379 DCHECK(view->GetWidget()); | |
| 380 gfx::Point view_center(view->width() / 2, view->height() / 2); | |
| 381 views::View::ConvertPointToScreen(view, &view_center); | |
| 382 SendMouseMove(view_center.x(), view_center.y()); | |
| 383 SendMouseEventsNotifyWhenDone(button, state, task); | |
| 384 } | |
| 385 | |
| 386 } // ui_controls | |
| OLD | NEW |