diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-29 19:58:47 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-29 19:58:47 +0000 |
commit | ffae8831cb1c0b281914b032617f7bbd7d86bcea (patch) | |
tree | db3e44ae7ab368ec2c6cdcd6bb376cf9bac4d004 | |
parent | 1d99bfac92febc985149d026563e53a0888f76af (diff) | |
download | chromium_src-ffae8831cb1c0b281914b032617f7bbd7d86bcea.zip chromium_src-ffae8831cb1c0b281914b032617f7bbd7d86bcea.tar.gz chromium_src-ffae8831cb1c0b281914b032617f7bbd7d86bcea.tar.bz2 |
New event style for pageAction API.
This makes us fire both the old deprecated style of pageActions click handler and the new pageAction style.
BUG=25833
TEST=none
Review URL: http://codereview.chromium.org/346006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30499 0039d316-1c4b-4281-b951-d872f2087c98
8 files changed, 125 insertions, 18 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index 4e9bab3..0fe39a9 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -93,6 +93,16 @@ static void DispatchEvent(Profile* profile, } } +static void DispatchEventWithTab(Profile* profile, + const char* event_name, + const TabContents* tab_contents) { + ListValue args; + args.Append(ExtensionTabUtil::CreateTabValue(tab_contents)); + std::string json_args; + base::JSONWriter::Write(&args, false, &json_args); + DispatchEvent(profile, event_name, json_args); +} + static void DispatchSimpleBrowserEvent(Profile* profile, const int window_id, const char* event_name) { @@ -204,12 +214,7 @@ void ExtensionBrowserEventRouter::OnBrowserSetLastActive( void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, int index, bool foreground) { - ListValue args; - args.Append(ExtensionTabUtil::CreateTabValue(contents)); - std::string json_args; - base::JSONWriter::Write(&args, false, &json_args); - - DispatchEvent(contents->profile(), events::kOnTabCreated, json_args); + DispatchEventWithTab(contents->profile(), events::kOnTabCreated, contents); RegisterForTabNotifications(contents); } @@ -393,7 +398,7 @@ void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, void ExtensionBrowserEventRouter::TabStripEmpty() { } -void ExtensionBrowserEventRouter::PageActionExecuted( +void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( Profile* profile, const std::string& extension_id, const std::string& page_action_id, @@ -412,22 +417,34 @@ void ExtensionBrowserEventRouter::PageActionExecuted( std::string json_args; base::JSONWriter::Write(&args, false, &json_args); - std::string event_name = std::string("pageAction/") + extension_id; + std::string event_name = std::string("pageActions/") + extension_id; DispatchEvent(profile, event_name.c_str(), json_args); } +void ExtensionBrowserEventRouter::PageActionExecuted( + Profile* profile, + const std::string& extension_id, + const std::string& page_action_id, + int tab_id, + const std::string& url, + int button) { + DispatchOldPageActionEvent(profile, extension_id, page_action_id, tab_id, url, + button); + TabContents* tab_contents = NULL; + if (!ExtensionTabUtil::GetTabById(tab_id, profile, NULL, NULL, &tab_contents, + NULL)) { + return; + } + std::string event_name = std::string("pageAction/") + extension_id; + DispatchEventWithTab(profile, event_name.c_str(), tab_contents); +} + void ExtensionBrowserEventRouter::BrowserActionExecuted( Profile* profile, const std::string& extension_id, Browser* browser) { TabContents* tab_contents = NULL; int tab_id = 0; if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) return; - - ListValue args; - args.Append(ExtensionTabUtil::CreateTabValue(tab_contents)); - std::string json_args; - base::JSONWriter::Write(&args, false, &json_args); - std::string event_name = std::string("browserAction/") + extension_id; - DispatchEvent(profile, event_name.c_str(), json_args); + DispatchEventWithTab(profile, event_name.c_str(), tab_contents); } diff --git a/chrome/browser/extensions/extension_browser_event_router.h b/chrome/browser/extensions/extension_browser_event_router.h index 49d3351..a740f58 100644 --- a/chrome/browser/extensions/extension_browser_event_router.h +++ b/chrome/browser/extensions/extension_browser_event_router.h @@ -77,6 +77,16 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, // and Observe/NAV_ENTRY_COMMITTED. void TabUpdated(TabContents* contents, bool did_navigate); + // Called to dispatch a deprecated style page action click event that was + // registered like: + // chrome.pageActions["name"].addListener(function(actionId, info){}) + void DispatchOldPageActionEvent(Profile* profile, + const std::string& extension_id, + const std::string& page_action_id, + int tab_id, + const std::string& url, + int button); + // Register ourselves to receive the various notifications we are interested // in for a browser. void RegisterForBrowserNotifications(const Browser* browser); diff --git a/chrome/browser/extensions/page_action_apitest.cc b/chrome/browser/extensions/page_action_apitest.cc index f3376d8..a0d6074 100644 --- a/chrome/browser/extensions/page_action_apitest.cc +++ b/chrome/browser/extensions/page_action_apitest.cc @@ -42,7 +42,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { { // Simulate the page action being clicked. ResultCatcher catcher; - int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + int tab_id = + ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( browser()->profile(), extension->id(), "", tab_id, "", 0); EXPECT_TRUE(catcher.GetNextResult()); @@ -60,3 +61,34 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { tab_id = browser()->GetSelectedTabContents()->controller().session_id().id(); EXPECT_FALSE(action->GetIcon(tab_id).isNull()); } + + +// Tests old-style pageActions API that is deprecated but we don't want to +// break. +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { + ASSERT_TRUE(RunExtensionTest("old_page_actions")) << message_; + + ExtensionsService* service = browser()->profile()->GetExtensionsService(); + ASSERT_EQ(1u, service->extensions()->size()); + Extension* extension = service->extensions()->at(0); + ASSERT_TRUE(extension); + + // Have the extension enable the page action. + { + ResultCatcher catcher; + ui_test_utils::NavigateToURL(browser(), + GURL(extension->GetResourceURL("page.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + } + + // Simulate the page action being clicked. + { + ResultCatcher catcher; + int tab_id = + ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( + browser()->profile(), extension->id(), "action", tab_id, "", 1); + EXPECT_TRUE(catcher.GetNextResult()); + } +} + diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index 7b76c48..dc4044b 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -192,12 +192,16 @@ var chrome = chrome || {}; // Page action events send (pageActionId, {tabId, tabUrl}). function setupPageActionEvents(extensionId) { var pageActions = GetCurrentPageActions(extensionId); - var eventName = "pageAction/" + extensionId; + + var oldStyleEventName = "pageActions/" + extensionId; // TODO(EXTENSIONS_DEPRECATED): only one page action for (var i = 0; i < pageActions.length; ++i) { // Setup events for each extension_id/page_action_id string we find. - chrome.pageActions[pageActions[i]] = new chrome.Event(eventName); + chrome.pageActions[pageActions[i]] = new chrome.Event(oldStyleEventName); } + + // Note this is singular. + var eventName = "pageAction/" + extensionId; chrome.pageAction = chrome.pageAction || {}; chrome.pageAction.onClicked = new chrome.Event(eventName); } diff --git a/chrome/test/data/extensions/api_test/old_page_actions/background.html b/chrome/test/data/extensions/api_test/old_page_actions/background.html new file mode 100644 index 0000000..650d37a --- /dev/null +++ b/chrome/test/data/extensions/api_test/old_page_actions/background.html @@ -0,0 +1,18 @@ +<html> +<head> +<script> + +chrome.pageActions["action"].addListener(function(actionId, info) { + if (actionId == "action" && typeof(info.tabId) == "number" && + typeof(info.tabUrl) == "string" && info.button == 1) { + chrome.test.notifyPass(); + } else { + chrome.test.notifyFail("pageActions listener failed"); + } +}); + +chrome.test.notifyPass(); + +</script> +</head> +</html> diff --git a/chrome/test/data/extensions/api_test/old_page_actions/icon.png b/chrome/test/data/extensions/api_test/old_page_actions/icon.png Binary files differnew file mode 100644 index 0000000..9a79a46 --- /dev/null +++ b/chrome/test/data/extensions/api_test/old_page_actions/icon.png diff --git a/chrome/test/data/extensions/api_test/old_page_actions/manifest.json b/chrome/test/data/extensions/api_test/old_page_actions/manifest.json new file mode 100644 index 0000000..b1d2b09 --- /dev/null +++ b/chrome/test/data/extensions/api_test/old_page_actions/manifest.json @@ -0,0 +1,13 @@ +{ + "name": "Test of old-style page actions API", + "version": "1.0", + "background_page": "background.html", + "permissions": ["tabs"], + "page_actions": [ + { "id": "action", + "name": "action", + "icons": ["icon.png"] + } + ], + "background_page": "background.html" +} diff --git a/chrome/test/data/extensions/api_test/old_page_actions/page.html b/chrome/test/data/extensions/api_test/old_page_actions/page.html new file mode 100644 index 0000000..686e9066 --- /dev/null +++ b/chrome/test/data/extensions/api_test/old_page_actions/page.html @@ -0,0 +1,13 @@ +<html> +<body> +<script> + +// Enable the page action on this tab. +chrome.tabs.getSelected(null, function(tab) { + chrome.pageActions.enableForTab("action", {"tabId":tab.id, "url":tab.url}); + chrome.test.notifyPass(); +}); + +</script> +</body> +</html> |