Commit 8f2ba5a4 authored by aa@chromium.org's avatar aa@chromium.org

Initialize content scripts the same way extension scripts are.

BUG=
TEST=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98469 0039d316-1c4b-4281-b951-d872f2087c98
parent bee01957
......@@ -12,6 +12,7 @@
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/extension_set.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/chrome_render_process_observer.h"
#include "chrome/renderer/extensions/bindings_utils.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_dispatcher.h"
......@@ -332,13 +333,13 @@ void EventBindings::HandleContextCreated(
contexts.push_back(linked_ptr<ContextInfo>(
new ContextInfo(persistent_context, extension_id, frame)));
// Content scripts get initialized in user_script_slave.cc.
if (!content_script) {
v8::HandleScope handle_scope;
v8::Handle<v8::Value> argv[1];
v8::Handle<v8::Value> argv[3];
argv[0] = v8::String::New(extension_id.c_str());
argv[1] = v8::Boolean::New(extension_dispatcher->is_extension_process());
argv[2] = v8::Boolean::New(
ChromeRenderProcessObserver::is_incognito_process());
CallFunctionInContext(context, "dispatchOnLoad", arraysize(argv), argv);
}
}
// static
......
......@@ -175,11 +175,6 @@ class ExtensionImpl : public ExtensionBase {
} else if (name->Equals(v8::String::New("SetIconCommon"))) {
return v8::FunctionTemplate::New(SetIconCommon,
v8::External::New(this));
} else if (name->Equals(v8::String::New("IsExtensionProcess"))) {
return v8::FunctionTemplate::New(IsExtensionProcess,
v8::External::New(this));
} else if (name->Equals(v8::String::New("IsIncognitoProcess"))) {
return v8::FunctionTemplate::New(IsIncognitoProcess);
} else if (name->Equals(v8::String::New("GetUniqueSubEventName"))) {
return v8::FunctionTemplate::New(GetUniqueSubEventName);
} else if (name->Equals(v8::String::New("GetLocalFileSystem"))) {
......@@ -550,17 +545,6 @@ class ExtensionImpl : public ExtensionBase {
return v8::Undefined();
return v8::Integer::New(renderview->routing_id());
}
static v8::Handle<v8::Value> IsExtensionProcess(const v8::Arguments& args) {
ExtensionImpl* v8_extension = GetFromArguments<ExtensionImpl>(args);
return v8::Boolean::New(
v8_extension->extension_dispatcher_->is_extension_process());
}
static v8::Handle<v8::Value> IsIncognitoProcess(const v8::Arguments& args) {
return v8::Boolean::New(
ChromeRenderProcessObserver::is_incognito_process());
}
};
} // namespace
......
......@@ -146,7 +146,6 @@ void UserScriptIdleScheduler::ExecuteCodeImpl(
} else {
std::vector<WebScriptSource> sources;
sources.push_back(source);
UserScriptSlave::InsertInitExtensionCode(&sources, params.extension_id);
frame->executeScriptInIsolatedWorld(
extension_dispatcher_->user_script_slave()->
GetIsolatedWorldIdForExtension(extension, frame),
......
......@@ -44,11 +44,6 @@ using WebKit::WebView;
static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
static const char kUserScriptTail[] = "\n})(window);";
// Sets up the chrome.extension module. This may be run multiple times per
// context, but the init method deletes itself after the first time.
static const char kInitExtension[] =
"if (chrome.initExtension) chrome.initExtension('%s', true, %s);";
int UserScriptSlave::GetIsolatedWorldIdForExtension(
const Extension* extension, WebFrame* frame) {
static int g_next_isolated_world_id = 1;
......@@ -234,17 +229,6 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
return true;
}
// static
void UserScriptSlave::InsertInitExtensionCode(
std::vector<WebScriptSource>* sources, const std::string& extension_id) {
DCHECK(sources);
bool incognito = ChromeRenderProcessObserver::is_incognito_process();
sources->insert(sources->begin(), WebScriptSource(WebString::fromUTF8(
base::StringPrintf(kInitExtension,
extension_id.c_str(),
incognito ? "true" : "false"))));
}
void UserScriptSlave::InjectScripts(WebFrame* frame,
UserScript::RunLocation location) {
// Normally we would use frame->document().url() to determine the document's
......@@ -297,6 +281,8 @@ void UserScriptSlave::InjectScripts(WebFrame* frame,
// We add this dumb function wrapper for standalone user script to
// emulate what Greasemonkey does.
// TODO(aa): I think that maybe "is_standalone" scripts don't exist
// anymore. Investigate.
if (script->is_standalone() || script->emulate_greasemonkey()) {
content.insert(0, kUserScriptHead);
content += kUserScriptTail;
......@@ -318,10 +304,9 @@ void UserScriptSlave::InjectScripts(WebFrame* frame,
// Setup chrome.self to contain an Extension object with the correct
// ID.
if (!script->extension_id().empty()) {
InsertInitExtensionCode(&sources, script->extension_id());
// TODO(aa): Can extension_id() ever be empty anymore?
if (!script->extension_id().empty())
isolated_world_id = GetIsolatedWorldIdForExtension(extension, frame);
}
PerfTimer exec_timer;
frame->executeScriptInIsolatedWorld(
......
......@@ -57,8 +57,6 @@ class UserScriptSlave {
void RemoveIsolatedWorld(const std::string& extension_id);
static void InsertInitExtensionCode(std::vector<WebScriptSource>* sources,
const std::string& extension_id);
private:
static void InitializeIsolatedWorld(int isolated_world_id,
const Extension* extension);
......
......@@ -232,8 +232,10 @@ var chrome = chrome || {};
chromeHidden.onLoad = new chrome.Event();
chromeHidden.onUnload = new chrome.Event();
chromeHidden.dispatchOnLoad = function(extensionId) {
chromeHidden.onLoad.dispatch(extensionId);
chromeHidden.dispatchOnLoad = function(extensionId, isExtensionProcess,
isIncognitoContext) {
chromeHidden.onLoad.dispatch(extensionId, isExtensionProcess,
isIncognitoContext);
};
chromeHidden.dispatchOnUnload = function() {
......
......@@ -18,8 +18,6 @@ var chrome = chrome || {};
native function OpenChannelToTab();
native function GetRenderViewId();
native function SetIconCommon();
native function IsExtensionProcess();
native function IsIncognitoProcess();
native function GetUniqueSubEventName(eventName);
native function GetLocalFileSystem(name, path);
native function DecodeJPEG(jpeg_image);
......@@ -27,19 +25,6 @@ var chrome = chrome || {};
var chromeHidden = GetChromeHidden();
// These bindings are for the extension process only. Since a chrome-extension
// URL can be loaded in an iframe of a regular renderer, we check here to
// ensure we don't expose the APIs in that case.
if (!IsExtensionProcess()) {
chromeHidden.onLoad.addListener(function (extensionId) {
if (!extensionId) {
return;
}
chrome.initExtension(extensionId, false);
});
return;
}
if (!chrome)
chrome = {};
......@@ -590,11 +575,10 @@ var chrome = chrome || {};
return "unknown";
}
chromeHidden.onLoad.addListener(function (extensionId) {
if (!extensionId) {
chromeHidden.onLoad.addListener(function(extensionId, isExtensionProcess,
isIncognitoProcess) {
if (!isExtensionProcess)
return;
}
chrome.initExtension(extensionId, false, IsIncognitoProcess());
// Setup the ChromeSetting class so we can use it to construct
// ChromeSetting objects from the API definition.
......
......@@ -173,9 +173,8 @@ var chrome = chrome || {};
// This function is called on context initialization for both content scripts
// and extension contexts.
chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess,
chromeHidden.onLoad.addListener(function(extensionId, isExtensionProcess,
inIncognitoContext) {
delete chrome.initExtension;
chromeHidden.extensionId = extensionId;
chrome.extension = chrome.extension || {};
......@@ -259,12 +258,11 @@ var chrome = chrome || {};
return GetL10nMessage(message_name, placeholders, extensionId);
};
if (warnOnPrivilegedApiAccess) {
if (!isExtensionProcess)
setupApiStubs();
}
};
});
var notSupportedSuffix = " is not supported in content scripts. " +
var notSupportedSuffix = " can only be used in extension processes. " +
"See the content scripts documentation for more details.";
// Setup to throw an error message when trying to access |name| on the chrome
......
<script>
// Simple success test: we want content-script APIs to be available (like
// sendRequest), but other APIs to be undefined.
var success = (chrome.tabs == undefined);
var success = false;
try {
var foo = chrome.tabs;
} catch (e) {
success = e.message.indexOf(
" can only be used in extension processes.") > -1;
}
chrome.extension.sendRequest({success: success});
</script>
......@@ -89,7 +89,7 @@ function testPath(path, expectError) {
return false;
}
var str = err.toString();
if (str.search("is not supported in content scripts") != -1) {
if (str.search("can only be used in extension processes.") != -1) {
console.log(" ok (correct error thrown): " + path);
return true;
} else {
......@@ -159,4 +159,3 @@ function doTest(privilegedPaths, unprivilegedPaths) {
reportFailure();
}
}
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