Example #1
0
 public ChromiumFxDispatcher(CfrBrowser browser, CfrV8Context context, IWebSessionLogger logger)
 {
     _Logger    = logger;
     _Browser   = browser;
     _Context   = context;
     TaskRunner = _Context.TaskRunner;
 }
Example #2
0
 public ChromiumFxDispatcher(CfrBrowser browser, CfrV8Context context, IWebSessionLogger logger)
 {
     _Logger            = logger;
     _Browser           = browser;
     _Context           = context;
     _RemoteCallContext = _Browser.CreateRemoteCallContext();
 }
Example #3
0
 private void SetProperties(CfrV8Context context, JSObject obj)
 {
     foreach (var p in obj)
     {
         var v8Value = p.Value.GetV8Value(context);
         context.Global.SetValue(p.Key, v8Value, CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly);
     }
 }
Example #4
0
 public ChromiumFxDispatcher(CfrBrowser browser, CfrV8Context context, IWebSessionLogger logger)
 {
     _Logger           = logger;
     _Browser          = browser;
     _Context          = context;
     TaskRunner        = _Context.TaskRunner;
     _CfrTask          = new CfrTask();
     _CfrTask.Execute += CfrTask_Execute;
 }
Example #5
0
 internal CfrV8Value GetV8Value(CfrV8Context context)
 {
     if (v8Value == null || !Object.ReferenceEquals(v8Context, context))
     {
         v8Context = context;
         v8Value   = CreateV8Value();
     }
     return(v8Value);
 }
 public ChromiumFxWebView(CfrBrowser cfrbrowser, IWebSessionLogger logger)
 {
     _Logger     = logger;
     _Browser    = cfrbrowser;
     _CfrFrame   = _Browser.MainFrame;
     V8Context   = _CfrFrame.V8Context;
     _Dispatcher = new ChromiumFxDispatcher(_Browser, V8Context, _Logger);
     Converter   = new ChromiumFxConverter(V8Context);
     Factory     = new ChromiumFxFactory(V8Context);
 }
Example #7
0
        private void JS_SubmitAsyncTestFunction(object sender, CfrV8HandlerExecuteEventArgs e)
        {
            var function = e.Arguments[0];

            LogWriteLine("JS_SubmitAsyncTestFunction: function.FunctionName = " + function.FunctionName);
            LogWriteLine("JS_SubmitAsyncTestFunction: CfrRuntime.CurrentlyOn(CfxThreadId.Renderer) = " + CfrRuntime.CurrentlyOn(CfxThreadId.Renderer));
            var ctx = CfrV8Context.GetCurrentContext();
            var t   = new Thread(() => { AsyncTestFunctionCallback(function, ctx); });

            t.Start();
        }
Example #8
0
        private async void TestExecute(object sender, CfrV8HandlerExecuteEventArgs e)
        {
            //save current context
            var v8Context = CfrV8Context.GetCurrentContext();
            var callback  = Array.Find(e.Arguments, p => p.IsFunction);

            //simulate async methods.
            await Task.Delay(5000);

            CallCallback(callback, v8Context, new KeyValuePair <string, object>("Name", 12));
        }
        internal ChromiumFxFactory(CfrV8Context context)
        {
            _CfrV8Context      = context;
            _Factory           = new Lazy <CfrV8Value>(FactoryCreator);
            _ObjectBuilder     = new Lazy <CfrV8Value>(ObjectBuilderCreator);
            _ObjectBulkBuilder = new Lazy <CfrV8Value>(ObjectBulkBuilderCreator);
            _ArrayBulkBuilder  = new Lazy <CfrV8Value>(ArrayBulkBuilderCreator);
            _BasicBulkBuilder  = new Lazy <CfrV8Value>(BasicBulkBuilderCreator);
            _ObjectWithConstructorBulkBuilder = new Lazy <CfrV8Value>(ObjectWithConstructorBulkBuilderCreator);

            _ObjectCreationCallbackFunction = new Lazy <CfrV8Value>(ObjectCreationCallbackFunctionCreator);
        }
