| 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_injector.h" | 5 #include "extensions/renderer/user_script_injector.h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 web_frame, web_frame->document().url(), script_->match_about_blank()); | 190 web_frame, web_frame->document().url(), script_->match_about_blank()); |
| 191 | 191 |
| 192 return injection_host->CanExecuteOnFrame( | 192 return injection_host->CanExecuteOnFrame( |
| 193 effective_document_url, | 193 effective_document_url, |
| 194 content::RenderFrame::FromWebFrame(web_frame), | 194 content::RenderFrame::FromWebFrame(web_frame), |
| 195 tab_id, | 195 tab_id, |
| 196 is_declarative_); | 196 is_declarative_); |
| 197 } | 197 } |
| 198 | 198 |
| 199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
| 200 UserScript::RunLocation run_location, | 200 UserScript::RunLocation run_location) const { |
| 201 ScriptsRunInfo* scripts_run_info) const { | |
| 202 std::vector<blink::WebScriptSource> sources; | 201 std::vector<blink::WebScriptSource> sources; |
| 203 if (!script_) | 202 if (!script_) |
| 204 return sources; | 203 return sources; |
| 205 | 204 |
| 206 DCHECK_EQ(script_->run_location(), run_location); | 205 DCHECK_EQ(script_->run_location(), run_location); |
| 207 | 206 |
| 208 const UserScript::FileList& js_scripts = script_->js_scripts(); | 207 const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 209 | 208 |
| 210 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); | 209 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); |
| 211 iter != js_scripts.end(); | 210 iter != js_scripts.end(); |
| 212 ++iter) { | 211 ++iter) { |
| 213 const GURL& script_url = iter->url(); | |
| 214 // Check if the script is already injected. | |
| 215 if (scripts_run_info->injected_scripts.count(script_url) != 0) | |
| 216 continue; | |
| 217 | |
| 218 std::string content = iter->GetContent().as_string(); | 212 std::string content = iter->GetContent().as_string(); |
| 219 | 213 |
| 220 // We add this dumb function wrapper for user scripts to emulate what | 214 // We add this dumb function wrapper for user scripts to emulate what |
| 221 // Greasemonkey does. | 215 // Greasemonkey does. |
| 222 if (script_->emulate_greasemonkey()) { | 216 if (script_->emulate_greasemonkey()) { |
| 223 content.insert(0, kUserScriptHead); | 217 content.insert(0, kUserScriptHead); |
| 224 content += kUserScriptTail; | 218 content += kUserScriptTail; |
| 225 } | 219 } |
| 226 sources.push_back(blink::WebScriptSource( | 220 sources.push_back(blink::WebScriptSource( |
| 227 blink::WebString::fromUTF8(content), script_url)); | 221 blink::WebString::fromUTF8(content), iter->url())); |
| 228 | |
| 229 scripts_run_info->injected_scripts.insert(script_url); | |
| 230 } | 222 } |
| 231 | 223 |
| 232 // Emulate Greasemonkey API for scripts that were converted to extension | 224 // Emulate Greasemonkey API for scripts that were converted to extension |
| 233 // user scripts. | 225 // user scripts. |
| 234 if (script_->emulate_greasemonkey()) | 226 if (script_->emulate_greasemonkey()) |
| 235 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); | 227 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); |
| 236 | 228 |
| 237 return sources; | 229 return sources; |
| 238 } | 230 } |
| 239 | 231 |
| 240 std::vector<std::string> UserScriptInjector::GetCssSources( | 232 std::vector<std::string> UserScriptInjector::GetCssSources( |
| 241 UserScript::RunLocation run_location, | 233 UserScript::RunLocation run_location) const { |
| 242 ScriptsRunInfo* scripts_run_info) const { | |
| 243 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); | 234 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 244 | 235 |
| 245 std::vector<std::string> sources; | 236 std::vector<std::string> sources; |
| 246 if (!script_) | 237 if (!script_) |
| 247 return sources; | 238 return sources; |
| 248 | 239 |
| 249 const UserScript::FileList& css_scripts = script_->css_scripts(); | 240 const UserScript::FileList& css_scripts = script_->css_scripts(); |
| 250 | |
| 251 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); | 241 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); |
| 252 iter != css_scripts.end(); | 242 iter != css_scripts.end(); |
| 253 ++iter) { | 243 ++iter) { |
| 254 const GURL& script_url = iter->url(); | 244 sources.push_back(iter->GetContent().as_string()); |
| 255 // Check if the stylesheet is already injected. | |
| 256 if (scripts_run_info->injected_scripts.find(script_url) == | |
| 257 scripts_run_info->injected_scripts.end()) { | |
| 258 sources.push_back(iter->GetContent().as_string()); | |
| 259 | |
| 260 scripts_run_info->injected_scripts.insert(script_url); | |
| 261 } | |
| 262 } | 245 } |
| 263 return sources; | 246 return sources; |
| 264 } | 247 } |
| 265 | 248 |
| 266 void UserScriptInjector::GetRunInfo( | 249 void UserScriptInjector::GetRunInfo( |
| 267 ScriptsRunInfo* scripts_run_info, | 250 ScriptsRunInfo* scripts_run_info, |
| 268 UserScript::RunLocation run_location) const { | 251 UserScript::RunLocation run_location) const { |
| 269 if (!script_) | 252 if (!script_) |
| 270 return; | 253 return; |
| 271 | 254 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 287 void UserScriptInjector::OnInjectionComplete( | 270 void UserScriptInjector::OnInjectionComplete( |
| 288 std::unique_ptr<base::Value> execution_result, | 271 std::unique_ptr<base::Value> execution_result, |
| 289 UserScript::RunLocation run_location, | 272 UserScript::RunLocation run_location, |
| 290 content::RenderFrame* render_frame) {} | 273 content::RenderFrame* render_frame) {} |
| 291 | 274 |
| 292 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, | 275 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 293 content::RenderFrame* render_frame) { | 276 content::RenderFrame* render_frame) { |
| 294 } | 277 } |
| 295 | 278 |
| 296 } // namespace extensions | 279 } // namespace extensions |
| OLD | NEW |