| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 if (current_location < run_location_) | 146 if (current_location < run_location_) |
| 147 return INJECTION_WAITING; // Wait for the right location. | 147 return INJECTION_WAITING; // Wait for the right location. |
| 148 | 148 |
| 149 if (request_id_ != kInvalidRequestId) { | 149 if (request_id_ != kInvalidRequestId) { |
| 150 // We're waiting for permission right now, try again later. | 150 // We're waiting for permission right now, try again later. |
| 151 return INJECTION_WAITING; | 151 return INJECTION_WAITING; |
| 152 } | 152 } |
| 153 | 153 |
| 154 if (!injection_host_) { | 154 if (!injection_host_) { |
| 155 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 155 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 156 return INJECTION_FINISHED; // We're done. | 156 return INJECTION_CANCELED; // We're done. |
| 157 } | 157 } |
| 158 | 158 |
| 159 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 159 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 160 switch (injector_->CanExecuteOnFrame( | 160 switch (injector_->CanExecuteOnFrame( |
| 161 injection_host_.get(), web_frame, | 161 injection_host_.get(), web_frame, |
| 162 ExtensionFrameHelper::Get(render_frame_)->tab_id())) { | 162 ExtensionFrameHelper::Get(render_frame_)->tab_id())) { |
| 163 case PermissionsData::ACCESS_DENIED: | 163 case PermissionsData::ACCESS_DENIED: |
| 164 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); | 164 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
| 165 return INJECTION_FINISHED; // We're done. | 165 return INJECTION_FINISHED; // We're done. |
| 166 case PermissionsData::ACCESS_WITHHELD: | 166 case PermissionsData::ACCESS_WITHHELD: |
| 167 RequestPermissionFromBrowser(); | 167 RequestPermissionFromBrowser(); |
| 168 return INJECTION_WAITING; // Wait around for permission. | 168 return INJECTION_WAITING; // Wait around for permission. |
| 169 case PermissionsData::ACCESS_ALLOWED: | 169 case PermissionsData::ACCESS_ALLOWED: |
| 170 InjectionResult result = Inject(scripts_run_info); | 170 InjectionResult result = Inject(scripts_run_info); |
| 171 // If the injection is blocked, we need to set the manager so we can | 171 // If the injection is blocked, we need to set the manager so we can |
| 172 // notify it upon completion. | 172 // notify it upon completion. |
| 173 if (result == INJECTION_BLOCKED) | 173 if (result == INJECTION_BLOCKED) |
| 174 async_completion_callback_ = async_completion_callback; | 174 async_completion_callback_ = async_completion_callback; |
| 175 else if (result == INJECTION_CANCELED) |
| 176 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| 175 return result; | 177 return result; |
| 176 } | 178 } |
| 177 | 179 |
| 178 NOTREACHED(); | 180 NOTREACHED(); |
| 179 return INJECTION_FINISHED; | 181 return INJECTION_FINISHED; |
| 180 } | 182 } |
| 181 | 183 |
| 182 ScriptInjection::InjectionResult ScriptInjection::OnPermissionGranted( | 184 ScriptInjection::InjectionResult ScriptInjection::OnPermissionGranted( |
| 183 ScriptsRunInfo* scripts_run_info) { | 185 ScriptsRunInfo* scripts_run_info) { |
| 184 if (!injection_host_) { | 186 if (!injection_host_) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 | 206 |
| 205 void ScriptInjection::NotifyWillNotInject( | 207 void ScriptInjection::NotifyWillNotInject( |
| 206 ScriptInjector::InjectFailureReason reason) { | 208 ScriptInjector::InjectFailureReason reason) { |
| 207 complete_ = true; | 209 complete_ = true; |
| 208 injector_->OnWillNotInject(reason, render_frame_); | 210 injector_->OnWillNotInject(reason, render_frame_); |
| 209 } | 211 } |
| 210 | 212 |
| 211 ScriptInjection::InjectionResult ScriptInjection::Inject( | 213 ScriptInjection::InjectionResult ScriptInjection::Inject( |
| 212 ScriptsRunInfo* scripts_run_info) { | 214 ScriptsRunInfo* scripts_run_info) { |
| 213 DCHECK(injection_host_); | 215 DCHECK(injection_host_); |
| 216 DCHECK(render_frame_); |
| 214 DCHECK(scripts_run_info); | 217 DCHECK(scripts_run_info); |
| 215 DCHECK(!complete_); | 218 DCHECK(!complete_); |
| 216 | 219 |
| 217 bool should_inject_js = injector_->ShouldInjectJs(run_location_); | 220 bool should_inject_js = injector_->ShouldInjectJs(run_location_); |
| 218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); | 221 bool should_inject_css = injector_->ShouldInjectCss(run_location_); |
| 219 DCHECK(should_inject_js || should_inject_css); | 222 DCHECK(should_inject_js || should_inject_css); |
| 220 | 223 |
| 221 if (should_inject_js) | 224 if (should_inject_js) { |
| 222 InjectJs(); | 225 InjectJs(); |
| 226 // InjectJs() may run arbitrary blocking JavaScript code for an unspecified |
| 227 // amount of time and destroy the frame and/or injection host. |
| 228 if (!injection_host_) |
| 229 return INJECTION_CANCELED; |
| 230 if (!render_frame_) |
| 231 return INJECTION_FINISHED; |
| 232 } |
| 223 if (should_inject_css) | 233 if (should_inject_css) |
| 224 InjectCss(); | 234 InjectCss(); |
| 225 | 235 |
| 226 complete_ = did_inject_js_ || !should_inject_js; | 236 complete_ = did_inject_js_ || !should_inject_js; |
| 227 | 237 |
| 228 injector_->GetRunInfo(scripts_run_info, run_location_); | 238 injector_->GetRunInfo(scripts_run_info, run_location_); |
| 229 | 239 |
| 230 if (complete_) { | 240 if (complete_) { |
| 231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 241 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
| 232 render_frame_); | 242 render_frame_); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 } else { | 277 } else { |
| 268 web_frame->requestExecuteScriptInIsolatedWorld( | 278 web_frame->requestExecuteScriptInIsolatedWorld( |
| 269 world_id, | 279 world_id, |
| 270 &sources.front(), | 280 &sources.front(), |
| 271 sources.size(), | 281 sources.size(), |
| 272 EXTENSION_GROUP_CONTENT_SCRIPTS, | 282 EXTENSION_GROUP_CONTENT_SCRIPTS, |
| 273 is_user_gesture, | 283 is_user_gesture, |
| 274 callback.release()); | 284 callback.release()); |
| 275 } | 285 } |
| 276 | 286 |
| 277 if (injection_host_->id().type() == HostID::EXTENSIONS) | 287 if (injection_host_ && injection_host_->id().type() == HostID::EXTENSIONS) |
| 278 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 288 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| 279 } | 289 } |
| 280 | 290 |
| 281 void ScriptInjection::OnJsInjectionCompleted( | 291 void ScriptInjection::OnJsInjectionCompleted( |
| 282 const blink::WebVector<v8::Local<v8::Value> >& results) { | 292 const blink::WebVector<v8::Local<v8::Value> >& results) { |
| 283 DCHECK(!did_inject_js_); | 293 DCHECK(!did_inject_js_); |
| 284 | 294 |
| 285 bool expects_results = injector_->ExpectsResults(); | 295 bool expects_results = injector_->ExpectsResults(); |
| 286 if (expects_results) { | 296 if (expects_results) { |
| 287 if (!results.isEmpty() && !results[0].IsEmpty()) { | 297 if (!results.isEmpty() && !results[0].IsEmpty()) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 313 | 323 |
| 314 void ScriptInjection::InjectCss() { | 324 void ScriptInjection::InjectCss() { |
| 315 std::vector<std::string> css_sources = | 325 std::vector<std::string> css_sources = |
| 316 injector_->GetCssSources(run_location_); | 326 injector_->GetCssSources(run_location_); |
| 317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 327 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 318 for (const std::string& css : css_sources) | 328 for (const std::string& css : css_sources) |
| 319 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); | 329 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); |
| 320 } | 330 } |
| 321 | 331 |
| 322 } // namespace extensions | 332 } // namespace extensions |
| OLD | NEW |