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" |
| (...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_.get()) { |
| 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::SendInjectionMessage(bool request_permission) { | 172 void ScriptInjection::SendInjectionMessage(bool request_permission) { |
| 173 content::RenderView* render_view = | 173 content::RenderView* render_view = |
| 174 content::RenderView::FromWebView(web_frame()->top()->view()); | 174 content::RenderView::FromWebView(web_frame()->top()->view()); |
| 175 | 175 |
| 176 // If we are just notifying the browser of the injection, then send an | 176 // If we are just notifying the browser of the injection, then send an |
| 177 // invalid request (which is treated like a notification). | 177 // invalid request (which is treated like a notification). |
| 178 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; | 178 request_id_ = request_permission ? g_next_pending_id++ : kInvalidRequestId; |
| 179 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( | 179 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
| 180 render_view->GetRoutingID(), | 180 render_view->GetRoutingID(), |
| 181 host_id_.id(), | 181 host_id().id(), |
| 182 injector_->script_type(), | 182 injector_->script_type(), |
| 183 request_id_)); | 183 request_id_)); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void ScriptInjection::NotifyWillNotInject( | 186 void ScriptInjection::NotifyWillNotInject( |
| 187 ScriptInjector::InjectFailureReason reason) { | 187 ScriptInjector::InjectFailureReason reason) { |
| 188 complete_ = true; | 188 complete_ = true; |
| 189 injector_->OnWillNotInject(reason); | 189 injector_->OnWillNotInject(reason); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void ScriptInjection::Inject(const InjectionHost* injection_host, | 192 void ScriptInjection::Inject(ScriptsRunInfo* scripts_run_info) { |
| 193 ScriptsRunInfo* scripts_run_info) { | 193 DCHECK(injection_host_.get()); |
|
Devlin
2015/02/20 22:39:13
scoped_ptr has a bool operator, so this can be wri
Xi Han
2015/02/23 16:14:59
Done.
| |
| 194 DCHECK(injection_host); | |
| 195 DCHECK(scripts_run_info); | 194 DCHECK(scripts_run_info); |
| 196 DCHECK(!complete_); | 195 DCHECK(!complete_); |
| 197 | 196 |
| 198 if (injection_host->ShouldNotifyBrowserOfInjection()) | 197 if (injection_host_->ShouldNotifyBrowserOfInjection()) |
| 199 SendInjectionMessage(false /* don't request permission */); | 198 SendInjectionMessage(false /* don't request permission */); |
| 200 | 199 |
| 201 std::vector<blink::WebFrame*> frame_vector; | 200 std::vector<blink::WebFrame*> frame_vector; |
| 202 frame_vector.push_back(web_frame_); | 201 frame_vector.push_back(web_frame_); |
| 203 if (injector_->ShouldExecuteInChildFrames()) | 202 if (injector_->ShouldExecuteInChildFrames()) |
| 204 AppendAllChildFrames(web_frame_, &frame_vector); | 203 AppendAllChildFrames(web_frame_, &frame_vector); |
| 205 | 204 |
| 206 scoped_ptr<blink::WebScopedUserGesture> gesture; | 205 scoped_ptr<blink::WebScopedUserGesture> gesture; |
| 207 if (injector_->IsUserGesture()) | 206 if (injector_->IsUserGesture()) |
| 208 gesture.reset(new blink::WebScopedUserGesture()); | 207 gesture.reset(new blink::WebScopedUserGesture()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 221 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); | 220 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); |
| 222 | 221 |
| 223 // We recheck access here in the renderer for extra safety against races | 222 // We recheck access here in the renderer for extra safety against races |
| 224 // with navigation, but different frames can have different URLs, and the | 223 // with navigation, but different frames can have different URLs, and the |
| 225 // injection host might only have access to a subset of them. | 224 // 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 | 225 // For child frames, we just skip ones the injection host doesn't have |
| 227 // access to and carry on. | 226 // access to and carry on. |
| 228 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to | 227 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to |
| 229 // surface a request for a child frame. | 228 // surface a request for a child frame. |
| 230 // TODO(rdevlin.cronin): We should ask for permission somehow. | 229 // TODO(rdevlin.cronin): We should ask for permission somehow. |
| 231 if (injector_->CanExecuteOnFrame(injection_host, frame, tab_id_, top_url) == | 230 if (injector_->CanExecuteOnFrame( |
| 232 PermissionsData::ACCESS_DENIED) { | 231 injection_host_.get(), frame, tab_id_, top_url) == |
| 232 PermissionsData::ACCESS_DENIED) { | |
| 233 DCHECK(frame->parent()); | 233 DCHECK(frame->parent()); |
| 234 continue; | 234 continue; |
| 235 } | 235 } |
| 236 if (inject_js) | 236 if (inject_js) |
| 237 InjectJs(injection_host, frame, execution_results.get()); | 237 InjectJs(frame, execution_results.get()); |
| 238 if (inject_css) | 238 if (inject_css) |
| 239 InjectCss(frame); | 239 InjectCss(frame); |
| 240 } | 240 } |
| 241 | 241 |
| 242 complete_ = true; | 242 complete_ = true; |
| 243 | 243 |
| 244 // TODO(hanxi): don't log these metrics for webUIs' injections. | 244 // TODO(hanxi): don't log these metrics for webUIs' injections. |
| 245 injector_->OnInjectionComplete(execution_results.Pass(), | 245 injector_->OnInjectionComplete(execution_results.Pass(), |
| 246 scripts_run_info, | 246 scripts_run_info, |
| 247 run_location_); | 247 run_location_); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void ScriptInjection::InjectJs(const InjectionHost* injection_host, | 250 void ScriptInjection::InjectJs(blink::WebLocalFrame* frame, |
| 251 blink::WebLocalFrame* frame, | |
| 252 base::ListValue* execution_results) { | 251 base::ListValue* execution_results) { |
| 253 std::vector<blink::WebScriptSource> sources = | 252 std::vector<blink::WebScriptSource> sources = |
| 254 injector_->GetJsSources(run_location_); | 253 injector_->GetJsSources(run_location_); |
| 255 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 254 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 256 int world_id = in_main_world | 255 int world_id = in_main_world |
| 257 ? DOMActivityLogger::kMainWorldId | 256 ? DOMActivityLogger::kMainWorldId |
| 258 : GetIsolatedWorldIdForInstance(injection_host, frame); | 257 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
| 258 frame); | |
| 259 bool expects_results = injector_->ExpectsResults(); | 259 bool expects_results = injector_->ExpectsResults(); |
| 260 | 260 |
| 261 base::ElapsedTimer exec_timer; | 261 base::ElapsedTimer exec_timer; |
| 262 if (injection_host->id().type() == HostID::EXTENSIONS) | 262 if (injection_host_->id().type() == HostID::EXTENSIONS) |
| 263 DOMActivityLogger::AttachToWorld(world_id, injection_host->id().id()); | 263 DOMActivityLogger::AttachToWorld(world_id, injection_host_->id().id()); |
| 264 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 264 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 265 v8::Local<v8::Value> script_value; | 265 v8::Local<v8::Value> script_value; |
| 266 if (in_main_world) { | 266 if (in_main_world) { |
| 267 // We only inject in the main world for javascript: urls. | 267 // We only inject in the main world for javascript: urls. |
| 268 DCHECK_EQ(1u, sources.size()); | 268 DCHECK_EQ(1u, sources.size()); |
| 269 | 269 |
| 270 const blink::WebScriptSource& source = sources.front(); | 270 const blink::WebScriptSource& source = sources.front(); |
| 271 if (expects_results) | 271 if (expects_results) |
| 272 script_value = frame->executeScriptAndReturnValue(source); | 272 script_value = frame->executeScriptAndReturnValue(source); |
| 273 else | 273 else |
| 274 frame->executeScript(source); | 274 frame->executeScript(source); |
| 275 } else { // in isolated world | 275 } else { // in isolated world |
| 276 scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; | 276 scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; |
| 277 if (expects_results) | 277 if (expects_results) |
| 278 results.reset(new blink::WebVector<v8::Local<v8::Value> >()); | 278 results.reset(new blink::WebVector<v8::Local<v8::Value> >()); |
| 279 frame->executeScriptInIsolatedWorld(world_id, | 279 frame->executeScriptInIsolatedWorld(world_id, |
| 280 &sources.front(), | 280 &sources.front(), |
| 281 sources.size(), | 281 sources.size(), |
| 282 EXTENSION_GROUP_CONTENT_SCRIPTS, | 282 EXTENSION_GROUP_CONTENT_SCRIPTS, |
| 283 results.get()); | 283 results.get()); |
| 284 if (expects_results && !results->isEmpty()) | 284 if (expects_results && !results->isEmpty()) |
| 285 script_value = (*results)[0]; | 285 script_value = (*results)[0]; |
| 286 } | 286 } |
| 287 | 287 |
| 288 if (injection_host->id().type() == HostID::EXTENSIONS) | 288 if (injection_host_->id().type() == HostID::EXTENSIONS) |
| 289 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 289 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| 290 | 290 |
| 291 if (expects_results) { | 291 if (expects_results) { |
| 292 // Right now, we only support returning single results (per frame). | 292 // Right now, we only support returning single results (per frame). |
| 293 scoped_ptr<content::V8ValueConverter> v8_converter( | 293 scoped_ptr<content::V8ValueConverter> v8_converter( |
| 294 content::V8ValueConverter::create()); | 294 content::V8ValueConverter::create()); |
| 295 // It's safe to always use the main world context when converting | 295 // It's safe to always use the main world context when converting |
| 296 // here. V8ValueConverterImpl shouldn't actually care about the | 296 // here. V8ValueConverterImpl shouldn't actually care about the |
| 297 // context scope, and it switches to v8::Object's creation context | 297 // context scope, and it switches to v8::Object's creation context |
| 298 // when encountered. | 298 // when encountered. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 310 std::vector<std::string> css_sources = | 310 std::vector<std::string> css_sources = |
| 311 injector_->GetCssSources(run_location_); | 311 injector_->GetCssSources(run_location_); |
| 312 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); | 312 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); |
| 313 iter != css_sources.end(); | 313 iter != css_sources.end(); |
| 314 ++iter) { | 314 ++iter) { |
| 315 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); | 315 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); |
| 316 } | 316 } |
| 317 } | 317 } |
| 318 | 318 |
| 319 } // namespace extensions | 319 } // namespace extensions |
| OLD | NEW |