| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return blink::WebScriptSource(source_); | 77 return blink::WebScriptSource(source_); |
| 78 } | 78 } |
| 79 | 79 |
| 80 base::LazyInstance<GreasemonkeyApiJsString>::Leaky g_greasemonkey_api = | 80 base::LazyInstance<GreasemonkeyApiJsString>::Leaky g_greasemonkey_api = |
| 81 LAZY_INSTANCE_INITIALIZER; | 81 LAZY_INSTANCE_INITIALIZER; |
| 82 | 82 |
| 83 bool ShouldInjectScripts(const UserScript::FileList& scripts, | 83 bool ShouldInjectScripts(const UserScript::FileList& scripts, |
| 84 const std::set<std::string>& injected_files) { | 84 const std::set<std::string>& injected_files) { |
| 85 for (const std::unique_ptr<UserScript::File>& file : scripts) { | 85 for (const std::unique_ptr<UserScript::File>& file : scripts) { |
| 86 // Check if the script is already injected. | 86 // Check if the script is already injected. |
| 87 if (injected_files.count(file->url().path()) == 0) { | 87 if (injected_files.count(file->url().path().as_string()) == 0) { |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 | 93 |
| 94 } // namespace | 94 } // namespace |
| 95 | 95 |
| 96 UserScriptInjector::UserScriptInjector(const UserScript* script, | 96 UserScriptInjector::UserScriptInjector(const UserScript* script, |
| 97 UserScriptSet* script_list, | 97 UserScriptSet* script_list, |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 const UserScript::FileList& js_scripts = script_->js_scripts(); | 220 const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 221 sources.reserve(js_scripts.size() + | 221 sources.reserve(js_scripts.size() + |
| 222 (script_->emulate_greasemonkey() ? 1 : 0)); | 222 (script_->emulate_greasemonkey() ? 1 : 0)); |
| 223 // Emulate Greasemonkey API for scripts that were converted to extension | 223 // Emulate Greasemonkey API for scripts that were converted to extension |
| 224 // user scripts. | 224 // user scripts. |
| 225 if (script_->emulate_greasemonkey()) | 225 if (script_->emulate_greasemonkey()) |
| 226 sources.push_back(g_greasemonkey_api.Get().GetSource()); | 226 sources.push_back(g_greasemonkey_api.Get().GetSource()); |
| 227 for (const std::unique_ptr<UserScript::File>& file : js_scripts) { | 227 for (const std::unique_ptr<UserScript::File>& file : js_scripts) { |
| 228 const GURL& script_url = file->url(); | 228 const GURL& script_url = file->url(); |
| 229 // Check if the script is already injected. | 229 // Check if the script is already injected. |
| 230 if (executing_scripts->count(script_url.path()) != 0) | 230 if (executing_scripts->count(script_url.path().as_string()) != 0) |
| 231 continue; | 231 continue; |
| 232 | 232 |
| 233 sources.push_back(blink::WebScriptSource( | 233 sources.push_back(blink::WebScriptSource( |
| 234 user_script_set_->GetJsSource(*file, script_->emulate_greasemonkey()), | 234 user_script_set_->GetJsSource(*file, script_->emulate_greasemonkey()), |
| 235 script_url)); | 235 script_url)); |
| 236 | 236 |
| 237 (*num_injected_js_scripts) += 1; | 237 (*num_injected_js_scripts) += 1; |
| 238 executing_scripts->insert(script_url.path()); | 238 executing_scripts->insert(script_url.path().as_string()); |
| 239 } | 239 } |
| 240 | 240 |
| 241 return sources; | 241 return sources; |
| 242 } | 242 } |
| 243 | 243 |
| 244 std::vector<blink::WebString> UserScriptInjector::GetCssSources( | 244 std::vector<blink::WebString> UserScriptInjector::GetCssSources( |
| 245 UserScript::RunLocation run_location, | 245 UserScript::RunLocation run_location, |
| 246 std::set<std::string>* injected_stylesheets, | 246 std::set<std::string>* injected_stylesheets, |
| 247 size_t* num_injected_stylesheets) const { | 247 size_t* num_injected_stylesheets) const { |
| 248 DCHECK(script_); | 248 DCHECK(script_); |
| 249 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); | 249 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 250 | 250 |
| 251 std::vector<blink::WebString> sources; | 251 std::vector<blink::WebString> sources; |
| 252 | 252 |
| 253 const UserScript::FileList& css_scripts = script_->css_scripts(); | 253 const UserScript::FileList& css_scripts = script_->css_scripts(); |
| 254 sources.reserve(css_scripts.size()); | 254 sources.reserve(css_scripts.size()); |
| 255 for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) { | 255 for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) { |
| 256 const std::string& stylesheet_path = file->url().path(); | 256 const std::string& stylesheet_path = file->url().path().as_string(); |
| 257 // Check if the stylesheet is already injected. | 257 // Check if the stylesheet is already injected. |
| 258 if (injected_stylesheets->count(stylesheet_path) != 0) | 258 if (injected_stylesheets->count(stylesheet_path) != 0) |
| 259 continue; | 259 continue; |
| 260 | 260 |
| 261 sources.push_back(user_script_set_->GetCssSource(*file)); | 261 sources.push_back(user_script_set_->GetCssSource(*file)); |
| 262 (*num_injected_stylesheets) += 1; | 262 (*num_injected_stylesheets) += 1; |
| 263 injected_stylesheets->insert(stylesheet_path); | 263 injected_stylesheets->insert(stylesheet_path); |
| 264 } | 264 } |
| 265 return sources; | 265 return sources; |
| 266 } | 266 } |
| 267 | 267 |
| 268 void UserScriptInjector::OnInjectionComplete( | 268 void UserScriptInjector::OnInjectionComplete( |
| 269 std::unique_ptr<base::Value> execution_result, | 269 std::unique_ptr<base::Value> execution_result, |
| 270 UserScript::RunLocation run_location, | 270 UserScript::RunLocation run_location, |
| 271 content::RenderFrame* render_frame) {} | 271 content::RenderFrame* render_frame) {} |
| 272 | 272 |
| 273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, | 273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 274 content::RenderFrame* render_frame) { | 274 content::RenderFrame* render_frame) { |
| 275 } | 275 } |
| 276 | 276 |
| 277 } // namespace extensions | 277 } // namespace extensions |
| OLD | NEW |