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_context.h" | 5 #include "extensions/renderer/script_context.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (!web_frame_) | 198 if (!web_frame_) |
199 return handle_scope.Escape(function->Call(global, argc, argv)); | 199 return handle_scope.Escape(function->Call(global, argc, argv)); |
200 return handle_scope.Escape( | 200 return handle_scope.Escape( |
201 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 201 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
202 function, global, argc, argv))); | 202 function, global, argc, argv))); |
203 } | 203 } |
204 | 204 |
205 void ScriptContext::SafeCallFunction(const v8::Local<v8::Function>& function, | 205 void ScriptContext::SafeCallFunction(const v8::Local<v8::Function>& function, |
206 int argc, | 206 int argc, |
207 v8::Local<v8::Value> argv[]) { | 207 v8::Local<v8::Value> argv[]) { |
| 208 SafeCallFunction(function, argc, argv, |
| 209 ScriptInjectionCallback::CompleteCallback()); |
| 210 } |
| 211 |
| 212 void ScriptContext::SafeCallFunction( |
| 213 const v8::Local<v8::Function>& function, |
| 214 int argc, |
| 215 v8::Local<v8::Value> argv[], |
| 216 const ScriptInjectionCallback::CompleteCallback& callback) { |
| 217 DCHECK(thread_checker_.CalledOnValidThread()); |
208 v8::HandleScope handle_scope(isolate()); | 218 v8::HandleScope handle_scope(isolate()); |
209 v8::Context::Scope scope(v8_context()); | 219 v8::Context::Scope scope(v8_context()); |
210 v8::MicrotasksScope microtasks(isolate(), | 220 v8::MicrotasksScope microtasks(isolate(), |
211 v8::MicrotasksScope::kDoNotRunMicrotasks); | 221 v8::MicrotasksScope::kDoNotRunMicrotasks); |
212 v8::Local<v8::Object> global = v8_context()->Global(); | 222 v8::Local<v8::Object> global = v8_context()->Global(); |
213 if (web_frame_) { | 223 if (web_frame_) { |
| 224 ScriptInjectionCallback* wrapper_callback = nullptr; |
| 225 if (!callback.is_null()) { |
| 226 // ScriptInjectionCallback manages its own lifetime. |
| 227 wrapper_callback = new ScriptInjectionCallback(callback); |
| 228 } |
214 web_frame_->requestExecuteV8Function(v8_context(), function, global, argc, | 229 web_frame_->requestExecuteV8Function(v8_context(), function, global, argc, |
215 argv, nullptr); | 230 argv, wrapper_callback); |
216 } else { | 231 } else { |
217 // TODO(devlin): This probably isn't safe. | 232 // TODO(devlin): This probably isn't safe. |
218 function->Call(global, argc, argv); | 233 v8::Local<v8::Value> result = function->Call(global, argc, argv); |
| 234 if (!callback.is_null()) { |
| 235 std::vector<v8::Local<v8::Value>> results(1, result); |
| 236 callback.Run(results); |
| 237 } |
219 } | 238 } |
220 } | 239 } |
221 | 240 |
222 Feature::Availability ScriptContext::GetAvailability( | 241 Feature::Availability ScriptContext::GetAvailability( |
223 const std::string& api_name) { | 242 const std::string& api_name) { |
224 return GetAvailability(api_name, CheckAliasStatus::ALLOWED); | 243 return GetAvailability(api_name, CheckAliasStatus::ALLOWED); |
225 } | 244 } |
226 | 245 |
227 Feature::Availability ScriptContext::GetAvailability( | 246 Feature::Availability ScriptContext::GetAvailability( |
228 const std::string& api_name, | 247 const std::string& api_name, |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 v8::Local<v8::Value> argv[]) { | 538 v8::Local<v8::Value> argv[]) { |
520 return context_->CallFunction(function, argc, argv); | 539 return context_->CallFunction(function, argc, argv); |
521 } | 540 } |
522 | 541 |
523 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 542 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
524 v8::HandleScope handle_scope(context_->isolate()); | 543 v8::HandleScope handle_scope(context_->isolate()); |
525 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 544 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
526 } | 545 } |
527 | 546 |
528 } // namespace extensions | 547 } // namespace extensions |
OLD | NEW |