Commit 59ef7dc0 authored by aa@chromium.org's avatar aa@chromium.org

Enable content scripts to receive extension API events.

BUG=80308


Review URL: http://codereview.chromium.org/8585005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110472 0039d316-1c4b-4281-b951-d872f2087c98
parent cb0d5a40
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h" #include "content/browser/tab_contents/tab_contents.h"
...@@ -114,9 +115,26 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptCSSLocalization) { ...@@ -114,9 +115,26 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptCSSLocalization) {
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptExtensionAPIs) { IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptExtensionAPIs) {
ASSERT_TRUE(StartTestServer()); ASSERT_TRUE(StartTestServer());
LoadExtension(test_data_dir_.AppendASCII("content_scripts/extension_api"));
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("content_scripts/extension_api"));
ResultCatcher catcher; ResultCatcher catcher;
ui_test_utils::NavigateToURL( ui_test_utils::NavigateToURL(
browser(), test_server()->GetURL("functions.html")); browser(), test_server()->GetURL("functions.html"));
EXPECT_TRUE(catcher.GetNextResult()); EXPECT_TRUE(catcher.GetNextResult());
// Navigate to a page that will cause a content script to run that starts
// listening for an extension event.
ui_test_utils::NavigateToURL(
browser(), test_server()->GetURL("events.html"));
// Navigate to an extension page that will fire the event events.js is
// listening for.
ui_test_utils::NavigateToURLWithDisposition(
browser(), extension->GetResourceURL("fire_event.html"),
NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_NONE);
EXPECT_TRUE(catcher.GetNextResult());
} }
...@@ -20,11 +20,13 @@ ...@@ -20,11 +20,13 @@
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/api/extension_api.h"
#include "content/browser/child_process_security_policy.h" #include "content/browser/child_process_security_policy.h"
#include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_process_host.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
using content::BrowserThread; using content::BrowserThread;
using extensions::ExtensionAPI;
namespace { namespace {
...@@ -274,8 +276,13 @@ void ExtensionEventRouter::DispatchEventImpl( ...@@ -274,8 +276,13 @@ void ExtensionEventRouter::DispatchEventImpl(
listener->process->browser_context()); listener->process->browser_context());
extensions::ProcessMap* process_map = extensions::ProcessMap* process_map =
listener_profile->GetExtensionService()->process_map(); listener_profile->GetExtensionService()->process_map();
if (!process_map->Contains(extension->id(), listener->process->id()))
// If the event is privileged, only send to extension processes. Otherwise,
// it's OK to send to normal renderers (e.g., for content scripts).
if (ExtensionAPI::GetInstance()->IsPrivileged(event->event_name) &&
!process_map->Contains(extension->id(), listener->process->id())) {
continue; continue;
}
// Is this event from a different profile than the renderer (ie, an // Is this event from a different profile than the renderer (ie, an
// incognito tab event sent to a normal process, or vice versa). // incognito tab event sent to a normal process, or vice versa).
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.experimental.settings.onChanged.addListener(function() {
chrome.test.notifyPass();
});
<script>
chrome.experimental.settings.set({foo:new Date().getTime()});
</script>
{ {
"name": "Content Script API Tests", "name": "Content Script API Tests",
"version": "1", "version": "1",
"permission": ["tabs"], "permissions": ["experimental"],
"content_scripts": [ "content_scripts": [
{ {
"matches": ["*://*/functions.html"], "matches": ["*://*/functions.html"],
"js": ["functions.js"] "js": ["functions.js"]
},
{
"matches": ["*://*/events.html"],
"js": ["events.js"]
} }
] ]
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment