Chromium Code Reviews| 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" |
| 11 #include "base/timer/elapsed_timer.h" | 11 #include "base/timer/elapsed_timer.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/public/child/v8_value_converter.h" | 13 #include "content/public/child/v8_value_converter.h" |
| 14 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
| 15 #include "extensions/common/extension_messages.h" | 15 #include "extensions/common/extension_messages.h" |
| 16 #include "extensions/common/host_id.h" | 16 #include "extensions/common/host_id.h" |
| 17 #include "extensions/common/manifest_handlers/csp_info.h" | 17 #include "extensions/common/manifest_handlers/csp_info.h" |
| 18 #include "extensions/renderer/dom_activity_logger.h" | 18 #include "extensions/renderer/dom_activity_logger.h" |
| 19 #include "extensions/renderer/extension_groups.h" | 19 #include "extensions/renderer/extension_groups.h" |
| 20 #include "extensions/renderer/extension_injection_host.h" | 20 #include "extensions/renderer/extension_injection_host.h" |
| 21 #include "extensions/renderer/extensions_renderer_client.h" | 21 #include "extensions/renderer/extensions_renderer_client.h" |
| 22 #include "extensions/renderer/script_injection_callback.h" | |
| 23 #include "extensions/renderer/script_injection_manager.h" | |
| 22 #include "third_party/WebKit/public/platform/WebString.h" | 24 #include "third_party/WebKit/public/platform/WebString.h" |
| 23 #include "third_party/WebKit/public/web/WebDocument.h" | 25 #include "third_party/WebKit/public/web/WebDocument.h" |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 27 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 26 #include "third_party/WebKit/public/web/WebScriptSource.h" | 28 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 29 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 28 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 29 | 31 |
| 30 namespace extensions { | 32 namespace extensions { |
| 31 | 33 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 blink::WebLocalFrame* web_frame, | 112 blink::WebLocalFrame* web_frame, |
| 111 const HostID& host_id, | 113 const HostID& host_id, |
| 112 UserScript::RunLocation run_location, | 114 UserScript::RunLocation run_location, |
| 113 int tab_id) | 115 int tab_id) |
| 114 : injector_(injector.Pass()), | 116 : injector_(injector.Pass()), |
| 115 web_frame_(web_frame), | 117 web_frame_(web_frame), |
| 116 host_id_(host_id), | 118 host_id_(host_id), |
| 117 run_location_(run_location), | 119 run_location_(run_location), |
| 118 tab_id_(tab_id), | 120 tab_id_(tab_id), |
| 119 request_id_(kInvalidRequestId), | 121 request_id_(kInvalidRequestId), |
| 120 complete_(false) { | 122 complete_(false), |
| 123 running_frames_(0), | |
| 124 execution_results_(new base::ListValue()), | |
| 125 all_injection_started_(false), | |
| 126 script_injection_manager_(nullptr) { | |
| 121 } | 127 } |
| 122 | 128 |
| 123 ScriptInjection::~ScriptInjection() { | 129 ScriptInjection::~ScriptInjection() { |
| 124 if (!complete_) | 130 if (!complete_) |
| 125 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); | 131 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); |
| 126 } | 132 } |
| 127 | 133 |
| 128 bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, | 134 ScriptInjection::InjectionResult ScriptInjection::TryToInject( |
| 129 const InjectionHost* injection_host, | 135 UserScript::RunLocation current_location, |
| 130 ScriptsRunInfo* scripts_run_info) { | 136 const InjectionHost* injection_host, |
| 137 ScriptsRunInfo* scripts_run_info) { | |
| 131 if (current_location < run_location_) | 138 if (current_location < run_location_) |
| 132 return false; // Wait for the right location. | 139 return INJECTION_WAITING; // Wait for the right location. |
| 133 | 140 |
| 134 if (request_id_ != kInvalidRequestId) | 141 if (request_id_ != kInvalidRequestId) { |
| 135 return false; // We're waiting for permission right now, try again later. | 142 // We're waiting for permission right now, try again later. |
| 143 return INJECTION_WAITING; | |
| 144 } | |
| 136 | 145 |
| 137 if (!injection_host) { | 146 if (!injection_host) { |
| 138 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 147 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 139 return true; // We're done. | 148 return INJECTION_FINISHED; // We're done. |
| 140 } | 149 } |
| 141 | 150 |
| 142 switch (injector_->CanExecuteOnFrame(injection_host, web_frame_, tab_id_, | 151 switch (injector_->CanExecuteOnFrame(injection_host, web_frame_, tab_id_, |
| 143 web_frame_->top()->document().url())) { | 152 web_frame_->top()->document().url())) { |
| 144 case PermissionsData::ACCESS_DENIED: | 153 case PermissionsData::ACCESS_DENIED: |
| 145 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); | 154 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
| 146 return true; // We're done. | 155 return INJECTION_FINISHED; // We're done. |
| 147 case PermissionsData::ACCESS_WITHHELD: | 156 case PermissionsData::ACCESS_WITHHELD: |
| 148 SendInjectionMessage(true /* request permission */); | 157 SendInjectionMessage(true /* request permission */); |
| 149 return false; // Wait around for permission. | 158 return INJECTION_WAITING; // Wait around for permission. |
| 150 case PermissionsData::ACCESS_ALLOWED: | 159 case PermissionsData::ACCESS_ALLOWED: |
| 151 Inject(injection_host, scripts_run_info); | 160 return Inject(injection_host, scripts_run_info); |
| 152 return true; // We're done! | |
| 153 } | 161 } |
| 154 | 162 |
| 155 NOTREACHED(); | 163 NOTREACHED(); |
| 156 return false; | 164 return INJECTION_FINISHED; |
| 157 } | 165 } |
| 158 | 166 |
| 159 bool ScriptInjection::OnPermissionGranted(const InjectionHost* injection_host, | 167 ScriptInjection::InjectionResult ScriptInjection::OnPermissionGranted( |
| 160 ScriptsRunInfo* scripts_run_info) { | 168 const InjectionHost* injection_host, |
| 169 ScriptsRunInfo* scripts_run_info) { | |
| 161 if (!injection_host) { | 170 if (!injection_host) { |
| 162 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 171 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 163 return false; | 172 return INJECTION_FINISHED; |
| 164 } | 173 } |
| 165 | 174 |
| 166 Inject(injection_host, scripts_run_info); | 175 return Inject(injection_host, scripts_run_info); |
| 167 return true; | |
| 168 } | 176 } |
| 169 | 177 |
| 170 void ScriptInjection::SendInjectionMessage(bool request_permission) { | 178 void ScriptInjection::SendInjectionMessage(bool request_permission) { |
| 171 content::RenderView* render_view = | 179 content::RenderView* render_view = |
| 172 content::RenderView::FromWebView(web_frame()->top()->view()); | 180 content::RenderView::FromWebView(web_frame()->top()->view()); |
| 173 | 181 |
| 174 // If we are just notifying the browser of the injection, then send an | 182 // If we are just notifying the browser of the injection, then send an |
| 175 // invalid request (which is treated like a notification). | 183 // invalid request (which is treated like a notification). |
| 176 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; | 184 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; |
| 177 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( | 185 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
| 178 render_view->GetRoutingID(), | 186 render_view->GetRoutingID(), |
| 179 host_id_.id(), | 187 host_id_.id(), |
| 180 injector_->script_type(), | 188 injector_->script_type(), |
| 181 request_id_)); | 189 request_id_)); |
| 182 } | 190 } |
| 183 | 191 |
| 184 void ScriptInjection::NotifyWillNotInject( | 192 void ScriptInjection::NotifyWillNotInject( |
| 185 ScriptInjector::InjectFailureReason reason) { | 193 ScriptInjector::InjectFailureReason reason) { |
| 186 complete_ = true; | 194 complete_ = true; |
| 187 injector_->OnWillNotInject(reason); | 195 injector_->OnWillNotInject(reason); |
| 188 } | 196 } |
| 189 | 197 |
| 190 void ScriptInjection::Inject(const InjectionHost* injection_host, | 198 ScriptInjection::InjectionResult ScriptInjection::Inject( |
| 191 ScriptsRunInfo* scripts_run_info) { | 199 const InjectionHost* injection_host, |
| 200 ScriptsRunInfo* scripts_run_info) { | |
| 192 DCHECK(injection_host); | 201 DCHECK(injection_host); |
| 193 DCHECK(scripts_run_info); | 202 DCHECK(scripts_run_info); |
| 194 DCHECK(!complete_); | 203 DCHECK(!complete_); |
| 195 | 204 |
| 196 if (injection_host->ShouldNotifyBrowserOfInjection()) | 205 if (injection_host->ShouldNotifyBrowserOfInjection()) |
| 197 SendInjectionMessage(false /* don't request permission */); | 206 SendInjectionMessage(false /* don't request permission */); |
| 198 | 207 |
| 199 std::vector<blink::WebFrame*> frame_vector; | 208 std::vector<blink::WebFrame*> frame_vector; |
| 200 frame_vector.push_back(web_frame_); | 209 frame_vector.push_back(web_frame_); |
| 201 if (injector_->ShouldExecuteInChildFrames()) | 210 if (injector_->ShouldExecuteInChildFrames()) |
| 202 AppendAllChildFrames(web_frame_, &frame_vector); | 211 AppendAllChildFrames(web_frame_, &frame_vector); |
| 203 | 212 |
| 204 scoped_ptr<blink::WebScopedUserGesture> gesture; | |
| 205 if (injector_->IsUserGesture()) | |
| 206 gesture.reset(new blink::WebScopedUserGesture()); | |
| 207 | |
| 208 bool inject_js = injector_->ShouldInjectJs(run_location_); | 213 bool inject_js = injector_->ShouldInjectJs(run_location_); |
| 209 bool inject_css = injector_->ShouldInjectCss(run_location_); | 214 bool inject_css = injector_->ShouldInjectCss(run_location_); |
| 210 DCHECK(inject_js || inject_css); | 215 DCHECK(inject_js || inject_css); |
| 211 | 216 |
| 212 scoped_ptr<base::ListValue> execution_results(new base::ListValue()); | |
| 213 GURL top_url = web_frame_->top()->document().url(); | 217 GURL top_url = web_frame_->top()->document().url(); |
| 214 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); | 218 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); |
| 215 iter != frame_vector.end(); | 219 iter != frame_vector.end(); |
| 216 ++iter) { | 220 ++iter) { |
| 217 // TODO(dcheng): Unfortunately, the code as written won't work in an OOPI | 221 // TODO(dcheng): Unfortunately, the code as written won't work in an OOPI |
| 218 // world. This is just a temporary hack to make things compile. | 222 // world. This is just a temporary hack to make things compile. |
| 219 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); | 223 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); |
| 220 | 224 |
| 221 // We recheck access here in the renderer for extra safety against races | 225 // We recheck access here in the renderer for extra safety against races |
| 222 // with navigation, but different frames can have different URLs, and the | 226 // with navigation, but different frames can have different URLs, and the |
| 223 // injection host might only have access to a subset of them. | 227 // injection host might only have access to a subset of them. |
| 224 // For child frames, we just skip ones the injection host doesn't have | 228 // For child frames, we just skip ones the injection host doesn't have |
| 225 // access to and carry on. | 229 // access to and carry on. |
| 226 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to | 230 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to |
| 227 // surface a request for a child frame. | 231 // surface a request for a child frame. |
| 228 // TODO(rdevlin.cronin): We should ask for permission somehow. | 232 // TODO(rdevlin.cronin): We should ask for permission somehow. |
| 229 if (injector_->CanExecuteOnFrame(injection_host, frame, tab_id_, top_url) == | 233 if (injector_->CanExecuteOnFrame(injection_host, frame, tab_id_, top_url) == |
| 230 PermissionsData::ACCESS_DENIED) { | 234 PermissionsData::ACCESS_DENIED) { |
| 231 DCHECK(frame->parent()); | 235 DCHECK(frame->parent()); |
| 232 continue; | 236 continue; |
| 233 } | 237 } |
| 234 if (inject_js) | 238 if (inject_js) |
| 235 InjectJs(injection_host, frame, execution_results.get()); | 239 InjectJs(injection_host, frame); |
| 236 if (inject_css) | 240 if (inject_css) |
| 237 InjectCss(frame); | 241 InjectCss(frame); |
| 238 } | 242 } |
| 239 | 243 |
| 240 complete_ = true; | 244 all_injection_started_ = true; |
| 241 | 245 injector_->GetRunInfo(scripts_run_info, run_location_); |
| 242 // TODO(hanxi): don't log these metrics for webUIs' injections. | 246 TryToFinish(); |
|
Devlin
2015/02/27 18:25:32
This TryToFinish seems wrong, since it will notify
kozy
2015/02/28 15:23:21
If we have synchronous injections in two frames th
| |
| 243 injector_->OnInjectionComplete(execution_results.Pass(), | 247 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
| 244 scripts_run_info, | |
| 245 run_location_); | |
| 246 } | 248 } |
| 247 | 249 |
| 248 void ScriptInjection::InjectJs(const InjectionHost* injection_host, | 250 void ScriptInjection::InjectJs(const InjectionHost* injection_host, |
| 249 blink::WebLocalFrame* frame, | 251 blink::WebLocalFrame* frame) { |
| 250 base::ListValue* execution_results) { | 252 running_frames_++; |
|
Devlin
2015/02/27 18:25:32
nit: chrome style prefers pre-increment.
kozy
2015/02/28 15:23:21
Done.
| |
| 251 std::vector<blink::WebScriptSource> sources = | 253 std::vector<blink::WebScriptSource> sources = |
| 252 injector_->GetJsSources(run_location_); | 254 injector_->GetJsSources(run_location_); |
| 253 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 255 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 254 int world_id = in_main_world | 256 int world_id = in_main_world |
| 255 ? DOMActivityLogger::kMainWorldId | 257 ? DOMActivityLogger::kMainWorldId |
| 256 : GetIsolatedWorldIdForInstance(injection_host, frame); | 258 : GetIsolatedWorldIdForInstance(injection_host, frame); |
| 257 bool expects_results = injector_->ExpectsResults(); | 259 bool is_user_gesture = injector_->IsUserGesture(); |
| 260 | |
| 261 scoped_ptr<blink::WebScriptExecutionCallback> callback( | |
| 262 new ScriptInjectionCallback(this, frame)); | |
|
Devlin
2015/02/27 18:25:32
nit: indentation off.
kozy
2015/02/28 15:23:21
Done.
| |
| 258 | 263 |
| 259 base::ElapsedTimer exec_timer; | 264 base::ElapsedTimer exec_timer; |
| 260 if (injection_host->id().type() == HostID::EXTENSIONS) | 265 if (injection_host->id().type() == HostID::EXTENSIONS) |
| 261 DOMActivityLogger::AttachToWorld(world_id, injection_host->id().id()); | 266 DOMActivityLogger::AttachToWorld(world_id, injection_host->id().id()); |
| 262 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 267 |
| 263 v8::Local<v8::Value> script_value; | |
| 264 if (in_main_world) { | 268 if (in_main_world) { |
| 265 // We only inject in the main world for javascript: urls. | 269 // We only inject in the main world for javascript: urls. |
| 266 DCHECK_EQ(1u, sources.size()); | 270 DCHECK_EQ(1u, sources.size()); |
| 267 | 271 |
| 268 const blink::WebScriptSource& source = sources.front(); | 272 frame->requestExecuteScriptAndReturnValue(sources.front(), |
| 269 if (expects_results) | 273 is_user_gesture, |
| 270 script_value = frame->executeScriptAndReturnValue(source); | 274 callback.release()); |
| 271 else | |
| 272 frame->executeScript(source); | |
| 273 } else { // in isolated world | 275 } else { // in isolated world |
| 274 scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; | 276 frame->requestExecuteScriptInIsolatedWorld(world_id, |
| 275 if (expects_results) | 277 &sources.front(), |
| 276 results.reset(new blink::WebVector<v8::Local<v8::Value> >()); | 278 sources.size(), |
| 277 frame->executeScriptInIsolatedWorld(world_id, | 279 EXTENSION_GROUP_CONTENT_SCRIPTS, |
| 278 &sources.front(), | 280 is_user_gesture, |
| 279 sources.size(), | 281 callback.release()); |
| 280 EXTENSION_GROUP_CONTENT_SCRIPTS, | |
| 281 results.get()); | |
| 282 if (expects_results && !results->isEmpty()) | |
| 283 script_value = (*results)[0]; | |
| 284 } | 282 } |
| 285 | 283 |
| 286 if (injection_host->id().type() == HostID::EXTENSIONS) | 284 if (injection_host->id().type() == HostID::EXTENSIONS) |
| 287 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 285 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| 286 } | |
| 288 | 287 |
| 288 void ScriptInjection::OnJSInjectionCompleted( | |
| 289 blink::WebLocalFrame* frame, | |
| 290 const blink::WebVector<v8::Local<v8::Value> >& results) { | |
| 291 DCHECK(running_frames_ > 0); | |
| 292 --running_frames_; | |
| 293 | |
| 294 bool expects_results = injector_->ExpectsResults(); | |
| 289 if (expects_results) { | 295 if (expects_results) { |
| 296 v8::Local<v8::Value> script_value; | |
| 297 if (!results.isEmpty()) | |
| 298 script_value = results[0]; | |
| 290 // Right now, we only support returning single results (per frame). | 299 // Right now, we only support returning single results (per frame). |
| 291 scoped_ptr<content::V8ValueConverter> v8_converter( | 300 scoped_ptr<content::V8ValueConverter> v8_converter( |
| 292 content::V8ValueConverter::create()); | 301 content::V8ValueConverter::create()); |
| 293 // It's safe to always use the main world context when converting | 302 // It's safe to always use the main world context when converting |
| 294 // here. V8ValueConverterImpl shouldn't actually care about the | 303 // here. V8ValueConverterImpl shouldn't actually care about the |
| 295 // context scope, and it switches to v8::Object's creation context | 304 // context scope, and it switches to v8::Object's creation context |
| 296 // when encountered. | 305 // when encountered. |
| 297 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 306 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 298 scoped_ptr<base::Value> result( | 307 scoped_ptr<base::Value> result( |
| 299 v8_converter->FromV8Value(script_value, context)); | 308 v8_converter->FromV8Value(script_value, context)); |
| 300 // Always append an execution result (i.e. no result == null result) | 309 // if current frame is main frame in injection |
|
Devlin
2015/02/27 18:25:32
This comment isn't really helpful (it just describ
kozy
2015/02/28 15:23:21
Done.
| |
| 301 // so that |execution_results| lines up with the frames. | 310 if (frame == web_frame_) { |
| 302 execution_results->Append(result.get() ? result.release() | 311 execution_results_->Insert(0, result.get() |
| 303 : base::Value::CreateNullValue()); | 312 ? result.release() |
| 313 : base::Value::CreateNullValue()); | |
|
Devlin
2015/02/27 18:25:32
instead of having this result.get() ? logic twice,
kozy
2015/02/28 15:23:21
Done.
| |
| 314 } else { | |
| 315 execution_results_->Append(result.get() ? result.release() | |
| 316 : base::Value::CreateNullValue()); | |
| 317 } | |
| 318 } | |
| 319 TryToFinish(); | |
| 320 } | |
| 321 | |
| 322 void ScriptInjection::SetScriptInjectionManager( | |
| 323 ScriptInjectionManager* manager){ | |
|
Devlin
2015/02/27 18:25:32
nit: indenation off.
kozy
2015/02/28 15:23:21
Done.
| |
| 324 script_injection_manager_ = manager; | |
| 325 } | |
| 326 | |
| 327 void ScriptInjection::TryToFinish() { | |
| 328 if (all_injection_started_ && running_frames_ == 0) { | |
| 329 complete_ = true; | |
| 330 injector_->OnInjectionComplete(execution_results_.Pass(), | |
| 331 run_location_); | |
| 332 | |
| 333 CHECK(script_injection_manager_); | |
| 334 // this object can be destroyed after next line | |
| 335 script_injection_manager_->OnInjectionFinished(this); | |
| 304 } | 336 } |
| 305 } | 337 } |
| 306 | 338 |
| 307 void ScriptInjection::InjectCss(blink::WebLocalFrame* frame) { | 339 void ScriptInjection::InjectCss(blink::WebLocalFrame* frame) { |
| 308 std::vector<std::string> css_sources = | 340 std::vector<std::string> css_sources = |
| 309 injector_->GetCssSources(run_location_); | 341 injector_->GetCssSources(run_location_); |
| 310 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); | 342 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); |
| 311 iter != css_sources.end(); | 343 iter != css_sources.end(); |
| 312 ++iter) { | 344 ++iter) { |
| 313 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); | 345 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); |
| 314 } | 346 } |
| 315 } | 347 } |
| 316 | 348 |
| 317 } // namespace extensions | 349 } // namespace extensions |
| OLD | NEW |