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

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

Issue 2250263004: [Extensions] Reduce string copy in renderer/ UserScript. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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') | no next file » | 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 UserScript::FileList& js_scripts = script_->js_scripts();
206 206
Devlin 2016/08/18 17:08:16 sources.reserve()? (was this done in your other p
lazyboy 2016/08/18 18:53:23 Done, thanks. No, this wasn't done on my other CL.
207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 207 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
208 iter != js_scripts.end(); 208 iter != js_scripts.end();
209 ++iter) { 209 ++iter) {
210 std::string content = iter->GetContent().as_string(); 210 base::StringPiece script_content = iter->GetContent();
211
212 // We add this dumb function wrapper for user scripts to emulate what
213 // Greasemonkey does.
214 if (script_->emulate_greasemonkey()) { 211 if (script_->emulate_greasemonkey()) {
215 content.insert(0, kUserScriptHead); 212 // We add this dumb function wrapper for user scripts to emulate what
216 content += kUserScriptTail; 213 // Greasemonkey does: |script_content| becomes:
Devlin 2016/08/18 17:08:16 nit: replace the first ':' (after 'does') with a '
lazyboy 2016/08/18 18:53:23 Done.
214 // concat(kUserScriptHead, script_content, kUserScriptTail)
215 std::string content;
216 content.reserve(strlen(kUserScriptHead) + content.length() +
217 strlen(kUserScriptTail));
218 content.append(kUserScriptHead);
219 script_content.AppendToString(&content);
220 content.append(kUserScriptTail);
221 sources.push_back(blink::WebScriptSource(
222 blink::WebString::fromUTF8(content), iter->url()));
223 } else {
224 sources.push_back(blink::WebScriptSource(
Devlin 2016/08/18 17:08:16 We should keep some kind of TODO here to optimize
lazyboy 2016/08/18 18:53:23 Done.
225 blink::WebString::fromUTF8(script_content.data(),
Devlin 2016/08/18 17:08:16 hmm... I find it slightly more readable when we re
lazyboy 2016/08/18 18:53:23 Done.
226 script_content.length()),
227 iter->url()));
217 } 228 }
218 sources.push_back(blink::WebScriptSource(
219 blink::WebString::fromUTF8(content), iter->url()));
220 } 229 }
221 230
222 // Emulate Greasemonkey API for scripts that were converted to extension 231 // Emulate Greasemonkey API for scripts that were converted to extension
223 // user scripts. 232 // user scripts.
224 if (script_->emulate_greasemonkey()) 233 if (script_->emulate_greasemonkey())
225 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 234 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
226 235
227 return sources; 236 return sources;
228 } 237 }
229 238
230 std::vector<std::string> UserScriptInjector::GetCssSources( 239 std::vector<blink::WebString> UserScriptInjector::GetCssSources(
231 UserScript::RunLocation run_location) const { 240 UserScript::RunLocation run_location) const {
232 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 241 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
233 242
234 std::vector<std::string> sources; 243 std::vector<blink::WebString> sources;
235 if (!script_) 244 if (!script_)
236 return sources; 245 return sources;
237 246
238 const UserScript::FileList& css_scripts = script_->css_scripts(); 247 const UserScript::FileList& css_scripts = script_->css_scripts();
248 sources.reserve(css_scripts.size());
239 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 249 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
240 iter != css_scripts.end(); 250 iter != css_scripts.end();
241 ++iter) { 251 ++iter) {
242 sources.push_back(iter->GetContent().as_string()); 252 base::StringPiece css_content = iter->GetContent();
253 sources.push_back(
254 blink::WebString::fromUTF8(css_content.data(), css_content.length()));
243 } 255 }
244 return sources; 256 return sources;
245 } 257 }
246 258
247 void UserScriptInjector::GetRunInfo( 259 void UserScriptInjector::GetRunInfo(
248 ScriptsRunInfo* scripts_run_info, 260 ScriptsRunInfo* scripts_run_info,
249 UserScript::RunLocation run_location) const { 261 UserScript::RunLocation run_location) const {
250 if (!script_) 262 if (!script_)
251 return; 263 return;
252 264
(...skipping 15 matching lines...) Expand all
268 void UserScriptInjector::OnInjectionComplete( 280 void UserScriptInjector::OnInjectionComplete(
269 std::unique_ptr<base::Value> execution_result, 281 std::unique_ptr<base::Value> execution_result,
270 UserScript::RunLocation run_location, 282 UserScript::RunLocation run_location,
271 content::RenderFrame* render_frame) {} 283 content::RenderFrame* render_frame) {}
272 284
273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 285 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
274 content::RenderFrame* render_frame) { 286 content::RenderFrame* render_frame) {
275 } 287 }
276 288
277 } // namespace extensions 289 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/user_script_injector.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698