public V8Callback GetOrCreateCallback(IV8Plugin plugin, string memberName, CefV8Value callback, V8CallbackType callbackType)
        {
            V8Callback adapter = null;

            _callbacksByMemberName.AddOrUpdate(
                memberName,
                key =>
            {
                var disposeCallback = new Action <V8Callback>(cb => DisposeCallback(memberName, cb));
                adapter             = new V8Callback(plugin, callback, callbackType, disposeCallback);
                return(new List <V8Callback> {
                    adapter
                });
            },
                (key, list) =>
            {
                adapter = list.FirstOrDefault(c => c.CallbackFunction != null && c.CallbackFunction.IsSame(callback));
                if (adapter == null)
                {
                    var disposeCallback = new Action <V8Callback>(cb => DisposeCallback(memberName, cb));
                    adapter             = new V8Callback(plugin, callback, callbackType, disposeCallback);
                    list.Add(adapter);
                }

                return(list);
            });

            return(adapter);
        }
        protected override void OnInvokeFunction(
            CefV8Context context,
            MethodDescriptor methodDescriptor,
            CefV8Value[] parameters,
            V8Callback v8Callback, out CefV8Value returnValue, out string exception)
        {
            returnValue = null;
            exception   = null;
            LocalV8Callback localCallback = null;

            // If this call is executing without a callback, create a local callback that gathers the results
            if (v8Callback == null)
            {
                localCallback = new LocalV8Callback(this);
                v8Callback    = localCallback;
            }

            _router.InvokeFunction(
                context,
                _plugin,
                methodDescriptor,
                new CefJavaScriptParameters(parameters),
                v8Callback);

            // If we are executing with a local callback set the returnValue and exception from the values gathered by it.
            if (localCallback != null)
            {
                returnValue = localCallback.Result;
                exception   = localCallback.Error;
            }

            exception = null;
        }
        /// <summary>
        /// Execute a native function.
        /// </summary>
        /// <param name="context">The current V8 JavaScript context.</param>
        /// <param name="functionName">The name of the native function to invoke.</param>
        /// <param name="parameters">The parameters to pass to the function.</param>
        /// <param name="callback">An optional callback for the native function to invoke when it is complete.</param>
        public void ExecuteFunction(CefV8Context context, string functionName,
                                    CefV8Value[] parameters, CefV8Value callback, out CefV8Value returnValue, out string exception)
        {
            Logger.Debug("ExecuteFunction Plugin {0} Method {1}", Descriptor.PluginId, functionName);

            MethodDescriptor methodDescriptor;

            if (string.IsNullOrEmpty(functionName) ||
                (methodDescriptor = Descriptor.Methods.Find(descriptor => descriptor.MethodName == functionName)) == null)
            {
                throw new MissingMethodException(functionName);
            }

            V8Callback v8Callback = null;

            if (callback != null)
            {
                // Native plugin methods that accept callbacks may include 'unsubscribe' patterns where the same callback
                // is reused or needs to be passed (wrapped as a C# delegate) to allow the unsubsribe.
                v8Callback = methodDescriptor.HasCallbackParameter
                    ? _retainedCallbacks.GetOrCreateCallback(this, "__PARAMETERCALLBACK", callback, V8CallbackType.ParameterCallback)
                    : new V8Callback(this, callback, V8CallbackType.FunctionCallback);
            }

            // Execute the call through the router, so that the router keeps track of the call
            OnInvokeFunction(context, methodDescriptor, parameters, v8Callback, out returnValue, out exception);
        }
        private void DisposeCallback(string memberName, V8Callback callback)
        {
            List <V8Callback> callbacks;

            if (_callbacksByMemberName.TryGetValue(memberName, out callbacks))
            {
                callbacks.Remove(callback);
            }
        }
예제 #5
0
        protected override void OnInvokeFunction(CefV8Context context, MethodDescriptor methodDescriptor, CefV8Value[] parameters, V8Callback v8Callback, out CefV8Value returnValue, out string exception)
        {
            returnValue = null;
            exception   = null;
            var jsonParameters = CefJsonValueConverter.ToJArray(parameters);

            _router.InvokeFunction(context, Descriptor, methodDescriptor, jsonParameters, v8Callback);
        }
예제 #6
0
 protected override void OnRemoveEventListener(CefV8Context context, string eventName, V8Callback v8Callback)
 {
     _router.RemoveEventListener(context, Descriptor, eventName, v8Callback);
 }
 protected override void OnAddEventListener(CefV8Context context, string eventName, V8Callback v8Callback)
 {
     _router.AddEventListener(context, _plugin, eventName, v8Callback);
 }
 protected abstract void OnInvokeFunction(CefV8Context context, MethodDescriptor methodDescriptor, CefV8Value[] parameters, V8Callback v8Callback, out CefV8Value returnValue, out string exception);
 protected abstract void OnRemoveEventListener(CefV8Context context, string eventName, V8Callback v8Callback);