Example #10
0
        void CfxHelloWorld_Execute(object sender, CfrV8HandlerExecuteEventArgs e)
        {
            if (e.Arguments.Length > 1)
            {
                var r1 = e.Arguments[0].IntValue;
                var r2 = e.Arguments[1].StringValue;
                LogWriteLine("CfxHelloWorld_Execute arguments: " + r1 + ", '" + r2 + "'");
            }
            LogCallback(sender, e);
            var context = CfrV8Context.GetEnteredContext();

            e.SetReturnValue("CfxHelloWorld returns this text.");
        }
Example #11
0
 private void RegisterObjectToGlobal(BrowserCore browser, CfrFrame frame, CfrV8Context context)
 {
     if (frame.IsMain)
     {
         SetProperties(context, browser.GlobalObject);
     }
     else
     {
         if (browser.frameGlobalObjects.TryGetValue(frame.Name, out JSObject obj))
         {
             SetProperties(context, obj);
         }
     }
 }
Example #12
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);
            }
        }
Example #13
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.");
        }
Example #14
0
        private async void MigrateExecute(object sender, CfrV8HandlerExecuteEventArgs e)
        {
            if (e.Arguments.Length != 6)
            {
                e.Exception = "6 parameters expected!";
                return;
            }

            var server   = e.Arguments[0];
            var port     = e.Arguments[1];
            var user     = e.Arguments[2];
            var password = e.Arguments[3];
            var database = e.Arguments[4];
            var callback = e.Arguments[5];
            var context  = CfrV8Context.GetCurrentContext();

            if (!server.IsString || !port.IsInt || !user.IsString || !password.IsString || !database.IsString || !callback.IsFunction)
            {
                //Invalid parameter;
                e.Exception = "Invalid parameter type";
                return;
            }

            Settings.Default.SQL_HOST     = server.StringValue;
            Settings.Default.SQL_PORT     = port.IntValue;
            Settings.Default.SQL_USER     = user.StringValue;
            Settings.Default.SQL_PASSWORD = password.StringValue;
            Settings.Default.SQL_DATABASE = database.StringValue;
            Settings.Default.Save();

            await Task.Run(() =>
            {
                var is_success = DataHelper.MigrateDB(true);
                AppHelper.CreateStartup();
                AppHelper.CreateInsDate();

                CallCallback(callback, context, new KeyValuePair <string, object>("success", is_success));
            });
        }
Example #15
0
 public BrowserV8ContextCreatedEventArgs(CfrBrowser browser, CfrFrame frame, CfrV8Context context)
 {
     Browser = browser;
     Frame   = frame;
     Context = context;
 }
Example #16
0
 internal void RaiseOnV8ContextCreated(CfrBrowser browser, CfrFrame frame, CfrV8Context context)
 {
     OnV8ContextCreated?.Invoke(this, new BrowserV8ContextCreatedEventArgs(browser, frame, context));
 }
Example #17
0
 internal ChromiumFxFactory(CfrV8Context context)
 {
     _CfrV8Context = context;
 }
Example #18
0
 public ChromiumFXContext(CfrV8Context context)
 {
     _Context = context;
     _Context.Enter();
 }
