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

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

Issue 2116613002: Prevent duplicate content script injection defined in manifest.json (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tests Added Created 4 years, 5 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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 web_frame, web_frame->document().url(), script_->match_about_blank()); 190 web_frame, web_frame->document().url(), script_->match_about_blank());
191 191
192 return injection_host->CanExecuteOnFrame( 192 return injection_host->CanExecuteOnFrame(
193 effective_document_url, 193 effective_document_url,
194 content::RenderFrame::FromWebFrame(web_frame), 194 content::RenderFrame::FromWebFrame(web_frame),
195 tab_id, 195 tab_id,
196 is_declarative_); 196 is_declarative_);
197 } 197 }
198 198
199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( 199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources(
200 UserScript::RunLocation run_location) const { 200 UserScript::RunLocation run_location,
201 ScriptsRunInfo* scripts_run_info) const {
201 std::vector<blink::WebScriptSource> sources; 202 std::vector<blink::WebScriptSource> sources;
202 if (!script_) 203 if (!script_)
203 return sources; 204 return sources;
204 205
205 DCHECK_EQ(script_->run_location(), run_location); 206 DCHECK_EQ(script_->run_location(), run_location);
206 207
207 const UserScript::FileList& js_scripts = script_->js_scripts(); 208 const UserScript::FileList& js_scripts = script_->js_scripts();
208 209
209 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 210 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
210 iter != js_scripts.end(); 211 iter != js_scripts.end();
211 ++iter) { 212 ++iter) {
212 std::string content = iter->GetContent().as_string(); 213 GURL scriptUrl = iter->url();
Devlin 2016/07/11 23:45:50 This creates a copy of GURL - prefer const &.
Devlin 2016/07/11 23:45:50 variable names should_be_underscored
catmullings 2016/07/12 22:23:26 Done.
catmullings 2016/07/12 22:23:26 Done.
214 // Check if the script is already injected
215 if(scripts_run_info->injected_scripts.find(scriptUrl)
Devlin 2016/07/11 23:45:50 space between 'if' and '('. Also, since injected_
catmullings 2016/07/12 22:23:26 Done.
216 == scripts_run_info->injected_scripts.end()) {
217 std::string content = iter->GetContent().as_string();
213 218
214 // We add this dumb function wrapper for user scripts to emulate what 219 // We add this dumb function wrapper for user scripts to emulate what
215 // Greasemonkey does. 220 // Greasemonkey does.
216 if (script_->emulate_greasemonkey()) { 221 if (script_->emulate_greasemonkey()) {
217 content.insert(0, kUserScriptHead); 222 content.insert(0, kUserScriptHead);
218 content += kUserScriptTail; 223 content += kUserScriptTail;
224 }
225 sources.push_back(blink::WebScriptSource(
226 blink::WebString::fromUTF8(content), scriptUrl));
227
228 scripts_run_info->injected_scripts.insert(scriptUrl);
219 } 229 }
220 sources.push_back(blink::WebScriptSource(
221 blink::WebString::fromUTF8(content), iter->url()));
222 } 230 }
223 231
224 // Emulate Greasemonkey API for scripts that were converted to extension 232 // Emulate Greasemonkey API for scripts that were converted to extension
225 // user scripts. 233 // user scripts.
226 if (script_->emulate_greasemonkey()) 234 if (script_->emulate_greasemonkey())
227 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 235 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
228 236
229 return sources; 237 return sources;
230 } 238 }
231 239
232 std::vector<std::string> UserScriptInjector::GetCssSources( 240 std::vector<std::string> UserScriptInjector::GetCssSources(
233 UserScript::RunLocation run_location) const { 241 UserScript::RunLocation run_location,
242 ScriptsRunInfo* scripts_run_info) const {
234 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 243 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
235 244
236 std::vector<std::string> sources; 245 std::vector<std::string> sources;
237 if (!script_) 246 if (!script_)
238 return sources; 247 return sources;
239 248
240 const UserScript::FileList& css_scripts = script_->css_scripts(); 249 const UserScript::FileList& css_scripts = script_->css_scripts();
250
241 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 251 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
242 iter != css_scripts.end(); 252 iter != css_scripts.end();
243 ++iter) { 253 ++iter) {
244 sources.push_back(iter->GetContent().as_string()); 254 GURL scriptUrl = iter->url();
255 // Check if the script is already injected
256 if(scripts_run_info->injected_scripts.find(scriptUrl)
257 == scripts_run_info->injected_scripts.end()) {
258 std::string content = iter->GetContent().as_string();
259 sources.push_back(iter->GetContent().as_string());
260
261 scripts_run_info->injected_scripts.insert(scriptUrl);
262 }
245 } 263 }
246 return sources; 264 return sources;
247 } 265 }
248 266
249 void UserScriptInjector::GetRunInfo( 267 void UserScriptInjector::GetRunInfo(
250 ScriptsRunInfo* scripts_run_info, 268 ScriptsRunInfo* scripts_run_info,
251 UserScript::RunLocation run_location) const { 269 UserScript::RunLocation run_location) const {
252 if (!script_) 270 if (!script_)
253 return; 271 return;
254 272
(...skipping 15 matching lines...) Expand all
270 void UserScriptInjector::OnInjectionComplete( 288 void UserScriptInjector::OnInjectionComplete(
271 std::unique_ptr<base::Value> execution_result, 289 std::unique_ptr<base::Value> execution_result,
272 UserScript::RunLocation run_location, 290 UserScript::RunLocation run_location,
273 content::RenderFrame* render_frame) {} 291 content::RenderFrame* render_frame) {}
274 292
275 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 293 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
276 content::RenderFrame* render_frame) { 294 content::RenderFrame* render_frame) {
277 } 295 }
278 296
279 } // namespace extensions 297 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698