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

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: Addressed code review on patch 8 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
« 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 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) {
213 const GURL& script_url = iter->url();
214 // Check if the script is already injected.
215 if (scripts_run_info->injected_scripts.count(script_url) != 0)
216 continue;
217
212 std::string content = iter->GetContent().as_string(); 218 std::string content = iter->GetContent().as_string();
213 219
214 // We add this dumb function wrapper for user scripts to emulate what 220 // We add this dumb function wrapper for user scripts to emulate what
215 // Greasemonkey does. 221 // Greasemonkey does.
216 if (script_->emulate_greasemonkey()) { 222 if (script_->emulate_greasemonkey()) {
217 content.insert(0, kUserScriptHead); 223 content.insert(0, kUserScriptHead);
218 content += kUserScriptTail; 224 content += kUserScriptTail;
219 } 225 }
220 sources.push_back(blink::WebScriptSource( 226 sources.push_back(blink::WebScriptSource(
221 blink::WebString::fromUTF8(content), iter->url())); 227 blink::WebString::fromUTF8(content), script_url));
228
229 scripts_run_info->injected_scripts.insert(script_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 const GURL& script_url = iter->url();
255 // Check if the stylesheet is already injected.
256 if (scripts_run_info->injected_scripts.find(script_url) ==
257 scripts_run_info->injected_scripts.end()) {
258 sources.push_back(iter->GetContent().as_string());
259
260 scripts_run_info->injected_scripts.insert(script_url);
261 }
245 } 262 }
246 return sources; 263 return sources;
247 } 264 }
248 265
249 void UserScriptInjector::GetRunInfo( 266 void UserScriptInjector::GetRunInfo(
250 ScriptsRunInfo* scripts_run_info, 267 ScriptsRunInfo* scripts_run_info,
251 UserScript::RunLocation run_location) const { 268 UserScript::RunLocation run_location) const {
252 if (!script_) 269 if (!script_)
253 return; 270 return;
254 271
(...skipping 15 matching lines...) Expand all
270 void UserScriptInjector::OnInjectionComplete( 287 void UserScriptInjector::OnInjectionComplete(
271 std::unique_ptr<base::Value> execution_result, 288 std::unique_ptr<base::Value> execution_result,
272 UserScript::RunLocation run_location, 289 UserScript::RunLocation run_location,
273 content::RenderFrame* render_frame) {} 290 content::RenderFrame* render_frame) {}
274 291
275 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 292 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
276 content::RenderFrame* render_frame) { 293 content::RenderFrame* render_frame) {
277 } 294 }
278 295
279 } // namespace extensions 296 } // 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