Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: extensions/renderer/user_script_injector.cc

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: uplaod with base Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 78
79 blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { 79 blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const {
80 return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); 80 return blink::WebScriptSource(blink::WebString::fromUTF8(source_));
81 } 81 }
82 82
83 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = 83 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api =
84 LAZY_INSTANCE_INITIALIZER; 84 LAZY_INSTANCE_INITIALIZER;
85 85
86 } // namespace 86 } // namespace
87 87
88 UserScriptInjector::UserScriptInjector(const UserScript* script, 88 UserScriptInjector::UserScriptInjector(const RendererUserScript* script,
89 UserScriptSet* script_list, 89 UserScriptSet* script_list,
90 bool is_declarative) 90 bool is_declarative)
91 : script_(script), 91 : script_(script),
92 script_id_(script_->id()), 92 script_id_(script_->id()),
93 host_id_(script_->host_id()), 93 host_id_(script_->host_id()),
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 std::vector<std::unique_ptr<RendererUserScript>>& 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<RendererUserScript>& 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.
115 if (script->id() == script_id_) { 115 if (script->id() == script_id_) {
116 script_ = script.get(); 116 script_ = script.get();
117 break; 117 break;
118 } 118 }
119 } 119 }
120 } 120 }
121 121
122 UserScript::InjectionType UserScriptInjector::script_type() const { 122 UserScript::InjectionType UserScriptInjector::script_type() const {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 const std::vector<std::unique_ptr<RendererScriptFile>>& js_scripts =
206 script_->js_scripts();
206 207
207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 208 for (const std::unique_ptr<RendererScriptFile>& iter : js_scripts) {
208 iter != js_scripts.end();
209 ++iter) {
210 std::string content = iter->GetContent().as_string(); 209 std::string content = iter->GetContent().as_string();
211 210
212 // We add this dumb function wrapper for user scripts to emulate what 211 // We add this dumb function wrapper for user scripts to emulate what
213 // Greasemonkey does. 212 // Greasemonkey does.
214 if (script_->emulate_greasemonkey()) { 213 if (script_->emulate_greasemonkey()) {
215 content.insert(0, kUserScriptHead); 214 content.insert(0, kUserScriptHead);
216 content += kUserScriptTail; 215 content += kUserScriptTail;
217 } 216 }
218 sources.push_back(blink::WebScriptSource( 217 sources.push_back(blink::WebScriptSource(
219 blink::WebString::fromUTF8(content), iter->url())); 218 blink::WebString::fromUTF8(content), iter->url()));
220 } 219 }
221 220
222 // Emulate Greasemonkey API for scripts that were converted to extension 221 // Emulate Greasemonkey API for scripts that were converted to extension
223 // user scripts. 222 // user scripts.
224 if (script_->emulate_greasemonkey()) 223 if (script_->emulate_greasemonkey())
225 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 224 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
226 225
227 return sources; 226 return sources;
228 } 227 }
229 228
230 std::vector<std::string> UserScriptInjector::GetCssSources( 229 std::vector<std::string> UserScriptInjector::GetCssSources(
231 UserScript::RunLocation run_location) const { 230 UserScript::RunLocation run_location) const {
232 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 231 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
233 232
234 std::vector<std::string> sources; 233 std::vector<std::string> sources;
235 if (!script_) 234 if (!script_)
236 return sources; 235 return sources;
237 236
238 const UserScript::FileList& css_scripts = script_->css_scripts(); 237 const std::vector<std::unique_ptr<RendererScriptFile>>& css_scripts =
239 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 238 script_->css_scripts();
240 iter != css_scripts.end(); 239 for (const std::unique_ptr<RendererScriptFile>& iter : css_scripts) {
241 ++iter) {
242 sources.push_back(iter->GetContent().as_string()); 240 sources.push_back(iter->GetContent().as_string());
243 } 241 }
244 return sources; 242 return sources;
245 } 243 }
246 244
247 void UserScriptInjector::GetRunInfo( 245 void UserScriptInjector::GetRunInfo(
248 ScriptsRunInfo* scripts_run_info, 246 ScriptsRunInfo* scripts_run_info,
249 UserScript::RunLocation run_location) const { 247 UserScript::RunLocation run_location) const {
250 if (!script_) 248 if (!script_)
251 return; 249 return;
252 250
253 if (ShouldInjectJs(run_location)) { 251 if (ShouldInjectJs(run_location)) {
254 const UserScript::FileList& js_scripts = script_->js_scripts(); 252 const std::vector<std::unique_ptr<RendererScriptFile>>& js_scripts =
253 script_->js_scripts();
255 scripts_run_info->num_js += js_scripts.size(); 254 scripts_run_info->num_js += js_scripts.size();
256 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 255 for (const std::unique_ptr<RendererScriptFile>& iter : js_scripts) {
257 iter != js_scripts.end();
258 ++iter) {
259 scripts_run_info->executing_scripts[host_id_.id()].insert( 256 scripts_run_info->executing_scripts[host_id_.id()].insert(
260 iter->url().path()); 257 iter->url().path());
261 } 258 }
262 } 259 }
263 260
264 if (ShouldInjectCss(run_location)) 261 if (ShouldInjectCss(run_location))
265 scripts_run_info->num_css += script_->css_scripts().size(); 262 scripts_run_info->num_css += script_->css_scripts().size();
266 } 263 }
267 264
268 void UserScriptInjector::OnInjectionComplete( 265 void UserScriptInjector::OnInjectionComplete(
269 std::unique_ptr<base::Value> execution_result, 266 std::unique_ptr<base::Value> execution_result,
270 UserScript::RunLocation run_location, 267 UserScript::RunLocation run_location,
271 content::RenderFrame* render_frame) {} 268 content::RenderFrame* render_frame) {}
272 269
273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 270 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
274 content::RenderFrame* render_frame) { 271 content::RenderFrame* render_frame) {
275 } 272 }
276 273
277 } // namespace extensions 274 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698