diff options
author | dennisjeffrey@google.com <dennisjeffrey@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-13 22:15:31 +0000 |
---|---|---|
committer | dennisjeffrey@google.com <dennisjeffrey@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-13 22:15:31 +0000 |
commit | cef7a5e3915d34a95fdad3024b5d48a33bd3c2aa (patch) | |
tree | aa8e368e39a8ae0c13389af65a32ff058578c639 /chrome/browser/automation | |
parent | e117df3aa43e7b5172fac07a0275233fb0674951 (diff) | |
download | chromium_src-cef7a5e3915d34a95fdad3024b5d48a33bd3c2aa.zip chromium_src-cef7a5e3915d34a95fdad3024b5d48a33bd3c2aa.tar.gz chromium_src-cef7a5e3915d34a95fdad3024b5d48a33bd3c2aa.tar.bz2 |
New automation hook to launch an app, with sample PyAuto test.
BUG=75205
TEST=None
Review URL: http://codereview.chromium.org/6815024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81496 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/automation')
4 files changed, 141 insertions, 0 deletions
diff --git a/chrome/browser/automation/automation_provider_observers.cc b/chrome/browser/automation/automation_provider_observers.cc index c4986f1..5b8c421 100644 --- a/chrome/browser/automation/automation_provider_observers.cc +++ b/chrome/browser/automation/automation_provider_observers.cc @@ -29,6 +29,7 @@ #include "chrome/browser/extensions/crx_installer.h" #include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_process_manager.h" +#include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_updater.h" #include "chrome/browser/history/top_sites.h" #include "chrome/browser/metrics/metric_event_duration_details.h" @@ -1977,6 +1978,63 @@ void NTPInfoObserver::OnTopSitesReceived( delete this; } +AppLaunchObserver::AppLaunchObserver( + NavigationController* controller, + AutomationProvider* automation, + IPC::Message* reply_message, + extension_misc::LaunchContainer launch_container) + : controller_(controller), + automation_(automation->AsWeakPtr()), + reply_message_(reply_message), + launch_container_(launch_container), + new_window_id_(extension_misc::kUnknownWindowId) { + if (launch_container_ == extension_misc::LAUNCH_TAB) { + // Need to wait for the currently-active tab to reload. + Source<NavigationController> source(controller_); + registrar_.Add(this, NotificationType::LOAD_STOP, source); + } else { + // Need to wait for a new tab in a new window to load. + registrar_.Add(this, NotificationType::LOAD_STOP, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::BROWSER_WINDOW_READY, + NotificationService::AllSources()); + } +} + +AppLaunchObserver::~AppLaunchObserver() {} + +void AppLaunchObserver::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type.value == NotificationType::LOAD_STOP) { + if (launch_container_ == extension_misc::LAUNCH_TAB) { + // The app has been launched in the new tab. + if (automation_) { + AutomationJSONReply(automation_, + reply_message_.release()).SendSuccess(NULL); + } + delete this; + return; + } else { + // The app has launched only if the loaded tab is in the new window. + int window_id = Source<NavigationController>(source)->window_id().id(); + if (window_id == new_window_id_) { + if (automation_) { + AutomationJSONReply(automation_, + reply_message_.release()).SendSuccess(NULL); + } + delete this; + return; + } + } + } else if (type.value == NotificationType::BROWSER_WINDOW_READY) { + new_window_id_ = ExtensionTabUtil::GetWindowId( + Source<Browser>(source).ptr()); + } else { + NOTREACHED(); + } +} + AutocompleteEditFocusedObserver::AutocompleteEditFocusedObserver( AutomationProvider* automation, AutocompleteEditModel* autocomplete_edit, diff --git a/chrome/browser/automation/automation_provider_observers.h b/chrome/browser/automation/automation_provider_observers.h index 7178b69..b6ee3b3 100644 --- a/chrome/browser/automation/automation_provider_observers.h +++ b/chrome/browser/automation/automation_provider_observers.h @@ -32,6 +32,7 @@ #include "chrome/browser/search_engines/template_url_model_observer.h" #include "chrome/browser/tabs/tab_strip_model.h" #include "chrome/common/automation_constants.h" +#include "chrome/common/extensions/extension_constants.h" #include "content/browser/cancelable_request.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" @@ -1135,6 +1136,31 @@ class NTPInfoObserver : public NotificationObserver { DISALLOW_COPY_AND_ASSIGN(NTPInfoObserver); }; +// Observes when an app has been launched, as indicated by a notification that +// a content load in some tab has stopped. +class AppLaunchObserver : public NotificationObserver { + public: + AppLaunchObserver(NavigationController* controller, + AutomationProvider* automation, + IPC::Message* reply_message, + extension_misc::LaunchContainer launch_container); + virtual ~AppLaunchObserver(); + + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + private: + NavigationController* controller_; + base::WeakPtr<AutomationProvider> automation_; + scoped_ptr<IPC::Message> reply_message_; + NotificationRegistrar registrar_; + extension_misc::LaunchContainer launch_container_; + int new_window_id_; + + DISALLOW_COPY_AND_ASSIGN(AppLaunchObserver); +}; + // Allows automation provider to wait until the autocomplete edit // has received focus class AutocompleteEditFocusedObserver : public NotificationObserver { diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index a2d9055..c3e623e 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -2272,6 +2272,8 @@ void TestingAutomationProvider::SendJSONRequest(int handle, browser_handler_map["SetNTPMenuMode"] = &TestingAutomationProvider::SetNTPMenuMode; + browser_handler_map["LaunchApp"] = &TestingAutomationProvider::LaunchApp; + if (handler_map.find(std::string(command)) != handler_map.end()) { (this->*handler_map[command])(dict_value, reply_message); } else if (browser_handler_map.find(std::string(command)) != @@ -4904,6 +4906,56 @@ void TestingAutomationProvider::SetNTPMenuMode( reply.SendSuccess(NULL); } +// Sample JSON input: { "command": "LaunchApp", +// "id": "ahfgeienlihckogmohjhadlkjgocpleb" } +// Sample JSON output: {} +void TestingAutomationProvider::LaunchApp( + Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message) { + std::string id; + if (!args->GetString("id", &id)) { + AutomationJSONReply(this, reply_message).SendError( + "Must include string id."); + return; + } + + ExtensionService* service = browser->profile()->GetExtensionService(); + if (!service) { + AutomationJSONReply(this, reply_message).SendError( + "No extensions service."); + return; + } + + const Extension* extension = service->GetExtensionById( + id, false /* do not include disabled extensions */); + if (!extension) { + AutomationJSONReply(this, reply_message).SendError( + StringPrintf("Extension with ID '%s' doesn't exist or is disabled.", + id.c_str())); + return; + } + + // Look at preferences to find the right launch container. If no preference + // is set, launch as a regular tab. + extension_misc::LaunchContainer launch_container = + service->extension_prefs()->GetLaunchContainer( + extension, ExtensionPrefs::LAUNCH_REGULAR); + + TabContents* old_contents = browser->GetSelectedTabContents(); + if (!old_contents) { + AutomationJSONReply(this, reply_message).SendError( + "Cannot identify selected tab contents."); + return; + } + + // This observer will delete itself. + new AppLaunchObserver(&old_contents->controller(), this, reply_message, + launch_container); + Browser::OpenApplication(profile(), extension, launch_container, + old_contents); +} + void TestingAutomationProvider::WaitForAllTabsToStopLoading( DictionaryValue* args, IPC::Message* reply_message) { diff --git a/chrome/browser/automation/testing_automation_provider.h b/chrome/browser/automation/testing_automation_provider.h index 3999ce69..b77c37c 100644 --- a/chrome/browser/automation/testing_automation_provider.h +++ b/chrome/browser/automation/testing_automation_provider.h @@ -782,6 +782,11 @@ class TestingAutomationProvider : public AutomationProvider, DictionaryValue* args, IPC::Message* reply_message); + // Launches the specified app from the currently-selected tab. + void LaunchApp(Browser* browser, + DictionaryValue* args, + IPC::Message* reply_message); + // Waits for all tabs to stop loading. void WaitForAllTabsToStopLoading(DictionaryValue* args, IPC::Message* reply_message); |