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 26 matching lines...) Expand all Loading... | |
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_); |
214 DCHECK(scripts_run_info); | 216 DCHECK(scripts_run_info); |
215 DCHECK(!complete_); | 217 DCHECK(!complete_); |
216 | 218 |
217 bool should_inject_js = injector_->ShouldInjectJs(run_location_); | 219 bool should_inject_js = injector_->ShouldInjectJs(run_location_); |
218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); | 220 bool should_inject_css = injector_->ShouldInjectCss(run_location_); |
219 DCHECK(should_inject_js || should_inject_css); | 221 DCHECK(should_inject_js || should_inject_css); |
220 | 222 |
221 if (should_inject_js) | 223 if (should_inject_js) { |
222 InjectJs(); | 224 InjectJs(); |
225 if (!injection_host_) | |
Devlin
2016/07/08 20:51:09
A comment explaining why/how this could happen wou
robwu
2016/07/09 05:22:02
Done.
| |
226 return INJECTION_CANCELED; | |
227 } | |
223 if (should_inject_css) | 228 if (should_inject_css) |
224 InjectCss(); | 229 InjectCss(); |
225 | 230 |
226 complete_ = did_inject_js_ || !should_inject_js; | 231 complete_ = did_inject_js_ || !should_inject_js; |
227 | 232 |
228 injector_->GetRunInfo(scripts_run_info, run_location_); | 233 injector_->GetRunInfo(scripts_run_info, run_location_); |
229 | 234 |
230 if (complete_) { | 235 if (complete_) { |
231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 236 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
232 render_frame_); | 237 render_frame_); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 } else { | 272 } else { |
268 web_frame->requestExecuteScriptInIsolatedWorld( | 273 web_frame->requestExecuteScriptInIsolatedWorld( |
269 world_id, | 274 world_id, |
270 &sources.front(), | 275 &sources.front(), |
271 sources.size(), | 276 sources.size(), |
272 EXTENSION_GROUP_CONTENT_SCRIPTS, | 277 EXTENSION_GROUP_CONTENT_SCRIPTS, |
273 is_user_gesture, | 278 is_user_gesture, |
274 callback.release()); | 279 callback.release()); |
275 } | 280 } |
276 | 281 |
277 if (injection_host_->id().type() == HostID::EXTENSIONS) | 282 if (injection_host_ && injection_host_->id().type() == HostID::EXTENSIONS) |
278 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 283 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
279 } | 284 } |
280 | 285 |
281 void ScriptInjection::OnJsInjectionCompleted( | 286 void ScriptInjection::OnJsInjectionCompleted( |
282 const blink::WebVector<v8::Local<v8::Value> >& results) { | 287 const blink::WebVector<v8::Local<v8::Value> >& results) { |
283 DCHECK(!did_inject_js_); | 288 DCHECK(!did_inject_js_); |
284 | 289 |
285 bool expects_results = injector_->ExpectsResults(); | 290 bool expects_results = injector_->ExpectsResults(); |
286 if (expects_results) { | 291 if (expects_results) { |
287 if (!results.isEmpty() && !results[0].IsEmpty()) { | 292 if (!results.isEmpty() && !results[0].IsEmpty()) { |
(...skipping 25 matching lines...) Expand all Loading... | |
313 | 318 |
314 void ScriptInjection::InjectCss() { | 319 void ScriptInjection::InjectCss() { |
315 std::vector<std::string> css_sources = | 320 std::vector<std::string> css_sources = |
316 injector_->GetCssSources(run_location_); | 321 injector_->GetCssSources(run_location_); |
317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 322 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
318 for (const std::string& css : css_sources) | 323 for (const std::string& css : css_sources) |
319 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); | 324 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); |
320 } | 325 } |
321 | 326 |
322 } // namespace extensions | 327 } // namespace extensions |
OLD | NEW |