| 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* top_render_view = |
| 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::RequiresActionForScriptExecution(extension)) { |
| 244 top_render_view->Send( |
| 245 new ExtensionHostMsg_NotifyExtensionScriptExecution( |
| 246 top_render_view->GetRoutingID(), |
| 247 extension->id(), |
| 248 top_render_view->GetPageId())); |
| 249 } |
| 237 num_scripts += script->js_scripts().size(); | 250 num_scripts += script->js_scripts().size(); |
| 238 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 251 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
| 239 UserScript::File& file = script->js_scripts()[j]; | 252 UserScript::File& file = script->js_scripts()[j]; |
| 240 std::string content = file.GetContent().as_string(); | 253 std::string content = file.GetContent().as_string(); |
| 241 | 254 |
| 242 // We add this dumb function wrapper for standalone user script to | 255 // We add this dumb function wrapper for standalone user script to |
| 243 // emulate what Greasemonkey does. | 256 // emulate what Greasemonkey does. |
| 244 // TODO(aa): I think that maybe "is_standalone" scripts don't exist | 257 // TODO(aa): I think that maybe "is_standalone" scripts don't exist |
| 245 // anymore. Investigate. | 258 // anymore. Investigate. |
| 246 if (script->is_standalone() || script->emulate_greasemonkey()) { | 259 if (script->is_standalone() || script->emulate_greasemonkey()) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 275 iter != sources.end(); | 288 iter != sources.end(); |
| 276 ++iter) { | 289 ++iter) { |
| 277 extensions_executing_scripts[extension->id()].insert( | 290 extensions_executing_scripts[extension->id()].insert( |
| 278 GURL(iter->url).path()); | 291 GURL(iter->url).path()); |
| 279 } | 292 } |
| 280 } | 293 } |
| 281 } | 294 } |
| 282 | 295 |
| 283 // Notify the browser if any extensions are now executing scripts. | 296 // Notify the browser if any extensions are now executing scripts. |
| 284 if (!extensions_executing_scripts.empty()) { | 297 if (!extensions_executing_scripts.empty()) { |
| 285 blink::WebFrame* top_frame = frame->top(); | 298 top_render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( |
| 286 content::RenderView* render_view = | 299 top_render_view->GetRoutingID(), |
| 287 content::RenderView::FromWebView(top_frame->view()); | |
| 288 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( | |
| 289 render_view->GetRoutingID(), | |
| 290 extensions_executing_scripts, | 300 extensions_executing_scripts, |
| 291 render_view->GetPageId(), | 301 top_render_view->GetPageId(), |
| 292 ScriptContext::GetDataSourceURLForFrame(top_frame))); | 302 ScriptContext::GetDataSourceURLForFrame(top_frame))); |
| 293 } | 303 } |
| 294 | 304 |
| 295 // Log debug info. | 305 // Log debug info. |
| 296 if (location == UserScript::DOCUMENT_START) { | 306 if (location == UserScript::DOCUMENT_START) { |
| 297 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 307 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
| 298 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 308 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
| 299 if (num_css || num_scripts) | 309 if (num_css || num_scripts) |
| 300 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 310 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
| 301 } else if (location == UserScript::DOCUMENT_END) { | 311 } else if (location == UserScript::DOCUMENT_END) { |
| 302 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 312 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
| 303 if (num_scripts) | 313 if (num_scripts) |
| 304 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 314 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
| 305 } else if (location == UserScript::DOCUMENT_IDLE) { | 315 } else if (location == UserScript::DOCUMENT_IDLE) { |
| 306 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 316 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
| 307 if (num_scripts) | 317 if (num_scripts) |
| 308 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 318 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
| 309 } else { | 319 } else { |
| 310 NOTREACHED(); | 320 NOTREACHED(); |
| 311 } | 321 } |
| 312 } | 322 } |
| 313 | 323 |
| 314 } // namespace extensions | 324 } // namespace extensions |
| OLD | NEW |