Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: chrome/renderer/user_script_slave.cc

Issue 2389004: Only inject content scripts into HTML documents (Closed) Base URL: git://codf21.jail/chromium.git
Patch Set: Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/user_script_slave.h" 5 #include "chrome/renderer/user_script_slave.h"
6 6
7 #include "app/resource_bundle.h" 7 #include "app/resource_bundle.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/histogram.h" 9 #include "base/histogram.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/perftimer.h" 11 #include "base/perftimer.h"
12 #include "base/pickle.h" 12 #include "base/pickle.h"
13 #include "base/shared_memory.h" 13 #include "base/shared_memory.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/extension_constants.h" 17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/renderer/extension_groups.h" 18 #include "chrome/renderer/extension_groups.h"
19 #include "chrome/renderer/render_thread.h" 19 #include "chrome/renderer/render_thread.h"
20 #include "googleurl/src/gurl.h" 20 #include "googleurl/src/gurl.h"
21 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
22 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
21 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" 23 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
22 24
23 #include "grit/renderer_resources.h" 25 #include "grit/renderer_resources.h"
24 26
25 using WebKit::WebFrame; 27 using WebKit::WebFrame;
26 using WebKit::WebString; 28 using WebKit::WebString;
27 29
28 // These two strings are injected before and after the Greasemonkey API and 30 // These two strings are injected before and after the Greasemonkey API and
29 // user script to wrap it in an anonymous scope. 31 // user script to wrap it in an anonymous scope.
30 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 32 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 incognito ? "true" : "false")))); 144 incognito ? "true" : "false"))));
143 } 145 }
144 146
145 bool UserScriptSlave::InjectScripts(WebFrame* frame, 147 bool UserScriptSlave::InjectScripts(WebFrame* frame,
146 UserScript::RunLocation location) { 148 UserScript::RunLocation location) {
147 GURL frame_url = GURL(frame->url()); 149 GURL frame_url = GURL(frame->url());
148 // Don't bother if this is not a URL we inject script into. 150 // Don't bother if this is not a URL we inject script into.
149 if (!URLPattern::IsValidScheme(frame_url.scheme())) 151 if (!URLPattern::IsValidScheme(frame_url.scheme()))
150 return true; 152 return true;
151 153
154 // Only inject user scripts into documents with an <html> tag as the root
155 // element. Note that WebCore fixes up html pages that lack a root HTML
156 // element so that they include one. Also, documents like text/plain and
157 // image/* are wrapped in a simple HTML document.
158 //
159 // Basically, this check filters out SVG documents and other types of XML
160 // documents.
161 if (frame->document().isNull() ||
162 frame->document().documentElement().isNull() ||
163 !frame->document().documentElement().hasTagName("html")) {
164 return true;
165 }
166
152 // Don't inject user scripts into the gallery itself. This prevents 167 // Don't inject user scripts into the gallery itself. This prevents
153 // a user script from removing the "report abuse" link, for example. 168 // a user script from removing the "report abuse" link, for example.
154 if (frame_url.host() == GURL(extension_urls::kGalleryBrowsePrefix).host()) 169 if (frame_url.host() == GURL(extension_urls::kGalleryBrowsePrefix).host())
155 return true; 170 return true;
156 171
157 PerfTimer timer; 172 PerfTimer timer;
158 int num_css = 0; 173 int num_css = 0;
159 int num_scripts = 0; 174 int num_scripts = 0;
160 175
161 for (size_t i = 0; i < scripts_.size(); ++i) { 176 for (size_t i = 0; i < scripts_.size(); ++i) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 if (num_scripts) 251 if (num_scripts)
237 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); 252 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed());
238 } else { 253 } else {
239 NOTREACHED(); 254 NOTREACHED();
240 } 255 }
241 256
242 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css << 257 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css <<
243 " css files into " << frame->url().spec().data(); 258 " css files into " << frame->url().spec().data();
244 return true; 259 return true;
245 } 260 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698