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

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: sync @tott 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
« no previous file with comments | « extensions/renderer/user_script_injector.h ('k') | extensions/renderer/user_script_set.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 UserScript::FileList& js_scripts = script_->js_scripts();
206 sources.reserve(js_scripts.size()); 206 sources.reserve(js_scripts.size());
207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 207 for (const std::unique_ptr<UserScript::File>& file : js_scripts) {
208 iter != js_scripts.end(); 208 base::StringPiece script_content = file->GetContent();
209 ++iter) {
210 base::StringPiece script_content = iter->GetContent();
211 blink::WebString source; 209 blink::WebString source;
212 if (script_->emulate_greasemonkey()) { 210 if (script_->emulate_greasemonkey()) {
213 // 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
214 // Greasemonkey does. |script_content| becomes: 212 // Greasemonkey does. |script_content| becomes:
215 // concat(kUserScriptHead, script_content, kUserScriptTail). 213 // concat(kUserScriptHead, script_content, kUserScriptTail).
216 std::string content; 214 std::string content;
217 content.reserve(strlen(kUserScriptHead) + script_content.length() + 215 content.reserve(strlen(kUserScriptHead) + script_content.length() +
218 strlen(kUserScriptTail)); 216 strlen(kUserScriptTail));
219 content.append(kUserScriptHead); 217 content.append(kUserScriptHead);
220 script_content.AppendToString(&content); 218 script_content.AppendToString(&content);
221 content.append(kUserScriptTail); 219 content.append(kUserScriptTail);
222 // TODO(lazyboy): |content| is copied to |source|, should be avoided. 220 // TODO(lazyboy): |content| is copied to |source|, should be avoided.
223 // Investigate if we can leverage WebString's cheap copying mechanism 221 // Investigate if we can leverage WebString's cheap copying mechanism
224 // somehow. 222 // somehow.
225 source = blink::WebString::fromUTF8(content); 223 source = blink::WebString::fromUTF8(content);
226 } else { 224 } else {
227 source = blink::WebString::fromUTF8(script_content.data(), 225 source = blink::WebString::fromUTF8(script_content.data(),
228 script_content.length()); 226 script_content.length());
229 } 227 }
230 sources.push_back(blink::WebScriptSource(source, iter->url())); 228 sources.push_back(blink::WebScriptSource(source, file->url()));
231 } 229 }
232 230
233 // Emulate Greasemonkey API for scripts that were converted to extension 231 // Emulate Greasemonkey API for scripts that were converted to extension
234 // user scripts. 232 // user scripts.
235 if (script_->emulate_greasemonkey()) 233 if (script_->emulate_greasemonkey())
236 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 234 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
237 235
238 return sources; 236 return sources;
239 } 237 }
240 238
241 std::vector<blink::WebString> UserScriptInjector::GetCssSources( 239 std::vector<blink::WebString> UserScriptInjector::GetCssSources(
242 UserScript::RunLocation run_location) const { 240 UserScript::RunLocation run_location) const {
243 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 241 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
244 242
245 std::vector<blink::WebString> sources; 243 std::vector<blink::WebString> sources;
246 if (!script_) 244 if (!script_)
247 return sources; 245 return sources;
248 246
249 const UserScript::FileList& css_scripts = script_->css_scripts(); 247 const UserScript::FileList& css_scripts = script_->css_scripts();
250 sources.reserve(css_scripts.size()); 248 sources.reserve(css_scripts.size());
251 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 249 for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) {
252 iter != css_scripts.end(); 250 // TODO(lazyboy): |css_content| string is copied into blink::WebString for
253 ++iter) { 251 // every frame in the current renderer process. Avoid the copy, possibly by
254 base::StringPiece css_content = iter->GetContent(); 252 // only performing the copy once.
253 base::StringPiece css_content = file->GetContent();
255 sources.push_back( 254 sources.push_back(
256 blink::WebString::fromUTF8(css_content.data(), css_content.length())); 255 blink::WebString::fromUTF8(css_content.data(), css_content.length()));
257 } 256 }
258 return sources; 257 return sources;
259 } 258 }
260 259
261 void UserScriptInjector::GetRunInfo( 260 void UserScriptInjector::GetRunInfo(
262 ScriptsRunInfo* scripts_run_info, 261 ScriptsRunInfo* scripts_run_info,
263 UserScript::RunLocation run_location) const { 262 UserScript::RunLocation run_location) const {
264 if (!script_) 263 if (!script_)
265 return; 264 return;
266 265
267 if (ShouldInjectJs(run_location)) { 266 if (ShouldInjectJs(run_location)) {
268 const UserScript::FileList& js_scripts = script_->js_scripts(); 267 const UserScript::FileList& js_scripts = script_->js_scripts();
269 scripts_run_info->num_js += js_scripts.size(); 268 scripts_run_info->num_js += js_scripts.size();
270 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 269 for (const std::unique_ptr<UserScript::File>& iter : js_scripts) {
271 iter != js_scripts.end();
272 ++iter) {
273 scripts_run_info->executing_scripts[host_id_.id()].insert( 270 scripts_run_info->executing_scripts[host_id_.id()].insert(
274 iter->url().path()); 271 iter->url().path());
275 } 272 }
276 } 273 }
277 274
278 if (ShouldInjectCss(run_location)) 275 if (ShouldInjectCss(run_location))
279 scripts_run_info->num_css += script_->css_scripts().size(); 276 scripts_run_info->num_css += script_->css_scripts().size();
280 } 277 }
281 278
282 void UserScriptInjector::OnInjectionComplete( 279 void UserScriptInjector::OnInjectionComplete(
283 std::unique_ptr<base::Value> execution_result, 280 std::unique_ptr<base::Value> execution_result,
284 UserScript::RunLocation run_location, 281 UserScript::RunLocation run_location,
285 content::RenderFrame* render_frame) {} 282 content::RenderFrame* render_frame) {}
286 283
287 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 284 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
288 content::RenderFrame* render_frame) { 285 content::RenderFrame* render_frame) {
289 } 286 }
290 287
291 } // namespace extensions 288 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/user_script_injector.h ('k') | extensions/renderer/user_script_set.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698