private static XrayHandle GetGlobalInternal(CefFrame frame) { if (!CefApi.CurrentlyOn(CefThreadId.Renderer)) { using (var callTask = new V8CallTask(() => GetGlobalInternal(frame))) { if (!CefApi.PostTask(CefThreadId.Renderer, callTask)) { throw new InvalidOperationException(); } return((XrayHandle)callTask.GetResult()); } } CefV8Context context = frame.V8Context; if (!context.Enter()) { throw new InvalidOperationException(); } try { CefV8Value globalObj = context.GetGlobal(); return((XrayHandle)CastCefV8ValueToDotnetType(context, globalObj, out bool isxray)); } finally { context.Exit(); } }
private static void TestV8ValueTypes(CefFrame frame) { var sb = new StringBuilder(); CefV8Context context = frame.V8Context; if (!context.Enter()) { return; } try { sb.Append("typeof 1 = ").Append(context.Eval("1", null).Type).AppendLine(); sb.Append("typeof true = ").Append(context.Eval("true", null).Type).AppendLine(); sb.Append("typeof 'string' = ").Append(context.Eval("'string'", null).Type).AppendLine(); sb.Append("typeof 2.2 = ").Append(context.Eval("2.2", null).Type).AppendLine(); sb.Append("typeof null = ").Append(context.Eval("null", null).Type).AppendLine(); sb.Append("typeof new Object() = ").Append(context.Eval("new Object()", null).Type).AppendLine(); sb.Append("typeof undefined = ").Append(context.Eval("undefined", null).Type).AppendLine(); sb.Append("typeof new Date() = ").Append(context.Eval("new Date()", null).Type).AppendLine(); sb.Append("(window == window) = ").Append(context.Eval("window", null) == context.Eval("window", null)).AppendLine(); } finally { context.Exit(); } var message = new CefProcessMessage("MessageBox.Show"); message.ArgumentList.SetString(0, sb.ToString()); frame.SendProcessMessage(CefProcessId.Browser, message); }
public void Dispose() { if (entered) { context.Exit(); } }
/// <summary> /// Run in context. /// </summary> /// <typeparam name="TReturn">The type of the return.</typeparam> /// <param name="context">The context.</param> /// <param name="action">The action.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// context /// or /// action /// </exception> public static TReturn RunInContext <TReturn>(CefV8Context context, Func <TReturn> action) { if (context == null) { throw new ArgumentNullException("context"); } if (action == null) { throw new ArgumentNullException("action"); } var contextEntered = false; if (!CefV8Context.InContext || !CefV8Context.GetEnteredContext().IsSame(context)) { context.Enter(); contextEntered = true; } try { return(action()); } finally { if (contextEntered) { context.Exit(); contextEntered = false; } } }
public virtual void Invoke(IV8PluginRouter router, CefV8Context context, object result, int errorCode, string error) { if (CallbackFunction == null) { throw new ObjectDisposedException("_callback"); } // Have to enter the context in order to be able to create object/array/function/date V8 instances context.Enter(); try { var args = new List <CefV8Value>(); switch (_callbackType) { case V8CallbackType.ParameterCallback: case V8CallbackType.EventListener: { var remoteResult = result as ResultData; var localArray = result as object[]; if (remoteResult != null) { if (remoteResult.Items != null) { args.AddRange(remoteResult.Items.Select(item => ToCefV8Value(router, item))); } } else if (localArray != null) { args.AddRange(localArray.Select(item => ToCefV8Value(router, item))); } break; } case V8CallbackType.FunctionCallback: { args.Add(ToCefV8Value(router, result)); args.Add(CefV8Value.CreateInt(errorCode)); args.Add(CefV8Value.CreateString(error)); break; } } var functionResult = CallbackFunction.ExecuteFunction(null, args.ToArray()); if (functionResult == null && CallbackFunction.HasException) { var exception = CallbackFunction.GetException(); Logger.Error("Error executing callback: ", exception.Message); } } finally { context.Exit(); } }
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context) { base.OnContextCreated(browser, frame, context); context.Enter(); CefWebBrowser.CefWebCreateJSObjectEventArgs args = new CefWebBrowser.CefWebCreateJSObjectEventArgs(browser, frame, context); _core.OnCreateJSObject(args); context.Exit(); }
public static void Acquire(this CefV8Context ctx, Action body) { ctx.Enter(); try { body.Invoke(); } finally { ctx.Exit(); } }
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context) { base.OnContextCreated(browser, frame, context); context.Enter(); //可在這裡面改V8引擎載入的js物件 var window = context.GetGlobal(); var computerName = CefV8Value.CreateString(Environment.MachineName); window.SetValue("computer_name", computerName, CefV8PropertyAttribute.DontDelete | CefV8PropertyAttribute.ReadOnly); context.Exit(); }
/// <summary> /// Creates a "createPromise" function on the JS side. /// </summary> private CefPromiseFactory CreatePromiseFactory() { context.Enter(); context.GetFrame().ExecuteJavaScript(@" window.createPromise = function (fn) { return new Promise(fn); }; ", null, 1); var global = context.GetGlobal(); var promiseFactoryFn = global.GetValue("createPromise"); var promiseFactory = new CefPromiseFactory(promiseFactoryFn); context.Exit(); return(promiseFactory); }
private CefV8Value DontMeetRequirements(CefV8Context context, CefV8Value callback) { var msg = JsonMapper.ToJson(new StatusHelper("Error", "Arguments do not meet function requirement")); if (callback != null) { var runner = CefTaskRunner.GetForCurrentThread(); new Task(() => { context.Enter(); runner.PostTask(new CallbackTask(context, callback, msg)); context.Exit(); }).Start(); return(CefV8Value.CreateNull()); } else { return(CefV8Value.CreateString(msg)); } }
void IDisposable.Dispose() { _CefV8Context.Exit(); }
private static object InvokeScript(CefV8Context context, string memberName, params object[] args) { if (context == null) throw new ArgumentNullException("context"); if (!context.Enter()) throw new CefException("Failed to enter V8 context."); try { // TODO: this list can be private list of context, 'cause we can invoke only one function at one time List<CefV8Value> proxies = new List<CefV8Value>(16); // javascript 'this' object CefV8Value obj = null; CefV8Value target = context.GetGlobal(); proxies.Add(target); if (!memberName.Contains('.')) { obj = target; target = obj.GetValue(memberName); proxies.Add(target); } else { foreach (var member in memberName.Split('.')) { obj = target; target = obj.GetValue(member); // TODO: do analysis of target - if it is not an object - throw if (!target.IsObject) throw new CefException("Argument 'memberName' must be member access expression to a function. Invalid object in path."); proxies.Add(target); } } if (!target.IsFunction) throw new ArgumentException("Argument 'memberName' must be member access expression to a function."); CefV8Value[] v8Args; if (args.Length == 0) v8Args = null; else { v8Args = new CefV8Value[args.Length]; // TODO: InvokeScript core can be optimized by prevent recreating arrays for (var i = 0; i < args.Length; i++) { var value = CefConvert.ToV8Value(args[i]); v8Args[i] = value; } } var v8RetVal = target.ExecuteFunctionWithContext(context, obj, v8Args); // force destroing of proxies, to avoid unneccessary GC load (CLR and V8) foreach (var proxy in proxies) proxy.Dispose(); proxies.Clear(); // FIXME: not sure if exception CAN be null, this need to be checked if (v8RetVal.HasException) { var exception = v8RetVal.GetException(); throw new JavaScriptException(exception.GetMessage()); } //if (!string.IsNullOrEmpty(exception)) var result = CefConvert.ToObject(v8RetVal); v8RetVal.Dispose(); return result; } finally { if (!context.Exit()) throw new CefException("Failed to exit V8 context."); } }
private static object InvokeScript(CefV8Context context, string memberName, params object[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (!context.Enter()) { throw new CefException("Failed to enter V8 context."); } try { // TODO: this list can be private list of context, 'cause we can invoke only one function at one time List <CefV8Value> proxies = new List <CefV8Value>(16); // javascript 'this' object CefV8Value obj = null; CefV8Value target = context.GetGlobal(); proxies.Add(target); if (!memberName.Contains('.')) { obj = target; target = obj.GetValue(memberName); proxies.Add(target); } else { foreach (var member in memberName.Split('.')) { obj = target; target = obj.GetValue(member); // TODO: do analysis of target - if it is not an object - throw if (!target.IsObject) { throw new CefException("Argument 'memberName' must be member access expression to a function. Invalid object in path."); } proxies.Add(target); } } if (!target.IsFunction) { throw new ArgumentException("Argument 'memberName' must be member access expression to a function."); } CefV8Value[] v8Args; if (args.Length == 0) { v8Args = null; } else { v8Args = new CefV8Value[args.Length]; // TODO: InvokeScript core can be optimized by prevent recreating arrays for (var i = 0; i < args.Length; i++) { var value = CefConvert.ToV8Value(args[i]); v8Args[i] = value; } } var v8RetVal = target.ExecuteFunctionWithContext(context, obj, v8Args); // force destroing of proxies, to avoid unneccessary GC load (CLR and V8) foreach (var proxy in proxies) { proxy.Dispose(); } proxies.Clear(); // FIXME: not sure if exception CAN be null, this need to be checked if (v8RetVal.HasException) { var exception = v8RetVal.GetException(); throw new JavaScriptException(exception.GetMessage()); } //if (!string.IsNullOrEmpty(exception)) var result = CefConvert.ToObject(v8RetVal); v8RetVal.Dispose(); return(result); } finally { if (!context.Exit()) { throw new CefException("Failed to exit V8 context."); } } }