diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-08 18:33:30 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-08 18:33:30 +0000 |
commit | 5bfb1eb0a729e8d1adb448b0a84580564a883d2e (patch) | |
tree | bdac0bcedc000d43f2b65fedac764bfdfa4b7c60 /chrome/renderer/user_script_slave.cc | |
parent | d8cc3a679233604df94571b882ac912614cb2bbe (diff) | |
download | chromium_src-5bfb1eb0a729e8d1adb448b0a84580564a883d2e.zip chromium_src-5bfb1eb0a729e8d1adb448b0a84580564a883d2e.tar.gz chromium_src-5bfb1eb0a729e8d1adb448b0a84580564a883d2e.tar.bz2 |
Try one more time to check in http://codereview.chromium.org/60112
Added this test to the list of skipped purify tests as it is
experience the same issue as ExtensionView test.
I also found an unrelated memory leak and created a patch
separately: http://codereview.chromium.org/63073
Review URL: http://codereview.chromium.org/63075
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13369 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/user_script_slave.cc')
-rw-r--r-- | chrome/renderer/user_script_slave.cc | 35 |
1 files changed, 28 insertions, 7 deletions
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. |