Beispiel #1
0
        public unsafe void InitAsChild(IntPtr handle, System.Drawing.Rectangle rect)
        {
            cef_window_info_t *pinfo = WindowInfo.FixedPtr;

            pinfo->parent_window = handle;
            pinfo->x             = rect.Left;
            pinfo->y             = rect.Top;
            pinfo->width         = rect.Width;
            pinfo->height        = rect.Height;

            CefWin.WriteDebugLine("Creating browser " + CefWin.ApplicationElapsed);

            Browser          = CefBrowser.CreateBrowserSync(WindowInfo, Client, Url, Settings);
            MainFrame        = Browser.GetMainFrame();
            BrowserHost      = Browser.GetHost();
            hostWindowHandle = BrowserHost.GetWindowHandle();

            CefWin.WriteDebugLine("Creating browser OK doc:" + Browser.HasDocument + " id:" + Browser.Identifier + ":" + MainFrame.Identifier + " " + CefWin.ApplicationElapsed);
            CefWin.WriteDebugLine("send msgfrombrowser");
            MainFrame.SendProcessMessage(cef_process_id_t.PID_RENDERER, "msgfrombrowser");

            //do not call here, document is not ready!
            //MainFrame.ExecuteJavaScript("console.log('hello CefLite')");

            //ShowDevTools();
        }
Beispiel #2
0
 protected override void OnLoad(EventArgs e)
 {
     Agent.Client.LoadHandler.LoadEnd += _LoadEnd;
     base.OnLoad(e); // Show(){ OnLoad(){ InitAsChild() } }
     _browserAgent.InitAsChild(this.Handle, this.ClientRectangle);
     _browserAgent.WaitBrowserReady();
     CefWin.Register(this);
 }
Beispiel #3
0
 public void WaitBrowserReady()
 {
     while (Browser == null)
     {
         CefWin.DoMessageLoopOnce();
     }
     CefWin.DoMessageLoopOnce();
 }
Beispiel #4
0
 static public void ShowDefaultDownloadForm(System.Windows.Forms.Form parentForm)
 {
     if (_defdlf == null || _defdlf.IsDisposed)
     {
         _defdlf = new DefaultDownloadForm();
         _defdlf.Show(parentForm);
     }
     else
     {
         CefWin.ActivateForm(parentForm);
     }
 }
Beispiel #5
0
 static public void PostVersionUpdateEvent()
 {
     if (_postingUpdate)
     {
         return;
     }
     _postingUpdate = true;
     CefWin.PostToAppThread(delegate
     {
         DataVersion++;
         _postingUpdate = false;
         DataVersionUpdated?.Invoke();
     });
 }
Beispiel #6
0
        static public SplashForm Show(string photofile)
        {
            Image img = null;

            try
            {
                var data = System.IO.File.ReadAllBytes(photofile);
                img = Image.FromStream(new System.IO.MemoryStream(data));
            }
            catch (Exception x)
            {
                CefWin.WriteDebugLine(x);
                CefWin.WriteDebugLine("Failed to load " + photofile);
                return(null);
            }
            return(Show(img));
        }
Beispiel #7
0
        public DefaultBrowserForm(string defaulturl)
        {
            InitializeComponent();

            if (CefWin.ApplicationIcon != null)
            {
                this.Icon = CefWin.ApplicationIcon;
            }

            this.Text = CefWin.ApplicationTitle;

            this.MinimumSize = new Size(360, 360);

            if (Application.OpenForms.Count - (CefWin._splashForm?.Visible == true ? 1 : 0) == 0)
            {
                this.StartPosition = FormStartPosition.CenterScreen;
            }

            this.Disposed += DefaultBrowserForm_Disposed;

            this.Resize += DefaultBrowserForm_Resize;

            _browserAgent     = new BrowserAgent();
            _browserAgent.Url = defaulturl;

            if (CefWin._splashForm != null)
            {
                this.Opacity = 0;
                //this.ShowInTaskbar = false;   //TODO:this will affect the Forms collection , handle it later
            }


            CefWin.PostToAppThread(delegate
            {
                BeforeLoad?.Invoke(this, EventArgs.Empty);
                CefWin.WriteDebugLine("BeforeLoad Invoked.");
            });
        }
Beispiel #8
0
        void _LoadEnd(CefLoadHandler handler, CefBrowser browser, CefFrame frame, int status)
        {
            this.Agent.Client.LoadHandler.LoadEnd -= _LoadEnd;
            CefWin.WriteDebugLine("LoadEnd:" + browser.Identifier + ":" + frame.Identifier + ":" + status);

            CefWin.CloseSplashScreen();

            if (this.Opacity < 1)
            {
                //this.ShowInTaskbar = true;
                WF.Timer timer = new WF.Timer();
                timer.Interval = 20;
                timer.Tick    += delegate
                {
                    this.Opacity += OpacityShowStepValue;
                    //WriteDebugLine("Opacity..." + form.Opacity);
                    if (this.Opacity >= 1)
                    {
                        timer.Dispose();
                    }
                };
                timer.Start();
            }
        }
Beispiel #9
0
 private void DefaultBrowserForm_Disposed(object sender, EventArgs e)
 {
     _browserAgent.Dispose();
     CefWin.NotifyFormDisposed();
     AfterDisposed?.Invoke(this, e);
 }
