예제 #1
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
        }