OLD | NEW |
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/script_injection.h" | 5 #include "extensions/renderer/script_injection.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 | 102 |
103 // static | 103 // static |
104 void ScriptInjection::RemoveIsolatedWorld(const std::string& host_id) { | 104 void ScriptInjection::RemoveIsolatedWorld(const std::string& host_id) { |
105 g_isolated_worlds.Get().erase(host_id); | 105 g_isolated_worlds.Get().erase(host_id); |
106 } | 106 } |
107 | 107 |
108 ScriptInjection::ScriptInjection( | 108 ScriptInjection::ScriptInjection( |
109 scoped_ptr<ScriptInjector> injector, | 109 scoped_ptr<ScriptInjector> injector, |
110 blink::WebLocalFrame* web_frame, | 110 blink::WebLocalFrame* web_frame, |
111 const HostID& host_id, | 111 scoped_ptr<const InjectionHost> injection_host, |
112 const UserScript::ConsumerInstanceType& consumer_instance_type, | 112 const UserScript::ConsumerInstanceType& consumer_instance_type, |
113 UserScript::RunLocation run_location, | 113 UserScript::RunLocation run_location, |
114 int tab_id) | 114 int tab_id) |
115 : injector_(injector.Pass()), | 115 : injector_(injector.Pass()), |
116 web_frame_(web_frame), | 116 web_frame_(web_frame), |
117 host_id_(host_id), | 117 injection_host_(injection_host.Pass()), |
118 consumer_instance_type_(consumer_instance_type), | 118 consumer_instance_type_(consumer_instance_type), |
119 run_location_(run_location), | 119 run_location_(run_location), |
120 tab_id_(tab_id), | 120 tab_id_(tab_id), |
121 request_id_(kInvalidRequestId), | 121 request_id_(kInvalidRequestId), |
122 complete_(false) { | 122 complete_(false) { |
| 123 CHECK(injection_host_.get()); |
123 } | 124 } |
124 | 125 |
125 ScriptInjection::~ScriptInjection() { | 126 ScriptInjection::~ScriptInjection() { |
126 if (!complete_) | 127 if (!complete_) |
127 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); | 128 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); |
128 } | 129 } |
129 | 130 |
130 bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, | 131 bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, |
131 const InjectionHost* injection_host, | |
132 ScriptsRunInfo* scripts_run_info) { | 132 ScriptsRunInfo* scripts_run_info) { |
133 if (current_location < run_location_) | 133 if (current_location < run_location_) |
134 return false; // Wait for the right location. | 134 return false; // Wait for the right location. |
135 | 135 |
136 if (request_id_ != kInvalidRequestId) | 136 if (request_id_ != kInvalidRequestId) |
137 return false; // We're waiting for permission right now, try again later. | 137 return false; // We're waiting for permission right now, try again later. |
138 | 138 |
139 if (!injection_host) { | 139 if (!injection_host_) { |
140 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 140 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
141 return true; // We're done. | 141 return true; // We're done. |
142 } | 142 } |
143 | 143 |
144 switch (injector_->CanExecuteOnFrame(injection_host, web_frame_, tab_id_, | 144 switch (injector_->CanExecuteOnFrame( |
145 web_frame_->top()->document().url())) { | 145 injection_host_.get(), web_frame_, tab_id_, |
| 146 web_frame_->top()->document().url())) { |
146 case PermissionsData::ACCESS_DENIED: | 147 case PermissionsData::ACCESS_DENIED: |
147 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); | 148 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
148 return true; // We're done. | 149 return true; // We're done. |
149 case PermissionsData::ACCESS_WITHHELD: | 150 case PermissionsData::ACCESS_WITHHELD: |
150 SendInjectionMessage(true /* request permission */); | 151 SendInjectionMessage(true /* request permission */); |
151 return false; // Wait around for permission. | 152 return false; // Wait around for permission. |
152 case PermissionsData::ACCESS_ALLOWED: | 153 case PermissionsData::ACCESS_ALLOWED: |
153 Inject(injection_host, scripts_run_info); | 154 Inject(scripts_run_info); |
154 return true; // We're done! | 155 return true; // We're done! |
155 } | 156 } |
156 | 157 |
157 NOTREACHED(); | 158 NOTREACHED(); |
158 return false; | 159 return false; |
159 } | 160 } |
160 | 161 |
161 bool ScriptInjection::OnPermissionGranted(const InjectionHost* injection_host, | 162 bool ScriptInjection::OnPermissionGranted(ScriptsRunInfo* scripts_run_info) { |
162 ScriptsRunInfo* scripts_run_info) { | 163 if (!injection_host_) { |
163 if (!injection_host) { | |
164 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 164 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
165 return false; | 165 return false; |
166 } | 166 } |
167 | 167 |
168 Inject(injection_host, scripts_run_info); | 168 Inject(scripts_run_info); |
169 return true; | 169 return true; |
170 } | 170 } |
171 | 171 |
| 172 void ScriptInjection::OnHostRemoved() { |
| 173 injection_host_.reset(nullptr); |
| 174 } |
| 175 |
172 void ScriptInjection::SendInjectionMessage(bool request_permission) { | 176 void ScriptInjection::SendInjectionMessage(bool request_permission) { |
173 content::RenderView* render_view = | 177 content::RenderView* render_view = |
174 content::RenderView::FromWebView(web_frame()->top()->view()); | 178 content::RenderView::FromWebView(web_frame()->top()->view()); |
175 | 179 |
176 // If we are just notifying the browser of the injection, then send an | 180 // If we are just notifying the browser of the injection, then send an |
177 // invalid request (which is treated like a notification). | 181 // invalid request (which is treated like a notification). |
178 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; | 182 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; |
179 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( | 183 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
180 render_view->GetRoutingID(), | 184 render_view->GetRoutingID(), |
181 host_id_.id(), | 185 host_id().id(), |
182 injector_->script_type(), | 186 injector_->script_type(), |
183 request_id_)); | 187 request_id_)); |
184 } | 188 } |
185 | 189 |
186 void ScriptInjection::NotifyWillNotInject( | 190 void ScriptInjection::NotifyWillNotInject( |
187 ScriptInjector::InjectFailureReason reason) { | 191 ScriptInjector::InjectFailureReason reason) { |
188 complete_ = true; | 192 complete_ = true; |
189 injector_->OnWillNotInject(reason); | 193 injector_->OnWillNotInject(reason); |
190 } | 194 } |
191 | 195 |
192 void ScriptInjection::Inject(const InjectionHost* injection_host, | 196 void ScriptInjection::Inject(ScriptsRunInfo* scripts_run_info) { |
193 ScriptsRunInfo* scripts_run_info) { | 197 DCHECK(injection_host_); |
194 DCHECK(injection_host); | |
195 DCHECK(scripts_run_info); | 198 DCHECK(scripts_run_info); |
196 DCHECK(!complete_); | 199 DCHECK(!complete_); |
197 | 200 |
198 if (injection_host->ShouldNotifyBrowserOfInjection()) | 201 if (injection_host_->ShouldNotifyBrowserOfInjection()) |
199 SendInjectionMessage(false /* don't request permission */); | 202 SendInjectionMessage(false /* don't request permission */); |
200 | 203 |
201 std::vector<blink::WebFrame*> frame_vector; | 204 std::vector<blink::WebFrame*> frame_vector; |
202 frame_vector.push_back(web_frame_); | 205 frame_vector.push_back(web_frame_); |
203 if (injector_->ShouldExecuteInChildFrames()) | 206 if (injector_->ShouldExecuteInChildFrames()) |
204 AppendAllChildFrames(web_frame_, &frame_vector); | 207 AppendAllChildFrames(web_frame_, &frame_vector); |
205 | 208 |
206 scoped_ptr<blink::WebScopedUserGesture> gesture; | 209 scoped_ptr<blink::WebScopedUserGesture> gesture; |
207 if (injector_->IsUserGesture()) | 210 if (injector_->IsUserGesture()) |
208 gesture.reset(new blink::WebScopedUserGesture()); | 211 gesture.reset(new blink::WebScopedUserGesture()); |
(...skipping 12 matching lines...) Expand all Loading... |
221 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); | 224 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); |
222 | 225 |
223 // We recheck access here in the renderer for extra safety against races | 226 // We recheck access here in the renderer for extra safety against races |
224 // with navigation, but different frames can have different URLs, and the | 227 // with navigation, but different frames can have different URLs, and the |
225 // injection host might only have access to a subset of them. | 228 // injection host might only have access to a subset of them. |
226 // For child frames, we just skip ones the injection host doesn't have | 229 // For child frames, we just skip ones the injection host doesn't have |
227 // access to and carry on. | 230 // access to and carry on. |
228 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to | 231 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to |
229 // surface a request for a child frame. | 232 // surface a request for a child frame. |
230 // TODO(rdevlin.cronin): We should ask for permission somehow. | 233 // TODO(rdevlin.cronin): We should ask for permission somehow. |
231 if (injector_->CanExecuteOnFrame(injection_host, frame, tab_id_, top_url) == | 234 if (injector_->CanExecuteOnFrame( |
232 PermissionsData::ACCESS_DENIED) { | 235 injection_host_.get(), frame, tab_id_, top_url) == |
| 236 PermissionsData::ACCESS_DENIED) { |
233 DCHECK(frame->parent()); | 237 DCHECK(frame->parent()); |
234 continue; | 238 continue; |
235 } | 239 } |
236 if (inject_js) | 240 if (inject_js) |
237 InjectJs(injection_host, frame, execution_results.get()); | 241 InjectJs(frame, execution_results.get()); |
238 if (inject_css) | 242 if (inject_css) |
239 InjectCss(frame); | 243 InjectCss(frame); |
240 } | 244 } |
241 | 245 |
242 complete_ = true; | 246 complete_ = true; |
243 | 247 |
244 // TODO(hanxi): don't log these metrics for webUIs' injections. | 248 // TODO(hanxi): don't log these metrics for webUIs' injections. |
245 injector_->OnInjectionComplete(execution_results.Pass(), | 249 injector_->OnInjectionComplete(execution_results.Pass(), |
246 scripts_run_info, | 250 scripts_run_info, |
247 run_location_); | 251 run_location_); |
248 } | 252 } |
249 | 253 |
250 void ScriptInjection::InjectJs(const InjectionHost* injection_host, | 254 void ScriptInjection::InjectJs(blink::WebLocalFrame* frame, |
251 blink::WebLocalFrame* frame, | |
252 base::ListValue* execution_results) { | 255 base::ListValue* execution_results) { |
253 std::vector<blink::WebScriptSource> sources = | 256 std::vector<blink::WebScriptSource> sources = |
254 injector_->GetJsSources(run_location_); | 257 injector_->GetJsSources(run_location_); |
255 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 258 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
256 int world_id = in_main_world | 259 int world_id = in_main_world |
257 ? DOMActivityLogger::kMainWorldId | 260 ? DOMActivityLogger::kMainWorldId |
258 : GetIsolatedWorldIdForInstance(injection_host, frame); | 261 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
| 262 frame); |
259 bool expects_results = injector_->ExpectsResults(); | 263 bool expects_results = injector_->ExpectsResults(); |
260 | 264 |
261 base::ElapsedTimer exec_timer; | 265 base::ElapsedTimer exec_timer; |
262 if (injection_host->id().type() == HostID::EXTENSIONS) | 266 if (injection_host_->id().type() == HostID::EXTENSIONS) |
263 DOMActivityLogger::AttachToWorld(world_id, injection_host->id().id()); | 267 DOMActivityLogger::AttachToWorld(world_id, injection_host_->id().id()); |
264 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 268 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
265 v8::Local<v8::Value> script_value; | 269 v8::Local<v8::Value> script_value; |
266 if (in_main_world) { | 270 if (in_main_world) { |
267 // We only inject in the main world for javascript: urls. | 271 // We only inject in the main world for javascript: urls. |
268 DCHECK_EQ(1u, sources.size()); | 272 DCHECK_EQ(1u, sources.size()); |
269 | 273 |
270 const blink::WebScriptSource& source = sources.front(); | 274 const blink::WebScriptSource& source = sources.front(); |
271 if (expects_results) | 275 if (expects_results) |
272 script_value = frame->executeScriptAndReturnValue(source); | 276 script_value = frame->executeScriptAndReturnValue(source); |
273 else | 277 else |
274 frame->executeScript(source); | 278 frame->executeScript(source); |
275 } else { // in isolated world | 279 } else { // in isolated world |
276 scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; | 280 scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; |
277 if (expects_results) | 281 if (expects_results) |
278 results.reset(new blink::WebVector<v8::Local<v8::Value> >()); | 282 results.reset(new blink::WebVector<v8::Local<v8::Value> >()); |
279 frame->executeScriptInIsolatedWorld(world_id, | 283 frame->executeScriptInIsolatedWorld(world_id, |
280 &sources.front(), | 284 &sources.front(), |
281 sources.size(), | 285 sources.size(), |
282 EXTENSION_GROUP_CONTENT_SCRIPTS, | 286 EXTENSION_GROUP_CONTENT_SCRIPTS, |
283 results.get()); | 287 results.get()); |
284 if (expects_results && !results->isEmpty()) | 288 if (expects_results && !results->isEmpty()) |
285 script_value = (*results)[0]; | 289 script_value = (*results)[0]; |
286 } | 290 } |
287 | 291 |
288 if (injection_host->id().type() == HostID::EXTENSIONS) | 292 if (injection_host_->id().type() == HostID::EXTENSIONS) |
289 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 293 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
290 | 294 |
291 if (expects_results) { | 295 if (expects_results) { |
292 // Right now, we only support returning single results (per frame). | 296 // Right now, we only support returning single results (per frame). |
293 scoped_ptr<content::V8ValueConverter> v8_converter( | 297 scoped_ptr<content::V8ValueConverter> v8_converter( |
294 content::V8ValueConverter::create()); | 298 content::V8ValueConverter::create()); |
295 // It's safe to always use the main world context when converting | 299 // It's safe to always use the main world context when converting |
296 // here. V8ValueConverterImpl shouldn't actually care about the | 300 // here. V8ValueConverterImpl shouldn't actually care about the |
297 // context scope, and it switches to v8::Object's creation context | 301 // context scope, and it switches to v8::Object's creation context |
298 // when encountered. | 302 // when encountered. |
(...skipping 11 matching lines...) Expand all Loading... |
310 std::vector<std::string> css_sources = | 314 std::vector<std::string> css_sources = |
311 injector_->GetCssSources(run_location_); | 315 injector_->GetCssSources(run_location_); |
312 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); | 316 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); |
313 iter != css_sources.end(); | 317 iter != css_sources.end(); |
314 ++iter) { | 318 ++iter) { |
315 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); | 319 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); |
316 } | 320 } |
317 } | 321 } |
318 | 322 |
319 } // namespace extensions | 323 } // namespace extensions |
OLD | NEW |