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

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

Issue 2134613002: Stop injection when injections are invalidated Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Expand scope of ScriptInjectionWatchers 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 bool UserScriptInjector::IsUserGesture() const { 132 bool UserScriptInjector::IsUserGesture() const {
133 return false; 133 return false;
134 } 134 }
135 135
136 bool UserScriptInjector::ExpectsResults() const { 136 bool UserScriptInjector::ExpectsResults() const {
137 return false; 137 return false;
138 } 138 }
139 139
140 bool UserScriptInjector::ShouldInjectJs( 140 bool UserScriptInjector::ShouldInjectJs(
141 UserScript::RunLocation run_location) const { 141 UserScript::RunLocation run_location) const {
142 return script_ && script_->run_location() == run_location && 142 CHECK(script_);
143 return script_->run_location() == run_location &&
143 !script_->js_scripts().empty(); 144 !script_->js_scripts().empty();
144 } 145 }
145 146
146 bool UserScriptInjector::ShouldInjectCss( 147 bool UserScriptInjector::ShouldInjectCss(
147 UserScript::RunLocation run_location) const { 148 UserScript::RunLocation run_location) const {
148 return script_ && run_location == UserScript::DOCUMENT_START && 149 CHECK(script_);
150 return run_location == UserScript::DOCUMENT_START &&
149 !script_->css_scripts().empty(); 151 !script_->css_scripts().empty();
150 } 152 }
151 153
152 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( 154 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame(
153 const InjectionHost* injection_host, 155 const InjectionHost* injection_host,
154 blink::WebLocalFrame* web_frame, 156 blink::WebLocalFrame* web_frame,
155 int tab_id) const { 157 int tab_id) const {
156 // There is no harm in allowing the injection when the script is gone, 158 CHECK(script_);
157 // because there is nothing to inject.
158 if (!script_)
159 return PermissionsData::ACCESS_ALLOWED;
160 159
161 if (script_->consumer_instance_type() == 160 if (script_->consumer_instance_type() ==
162 UserScript::ConsumerInstanceType::WEBVIEW) { 161 UserScript::ConsumerInstanceType::WEBVIEW) {
163 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) 162 int routing_id = content::RenderView::FromWebView(web_frame->top()->view())
164 ->GetRoutingID(); 163 ->GetRoutingID();
165 164
166 RoutingInfoKey key(routing_id, script_->id()); 165 RoutingInfoKey key(routing_id, script_->id());
167 166
168 RoutingInfoMap& map = g_routing_info_map.Get(); 167 RoutingInfoMap& map = g_routing_info_map.Get();
169 auto iter = map.find(key); 168 auto iter = map.find(key);
(...skipping 21 matching lines...) Expand all
191 190
192 return injection_host->CanExecuteOnFrame( 191 return injection_host->CanExecuteOnFrame(
193 effective_document_url, 192 effective_document_url,
194 content::RenderFrame::FromWebFrame(web_frame), 193 content::RenderFrame::FromWebFrame(web_frame),
195 tab_id, 194 tab_id,
196 is_declarative_); 195 is_declarative_);
197 } 196 }
198 197
199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( 198 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources(
200 UserScript::RunLocation run_location) const { 199 UserScript::RunLocation run_location) const {
201 std::vector<blink::WebScriptSource> sources; 200 CHECK(script_);
202 if (!script_)
203 return sources;
204
205 DCHECK_EQ(script_->run_location(), run_location); 201 DCHECK_EQ(script_->run_location(), run_location);
206 202
207 const UserScript::FileList& js_scripts = script_->js_scripts(); 203 const UserScript::FileList& js_scripts = script_->js_scripts();
208 204
205 std::vector<blink::WebScriptSource> sources;
209 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 206 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
210 iter != js_scripts.end(); 207 iter != js_scripts.end();
211 ++iter) { 208 ++iter) {
212 std::string content = iter->GetContent().as_string(); 209 std::string content = iter->GetContent().as_string();
213 210
214 // 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
215 // Greasemonkey does. 212 // Greasemonkey does.
216 if (script_->emulate_greasemonkey()) { 213 if (script_->emulate_greasemonkey()) {
217 content.insert(0, kUserScriptHead); 214 content.insert(0, kUserScriptHead);
218 content += kUserScriptTail; 215 content += kUserScriptTail;
219 } 216 }
220 sources.push_back(blink::WebScriptSource( 217 sources.push_back(blink::WebScriptSource(
221 blink::WebString::fromUTF8(content), iter->url())); 218 blink::WebString::fromUTF8(content), iter->url()));
222 } 219 }
223 220
224 // Emulate Greasemonkey API for scripts that were converted to extension 221 // Emulate Greasemonkey API for scripts that were converted to extension
225 // user scripts. 222 // user scripts.
226 if (script_->emulate_greasemonkey()) 223 if (script_->emulate_greasemonkey())
227 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 224 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
228 225
229 return sources; 226 return sources;
230 } 227 }
231 228
232 std::vector<std::string> UserScriptInjector::GetCssSources( 229 std::vector<std::string> UserScriptInjector::GetCssSources(
233 UserScript::RunLocation run_location) const { 230 UserScript::RunLocation run_location) const {
234 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 231 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
232 CHECK(script_);
235 233
236 std::vector<std::string> sources; 234 std::vector<std::string> sources;
237 if (!script_)
238 return sources;
239
240 const UserScript::FileList& css_scripts = script_->css_scripts(); 235 const UserScript::FileList& css_scripts = script_->css_scripts();
241 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 236 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
242 iter != css_scripts.end(); 237 iter != css_scripts.end();
243 ++iter) { 238 ++iter) {
244 sources.push_back(iter->GetContent().as_string()); 239 sources.push_back(iter->GetContent().as_string());
245 } 240 }
246 return sources; 241 return sources;
247 } 242 }
248 243
249 void UserScriptInjector::GetRunInfo( 244 void UserScriptInjector::GetRunInfo(
250 ScriptsRunInfo* scripts_run_info, 245 ScriptsRunInfo* scripts_run_info,
251 UserScript::RunLocation run_location) const { 246 UserScript::RunLocation run_location) const {
252 if (!script_) 247 CHECK(script_);
253 return;
254 248
255 if (ShouldInjectJs(run_location)) { 249 if (ShouldInjectJs(run_location)) {
256 const UserScript::FileList& js_scripts = script_->js_scripts(); 250 const UserScript::FileList& js_scripts = script_->js_scripts();
257 scripts_run_info->num_js += js_scripts.size(); 251 scripts_run_info->num_js += js_scripts.size();
258 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 252 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
259 iter != js_scripts.end(); 253 iter != js_scripts.end();
260 ++iter) { 254 ++iter) {
261 scripts_run_info->executing_scripts[host_id_.id()].insert( 255 scripts_run_info->executing_scripts[host_id_.id()].insert(
262 iter->url().path()); 256 iter->url().path());
263 } 257 }
264 } 258 }
265 259
266 if (ShouldInjectCss(run_location)) 260 if (ShouldInjectCss(run_location))
267 scripts_run_info->num_css += script_->css_scripts().size(); 261 scripts_run_info->num_css += script_->css_scripts().size();
268 } 262 }
269 263
270 void UserScriptInjector::OnInjectionComplete( 264 void UserScriptInjector::OnInjectionComplete(
271 std::unique_ptr<base::Value> execution_result, 265 std::unique_ptr<base::Value> execution_result,
272 UserScript::RunLocation run_location, 266 UserScript::RunLocation run_location,
273 content::RenderFrame* render_frame) {} 267 content::RenderFrame* render_frame) {}
274 268
275 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 269 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
276 content::RenderFrame* render_frame) { 270 content::RenderFrame* render_frame) {
277 } 271 }
278 272
279 } // namespace extensions 273 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698