OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/user_script_slave.h" | 5 #include "chrome/renderer/extensions/user_script_slave.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.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/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/extensions/extension_set.h" | 17 #include "chrome/common/extensions/extension_set.h" |
18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
19 #include "chrome/renderer/chrome_render_process_observer.h" | 19 #include "chrome/renderer/chrome_render_process_observer.h" |
20 #include "chrome/renderer/extensions/extension_dispatcher.h" | 20 #include "chrome/renderer/extensions/extension_dispatcher.h" |
21 #include "chrome/renderer/extensions/extension_groups.h" | 21 #include "chrome/renderer/extensions/extension_groups.h" |
22 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
23 #include "grit/renderer_resources.h" | 23 #include "grit/renderer_resources.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
30 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
31 | 33 |
32 using WebKit::WebFrame; | 34 using WebKit::WebFrame; |
33 using WebKit::WebSecurityOrigin; | 35 using WebKit::WebSecurityOrigin; |
34 using WebKit::WebSecurityPolicy; | 36 using WebKit::WebSecurityPolicy; |
35 using WebKit::WebString; | 37 using WebKit::WebString; |
36 using WebKit::WebVector; | 38 using WebKit::WebVector; |
37 using WebKit::WebView; | 39 using WebKit::WebView; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 DCHECK(sources); | 231 DCHECK(sources); |
230 bool incognito = ChromeRenderProcessObserver::is_incognito_process(); | 232 bool incognito = ChromeRenderProcessObserver::is_incognito_process(); |
231 sources->insert(sources->begin(), WebScriptSource(WebString::fromUTF8( | 233 sources->insert(sources->begin(), WebScriptSource(WebString::fromUTF8( |
232 base::StringPrintf(kInitExtension, | 234 base::StringPrintf(kInitExtension, |
233 extension_id.c_str(), | 235 extension_id.c_str(), |
234 incognito ? "true" : "false")))); | 236 incognito ? "true" : "false")))); |
235 } | 237 } |
236 | 238 |
237 void UserScriptSlave::InjectScripts(WebFrame* frame, | 239 void UserScriptSlave::InjectScripts(WebFrame* frame, |
238 UserScript::RunLocation location) { | 240 UserScript::RunLocation location) { |
239 GURL document_url = GURL(frame->document().url()); | 241 // Normally we would use frame->document().url() to determine the document's |
240 if (document_url.is_empty()) | 242 // URL, but to decide whether to inject a content script, we use the URL from |
| 243 // the data source. This "quirk" helps prevents content scripts from |
| 244 // inadvertently adding DOM elements to the compose iframe in Gmail because |
| 245 // the compose iframe's dataSource URL is about:blank, but the document URL |
| 246 // changes to match the parent document after Gmail document.writes into |
| 247 // it to create the editor. |
| 248 // http://code.google.com/p/chromium/issues/detail?id=86742 |
| 249 GURL data_source_url = GURL(frame->dataSource()->request().url()); |
| 250 if (data_source_url.is_empty()) |
241 return; | 251 return; |
242 | 252 |
243 if (frame->isViewSourceModeEnabled()) | 253 if (frame->isViewSourceModeEnabled()) |
244 document_url = GURL(chrome::kViewSourceScheme + std::string(":") + | 254 data_source_url = GURL(chrome::kViewSourceScheme + std::string(":") + |
245 document_url.spec()); | 255 data_source_url.spec()); |
246 | 256 |
247 PerfTimer timer; | 257 PerfTimer timer; |
248 int num_css = 0; | 258 int num_css = 0; |
249 int num_scripts = 0; | 259 int num_scripts = 0; |
250 | 260 |
251 for (size_t i = 0; i < scripts_.size(); ++i) { | 261 for (size_t i = 0; i < scripts_.size(); ++i) { |
252 std::vector<WebScriptSource> sources; | 262 std::vector<WebScriptSource> sources; |
253 UserScript* script = scripts_[i]; | 263 UserScript* script = scripts_[i]; |
254 | 264 |
255 if (frame->parent() && !script->match_all_frames()) | 265 if (frame->parent() && !script->match_all_frames()) |
256 continue; // Only match subframes if the script declared it wanted to. | 266 continue; // Only match subframes if the script declared it wanted to. |
257 | 267 |
258 const Extension* extension = extensions_->GetByID(script->extension_id()); | 268 const Extension* extension = extensions_->GetByID(script->extension_id()); |
259 | 269 |
260 // Since extension info is sent separately from user script info, they can | 270 // Since extension info is sent separately from user script info, they can |
261 // be out of sync. We just ignore this situation. | 271 // be out of sync. We just ignore this situation. |
262 if (!extension) | 272 if (!extension) |
263 continue; | 273 continue; |
264 | 274 |
265 if (!extension->CanExecuteScriptOnPage(document_url, script, NULL)) | 275 if (!extension->CanExecuteScriptOnPage(data_source_url, script, NULL)) |
266 continue; | 276 continue; |
267 | 277 |
268 // We rely on WebCore for CSS injection, but it's still useful to know how | 278 // We rely on WebCore for CSS injection, but it's still useful to know how |
269 // many css files there are. | 279 // many css files there are. |
270 if (location == UserScript::DOCUMENT_START) | 280 if (location == UserScript::DOCUMENT_START) |
271 num_css += script->css_scripts().size(); | 281 num_css += script->css_scripts().size(); |
272 | 282 |
273 if (script->run_location() == location) { | 283 if (script->run_location() == location) { |
274 num_scripts += script->js_scripts().size(); | 284 num_scripts += script->js_scripts().size(); |
275 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 285 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 if (num_scripts) | 333 if (num_scripts) |
324 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 334 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
325 } else if (location == UserScript::DOCUMENT_IDLE) { | 335 } else if (location == UserScript::DOCUMENT_IDLE) { |
326 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 336 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
327 if (num_scripts) | 337 if (num_scripts) |
328 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 338 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
329 } else { | 339 } else { |
330 NOTREACHED(); | 340 NOTREACHED(); |
331 } | 341 } |
332 } | 342 } |
OLD | NEW |