Exemplo n.º 1
0
        protected void CallCallback(CfrV8Value callback, CfrV8Context v8Context, params KeyValuePair <string, object>[] par)
        {
            if (callback != null)
            {
                //get render process context
                var rc = callback.CreateRemoteCallContext();

                //enter render process
                rc.Enter();

                //create render task
                var task = new CfrTask();
                task.Execute += (_, taskArgs) =>
                {
                    //enter saved context
                    v8Context.Enter();

                    var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor());

                    foreach (var val in par)
                    {
                        var validValue = ConvertValue(val.Value);
                        callbackArgs.SetValue(val.Key, validValue, CfxV8PropertyAttribute.ReadOnly);
                    }

                    //execute callback
                    callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs });

                    v8Context.Exit();

                    //lock task from gc
                    lock (task)
                    {
                        Monitor.PulseAll(task);
                    }
                };

                lock (task)
                {
                    //post task to render process
                    v8Context.TaskRunner.PostTask(task);
                }

                rc.Exit();

                GC.KeepAlive(task);
            }
        }
Exemplo n.º 2
0
        private void AsyncTestFunctionCallback(CfrV8Value function, CfrV8Context v8Context)
        {
            LogWriteLine("AsyncTestFunctionCallback: sleep 2 secs, don't browse away...");
            Thread.Sleep(2000);

            var rpcContext = function.CreateRemoteCallContext();

            rpcContext.Enter();

            // since v8 values can only be accessed from the thread on which they are created, the task
            // has to be posted for execution on the remote renderer thread (see CfxV8Value summary)

            var    task   = new CfrTask();
            string result = null;

            task.Execute += (s, e) => {
                v8Context.Enter();
                LogWriteLine("AsyncTestFunctionCallback -> task.Execute: function.FunctionName = " + function.FunctionName);
                var args   = new CfrV8Value[] { "This is the alert text." };
                var retval = function.ExecuteFunction(null, args);
                result = retval.StringValue;
                v8Context.Exit();
                // release the waiting thread.
                lock (task) {
                    Monitor.PulseAll(task);
                }
            };

            // wait until the return value is available.
            lock (task) {
                CfrTaskRunner.GetForThread(CfxThreadId.Renderer).PostTask(task);
                Monitor.Wait(task);
            }

            rpcContext.Exit();

            LogWriteLine("AsyncTestFunctionCallback: result from callback = " + result);
            LogWriteLine("AsyncTestFunctionCallback: done.");
        }