Chromium Code Reviews| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 is_declarative_(is_declarative), | 94 is_declarative_(is_declarative), |
| 95 user_script_set_observer_(this) { | 95 user_script_set_observer_(this) { |
| 96 user_script_set_observer_.Add(script_list); | 96 user_script_set_observer_.Add(script_list); |
| 97 } | 97 } |
| 98 | 98 |
| 99 UserScriptInjector::~UserScriptInjector() { | 99 UserScriptInjector::~UserScriptInjector() { |
| 100 } | 100 } |
| 101 | 101 |
| 102 void UserScriptInjector::OnUserScriptsUpdated( | 102 void UserScriptInjector::OnUserScriptsUpdated( |
| 103 const std::set<HostID>& changed_hosts, | 103 const std::set<HostID>& changed_hosts, |
| 104 const std::vector<std::unique_ptr<UserScript>>& scripts) { | 104 const UserScriptList& scripts) { |
| 105 // If the host causing this injection changed, then this injection | 105 // If the host causing this injection changed, then this injection |
| 106 // will be removed, and there's no guarantee the backing script still exists. | 106 // will be removed, and there's no guarantee the backing script still exists. |
| 107 if (changed_hosts.count(host_id_) > 0) { | 107 if (changed_hosts.count(host_id_) > 0) { |
| 108 script_ = nullptr; | 108 script_ = nullptr; |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 for (const std::unique_ptr<UserScript>& script : scripts) { | 112 for (const std::unique_ptr<UserScript>& script : scripts) { |
| 113 // We need to compare to |script_id_| (and not to script_->id()) because the | 113 // We need to compare to |script_id_| (and not to script_->id()) because the |
| 114 // old |script_| may be deleted by now. | 114 // old |script_| may be deleted by now. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 } | 195 } |
| 196 | 196 |
| 197 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 197 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
| 198 UserScript::RunLocation run_location) const { | 198 UserScript::RunLocation run_location) const { |
| 199 std::vector<blink::WebScriptSource> sources; | 199 std::vector<blink::WebScriptSource> sources; |
| 200 if (!script_) | 200 if (!script_) |
| 201 return sources; | 201 return sources; |
| 202 | 202 |
| 203 DCHECK_EQ(script_->run_location(), run_location); | 203 DCHECK_EQ(script_->run_location(), run_location); |
| 204 | 204 |
| 205 const UserScript::FileList& js_scripts = script_->js_scripts(); | 205 for (const std::unique_ptr<UserScript::File>& iter : script_->js_scripts()) { |
|
Devlin
2016/08/17 16:39:31
s/iter/file
lazyboy
2016/08/17 18:55:52
Done.
| |
| 206 | |
| 207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); | |
| 208 iter != js_scripts.end(); | |
| 209 ++iter) { | |
| 210 std::string content = iter->GetContent().as_string(); | 206 std::string content = iter->GetContent().as_string(); |
|
Devlin
2016/08/17 16:39:31
wait. Doesn't this mean that we're performing *an
lazyboy
2016/08/17 18:55:52
As discussed offline, there're actually two copies
| |
| 211 | |
| 212 // We add this dumb function wrapper for user scripts to emulate what | 207 // We add this dumb function wrapper for user scripts to emulate what |
| 213 // Greasemonkey does. | 208 // Greasemonkey does. |
| 214 if (script_->emulate_greasemonkey()) { | 209 if (script_->emulate_greasemonkey()) { |
| 215 content.insert(0, kUserScriptHead); | 210 content.insert(0, kUserScriptHead); |
| 216 content += kUserScriptTail; | 211 content += kUserScriptTail; |
| 217 } | 212 } |
| 218 sources.push_back(blink::WebScriptSource( | 213 sources.push_back(blink::WebScriptSource( |
| 219 blink::WebString::fromUTF8(content), iter->url())); | 214 blink::WebString::fromUTF8(content), iter->url())); |
| 220 } | 215 } |
| 221 | 216 |
| 222 // Emulate Greasemonkey API for scripts that were converted to extension | 217 // Emulate Greasemonkey API for scripts that were converted to extension |
| 223 // user scripts. | 218 // user scripts. |
| 224 if (script_->emulate_greasemonkey()) | 219 if (script_->emulate_greasemonkey()) |
| 225 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); | 220 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); |
| 226 | 221 |
| 227 return sources; | 222 return sources; |
| 228 } | 223 } |
| 229 | 224 |
| 230 std::vector<std::string> UserScriptInjector::GetCssSources( | 225 std::vector<std::string> UserScriptInjector::GetCssSources( |
| 231 UserScript::RunLocation run_location) const { | 226 UserScript::RunLocation run_location) const { |
| 232 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); | 227 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 233 | 228 |
| 234 std::vector<std::string> sources; | 229 std::vector<std::string> sources; |
| 235 if (!script_) | 230 if (!script_) |
| 236 return sources; | 231 return sources; |
| 237 | 232 |
| 238 const UserScript::FileList& css_scripts = script_->css_scripts(); | 233 for (const std::unique_ptr<UserScript::File>& iter : script_->css_scripts()) |
| 239 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); | |
| 240 iter != css_scripts.end(); | |
| 241 ++iter) { | |
| 242 sources.push_back(iter->GetContent().as_string()); | 234 sources.push_back(iter->GetContent().as_string()); |
|
Devlin
2016/08/17 16:39:31
ditto :(
lazyboy
2016/08/17 18:55:52
Added TODO
| |
| 243 } | |
| 244 return sources; | 235 return sources; |
| 245 } | 236 } |
| 246 | 237 |
| 247 void UserScriptInjector::GetRunInfo( | 238 void UserScriptInjector::GetRunInfo( |
| 248 ScriptsRunInfo* scripts_run_info, | 239 ScriptsRunInfo* scripts_run_info, |
| 249 UserScript::RunLocation run_location) const { | 240 UserScript::RunLocation run_location) const { |
| 250 if (!script_) | 241 if (!script_) |
| 251 return; | 242 return; |
| 252 | 243 |
| 253 if (ShouldInjectJs(run_location)) { | 244 if (ShouldInjectJs(run_location)) { |
| 254 const UserScript::FileList& js_scripts = script_->js_scripts(); | 245 const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 255 scripts_run_info->num_js += js_scripts.size(); | 246 scripts_run_info->num_js += js_scripts.size(); |
| 256 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); | 247 for (const std::unique_ptr<UserScript::File>& iter : js_scripts) { |
| 257 iter != js_scripts.end(); | |
| 258 ++iter) { | |
| 259 scripts_run_info->executing_scripts[host_id_.id()].insert( | 248 scripts_run_info->executing_scripts[host_id_.id()].insert( |
| 260 iter->url().path()); | 249 iter->url().path()); |
| 261 } | 250 } |
| 262 } | 251 } |
| 263 | 252 |
| 264 if (ShouldInjectCss(run_location)) | 253 if (ShouldInjectCss(run_location)) |
| 265 scripts_run_info->num_css += script_->css_scripts().size(); | 254 scripts_run_info->num_css += script_->css_scripts().size(); |
| 266 } | 255 } |
| 267 | 256 |
| 268 void UserScriptInjector::OnInjectionComplete( | 257 void UserScriptInjector::OnInjectionComplete( |
| 269 std::unique_ptr<base::Value> execution_result, | 258 std::unique_ptr<base::Value> execution_result, |
| 270 UserScript::RunLocation run_location, | 259 UserScript::RunLocation run_location, |
| 271 content::RenderFrame* render_frame) {} | 260 content::RenderFrame* render_frame) {} |
| 272 | 261 |
| 273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, | 262 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 274 content::RenderFrame* render_frame) { | 263 content::RenderFrame* render_frame) { |
| 275 } | 264 } |
| 276 | 265 |
| 277 } // namespace extensions | 266 } // namespace extensions |
| OLD | NEW |