Example #19
0
        public Form1()
            : base("http://res.app.local/www/index.html")
        {
            InitializeComponent();

            LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd;

            //register the "my" object
            var myObject = GlobalObject.AddObject("my");

            //add property "name" to my, you should implemnt the getter/setter of name property by using PropertyGet/PropertySet events.
            var nameProp = myObject.AddDynamicProperty("name");

            nameProp.PropertyGet += (prop, args) =>
            {
                // getter - if js code "my.name" executes, it'll get the string "NanUI".
                args.Retval = CfrV8Value.CreateString("NanUI");
                args.SetReturnValue(true);
            };
            nameProp.PropertySet += (prop, args) =>
            {
                // setter's value from js context, here we do nothing, so it will store or igrone by your mind.
                var value = args.Value;
                args.SetReturnValue(true);
            };


            //add a function showCSharpMessageBox
            var showMessageBoxFunc = myObject.AddFunction("showCSharpMessageBox");

            showMessageBoxFunc.Execute += (func, args) =>
            {
                //it will be raised by js code "my.showCSharpMessageBox(`some text`)" executed.
                //get the first string argument in Arguments, it pass by js function.
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);

                if (stringArgument != null)
                {
                    MessageBox.Show(this, stringArgument.StringValue, "C# Messagebox", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            };

            //add a function getArrayFromCSharp, this function has an argument, it will combind C# string array with js array and return to js context.
            var friends = new string[] { "Mr.JSON", "Mr.Lee", "Mr.BONG" };

            var getArrayFromCSFunc = myObject.AddFunction("getArrayFromCSharp");

            getArrayFromCSFunc.Execute += (func, args) =>
            {
                var jsArray = args.Arguments.FirstOrDefault(p => p.IsArray);



                if (jsArray == null)
                {
                    jsArray = CfrV8Value.CreateArray(friends.Length);
                    for (int i = 0; i < friends.Length; i++)
                    {
                        jsArray.SetValue(i, CfrV8Value.CreateString(friends[i]));
                    }
                }
                else
                {
                    var newArray = CfrV8Value.CreateArray(jsArray.ArrayLength + friends.Length);

                    for (int i = 0; i < jsArray.ArrayLength; i++)
                    {
                        newArray.SetValue(i, jsArray.GetValue(i));
                    }

                    var jsArrayLength = jsArray.ArrayLength;

                    for (int i = 0; i < friends.Length; i++)
                    {
                        newArray.SetValue(i + jsArrayLength, CfrV8Value.CreateString(friends[i]));
                    }


                    jsArray = newArray;
                }


                //return the array to js context

                args.SetReturnValue(jsArray);

                //in js context, use code "my.getArrayFromCSharp()" will get an array like ["Mr.JSON", "Mr.Lee", "Mr.BONG"]
            };

            //add a function getObjectFromCSharp, this function has no arguments, but it will return a Object to js context.
            var getObjectFormCSFunc = myObject.AddFunction("getObjectFromCSharp");

            getObjectFormCSFunc.Execute += (func, args) =>
            {
                //create the CfrV8Value object and the accssor of this Object.
                var jsObjectAccessor = new CfrV8Accessor();
                var jsObject         = CfrV8Value.CreateObject(jsObjectAccessor);

                //create a CfrV8Value array
                var jsArray = CfrV8Value.CreateArray(friends.Length);

                for (int i = 0; i < friends.Length; i++)
                {
                    jsArray.SetValue(i, CfrV8Value.CreateString(friends[i]));
                }

                jsObject.SetValue("libName", CfrV8Value.CreateString("NanUI"), CfxV8PropertyAttribute.ReadOnly);
                jsObject.SetValue("friends", jsArray, CfxV8PropertyAttribute.DontDelete);


                args.SetReturnValue(jsObject);

                //in js context, use code "my.getObjectFromCSharp()" will get an object like { friends:["Mr.JSON", "Mr.Lee", "Mr.BONG"], libName:"NanUI" }
            };


            //add a function with callback

            var callbackTestFunc = GlobalObject.AddFunction("callbackTest");

            callbackTestFunc.Execute += (func, args) => {
                var callback = args.Arguments.FirstOrDefault(p => p.IsFunction);
                if (callback != null)
                {
                    var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor());
                    callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly);
                    callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), CfxV8PropertyAttribute.ReadOnly);

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


            //add a function with async callback
            var asyncCallbackTestFunc = GlobalObject.AddFunction("asyncCallbackTest");

            asyncCallbackTestFunc.Execute += async(func, args) => {
                //save current context
                var v8Context = CfrV8Context.GetCurrentContext();
                var callback  = args.Arguments.FirstOrDefault(p => p.IsFunction);

                //simulate async methods.
                await Task.Delay(5000);

                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();

                        //create callback argument
                        var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor());
                        callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly);
                        callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), 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);
                }
            };
        }
Example #20
0
 public ChromiumFxContext(CfrV8Context context, ChromiumFxDispatcher dispatcher)
 {
     _Dispatcher = dispatcher;
     _Context    = context;
     _Context.Enter();
 }
Example #21
0
 internal ChromiumFxConverter(CfrV8Context context)
 {
     _CfrV8Context = context;
 }
Example #22
0
 public ChromiumFxContext(CfrV8Context context, IWebSessionLogger logger)
 {
     _Logger  = logger;
     _Context = context;
     _Context.Enter();
 }