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