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

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: address comments from Devlin 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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>& file : script_->js_scripts()) {
206 206 // TODO(lazyboy): Script contents are copied twice here! We could explore
207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 207 // leveraging WebString-s refcounting, which might be hard. At the very
208 iter != js_scripts.end(); 208 // least, we should make single copy for non-greasemonkey case.
209 ++iter) { 209 std::string content = file->GetContent().as_string();
210 std::string content = iter->GetContent().as_string();
211
212 // We add this dumb function wrapper for user scripts to emulate what 210 // We add this dumb function wrapper for user scripts to emulate what
213 // Greasemonkey does. 211 // Greasemonkey does.
214 if (script_->emulate_greasemonkey()) { 212 if (script_->emulate_greasemonkey()) {
215 content.insert(0, kUserScriptHead); 213 content.insert(0, kUserScriptHead);
216 content += kUserScriptTail; 214 content += kUserScriptTail;
217 } 215 }
218 sources.push_back(blink::WebScriptSource( 216 sources.push_back(blink::WebScriptSource(
219 blink::WebString::fromUTF8(content), iter->url())); 217 blink::WebString::fromUTF8(content), file->url()));
220 } 218 }
221 219
222 // Emulate Greasemonkey API for scripts that were converted to extension 220 // Emulate Greasemonkey API for scripts that were converted to extension
223 // user scripts. 221 // user scripts.
224 if (script_->emulate_greasemonkey()) 222 if (script_->emulate_greasemonkey())
225 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 223 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
226 224
227 return sources; 225 return sources;
228 } 226 }
229 227
230 std::vector<std::string> UserScriptInjector::GetCssSources( 228 std::vector<std::string> UserScriptInjector::GetCssSources(
231 UserScript::RunLocation run_location) const { 229 UserScript::RunLocation run_location) const {
232 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 230 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
233 231
234 std::vector<std::string> sources; 232 std::vector<std::string> sources;
235 if (!script_) 233 if (!script_)
236 return sources; 234 return sources;
237 235
238 const UserScript::FileList& css_scripts = script_->css_scripts(); 236 for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) {
239 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 237 // TODO(lazyboy): This is a StringPiece -> std::string copy, and then it
240 iter != css_scripts.end(); 238 // gets copied into a WebString where GetCssSources() is used :( Getting
241 ++iter) { 239 // rid of first copy should be trivial and we should also try to get rid of
242 sources.push_back(iter->GetContent().as_string()); 240 // the second copy.
241 sources.push_back(file->GetContent().as_string());
243 } 242 }
244 return sources; 243 return sources;
245 } 244 }
246 245
247 void UserScriptInjector::GetRunInfo( 246 void UserScriptInjector::GetRunInfo(
248 ScriptsRunInfo* scripts_run_info, 247 ScriptsRunInfo* scripts_run_info,
249 UserScript::RunLocation run_location) const { 248 UserScript::RunLocation run_location) const {
250 if (!script_) 249 if (!script_)
251 return; 250 return;
252 251
253 if (ShouldInjectJs(run_location)) { 252 if (ShouldInjectJs(run_location)) {
254 const UserScript::FileList& js_scripts = script_->js_scripts(); 253 const UserScript::FileList& js_scripts = 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<UserScript::File>& 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