Beispiel #10
0
        public void InitClientCommon(CefClient client)
        {
            uint   lastdownloadid = 0;
            string lastdownloadop = null;

            client.DownloadHandler.BeforeDownload +=
                (CefDownloadHandler handler, CefBrowser browser, CefDownloadItem item, string suggested_name, CefBeforeDownloadCallback callback) =>
            {
                lastdownloadid = item.Id;
                lastdownloadop = null;

                CefWin.WriteDebugLine("OnBeforeDownload:" + suggested_name + " " + browser.IsPopup + ":" + browser.HasDocument);
                CefWin.WriteDebugLine(item.Id + ":" + item.TotalBytes + ":" + item.Url);

                CefWin.InvokeInAppThread(delegate
                {
                    var parentForm = System.Windows.Forms.Control.FromChildHandle(browser.GetHost().GetWindowHandle())?.FindForm();

                    using (var dialog = new System.Windows.Forms.SaveFileDialog())
                    {
                        dialog.FileName = suggested_name;
                        var result      = dialog.ShowDialog(parentForm);
                        if (result == System.Windows.Forms.DialogResult.OK)
                        {
                            CefWin.WriteDebugLine("Cont:" + dialog.FileName);
                            callback.Cont(dialog.FileName, false);
                            lastdownloadop = "continue";
                        }
                        else
                        {
                            lastdownloadop = "cancel";
                        }
                    }

                    if (!browser.HasDocument && parentForm != null)
                    {
                        parentForm.Hide();
                    }

                    if (lastdownloadop == "cancel")
                    {
                        return;
                    }


                    CefWin.PostToAppThread(delegate
                    {
                        DownloadItem.ShowDownloadForm(parentForm?.Visible == true ? parentForm : null);
                    });
                });
            };

            client.DownloadHandler.DownloadUpdated +=
                (CefDownloadHandler handler, CefBrowser browser, CefDownloadItem item, CefDownloadItemCallback callback) =>
            {
                CefWin.WriteDebugLine("OnDownloadUpdated:" + " " + browser.IsPopup + ":" + browser.HasDocument);
                CefWin.WriteDebugLine(item.Id + ":" + item.ReceivedBytes + "/" + item.TotalBytes + ":" + item.Url);
                CefWin.WriteDebugLine(item.IsInProgress + ":" + item.IsCanceled + ":" + item.IsComplete + ":" + item.FullPath);

                if (lastdownloadid == item.Id && lastdownloadop == "cancel")
                {
                    CefWin.WriteDebugLine("Cancel.");
                    callback.Cancel();
                    return;
                }

                if (lastdownloadid == item.Id && lastdownloadop == "continue")
                {
                    lastdownloadop = "download";
                    DownloadItem.Show(item, callback);
                }

                if (item.TotalBytes > 0)
                {
                    DownloadItem.Update(item, callback);
                }

                //callback.Resume();
                if (!browser.HasDocument && (item.IsCanceled || item.IsComplete))
                {
                    //Problem , close the browser will stop the download??
                    browser.GetHost().CloseBrowser();
                }
            };

            client.LifeSpanHandler.AfterCreated += (a, b) =>
            {
                CefWin.WriteDebugLine("OnAfterCreated:" + CefWin.ApplicationElapsed);
            };

            client.LifeSpanHandler.BeforePopup +=
                (CefLifeSpanHandler lifeSpanHandler, CefBrowser browser, CefFrame frame, string url, string name, cef_window_open_disposition_t dispostion, int user_gesture, CefPopupFeatures features, CefWindowInfo wininfo, ref CefClient cient, CefBrowserSettings settings, CefDictionaryValue extra_info, ref int no_javascript_access) =>
            {
                CefWin.WriteDebugLine("OnBeforePopup:" + url + ":" + name);
                //Show Popup in DefaultBrowserForm
                CefWin.OpenBrowser(url);
                return(1);
            };
            client.RequestHandler.OpenUrlFromTab +=
                (CefRequestHandler lifeSpanHandler, CefBrowser browser, CefFrame frame, string url, cef_window_open_disposition_t disposition) =>
            {
                //This event means shift+click a link.
                CefWin.WriteDebugLine("OnOpenUrlFromTab:" + url);
                CefWin.OpenBrowser(url);
            };
#if DEBUG
            client.RequestHandler.BeforeBrowse +=
                (CefRequestHandler lifeSpanHandler, CefBrowser browser, CefFrame frame, CefRequest request, int user_gesture, int is_redirect) =>
            {
                CefWin.WriteDebugLine("OnBeforeBrowse:" + user_gesture + ":" + is_redirect + " , " + request.IsReadOnly + ":" + request.Url);
                //No effect , readonly.
                //request.SetHeaderByName("Browser-Agent-Id", this.Id.ToString(),true);
                return(0);
            };

            //client.RequestHandler.ResourceRequestHandler.BeforeResourceLoad +=
            //    (CefResourceRequestHandler handler, CefBrowser browser, CefFrame frame, CefRequest request, CefRequestCallback callback) =>
            //    {
            //        //Not able to capture websocket ?
            //        string url = request.Url;
            //        if (url.StartsWith("devtools://"))
            //            return cef_return_value_t.RV_CONTINUE;
            //        CefWin.WriteDebugLine("OnBeforeResourceLoad:" + request.IsReadOnly + ":" + url);
            //        //request.SetHeaderByName("CefWinBrowserId", browser.Identifier.ToString(), true);
            //        //CefWin.WriteDebugLine("BrowserAgentId:" + request.GetHeaderByName("BrowserAgentId"));
            //        return cef_return_value_t.RV_CONTINUE;
            //    };
#endif
        }