diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-17 00:06:27 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-17 00:06:27 +0000 |
commit | 49fed325fbf10a6c612b82360b941d500d8a2fa8 (patch) | |
tree | d4605501b1940f2df25948714a4540a7c8b33b7c | |
parent | 5df5165d0e0d7e6493dbca6d4c98a173a616467d (diff) | |
download | chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.zip chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.tar.gz chromium_src-49fed325fbf10a6c612b82360b941d500d8a2fa8.tar.bz2 |
First pass as implementing the greasemonkey API. This patch
includes a javascript file with stubbed versions of all the
greasemoney API methods as well as changes to the Greasemonkey
slave to inject this javascript file along with other user
scripts into the page.I also have a userscript I'm using for
the unit testing of the API methods. I don't think there is
a good place for it in the chrome tree (if there is I'd like
to know), but you can view it here:
http://skrul.com/greasemonkey/test.user.js
Right now all the tests fail except for testLog and
testUnsafeWindow.
Review URL: http://codereview.chromium.org/18183
Patch from Steve Krulewitz <skrulx@gmail.com>.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8247 0039d316-1c4b-4281-b951-d872f2087c98
-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 | ||||
-rw-r--r-- | webkit/glue/webframe.h | 6 | ||||
-rw-r--r-- | webkit/glue/webframe_impl.cc | 5 | ||||
-rw-r--r-- | webkit/glue/webframe_impl.h | 3 |
9 files changed, 59 insertions, 11 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); }; diff --git a/webkit/glue/webframe.h b/webkit/glue/webframe.h index 212bcab..a2a902a 100644 --- a/webkit/glue/webframe.h +++ b/webkit/glue/webframe.h @@ -95,9 +95,11 @@ class WebFrame : public base::RefCounted<WebFrame> { // Executes a string of JavaScript in the web frame. The script_url param is // the URL where the script in question can be found, if any. The renderer may - // request this URL to show the developer the source of the error. + // request this URL to show the developer the source of the error. The + // start_line parameter is the base line number to use for error reporting. virtual void ExecuteJavaScript(const std::string& js_code, - const GURL& script_url) = 0; + const GURL& script_url, + int start_line) = 0; // Returns a string representing the state of the previous page load for // later use when loading. The previous page is the page that was loaded diff --git a/webkit/glue/webframe_impl.cc b/webkit/glue/webframe_impl.cc index e8e009b..a80a36c 100644 --- a/webkit/glue/webframe_impl.cc +++ b/webkit/glue/webframe_impl.cc @@ -1552,11 +1552,12 @@ void WebFrameImpl::LoadAlternateHTMLErrorPage(const WebRequest* request, } void WebFrameImpl::ExecuteJavaScript(const std::string& js_code, - const GURL& script_url) { + const GURL& script_url, + int start_line) { WebCore::ScriptSourceCode source_code( webkit_glue::StdStringToString(js_code), webkit_glue::GURLToKURL(script_url), - 1); // base line number (for errors) + start_line); frame_->loader()->executeScript(source_code); } diff --git a/webkit/glue/webframe_impl.h b/webkit/glue/webframe_impl.h index 87f630c..692cae0 100644 --- a/webkit/glue/webframe_impl.h +++ b/webkit/glue/webframe_impl.h @@ -96,7 +96,8 @@ class WebFrameImpl : public WebFrame { bool replace, const GURL& fake_url); virtual void ExecuteJavaScript(const std::string& js_code, - const GURL& script_url); + const GURL& script_url, + int start_line); virtual bool GetPreviousHistoryState(std::string* history_state) const; virtual bool GetCurrentHistoryState(std::string* history_state) const; virtual bool HasCurrentHistoryState() const; |