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