Beispiel #1
0
        public static void Register(this RpcServer server, String procedure)
        {
            Log.Trace("RpcBroker.Register");

            if (String.IsNullOrWhiteSpace(procedure))
            {
                throw new ArgumentException("procedure");
            }

            if (s_serverMethods.TryAdd(procedure, server) == false)
            {
                throw new InvalidOperationException("Procedure '" + procedure + "' has already been registered");
            }
        }
Beispiel #2
0
        public static void Unregister(this RpcServer server, String procedure)
        {
            Log.Trace("RpcBroker.Unregister");

            if (String.IsNullOrWhiteSpace(procedure))
            {
                throw new ArgumentException("procedure");
            }

            RpcServer unused;

            if (s_serverMethods.TryRemove(procedure, out unused) == false)
            {
                throw new InvalidOperationException("Procedure '" + procedure + "' has is not registered");
            }
        }
Beispiel #3
0
        public static void SendReply(this RpcServer server, Message reply)
        {
            Log.Trace("RpcBroker.SendReply");

            var processMessage = CefProcessMessage.Create(reply.Token);

            processMessage.Arguments.SetString(0, reply.Token);
            processMessage.Arguments.SetString(1, reply.Procedure);
            if (reply.Data != null)
            {
                processMessage.Arguments.SetList(2, reply.Data.AsCefListValue());
            }
            else
            {
                processMessage.Arguments.SetList(2, CefListValue.Create());
            }

            server.Browser.SendProcessMessage(CefProcessId.Browser, processMessage);
        }
        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            if (frame.IsMain == false)
            {
                base.OnContextCreated(browser, frame, context);
                return;
            }

            Log.Trace("RenderProcessHandler.OnContextCreated( browser: {0}, frame: {1} )",
                      browser.Identifier,
                      frame.Identifier);

            m_javascriptInvokeServer = new RpcServer(browser, "javascript:invoke");

            m_javascriptInvokeServer.RequestHandler = request =>
            {
                var function  = request[0] as String ?? "";
                var arguments = request[1] as Object[] ?? new Object[0];

                var ctx = m_javascriptInvokeServer.Browser.GetMainFrame().V8Context;

                if (ctx.Enter() == false)
                {
                    throw new InvalidOperationException("Could not acquire the V8 context");
                }

                JSValue result;

                try
                {
                    var global = ctx.GetGlobal();

                    var func = global.GetValue(function);

                    var rawArgs = (Object[])(new JSValue(arguments).Value);

                    var v8Args = rawArgs.Select(arg => new JSValue(arg).AsV8Value()).ToArray();

                    var v8Return = func.ExecuteFunctionWithContext(ctx, global, v8Args);

                    result = new JSValue(v8Return);
                }
                finally
                {
                    ctx.Exit();
                }

                return(result);
            };

            m_javascriptEvalServer = new RpcServer(browser, "javascript:eval");

            m_javascriptEvalServer.RequestHandler = request =>
            {
                var eval = request[0] as String ?? "";

                var ctx = m_javascriptEvalServer.Browser.GetMainFrame().V8Context;

                if (ctx.Enter() == false)
                {
                    throw new InvalidOperationException("Could not acquire the V8 context");
                }

                try
                {
                    var global = ctx.GetGlobal();

                    var func = global.GetValue("eval");

                    func.ExecuteFunctionWithContext(ctx, global, new[] { CefV8Value.CreateString(eval) });
                }
                finally
                {
                    ctx.Exit();
                }

                return(null);
            };

            base.OnContextCreated(browser, frame, context);
        }