summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-07 08:01:50 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-07 08:01:50 +0000
commit4259f9cc708d8f67d7f935d267da9a7ffa852b65 (patch)
tree48992e2008df8ff57c600b65f71946774ab074e3 /chrome/renderer
parent86fd92283a371973f175b34f39ba8dd6ef86d7bc (diff)
downloadchromium_src-4259f9cc708d8f67d7f935d267da9a7ffa852b65.zip
chromium_src-4259f9cc708d8f67d7f935d267da9a7ffa852b65.tar.gz
chromium_src-4259f9cc708d8f67d7f935d267da9a7ffa852b65.tar.bz2
Implement chromium.self in content scripts, so that developers don't
have to know and copy/paste their extension ID. This required moving the code that defaults the extension ID earlier in the load process. Also fixed some bugs: * fixed a bug that was causing all user scripts to get executed in the same context. * made the greasemonkey api only available in 'standalone' user scripts. * re-added the anonymous function wrapper that is supposed to wrap content scripts. Also added unit tests for the fixed bugs. Review URL: http://codereview.chromium.org/60112 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/resources/extension_process_bindings.js6
-rw-r--r--chrome/renderer/user_script_slave.cc35
2 files changed, 31 insertions, 10 deletions
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js
index b109a76..221f3fd 100644
--- a/chrome/renderer/resources/extension_process_bindings.js
+++ b/chrome/renderer/resources/extension_process_bindings.js
@@ -9,9 +9,9 @@ var chromium;
// We shouldn't be receiving evil JSON unless the browser is owned, but just
// to be safe, we sanitize it. This regex mania was borrowed from json2,
// from json.org.
- if (!/^[\],:{}\s]*$/.test(
- str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
- replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
+ if (!/^[\],:{}\s]*$/.test(
+ str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
+ replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, '')))
throw new Error("Unexpected characters in incoming JSON response.");
diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc
index aedc345..bd5a988 100644
--- a/chrome/renderer/user_script_slave.cc
+++ b/chrome/renderer/user_script_slave.cc
@@ -25,6 +25,8 @@ using WebKit::WebString;
static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
static const char kUserScriptTail[] = "\n})(window);";
+static const char kInitSelf[] = "chromium.self = new chromium.Extension('%s')";
+
UserScriptSlave::UserScriptSlave()
: shared_memory_(NULL),
script_deleter_(&scripts_),
@@ -110,8 +112,8 @@ bool UserScriptSlave::InjectScripts(WebFrame* frame,
PerfTimer timer;
int num_matched = 0;
- std::vector<WebScriptSource> sources;
for (size_t i = 0; i < scripts_.size(); ++i) {
+ std::vector<WebScriptSource> sources;
UserScript* script = scripts_[i];
if (!script->MatchesUrl(frame->GetURL()))
continue; // This frame doesn't match the script url pattern, skip it.
@@ -127,16 +129,35 @@ bool UserScriptSlave::InjectScripts(WebFrame* frame,
if (script->run_location() == location) {
for (size_t j = 0; j < script->js_scripts().size(); ++j) {
UserScript::File &file = script->js_scripts()[j];
+ std::string content = file.GetContent().as_string();
+
+ // We add this dumb function wrapper for standalone user script to
+ // emulate what Greasemonkey does.
+ if (script->is_standalone()) {
+ content.insert(0, kUserScriptHead);
+ content += kUserScriptTail;
+ }
sources.push_back(WebScriptSource(
- WebString::fromUTF8(file.GetContent()), file.url()));
+ WebString::fromUTF8(content.c_str(), content.length()),
+ file.url()));
}
}
- }
- if (!sources.empty()) {
- sources.insert(
- sources.begin(), WebScriptSource(WebString::fromUTF8(api_js_)));
- frame->ExecuteScriptInNewContext(&sources.front(), sources.size());
+ if (!sources.empty()) {
+ if (script->is_standalone()) {
+ // For standalone scripts, we try to emulate the Greasemonkey API.
+ sources.insert(sources.begin(),
+ WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
+ } else {
+ // Setup chromium.self to contain an Extension object with the correct
+ // ID.
+ sources.insert(sources.begin(),
+ WebScriptSource(WebString::fromUTF8(
+ StringPrintf(kInitSelf, script->extension_id().c_str()))));
+ }
+
+ frame->ExecuteScriptInNewContext(&sources.front(), sources.size());
+ }
}
// Log debug info.