OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/user_script_slave.h" | 5 #include "extensions/renderer/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" |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 if (frame->isViewSourceModeEnabled()) | 187 if (frame->isViewSourceModeEnabled()) |
188 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 188 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
189 data_source_url.spec()); | 189 data_source_url.spec()); |
190 | 190 |
191 base::ElapsedTimer timer; | 191 base::ElapsedTimer timer; |
192 int num_css = 0; | 192 int num_css = 0; |
193 int num_scripts = 0; | 193 int num_scripts = 0; |
194 | 194 |
195 ExecutingScriptsMap extensions_executing_scripts; | 195 ExecutingScriptsMap extensions_executing_scripts; |
196 | 196 |
197 blink::WebFrame* top_frame = frame->top(); | |
198 content::RenderView* render_view = | |
not at google - send to devlin
2014/05/07 22:49:02
top_render_view?
Devlin
2014/05/08 18:15:46
Why not.
| |
199 content::RenderView::FromWebView(top_frame->view()); | |
200 | |
197 for (size_t i = 0; i < scripts_.size(); ++i) { | 201 for (size_t i = 0; i < scripts_.size(); ++i) { |
198 std::vector<WebScriptSource> sources; | 202 std::vector<WebScriptSource> sources; |
199 UserScript* script = scripts_[i]; | 203 UserScript* script = scripts_[i]; |
200 | 204 |
201 if (frame->parent() && !script->match_all_frames()) | 205 if (frame->parent() && !script->match_all_frames()) |
202 continue; // Only match subframes if the script declared it wanted to. | 206 continue; // Only match subframes if the script declared it wanted to. |
203 | 207 |
204 const Extension* extension = extensions_->GetByID(script->extension_id()); | 208 const Extension* extension = extensions_->GetByID(script->extension_id()); |
205 | 209 |
206 // Since extension info is sent separately from user script info, they can | 210 // Since extension info is sent separately from user script info, they can |
207 // be out of sync. We just ignore this situation. | 211 // be out of sync. We just ignore this situation. |
208 if (!extension) | 212 if (!extension) |
209 continue; | 213 continue; |
210 | 214 |
211 // Content scripts are not tab-specific. | 215 // Content scripts are not tab-specific. |
212 const int kNoTabId = -1; | 216 const int kNoTabId = -1; |
213 // We don't have a process id in this context. | 217 // We don't have a process id in this context. |
214 const int kNoProcessId = -1; | 218 const int kNoProcessId = -1; |
215 if (!PermissionsData::CanExecuteScriptOnPage(extension, | 219 if (!PermissionsData::CanExecuteScriptOnPage(extension, |
216 data_source_url, | 220 data_source_url, |
217 frame->top()->document().url(), | 221 top_frame->document().url(), |
218 kNoTabId, | 222 kNoTabId, |
219 script, | 223 script, |
220 kNoProcessId, | 224 kNoProcessId, |
221 NULL)) { | 225 NULL)) { |
222 continue; | 226 continue; |
223 } | 227 } |
224 | 228 |
225 if (location == UserScript::DOCUMENT_START) { | 229 if (location == UserScript::DOCUMENT_START) { |
226 num_css += script->css_scripts().size(); | 230 num_css += script->css_scripts().size(); |
227 for (UserScript::FileList::const_iterator iter = | 231 for (UserScript::FileList::const_iterator iter = |
228 script->css_scripts().begin(); | 232 script->css_scripts().begin(); |
229 iter != script->css_scripts().end(); | 233 iter != script->css_scripts().end(); |
230 ++iter) { | 234 ++iter) { |
231 frame->document().insertStyleSheet( | 235 frame->document().insertStyleSheet( |
232 WebString::fromUTF8(iter->GetContent().as_string())); | 236 WebString::fromUTF8(iter->GetContent().as_string())); |
233 } | 237 } |
234 } | 238 } |
235 | 239 |
236 if (script->run_location() == location) { | 240 if (script->run_location() == location) { |
241 // TODO(rdevlin.cronin): Right now, this is just a notification, but soon | |
242 // we should block without user consent. | |
243 if (PermissionsData::ShouldNotifyForScriptExecution(extension)) { | |
244 render_view->Send( | |
245 new ExtensionHostMsg_NotifyExtensionScriptExecution( | |
246 render_view->GetRoutingID(), | |
not at google - send to devlin
2014/05/07 22:49:02
(need page ID)
Devlin
2014/05/08 18:15:46
Done.
| |
247 extension->id())); | |
248 } | |
237 num_scripts += script->js_scripts().size(); | 249 num_scripts += script->js_scripts().size(); |
238 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 250 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
239 UserScript::File& file = script->js_scripts()[j]; | 251 UserScript::File& file = script->js_scripts()[j]; |
240 std::string content = file.GetContent().as_string(); | 252 std::string content = file.GetContent().as_string(); |
241 | 253 |
242 // We add this dumb function wrapper for standalone user script to | 254 // We add this dumb function wrapper for standalone user script to |
243 // emulate what Greasemonkey does. | 255 // emulate what Greasemonkey does. |
244 // TODO(aa): I think that maybe "is_standalone" scripts don't exist | 256 // TODO(aa): I think that maybe "is_standalone" scripts don't exist |
245 // anymore. Investigate. | 257 // anymore. Investigate. |
246 if (script->is_standalone() || script->emulate_greasemonkey()) { | 258 if (script->is_standalone() || script->emulate_greasemonkey()) { |
(...skipping 28 matching lines...) Expand all Loading... | |
275 iter != sources.end(); | 287 iter != sources.end(); |
276 ++iter) { | 288 ++iter) { |
277 extensions_executing_scripts[extension->id()].insert( | 289 extensions_executing_scripts[extension->id()].insert( |
278 GURL(iter->url).path()); | 290 GURL(iter->url).path()); |
279 } | 291 } |
280 } | 292 } |
281 } | 293 } |
282 | 294 |
283 // Notify the browser if any extensions are now executing scripts. | 295 // Notify the browser if any extensions are now executing scripts. |
284 if (!extensions_executing_scripts.empty()) { | 296 if (!extensions_executing_scripts.empty()) { |
285 blink::WebFrame* top_frame = frame->top(); | |
286 content::RenderView* render_view = | |
287 content::RenderView::FromWebView(top_frame->view()); | |
288 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( | 297 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( |
289 render_view->GetRoutingID(), | 298 render_view->GetRoutingID(), |
290 extensions_executing_scripts, | 299 extensions_executing_scripts, |
291 render_view->GetPageId(), | 300 render_view->GetPageId(), |
292 ScriptContext::GetDataSourceURLForFrame(top_frame))); | 301 ScriptContext::GetDataSourceURLForFrame(top_frame))); |
293 } | 302 } |
294 | 303 |
295 // Log debug info. | 304 // Log debug info. |
296 if (location == UserScript::DOCUMENT_START) { | 305 if (location == UserScript::DOCUMENT_START) { |
297 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 306 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
298 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 307 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
299 if (num_css || num_scripts) | 308 if (num_css || num_scripts) |
300 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 309 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
301 } else if (location == UserScript::DOCUMENT_END) { | 310 } else if (location == UserScript::DOCUMENT_END) { |
302 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 311 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
303 if (num_scripts) | 312 if (num_scripts) |
304 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 313 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
305 } else if (location == UserScript::DOCUMENT_IDLE) { | 314 } else if (location == UserScript::DOCUMENT_IDLE) { |
306 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 315 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
307 if (num_scripts) | 316 if (num_scripts) |
308 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 317 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
309 } else { | 318 } else { |
310 NOTREACHED(); | 319 NOTREACHED(); |
311 } | 320 } |
312 } | 321 } |
313 | 322 |
314 } // namespace extensions | 323 } // namespace extensions |
OLD | NEW |