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 @@ ...@@ -12,6 +12,7 @@
#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/extension_set.h" #include "chrome/common/extensions/extension_set.h"
#include "chrome/common/url_constants.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/bindings_utils.h"
#include "chrome/renderer/extensions/event_bindings.h" #include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_dispatcher.h" #include "chrome/renderer/extensions/extension_dispatcher.h"
...@@ -332,13 +333,13 @@ void EventBindings::HandleContextCreated( ...@@ -332,13 +333,13 @@ void EventBindings::HandleContextCreated(
contexts.push_back(linked_ptr<ContextInfo>( contexts.push_back(linked_ptr<ContextInfo>(
new ContextInfo(persistent_context, extension_id, frame))); new ContextInfo(persistent_context, extension_id, frame)));
// Content scripts get initialized in user_script_slave.cc.
if (!content_script) {
v8::HandleScope handle_scope; 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[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); CallFunctionInContext(context, "dispatchOnLoad", arraysize(argv), argv);
}
} }
// static // static
......
...@@ -175,11 +175,6 @@ class ExtensionImpl : public ExtensionBase { ...@@ -175,11 +175,6 @@ class ExtensionImpl : public ExtensionBase {
} else if (name->Equals(v8::String::New("SetIconCommon"))) { } else if (name->Equals(v8::String::New("SetIconCommon"))) {
return v8::FunctionTemplate::New(SetIconCommon, return v8::FunctionTemplate::New(SetIconCommon,
v8::External::New(this)); 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"))) { } else if (name->Equals(v8::String::New("GetUniqueSubEventName"))) {
return v8::FunctionTemplate::New(GetUniqueSubEventName); return v8::FunctionTemplate::New(GetUniqueSubEventName);
} else if (name->Equals(v8::String::New("GetLocalFileSystem"))) { } else if (name->Equals(v8::String::New("GetLocalFileSystem"))) {
...@@ -550,17 +545,6 @@ class ExtensionImpl : public ExtensionBase { ...@@ -550,17 +545,6 @@ class ExtensionImpl : public ExtensionBase {
return v8::Undefined(); return v8::Undefined();
return v8::Integer::New(renderview->routing_id()); 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 } // namespace
......
...@@ -146,7 +146,6 @@ void UserScriptIdleScheduler::ExecuteCodeImpl( ...@@ -146,7 +146,6 @@ void UserScriptIdleScheduler::ExecuteCodeImpl(
} else { } else {
std::vector<WebScriptSource> sources; std::vector<WebScriptSource> sources;
sources.push_back(source); sources.push_back(source);
UserScriptSlave::InsertInitExtensionCode(&sources, params.extension_id);
frame->executeScriptInIsolatedWorld( frame->executeScriptInIsolatedWorld(
extension_dispatcher_->user_script_slave()-> extension_dispatcher_->user_script_slave()->
GetIsolatedWorldIdForExtension(extension, frame), GetIsolatedWorldIdForExtension(extension, frame),
......
...@@ -44,11 +44,6 @@ using WebKit::WebView; ...@@ -44,11 +44,6 @@ using WebKit::WebView;
static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
static const char kUserScriptTail[] = "\n})(window);"; 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( int UserScriptSlave::GetIsolatedWorldIdForExtension(
const Extension* extension, WebFrame* frame) { const Extension* extension, WebFrame* frame) {
static int g_next_isolated_world_id = 1; static int g_next_isolated_world_id = 1;
...@@ -234,17 +229,6 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { ...@@ -234,17 +229,6 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
return true; 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, void UserScriptSlave::InjectScripts(WebFrame* frame,
UserScript::RunLocation location) { UserScript::RunLocation location) {
// Normally we would use frame->document().url() to determine the document's // Normally we would use frame->document().url() to determine the document's
...@@ -297,6 +281,8 @@ void UserScriptSlave::InjectScripts(WebFrame* frame, ...@@ -297,6 +281,8 @@ void UserScriptSlave::InjectScripts(WebFrame* frame,
// We add this dumb function wrapper for standalone user script to // We add this dumb function wrapper for standalone user script to
// emulate what Greasemonkey does. // 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()) { if (script->is_standalone() || script->emulate_greasemonkey()) {
content.insert(0, kUserScriptHead); content.insert(0, kUserScriptHead);
content += kUserScriptTail; content += kUserScriptTail;
...@@ -318,10 +304,9 @@ void UserScriptSlave::InjectScripts(WebFrame* frame, ...@@ -318,10 +304,9 @@ void UserScriptSlave::InjectScripts(WebFrame* frame,
// Setup chrome.self to contain an Extension object with the correct // Setup chrome.self to contain an Extension object with the correct
// ID. // ID.
if (!script->extension_id().empty()) { // TODO(aa): Can extension_id() ever be empty anymore?
InsertInitExtensionCode(&sources, script->extension_id()); if (!script->extension_id().empty())
isolated_world_id = GetIsolatedWorldIdForExtension(extension, frame); isolated_world_id = GetIsolatedWorldIdForExtension(extension, frame);
}
PerfTimer exec_timer; PerfTimer exec_timer;
frame->executeScriptInIsolatedWorld( frame->executeScriptInIsolatedWorld(
......
...@@ -57,8 +57,6 @@ class UserScriptSlave { ...@@ -57,8 +57,6 @@ class UserScriptSlave {
void RemoveIsolatedWorld(const std::string& extension_id); void RemoveIsolatedWorld(const std::string& extension_id);
static void InsertInitExtensionCode(std::vector<WebScriptSource>* sources,
const std::string& extension_id);
private: private:
static void InitializeIsolatedWorld(int isolated_world_id, static void InitializeIsolatedWorld(int isolated_world_id,
const Extension* extension); const Extension* extension);
......
...@@ -232,8 +232,10 @@ var chrome = chrome || {}; ...@@ -232,8 +232,10 @@ var chrome = chrome || {};
chromeHidden.onLoad = new chrome.Event(); chromeHidden.onLoad = new chrome.Event();
chromeHidden.onUnload = new chrome.Event(); chromeHidden.onUnload = new chrome.Event();
chromeHidden.dispatchOnLoad = function(extensionId) { chromeHidden.dispatchOnLoad = function(extensionId, isExtensionProcess,
chromeHidden.onLoad.dispatch(extensionId); isIncognitoContext) {
chromeHidden.onLoad.dispatch(extensionId, isExtensionProcess,
isIncognitoContext);
}; };
chromeHidden.dispatchOnUnload = function() { chromeHidden.dispatchOnUnload = function() {
......
...@@ -18,8 +18,6 @@ var chrome = chrome || {}; ...@@ -18,8 +18,6 @@ var chrome = chrome || {};
native function OpenChannelToTab(); native function OpenChannelToTab();
native function GetRenderViewId(); native function GetRenderViewId();
native function SetIconCommon(); native function SetIconCommon();
native function IsExtensionProcess();
native function IsIncognitoProcess();
native function GetUniqueSubEventName(eventName); native function GetUniqueSubEventName(eventName);
native function GetLocalFileSystem(name, path); native function GetLocalFileSystem(name, path);
native function DecodeJPEG(jpeg_image); native function DecodeJPEG(jpeg_image);
...@@ -27,19 +25,6 @@ var chrome = chrome || {}; ...@@ -27,19 +25,6 @@ var chrome = chrome || {};
var chromeHidden = GetChromeHidden(); 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) if (!chrome)
chrome = {}; chrome = {};
...@@ -590,11 +575,10 @@ var chrome = chrome || {}; ...@@ -590,11 +575,10 @@ var chrome = chrome || {};
return "unknown"; return "unknown";
} }
chromeHidden.onLoad.addListener(function (extensionId) { chromeHidden.onLoad.addListener(function(extensionId, isExtensionProcess,
if (!extensionId) { isIncognitoProcess) {
if (!isExtensionProcess)
return; return;
}
chrome.initExtension(extensionId, false, IsIncognitoProcess());
// Setup the ChromeSetting class so we can use it to construct // Setup the ChromeSetting class so we can use it to construct
// ChromeSetting objects from the API definition. // ChromeSetting objects from the API definition.
......
...@@ -173,9 +173,8 @@ var chrome = chrome || {}; ...@@ -173,9 +173,8 @@ var chrome = chrome || {};
// This function is called on context initialization for both content scripts // This function is called on context initialization for both content scripts
// and extension contexts. // and extension contexts.
chrome.initExtension = function(extensionId, warnOnPrivilegedApiAccess, chromeHidden.onLoad.addListener(function(extensionId, isExtensionProcess,
inIncognitoContext) { inIncognitoContext) {
delete chrome.initExtension;
chromeHidden.extensionId = extensionId; chromeHidden.extensionId = extensionId;
chrome.extension = chrome.extension || {}; chrome.extension = chrome.extension || {};
...@@ -259,12 +258,11 @@ var chrome = chrome || {}; ...@@ -259,12 +258,11 @@ var chrome = chrome || {};
return GetL10nMessage(message_name, placeholders, extensionId); return GetL10nMessage(message_name, placeholders, extensionId);
}; };
if (warnOnPrivilegedApiAccess) { if (!isExtensionProcess)
setupApiStubs(); 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."; "See the content scripts documentation for more details.";
// Setup to throw an error message when trying to access |name| on the chrome // Setup to throw an error message when trying to access |name| on the chrome
......
<script> <script>
// Simple success test: we want content-script APIs to be available (like // Simple success test: we want content-script APIs to be available (like
// sendRequest), but other APIs to be undefined. // 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}); chrome.extension.sendRequest({success: success});
</script> </script>
...@@ -89,7 +89,7 @@ function testPath(path, expectError) { ...@@ -89,7 +89,7 @@ function testPath(path, expectError) {
return false; return false;
} }
var str = err.toString(); 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); console.log(" ok (correct error thrown): " + path);
return true; return true;
} else { } else {
...@@ -159,4 +159,3 @@ function doTest(privilegedPaths, unprivilegedPaths) { ...@@ -159,4 +159,3 @@ function doTest(privilegedPaths, unprivilegedPaths) {
reportFailure(); 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