| 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/testing_automation_provider.h" | 5 #include "chrome/browser/automation/testing_automation_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 2052 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2063 handler_map["CloseTab"] = | 2063 handler_map["CloseTab"] = |
| 2064 &TestingAutomationProvider::CloseTabJSON; | 2064 &TestingAutomationProvider::CloseTabJSON; |
| 2065 handler_map["WebkitMouseMove"] = | 2065 handler_map["WebkitMouseMove"] = |
| 2066 &TestingAutomationProvider::WebkitMouseMove; | 2066 &TestingAutomationProvider::WebkitMouseMove; |
| 2067 handler_map["WebkitMouseClick"] = | 2067 handler_map["WebkitMouseClick"] = |
| 2068 &TestingAutomationProvider::WebkitMouseClick; | 2068 &TestingAutomationProvider::WebkitMouseClick; |
| 2069 handler_map["WebkitMouseDrag"] = | 2069 handler_map["WebkitMouseDrag"] = |
| 2070 &TestingAutomationProvider::WebkitMouseDrag; | 2070 &TestingAutomationProvider::WebkitMouseDrag; |
| 2071 handler_map["SendWebkitKeyEvent"] = | 2071 handler_map["SendWebkitKeyEvent"] = |
| 2072 &TestingAutomationProvider::SendWebkitKeyEvent; | 2072 &TestingAutomationProvider::SendWebkitKeyEvent; |
| 2073 handler_map["SendOSLevelKeyEvent"] = |
| 2074 &TestingAutomationProvider::SendOSLevelKeyEvent; |
| 2073 handler_map["ActivateTab"] = | 2075 handler_map["ActivateTab"] = |
| 2074 &TestingAutomationProvider::ActivateTabJSON; | 2076 &TestingAutomationProvider::ActivateTabJSON; |
| 2075 #if defined(OS_CHROMEOS) | 2077 #if defined(OS_CHROMEOS) |
| 2076 handler_map["GetLoginInfo"] = &TestingAutomationProvider::GetLoginInfo; | 2078 handler_map["GetLoginInfo"] = &TestingAutomationProvider::GetLoginInfo; |
| 2077 handler_map["LoginAsGuest"] = &TestingAutomationProvider::LoginAsGuest; | 2079 handler_map["LoginAsGuest"] = &TestingAutomationProvider::LoginAsGuest; |
| 2078 handler_map["Login"] = &TestingAutomationProvider::Login; | 2080 handler_map["Login"] = &TestingAutomationProvider::Login; |
| 2079 | 2081 |
| 2080 handler_map["LockScreen"] = &TestingAutomationProvider::LockScreen; | 2082 handler_map["LockScreen"] = &TestingAutomationProvider::LockScreen; |
| 2081 handler_map["UnlockScreen"] = &TestingAutomationProvider::UnlockScreen; | 2083 handler_map["UnlockScreen"] = &TestingAutomationProvider::UnlockScreen; |
| 2082 handler_map["SignoutInScreenLocker"] = | 2084 handler_map["SignoutInScreenLocker"] = |
| (...skipping 2432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4515 if (!base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { | 4517 if (!base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { |
| 4516 AutomationJSONReply(this, reply_message).SendError(base::StringPrintf( | 4518 AutomationJSONReply(this, reply_message).SendError(base::StringPrintf( |
| 4517 "Failed to open process handle for pid %d", pid)); | 4519 "Failed to open process handle for pid %d", pid)); |
| 4518 return; | 4520 return; |
| 4519 } | 4521 } |
| 4520 new RendererProcessClosedObserver(this, reply_message); | 4522 new RendererProcessClosedObserver(this, reply_message); |
| 4521 base::KillProcess(process, 0, false); | 4523 base::KillProcess(process, 0, false); |
| 4522 base::CloseProcessHandle(process); | 4524 base::CloseProcessHandle(process); |
| 4523 } | 4525 } |
| 4524 | 4526 |
| 4527 bool TestingAutomationProvider::BuildWebKeyEventFromArgs( |
| 4528 DictionaryValue* args, |
| 4529 IPC::Message* reply_message, |
| 4530 NativeWebKeyboardEvent* event) { |
| 4531 |
| 4532 int type, modifiers; |
| 4533 bool is_system_key; |
| 4534 string16 unmodified_text, text; |
| 4535 std::string key_identifier; |
| 4536 if (!args->GetInteger("type", &type)) { |
| 4537 AutomationJSONReply(this, reply_message) |
| 4538 .SendError("'type' missing or invalid."); |
| 4539 return false; |
| 4540 } |
| 4541 if (!args->GetBoolean("isSystemKey", &is_system_key)) { |
| 4542 AutomationJSONReply(this, reply_message) |
| 4543 .SendError("'isSystemKey' missing or invalid."); |
| 4544 return false; |
| 4545 } |
| 4546 if (!args->GetString("unmodifiedText", &unmodified_text)) { |
| 4547 AutomationJSONReply(this, reply_message) |
| 4548 .SendError("'unmodifiedText' missing or invalid."); |
| 4549 return false; |
| 4550 } |
| 4551 if (!args->GetString("text", &text)) { |
| 4552 AutomationJSONReply(this, reply_message) |
| 4553 .SendError("'text' missing or invalid."); |
| 4554 return false; |
| 4555 } |
| 4556 if (!args->GetInteger("nativeKeyCode", &event->nativeKeyCode)) { |
| 4557 AutomationJSONReply(this, reply_message) |
| 4558 .SendError("'nativeKeyCode' missing or invalid."); |
| 4559 return false; |
| 4560 } |
| 4561 if (!args->GetInteger("windowsKeyCode", &event->windowsKeyCode)) { |
| 4562 AutomationJSONReply(this, reply_message) |
| 4563 .SendError("'windowsKeyCode' missing or invalid."); |
| 4564 return false; |
| 4565 } |
| 4566 if (!args->GetInteger("modifiers", &modifiers)) { |
| 4567 AutomationJSONReply(this, reply_message) |
| 4568 .SendError("'modifiers' missing or invalid."); |
| 4569 return false; |
| 4570 } |
| 4571 if (args->GetString("keyIdentifier", &key_identifier)) { |
| 4572 base::strlcpy(event->keyIdentifier, |
| 4573 key_identifier.c_str(), |
| 4574 WebKit::WebKeyboardEvent::keyIdentifierLengthCap); |
| 4575 } else { |
| 4576 event->setKeyIdentifierFromWindowsKeyCode(); |
| 4577 } |
| 4578 |
| 4579 if (type == automation::kRawKeyDownType) { |
| 4580 event->type = WebKit::WebInputEvent::RawKeyDown; |
| 4581 } else if (type == automation::kKeyDownType) { |
| 4582 event->type = WebKit::WebInputEvent::KeyDown; |
| 4583 } else if (type == automation::kKeyUpType) { |
| 4584 event->type = WebKit::WebInputEvent::KeyUp; |
| 4585 } else if (type == automation::kCharType) { |
| 4586 event->type = WebKit::WebInputEvent::Char; |
| 4587 } else { |
| 4588 AutomationJSONReply(this, reply_message) |
| 4589 .SendError("'type' refers to an unrecognized keyboard event type"); |
| 4590 return false; |
| 4591 } |
| 4592 |
| 4593 string16 unmodified_text_truncated = unmodified_text.substr( |
| 4594 0, WebKit::WebKeyboardEvent::textLengthCap - 1); |
| 4595 memcpy(event->unmodifiedText, |
| 4596 unmodified_text_truncated.c_str(), |
| 4597 unmodified_text_truncated.length() + 1); |
| 4598 string16 text_truncated = text.substr( |
| 4599 0, WebKit::WebKeyboardEvent::textLengthCap - 1); |
| 4600 memcpy(event->text, text_truncated.c_str(), text_truncated.length() + 1); |
| 4601 |
| 4602 event->modifiers = 0; |
| 4603 if (modifiers & automation::kShiftKeyMask) |
| 4604 event->modifiers |= WebKit::WebInputEvent::ShiftKey; |
| 4605 if (modifiers & automation::kControlKeyMask) |
| 4606 event->modifiers |= WebKit::WebInputEvent::ControlKey; |
| 4607 if (modifiers & automation::kAltKeyMask) |
| 4608 event->modifiers |= WebKit::WebInputEvent::AltKey; |
| 4609 if (modifiers & automation::kMetaKeyMask) |
| 4610 event->modifiers |= WebKit::WebInputEvent::MetaKey; |
| 4611 |
| 4612 event->isSystemKey = is_system_key; |
| 4613 event->timeStampSeconds = base::Time::Now().ToDoubleT(); |
| 4614 event->skip_in_browser = true; |
| 4615 return true; |
| 4616 } |
| 4617 |
| 4525 void TestingAutomationProvider::SendWebkitKeyEvent( | 4618 void TestingAutomationProvider::SendWebkitKeyEvent( |
| 4526 DictionaryValue* args, | 4619 DictionaryValue* args, |
| 4527 IPC::Message* reply_message) { | 4620 IPC::Message* reply_message) { |
| 4621 NativeWebKeyboardEvent event; |
| 4622 // BuildWebKeyEventFromArgs handles telling what when wrong and sending |
| 4623 // the reply message, if it failed we just have to stop here. |
| 4624 if (!BuildWebKeyEventFromArgs(args, reply_message, &event)) |
| 4625 return; |
| 4626 |
| 4528 TabContents* tab_contents; | 4627 TabContents* tab_contents; |
| 4529 std::string error; | 4628 std::string error; |
| 4530 if (!GetTabFromJSONArgs(args, &tab_contents, &error)) { | 4629 if (!GetTabFromJSONArgs(args, &tab_contents, &error)) { |
| 4531 AutomationJSONReply(this, reply_message).SendError(error); | 4630 AutomationJSONReply(this, reply_message).SendError(error); |
| 4532 return; | 4631 return; |
| 4533 } | 4632 } |
| 4633 new InputEventAckNotificationObserver(this, reply_message, event.type); |
| 4634 tab_contents->render_view_host()->ForwardKeyboardEvent(event); |
| 4635 } |
| 4534 | 4636 |
| 4535 int type, modifiers; | 4637 void TestingAutomationProvider::SendSuccessReply(IPC::Message* reply_message) { |
| 4536 bool is_system_key; | 4638 AutomationJSONReply(this, reply_message).SendSuccess(NULL); |
| 4537 string16 unmodified_text, text; | 4639 } |
| 4538 std::string key_identifier; | 4640 |
| 4539 NativeWebKeyboardEvent event; | 4641 void TestingAutomationProvider::SendOSLevelKeyEvent( |
| 4540 if (!args->GetInteger("type", &type)) { | 4642 DictionaryValue* args, |
| 4541 AutomationJSONReply reply(this, reply_message); | 4643 IPC::Message* reply_message) { |
| 4542 reply.SendError("'type' missing or invalid."); | 4644 int modifiers, keycode; |
| 4543 return; | 4645 if (!args->GetInteger("nativeKeyCode", &keycode)) { |
| 4544 } | 4646 AutomationJSONReply(this, reply_message) |
| 4545 if (!args->GetBoolean("isSystemKey", &is_system_key)) { | 4647 .SendError("'nativeKeyCode' missing or invalid."); |
| 4546 AutomationJSONReply reply(this, reply_message); | |
| 4547 reply.SendError("'isSystemKey' missing or invalid."); | |
| 4548 return; | |
| 4549 } | |
| 4550 if (!args->GetString("unmodifiedText", &unmodified_text)) { | |
| 4551 AutomationJSONReply reply(this, reply_message); | |
| 4552 reply.SendError("'unmodifiedText' missing or invalid."); | |
| 4553 return; | |
| 4554 } | |
| 4555 if (!args->GetString("text", &text)) { | |
| 4556 AutomationJSONReply reply(this, reply_message); | |
| 4557 reply.SendError("'text' missing or invalid."); | |
| 4558 return; | |
| 4559 } | |
| 4560 if (!args->GetInteger("nativeKeyCode", &event.nativeKeyCode)) { | |
| 4561 AutomationJSONReply reply(this, reply_message); | |
| 4562 reply.SendError("'nativeKeyCode' missing or invalid."); | |
| 4563 return; | |
| 4564 } | |
| 4565 if (!args->GetInteger("windowsKeyCode", &event.windowsKeyCode)) { | |
| 4566 AutomationJSONReply reply(this, reply_message); | |
| 4567 reply.SendError("'windowsKeyCode' missing or invalid."); | |
| 4568 return; | 4648 return; |
| 4569 } | 4649 } |
| 4570 if (!args->GetInteger("modifiers", &modifiers)) { | 4650 if (!args->GetInteger("modifiers", &modifiers)) { |
| 4571 AutomationJSONReply reply(this, reply_message); | 4651 AutomationJSONReply(this, reply_message) |
| 4572 reply.SendError("'modifiers' missing or invalid."); | 4652 .SendError("'modifiers' missing or invalid."); |
| 4573 return; | |
| 4574 } | |
| 4575 if (args->GetString("keyIdentifier", &key_identifier)) { | |
| 4576 base::strlcpy(event.keyIdentifier, | |
| 4577 key_identifier.c_str(), | |
| 4578 WebKit::WebKeyboardEvent::keyIdentifierLengthCap); | |
| 4579 } else { | |
| 4580 event.setKeyIdentifierFromWindowsKeyCode(); | |
| 4581 } | |
| 4582 | |
| 4583 if (type == automation::kRawKeyDownType) { | |
| 4584 event.type = WebKit::WebInputEvent::RawKeyDown; | |
| 4585 } else if (type == automation::kKeyDownType) { | |
| 4586 event.type = WebKit::WebInputEvent::KeyDown; | |
| 4587 } else if (type == automation::kKeyUpType) { | |
| 4588 event.type = WebKit::WebInputEvent::KeyUp; | |
| 4589 } else if (type == automation::kCharType) { | |
| 4590 event.type = WebKit::WebInputEvent::Char; | |
| 4591 } else { | |
| 4592 AutomationJSONReply reply(this, reply_message); | |
| 4593 reply.SendError("'type' refers to an unrecognized keyboard event type"); | |
| 4594 return; | 4653 return; |
| 4595 } | 4654 } |
| 4596 | 4655 |
| 4597 string16 unmodified_text_truncated = unmodified_text.substr( | 4656 std::string error; |
| 4598 0, WebKit::WebKeyboardEvent::textLengthCap - 1); | 4657 Browser* browser; |
| 4599 memcpy(event.unmodifiedText, | 4658 TabContents* tab_contents; |
| 4600 unmodified_text_truncated.c_str(), | 4659 if (!GetBrowserAndTabFromJSONArgs(args, &browser, &tab_contents, &error)) { |
| 4601 unmodified_text_truncated.length() + 1); | 4660 AutomationJSONReply(this, reply_message).SendError(error); |
| 4602 string16 text_truncated = text.substr( | 4661 return; |
| 4603 0, WebKit::WebKeyboardEvent::textLengthCap - 1); | 4662 } |
| 4604 memcpy(event.text, text_truncated.c_str(), text_truncated.length() + 1); | 4663 // The key events will be sent ot the browser level, we need the current tab |
| 4664 // containing the element we send the text in to be shown. |
| 4665 browser->SelectTabContentsAt( |
| 4666 browser->GetIndexOfController(&tab_contents->controller()), true); |
| 4605 | 4667 |
| 4606 event.modifiers = 0; | 4668 BrowserWindow* browser_window = browser->window(); |
| 4607 if (modifiers & automation::kShiftKeyMask) | 4669 if (!browser_window) { |
| 4608 event.modifiers |= WebKit::WebInputEvent::ShiftKey; | 4670 AutomationJSONReply(this, reply_message) |
| 4609 if (modifiers & automation::kControlKeyMask) | 4671 .SendError("Could not get the browser window"); |
| 4610 event.modifiers |= WebKit::WebInputEvent::ControlKey; | 4672 return; |
| 4611 if (modifiers & automation::kAltKeyMask) | 4673 } |
| 4612 event.modifiers |= WebKit::WebInputEvent::AltKey; | 4674 gfx::NativeWindow window = browser_window->GetNativeHandle(); |
| 4613 if (modifiers & automation::kMetaKeyMask) | 4675 if (!window) { |
| 4614 event.modifiers |= WebKit::WebInputEvent::MetaKey; | 4676 AutomationJSONReply(this, reply_message) |
| 4677 .SendError("Could not get the browser window handle"); |
| 4678 return; |
| 4679 } |
| 4615 | 4680 |
| 4616 event.isSystemKey = is_system_key; | 4681 bool control = !!(modifiers & automation::kControlKeyMask); |
| 4617 event.timeStampSeconds = base::Time::Now().ToDoubleT(); | 4682 bool shift = !!(modifiers & automation::kShiftKeyMask); |
| 4618 event.skip_in_browser = true; | 4683 bool alt = !!(modifiers & automation::kAltKeyMask); |
| 4619 new InputEventAckNotificationObserver(this, reply_message, event.type); | 4684 bool meta = !!(modifiers & automation::kMetaKeyMask); |
| 4620 tab_contents->render_view_host()->ForwardKeyboardEvent(event); | 4685 if (!ui_controls::SendKeyPressNotifyWhenDone( |
| 4686 window, static_cast<ui::KeyboardCode>(keycode), |
| 4687 control, shift, alt, meta, |
| 4688 NewRunnableMethod(this, |
| 4689 &TestingAutomationProvider::SendSuccessReply, reply_message))) { |
| 4690 AutomationJSONReply(this, reply_message) |
| 4691 .SendError("Could not send the native key event"); |
| 4692 } |
| 4621 } | 4693 } |
| 4622 | 4694 |
| 4623 // Sample JSON input: { "command": "GetNTPThumbnailMode" } | 4695 // Sample JSON input: { "command": "GetNTPThumbnailMode" } |
| 4624 // For output, refer to GetNTPThumbnailMode() in | 4696 // For output, refer to GetNTPThumbnailMode() in |
| 4625 // chrome/test/pyautolib/pyauto.py. | 4697 // chrome/test/pyautolib/pyauto.py. |
| 4626 void TestingAutomationProvider::GetNTPThumbnailMode( | 4698 void TestingAutomationProvider::GetNTPThumbnailMode( |
| 4627 Browser* browser, | 4699 Browser* browser, |
| 4628 DictionaryValue* args, | 4700 DictionaryValue* args, |
| 4629 IPC::Message* reply_message) { | 4701 IPC::Message* reply_message) { |
| 4630 const int shown_sections = ShownSectionsHandler::GetShownSections( | 4702 const int shown_sections = ShownSectionsHandler::GetShownSections( |
| (...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5198 // If you change this, update Observer for NotificationType::SESSION_END | 5270 // If you change this, update Observer for NotificationType::SESSION_END |
| 5199 // below. | 5271 // below. |
| 5200 MessageLoop::current()->PostTask(FROM_HERE, | 5272 MessageLoop::current()->PostTask(FROM_HERE, |
| 5201 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider)); | 5273 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider)); |
| 5202 } | 5274 } |
| 5203 } | 5275 } |
| 5204 | 5276 |
| 5205 void TestingAutomationProvider::OnRemoveProvider() { | 5277 void TestingAutomationProvider::OnRemoveProvider() { |
| 5206 AutomationProviderList::GetInstance()->RemoveProvider(this); | 5278 AutomationProviderList::GetInstance()->RemoveProvider(this); |
| 5207 } | 5279 } |
| OLD | NEW |