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

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

Issue 2116923002: Avoid using stale UserScript pointers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove browser test, see bug for details 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 | « no previous file | 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
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<UserScript*>& scripts) { 104 const std::vector<UserScript*>& 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;
Devlin 2016/07/06 15:22:22 We should call ScriptInjection::OnHostRemoved() wh
robwu 2016/07/06 16:12:06 I considered it, but because the bug is only relat
Devlin 2016/07/06 16:17:58 I think I'd prefer checking the host in ScriptInje
robwu 2016/07/06 16:26:57 That's a good one. I will submit a separate CL sin
Devlin 2016/07/06 16:40:56 Sounds good, thanks!
108 return; 109 return;
110 }
109 111
110 for (std::vector<UserScript*>::const_iterator iter = scripts.begin(); 112 for (std::vector<UserScript*>::const_iterator iter = scripts.begin();
111 iter != scripts.end(); 113 iter != scripts.end();
112 ++iter) { 114 ++iter) {
113 // We need to compare to |script_id_| (and not to script_->id()) because the 115 // We need to compare to |script_id_| (and not to script_->id()) because the
114 // old |script_| may be deleted by now. 116 // old |script_| may be deleted by now.
115 if ((*iter)->id() == script_id_) { 117 if ((*iter)->id() == script_id_) {
116 script_ = *iter; 118 script_ = *iter;
117 break; 119 break;
118 } 120 }
(...skipping 11 matching lines...) Expand all
130 bool UserScriptInjector::IsUserGesture() const { 132 bool UserScriptInjector::IsUserGesture() const {
131 return false; 133 return false;
132 } 134 }
133 135
134 bool UserScriptInjector::ExpectsResults() const { 136 bool UserScriptInjector::ExpectsResults() const {
135 return false; 137 return false;
136 } 138 }
137 139
138 bool UserScriptInjector::ShouldInjectJs( 140 bool UserScriptInjector::ShouldInjectJs(
139 UserScript::RunLocation run_location) const { 141 UserScript::RunLocation run_location) const {
140 return script_->run_location() == run_location && 142 return script_ && script_->run_location() == run_location &&
141 !script_->js_scripts().empty(); 143 !script_->js_scripts().empty();
142 } 144 }
143 145
144 bool UserScriptInjector::ShouldInjectCss( 146 bool UserScriptInjector::ShouldInjectCss(
145 UserScript::RunLocation run_location) const { 147 UserScript::RunLocation run_location) const {
146 return run_location == UserScript::DOCUMENT_START && 148 return script_ && run_location == UserScript::DOCUMENT_START &&
147 !script_->css_scripts().empty(); 149 !script_->css_scripts().empty();
148 } 150 }
149 151
150 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( 152 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame(
151 const InjectionHost* injection_host, 153 const InjectionHost* injection_host,
152 blink::WebLocalFrame* web_frame, 154 blink::WebLocalFrame* web_frame,
153 int tab_id) const { 155 int tab_id) const {
156 // There is no harm in allowing the injection when the script is gone,
157 // because there is nothing to inject.
158 if (!script_)
159 return PermissionsData::ACCESS_ALLOWED;
160
154 if (script_->consumer_instance_type() == 161 if (script_->consumer_instance_type() ==
155 UserScript::ConsumerInstanceType::WEBVIEW) { 162 UserScript::ConsumerInstanceType::WEBVIEW) {
156 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) 163 int routing_id = content::RenderView::FromWebView(web_frame->top()->view())
157 ->GetRoutingID(); 164 ->GetRoutingID();
158 165
159 RoutingInfoKey key(routing_id, script_->id()); 166 RoutingInfoKey key(routing_id, script_->id());
160 167
161 RoutingInfoMap& map = g_routing_info_map.Get(); 168 RoutingInfoMap& map = g_routing_info_map.Get();
162 auto iter = map.find(key); 169 auto iter = map.find(key);
163 170
(...skipping 20 matching lines...) Expand all
184 191
185 return injection_host->CanExecuteOnFrame( 192 return injection_host->CanExecuteOnFrame(
186 effective_document_url, 193 effective_document_url,
187 content::RenderFrame::FromWebFrame(web_frame), 194 content::RenderFrame::FromWebFrame(web_frame),
188 tab_id, 195 tab_id,
189 is_declarative_); 196 is_declarative_);
190 } 197 }
191 198
192 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( 199 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources(
193 UserScript::RunLocation run_location) const { 200 UserScript::RunLocation run_location) const {
201 std::vector<blink::WebScriptSource> sources;
202 if (!script_)
203 return sources;
204
194 DCHECK_EQ(script_->run_location(), run_location); 205 DCHECK_EQ(script_->run_location(), run_location);
195 206
196 std::vector<blink::WebScriptSource> sources;
197 const UserScript::FileList& js_scripts = script_->js_scripts(); 207 const UserScript::FileList& js_scripts = script_->js_scripts();
198 208
199 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 209 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
200 iter != js_scripts.end(); 210 iter != js_scripts.end();
201 ++iter) { 211 ++iter) {
202 std::string content = iter->GetContent().as_string(); 212 std::string content = iter->GetContent().as_string();
203 213
204 // We add this dumb function wrapper for user scripts to emulate what 214 // We add this dumb function wrapper for user scripts to emulate what
205 // Greasemonkey does. 215 // Greasemonkey does.
206 if (script_->emulate_greasemonkey()) { 216 if (script_->emulate_greasemonkey()) {
(...skipping 10 matching lines...) Expand all
217 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); 227 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource());
218 228
219 return sources; 229 return sources;
220 } 230 }
221 231
222 std::vector<std::string> UserScriptInjector::GetCssSources( 232 std::vector<std::string> UserScriptInjector::GetCssSources(
223 UserScript::RunLocation run_location) const { 233 UserScript::RunLocation run_location) const {
224 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); 234 DCHECK_EQ(UserScript::DOCUMENT_START, run_location);
225 235
226 std::vector<std::string> sources; 236 std::vector<std::string> sources;
237 if (!script_)
238 return sources;
239
227 const UserScript::FileList& css_scripts = script_->css_scripts(); 240 const UserScript::FileList& css_scripts = script_->css_scripts();
228 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 241 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
229 iter != css_scripts.end(); 242 iter != css_scripts.end();
230 ++iter) { 243 ++iter) {
231 sources.push_back(iter->GetContent().as_string()); 244 sources.push_back(iter->GetContent().as_string());
232 } 245 }
233 return sources; 246 return sources;
234 } 247 }
235 248
236 void UserScriptInjector::GetRunInfo( 249 void UserScriptInjector::GetRunInfo(
237 ScriptsRunInfo* scripts_run_info, 250 ScriptsRunInfo* scripts_run_info,
238 UserScript::RunLocation run_location) const { 251 UserScript::RunLocation run_location) const {
252 if (!script_)
253 return;
254
239 if (ShouldInjectJs(run_location)) { 255 if (ShouldInjectJs(run_location)) {
240 const UserScript::FileList& js_scripts = script_->js_scripts(); 256 const UserScript::FileList& js_scripts = script_->js_scripts();
241 scripts_run_info->num_js += js_scripts.size(); 257 scripts_run_info->num_js += js_scripts.size();
242 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); 258 for (UserScript::FileList::const_iterator iter = js_scripts.begin();
243 iter != js_scripts.end(); 259 iter != js_scripts.end();
244 ++iter) { 260 ++iter) {
245 scripts_run_info->executing_scripts[host_id_.id()].insert( 261 scripts_run_info->executing_scripts[host_id_.id()].insert(
246 iter->url().path()); 262 iter->url().path());
247 } 263 }
248 } 264 }
249 265
250 if (ShouldInjectCss(run_location)) 266 if (ShouldInjectCss(run_location))
251 scripts_run_info->num_css += script_->css_scripts().size(); 267 scripts_run_info->num_css += script_->css_scripts().size();
252 } 268 }
253 269
254 void UserScriptInjector::OnInjectionComplete( 270 void UserScriptInjector::OnInjectionComplete(
255 std::unique_ptr<base::Value> execution_result, 271 std::unique_ptr<base::Value> execution_result,
256 UserScript::RunLocation run_location, 272 UserScript::RunLocation run_location,
257 content::RenderFrame* render_frame) {} 273 content::RenderFrame* render_frame) {}
258 274
259 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, 275 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason,
260 content::RenderFrame* render_frame) { 276 content::RenderFrame* render_frame) {
261 } 277 }
262 278
263 } // namespace extensions 279 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698