OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/shared_memory.h" | 11 #include "base/memory/shared_memory.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/timer/elapsed_timer.h" | 15 #include "base/timer/elapsed_timer.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "chrome/renderer/chrome_render_process_observer.h" | 17 #include "chrome/renderer/chrome_render_process_observer.h" |
18 #include "chrome/renderer/extensions/dom_activity_logger.h" | 18 #include "chrome/renderer/extensions/dom_activity_logger.h" |
19 #include "chrome/renderer/extensions/extension_groups.h" | 19 #include "chrome/renderer/extensions/extension_groups.h" |
20 #include "chrome/renderer/isolated_world_ids.h" | 20 #include "chrome/renderer/isolated_world_ids.h" |
21 #include "content/public/renderer/render_thread.h" | 21 #include "content/public/renderer/render_thread.h" |
22 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
23 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
24 #include "extensions/common/extension_messages.h" | 24 #include "extensions/common/extension_messages.h" |
25 #include "extensions/common/extension_set.h" | 25 #include "extensions/common/extension_set.h" |
26 #include "extensions/common/manifest_handlers/csp_info.h" | 26 #include "extensions/common/manifest_handlers/csp_info.h" |
27 #include "extensions/common/permissions/permissions_data.h" | 27 #include "extensions/common/permissions/permissions_data.h" |
| 28 #include "extensions/renderer/script_context.h" |
28 #include "grit/renderer_resources.h" | 29 #include "grit/renderer_resources.h" |
29 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 30 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
30 #include "third_party/WebKit/public/platform/WebVector.h" | 31 #include "third_party/WebKit/public/platform/WebVector.h" |
31 #include "third_party/WebKit/public/web/WebDataSource.h" | |
32 #include "third_party/WebKit/public/web/WebDocument.h" | 32 #include "third_party/WebKit/public/web/WebDocument.h" |
33 #include "third_party/WebKit/public/web/WebFrame.h" | 33 #include "third_party/WebKit/public/web/WebFrame.h" |
34 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 34 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
35 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 35 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
36 #include "third_party/WebKit/public/web/WebView.h" | 36 #include "third_party/WebKit/public/web/WebView.h" |
37 #include "ui/base/resource/resource_bundle.h" | 37 #include "ui/base/resource/resource_bundle.h" |
38 #include "url/gurl.h" | 38 #include "url/gurl.h" |
39 | 39 |
40 using blink::WebFrame; | 40 using blink::WebFrame; |
41 using blink::WebSecurityOrigin; | 41 using blink::WebSecurityOrigin; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 if (only_inject_incognito && !script->is_incognito_enabled()) { | 172 if (only_inject_incognito && !script->is_incognito_enabled()) { |
173 // This script shouldn't run in an incognito tab. | 173 // This script shouldn't run in an incognito tab. |
174 delete script; | 174 delete script; |
175 scripts_.pop_back(); | 175 scripts_.pop_back(); |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 return true; | 179 return true; |
180 } | 180 } |
181 | 181 |
182 GURL UserScriptSlave::GetDataSourceURLForFrame(const WebFrame* frame) { | |
183 // Normally we would use frame->document().url() to determine the document's | |
184 // URL, but to decide whether to inject a content script, we use the URL from | |
185 // the data source. This "quirk" helps prevents content scripts from | |
186 // inadvertently adding DOM elements to the compose iframe in Gmail because | |
187 // the compose iframe's dataSource URL is about:blank, but the document URL | |
188 // changes to match the parent document after Gmail document.writes into | |
189 // it to create the editor. | |
190 // http://code.google.com/p/chromium/issues/detail?id=86742 | |
191 blink::WebDataSource* data_source = frame->provisionalDataSource() ? | |
192 frame->provisionalDataSource() : frame->dataSource(); | |
193 CHECK(data_source); | |
194 return GURL(data_source->request().url()); | |
195 } | |
196 | |
197 void UserScriptSlave::InjectScripts(WebFrame* frame, | 182 void UserScriptSlave::InjectScripts(WebFrame* frame, |
198 UserScript::RunLocation location) { | 183 UserScript::RunLocation location) { |
199 GURL data_source_url = GetDataSourceURLForFrame(frame); | 184 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
200 if (data_source_url.is_empty()) | 185 if (data_source_url.is_empty()) |
201 return; | 186 return; |
202 | 187 |
203 if (frame->isViewSourceModeEnabled()) | 188 if (frame->isViewSourceModeEnabled()) |
204 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 189 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
205 data_source_url.spec()); | 190 data_source_url.spec()); |
206 | 191 |
207 base::ElapsedTimer timer; | 192 base::ElapsedTimer timer; |
208 int num_css = 0; | 193 int num_css = 0; |
209 int num_scripts = 0; | 194 int num_scripts = 0; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 280 |
296 // Notify the browser if any extensions are now executing scripts. | 281 // Notify the browser if any extensions are now executing scripts. |
297 if (!extensions_executing_scripts.empty()) { | 282 if (!extensions_executing_scripts.empty()) { |
298 blink::WebFrame* top_frame = frame->top(); | 283 blink::WebFrame* top_frame = frame->top(); |
299 content::RenderView* render_view = | 284 content::RenderView* render_view = |
300 content::RenderView::FromWebView(top_frame->view()); | 285 content::RenderView::FromWebView(top_frame->view()); |
301 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( | 286 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( |
302 render_view->GetRoutingID(), | 287 render_view->GetRoutingID(), |
303 extensions_executing_scripts, | 288 extensions_executing_scripts, |
304 render_view->GetPageId(), | 289 render_view->GetPageId(), |
305 GetDataSourceURLForFrame(top_frame))); | 290 ScriptContext::GetDataSourceURLForFrame(top_frame))); |
306 } | 291 } |
307 | 292 |
308 // Log debug info. | 293 // Log debug info. |
309 if (location == UserScript::DOCUMENT_START) { | 294 if (location == UserScript::DOCUMENT_START) { |
310 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 295 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
311 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 296 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
312 if (num_css || num_scripts) | 297 if (num_css || num_scripts) |
313 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 298 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
314 } else if (location == UserScript::DOCUMENT_END) { | 299 } else if (location == UserScript::DOCUMENT_END) { |
315 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 300 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
316 if (num_scripts) | 301 if (num_scripts) |
317 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 302 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
318 } else if (location == UserScript::DOCUMENT_IDLE) { | 303 } else if (location == UserScript::DOCUMENT_IDLE) { |
319 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 304 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
320 if (num_scripts) | 305 if (num_scripts) |
321 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 306 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
322 } else { | 307 } else { |
323 NOTREACHED(); | 308 NOTREACHED(); |
324 } | 309 } |
325 } | 310 } |
326 | 311 |
327 } // namespace extensions | 312 } // namespace extensions |
OLD | NEW |