示例#1
0
        internal static void Setup(ChromeWidgetMessageInterceptor interceptor, Formium formium, Func <Message, bool> forwardAction)
        {
            Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        if (BrowserWidgetHandleFinder.TryFindHandle(formium.BrowserWindowHandle, out IntPtr chromeWidgetHostHandle))
                        {
                            interceptor = new ChromeWidgetMessageInterceptor(formium.HostWindowInternal, chromeWidgetHostHandle, forwardAction);

                            if (WinFormium.Runtime.IsDebuggingMode)
                            {
                                WinFormium.GetLogger().Debug("BrowserWindow has been attached successfully.");
                            }

                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                }
                catch
                {
                }
            });
        }
示例#2
0
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);

            try
            {
                WindowUtils.EnableAcrylic(this);
            }
            catch (Exception ex)
            {
                WinFormium.GetLogger().Error(ex);
            }
        }
        private void AcceptClient(NamedPipeServerStream pipe)
        {
            Task.Run(() =>
            {
                try
                {
                    var buff = new byte[BUFFER_SIZE];
                    var ms   = new MemoryStream();

                    do
                    {
                        ms.Write(buff, 0, pipe.Read(buff, 0, buff.Length));
                    }while (!pipe.IsMessageComplete);

                    var body = Encoding.UTF8.GetString(ms.ToArray());

                    ms.Close();
                    ms.Dispose();
                    buff = null;

                    var request = MessageBridgeRequest.Create(body);

                    MessageBridgeResponse response = null;

                    foreach (var handler in JavascriptBridge.JavascriptRequestHandlers)
                    {
                        try
                        {
                            var retval = handler?.Invoke(request);
                            if (retval != null)
                            {
                                response = retval;
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            response = MessageBridgeResponse.CreateFailureResponse(ex.Message);
                        }
                    }

                    if (response == null)
                    {
                        response = MessageBridgeResponse.CreateFailureResponse("Can't found handler for this request.");
                    }
                    buff = Encoding.UTF8.GetBytes(response.ToJson());
                    try
                    {
                        pipe.Write(buff, 0, buff.Length);
                        pipe.Flush();
                        pipe.WaitForPipeDrain();
                    }
                    catch (Exception ex)
                    {
                        WinFormium.GetLogger().Debug($"NamedPipeServer can't write to client. {ex.Message}");
                    }
                    finally
                    {
                        pipe.Disconnect();
                    }
                }
                catch (Exception ex)
                {
                    WinFormium.GetLogger().Error(ex);
                }
            }, CancellationToken);
        }