public void InvokeFunctionDirect(IPluginManager pluginManager, int browserId, long frameId, int contextId, string methodName, IJavaScriptParameters parameters, IJavaScriptPluginCallback callback) { var invoker = CreateInvoker(pluginManager, browserId, frameId, contextId, methodName, parameters, callback); invoker(); }
/// <summary> /// Invoke a local function. /// </summary> /// <param name="context"> /// The current V8 context that is making the function call. /// </param> /// <param name="targetPlugin"> /// The local plugin that owns the target method. /// </param> /// <param name="methodDescriptor"> /// The method to invoke. /// </param> /// <param name="parameters"> /// An interface for the local plugin to obtain the parameters from before invoking the function. /// </param> /// <param name="callback"> /// Optional callback into V8. /// </param> public void InvokeFunction( CefV8Context context, JavaScriptPlugin targetPlugin, MethodDescriptor methodDescriptor, IJavaScriptParameters parameters, IV8Callback callback) { Logger.Info("InvokeFunction Local Plugin {0} Method {1}", targetPlugin.Descriptor.PluginId, methodDescriptor.MethodName); if (!EnsureOnRendererThread()) { return; } if (!targetPlugin.IsValid) { Logger.Warn("InvokeFunction Local Plugin {0} is invalid", targetPlugin.Descriptor.PluginId); if (callback != null) { callback.Invoke(this, context, null, CallInfo.ErrorCodeCallCanceled, CallInfo.ErrorCallCanceled); } return; } var parameterCallbackInfo = GetOrCreateParameterCallback(context, methodDescriptor, callback); var functionInvokeMessage = CreateMessage(context, targetPlugin.Descriptor, methodDescriptor.MethodName, callback); functionInvokeMessage.MessageId = Guid.NewGuid(); functionInvokeMessage.MessageType = PluginMessageType.FunctionInvoke; // Add the call info into the pending calls for the browser var info = methodDescriptor.HasCallbackParameter ? AddLocalCallback(functionInvokeMessage, null, parameterCallbackInfo) : AddLocalCallback(functionInvokeMessage, callback, null); try { targetPlugin.InvokeFunctionDirect( null, functionInvokeMessage.BrowserId, functionInvokeMessage.FrameId, functionInvokeMessage.ContextId, methodDescriptor.MethodName, parameters, info); } catch (Exception ex) { LocalCallbackInvoked(info, null, -1, ex.Message); Logger.Error("InvokeFunction Failed Local Plugin {0} Method {1}: {2}", targetPlugin.Descriptor.PluginId, methodDescriptor.MethodName, ex); } }
public void InvokeFunction( IPluginManager pluginManager, int browserId, long frameId, int contextId, string methodName, IJavaScriptParameters parameters, IJavaScriptPluginCallback callback) { var invoker = CreateInvoker(pluginManager, browserId, frameId, contextId, methodName, parameters, callback); if (_callbackThread == CallbackThread.Main) { ParagonRuntime.MainThreadContext.Post(o => invoker(), null); } else { ThreadPool.QueueUserWorkItem(o => invoker()); } }
private Action CreateInvoker(IPluginManager pluginManager, int browserId, long frameId, int contextId, string methodName, IJavaScriptParameters parameters, IJavaScriptPluginCallback callback) { ThrowIfDisposed(); if (parameters == null) { throw new ArgumentNullException("parameters"); } MethodInfo method; if (!_methods.TryGetValue(methodName, out method)) { if (callback != null) { var msg = string.Format("Error executing {0} on plugin {1} - method not found", methodName, _pluginId); InvokeReturnCallback(callback, null, 0, msg); } } var nativeObject = NativeObject; if (nativeObject == null) { if (callback != null) { var msg = string.Format("Error executing {0} on plugin {1} - plugin object has been disposed", methodName, _pluginId); InvokeReturnCallback(callback, null, 0, msg); } } object[] arguments; var hasCallbackParameter = HasCallbackParameter(method); var methodParams = method.GetParameters(); var parameterDefinitions = hasCallbackParameter ? methodParams.Take(methodParams.Length - 1).ToArray() : methodParams; try { arguments = parameters.GetConvertedParameters(parameterDefinitions, pluginManager); if (hasCallbackParameter) { // Create a new args array with length + 1 so we can add the callback param to it. var args = new object[arguments.Length + 1]; Array.Copy(arguments, args, arguments.Length); // Extract the callback and wrap it. JavaScriptPluginCallback callbackParam = null; if (callback != null) { var parameterCallback = callback.GetParameterCallback(); if (parameterCallback != null) { callbackParam = parameterCallback.Invoke; } } // Add the wrapped callback to the args list. args[args.Length - 1] = callbackParam; arguments = args; } } catch (Exception ex) { Logger.Error("Error converting plugin invocation parameters: " + ex); if (callback != null) { InvokeReturnCallback(callback, null, -1, ex.Message); } return(null); } var invoke = new Action( () => { using (PluginExecutionContext.Create(browserId, contextId, frameId)) { object result = null; var errorCode = 0; var error = string.Empty; try { result = method.Invoke(nativeObject, arguments); } catch (Exception e) { while (e is TargetInvocationException) { e = e.InnerException; } errorCode = -1; error = e.Message; Logger.Error("Error executing plugin method: " + e); } if (callback != null) { InvokeReturnCallback(callback, result, errorCode, error); } } }); return(invoke); }