summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-14 04:15:16 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-14 04:15:16 +0000
commit0afe827a49501c6801c3c9efbbdb1f64aa010beb (patch)
treedba6031dd45eb3f0a0435b9750976b1cd432abc1 /chrome/renderer
parent27eef9c8edf84061f4e36a3bd26ff7538092a22b (diff)
downloadchromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.zip
chromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.tar.gz
chromium_src-0afe827a49501c6801c3c9efbbdb1f64aa010beb.tar.bz2
Add early-injection capability to user scripts.
Review URL: http://codereview.chromium.org/19624 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9822 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/render_view.cc18
-rw-r--r--chrome/renderer/render_view.h2
-rw-r--r--chrome/renderer/user_script_slave.cc20
-rw-r--r--chrome/renderer/user_script_slave.h2
4 files changed, 30 insertions, 12 deletions
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index bbe14c4..a8adb2f 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -1454,15 +1454,9 @@ void RenderView::DidFinishDocumentLoadForFrame(WebView* webview,
// Check whether we have new encoding name.
UpdateEncoding(frame, webview->GetMainFrameEncodingName());
- // Inject any user scripts. Do not inject into chrome UI pages, but do inject
- // into any other document.
- const GURL &gurl = frame->GetURL();
- if (g_render_thread && // Will be NULL when testing.
- (gurl.SchemeIs("file") ||
- gurl.SchemeIs("http") ||
- gurl.SchemeIs("https"))) {
- g_render_thread->user_script_slave()->InjectScripts(frame);
- }
+ if (g_render_thread) // Will be NULL during unit tests.
+ g_render_thread->user_script_slave()->InjectScripts(
+ frame, UserScript::DOCUMENT_END);
}
void RenderView::DidHandleOnloadEventsForFrame(WebView* webview,
@@ -1530,6 +1524,12 @@ void RenderView::WindowObjectCleared(WebFrame* webframe) {
#endif
}
+void RenderView::DocumentElementAvailable(WebFrame* frame) {
+ if (g_render_thread) // Will be NULL during unit tests.
+ g_render_thread->user_script_slave()->InjectScripts(
+ frame, UserScript::DOCUMENT_START);
+}
+
WindowOpenDisposition RenderView::DispositionForNavigationAction(
WebView* webview,
WebFrame* frame,
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index ee0446e..6cafaa6 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -214,6 +214,8 @@ class RenderView : public RenderWidget,
const GURL& source);
virtual void WindowObjectCleared(WebFrame* webframe);
+ virtual void DocumentElementAvailable(WebFrame* webframe);
+
virtual WindowOpenDisposition DispositionForNavigationAction(
WebView* webview,
WebFrame* frame,
diff --git a/chrome/renderer/user_script_slave.cc b/chrome/renderer/user_script_slave.cc
index 8fc1e3a..bb4f54d 100644
--- a/chrome/renderer/user_script_slave.cc
+++ b/chrome/renderer/user_script_slave.cc
@@ -4,7 +4,9 @@
#include "chrome/renderer/user_script_slave.h"
+#include "base/histogram.h"
#include "base/logging.h"
+#include "base/perftimer.h"
#include "base/pickle.h"
#include "base/shared_memory.h"
#include "chrome/common/resource_bundle.h"
@@ -89,10 +91,15 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
return true;
}
-bool UserScriptSlave::InjectScripts(WebFrame* frame) {
+bool UserScriptSlave::InjectScripts(WebFrame* frame,
+ UserScript::RunLocation location) {
+ PerfTimer timer;
+ int num_matched = 0;
+
for (std::vector<UserScript*>::iterator script = scripts_.begin();
script != scripts_.end(); ++script) {
- if ((*script)->MatchesUrl(frame->GetURL())) {
+ if ((*script)->MatchesUrl(frame->GetURL()) &&
+ (*script)->run_location() == location) {
std::string inject(kUserScriptHead);
inject.append(api_js_.as_string());
inject.append(script_contents_[*script].as_string());
@@ -100,8 +107,17 @@ bool UserScriptSlave::InjectScripts(WebFrame* frame) {
frame->ExecuteJavaScript(inject,
GURL((*script)->url().spec()),
-user_script_start_line_);
+ ++num_matched;
}
}
+ if (location == UserScript::DOCUMENT_START) {
+ HISTOGRAM_COUNTS_100(L"UserScripts:DocStart:Count", num_matched);
+ HISTOGRAM_TIMES(L"UserScripts:DocStart:Time", timer.Elapsed());
+ } else {
+ HISTOGRAM_COUNTS_100(L"UserScripts:DocEnd:Count", num_matched);
+ HISTOGRAM_TIMES(L"UserScripts:DocEnd:Time", timer.Elapsed());
+ }
+
return true;
}
diff --git a/chrome/renderer/user_script_slave.h b/chrome/renderer/user_script_slave.h
index eeb34ae..a3363c1 100644
--- a/chrome/renderer/user_script_slave.h
+++ b/chrome/renderer/user_script_slave.h
@@ -27,7 +27,7 @@ class UserScriptSlave {
// Inject the appropriate scripts into a frame based on its URL.
// TODO(aa): Extract a UserScriptFrame interface out of this to improve
// testability.
- bool InjectScripts(WebFrame* frame);
+ bool InjectScripts(WebFrame* frame, UserScript::RunLocation location);
private:
// Shared memory containing raw script data.