diff options
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/render_view.cc | 4 | ||||
-rw-r--r-- | chrome/renderer/render_view_unittest.cc | 4 | ||||
-rw-r--r-- | chrome/renderer/renderer_resources.h | 1 | ||||
-rw-r--r-- | chrome/renderer/renderer_resources.rc | 3 | ||||
-rw-r--r-- | chrome/renderer/user_script_slave.cc | 36 | ||||
-rw-r--r-- | chrome/renderer/user_script_slave.h | 8 |
6 files changed, 50 insertions, 6 deletions
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index 7c85b65..9927171 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -2414,7 +2414,9 @@ void RenderView::EvaluateScript(const std::wstring& frame_xpath, if (!web_frame) return; - web_frame->ExecuteJavaScript(WideToUTF8(script), GURL()); + web_frame->ExecuteJavaScript(WideToUTF8(script), + GURL(), // script url + 1); // base line number } void RenderView::OnScriptEvalRequest(const std::wstring& frame_xpath, diff --git a/chrome/renderer/render_view_unittest.cc b/chrome/renderer/render_view_unittest.cc index 71ab2e2..2477c93 100644 --- a/chrome/renderer/render_view_unittest.cc +++ b/chrome/renderer/render_view_unittest.cc @@ -36,7 +36,9 @@ class RenderViewTest : public testing::Test { // Executes the given JavaScript in the context of the main frame. The input // is a NULL-terminated UTF-8 string. void ExecuteJavaScript(const char* js) { - GetMainFrame()->ExecuteJavaScript(js, GURL()); + GetMainFrame()->ExecuteJavaScript(js, + GURL(), // script url + 1); // base line number } // Loads the given HTML into the main frame as a data: URL. diff --git a/chrome/renderer/renderer_resources.h b/chrome/renderer/renderer_resources.h index 21cc7be..06bff27 100644 --- a/chrome/renderer/renderer_resources.h +++ b/chrome/renderer/renderer_resources.h @@ -3,3 +3,4 @@ #define IDR_NET_ERROR_HTML 500 #define IDR_INSECURE_CONTENT_STAMP 501 #define IDR_ERROR_NO_DETAILS_HTML 502 +#define IDR_GREASEMONKEY_API_JS 503 diff --git a/chrome/renderer/renderer_resources.rc b/chrome/renderer/renderer_resources.rc index 55bcd0b..13cbe75 100644 --- a/chrome/renderer/renderer_resources.rc +++ b/chrome/renderer/renderer_resources.rc @@ -15,4 +15,5 @@ IDR_NET_ERROR_HTML BINDATA "renderer\\resources\\neterror.html" IDR_INSECURE_CONTENT_STAMP BINDATA "renderer\\resources\\insecure_content_stamp.png" -IDR_ERROR_NO_DETAILS_HTML BINDATA "renderer\\resources\\error_no_details.html"
\ No newline at end of file +IDR_ERROR_NO_DETAILS_HTML BINDATA "renderer\\resources\\error_no_details.html" +IDR_GREASEMONKEY_API_JS BINDATA "renderer\\resources\\greasemonkey_api.js" diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc index 80cc329..52d9529 100644 --- a/chrome/renderer/user_script_slave.cc +++ b/chrome/renderer/user_script_slave.cc @@ -7,8 +7,14 @@ #include "base/logging.h" #include "base/pickle.h" #include "base/shared_memory.h" +#include "chrome/common/resource_bundle.h" +#include "chrome/renderer/renderer_resources.h" #include "googleurl/src/gurl.h" +// These two strings are injected before and after the Greasemonkey API and +// user script to wrap it in an anonymous scope. +static const char kUserScriptHead[] = "(function (unsafeWindow) {"; +static const char kUserScriptTail[] = "\n})(window);"; // UserScript @@ -110,7 +116,26 @@ std::string UserScript::EscapeGlob(const std::string& input_pattern) { // UserScriptSlave -UserScriptSlave::UserScriptSlave() : shared_memory_(NULL) { +UserScriptSlave::UserScriptSlave() + : shared_memory_(NULL), + user_script_start_line_(0) { + // TODO: Only windows supports resources and only windows supports user + // scrips, so only load the Greasemonkey API on windows. Fix this when + // better cross platofrm support is available. +#if defined(OS_WIN) + api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( + IDR_GREASEMONKEY_API_JS); +#endif + + // Count the number of lines that will be injected before the user script. + StringPiece::size_type pos = 0; + while ((pos = api_js_.find('\n', pos)) != StringPiece::npos) { + user_script_start_line_++; + pos++; + } + + // Add one more line to account for the function that wraps everything. + user_script_start_line_++; } bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { @@ -161,8 +186,13 @@ bool UserScriptSlave::InjectScripts(WebFrame* frame) { for (std::vector<UserScript>::iterator script = scripts_.begin(); script != scripts_.end(); ++script) { if (script->MatchesUrl(frame->GetURL())) { - frame->ExecuteJavaScript(script->GetBody().as_string(), - GURL(script->GetURL().as_string())); + std::string inject(kUserScriptHead); + inject.append(api_js_.as_string()); + inject.append(script->GetBody().as_string()); + inject.append(kUserScriptTail); + frame->ExecuteJavaScript(inject, + GURL(script->GetURL().as_string()), + -user_script_start_line_); } } diff --git a/chrome/renderer/user_script_slave.h b/chrome/renderer/user_script_slave.h index dd40960..21b67dc 100644 --- a/chrome/renderer/user_script_slave.h +++ b/chrome/renderer/user_script_slave.h @@ -89,6 +89,14 @@ class UserScriptSlave { // Parsed script data. std::vector<UserScript> scripts_; + // Greasemonkey API source that is injected with the scripts. + StringPiece api_js_; + + // The line number of the first line of the user script among all of the + // injected javascript. This is used to make reported errors correspond with + // the proper line in the user script. + int user_script_start_line_; + DISALLOW_COPY_AND_ASSIGN(UserScriptSlave); }; |