Exemplo n.º 1
0
        public virtual void ProcessRequest(ResourceRequest request, CoreWebView2Deferral deferral, Action <ResourceResponse, CoreWebView2Deferral> callback)
        {
            Task.Run(async() =>
            {
                try
                {
                    ResourceResponse response = null;
                    response = await RequestInterceptor.ProcessRequest(_appFunc, request);

                    if (IsBadRequest((HttpStatusCode)response.StatusCode) || IsRouteNotFound((HttpStatusCode)response.StatusCode))
                    {
                        string redirectUrl = GetHomeUrl(request.Url);
                        var newRequest     = new Chromium.AspNetCore.Bridge.ResourceRequest(redirectUrl, "GET", request.Headers, null);
                        response           = await RequestInterceptor.ProcessRequest(_appFunc, newRequest);
                    }

                    ((App)Application.Current).Dispatcher.Invoke(
                        DispatcherPriority.Background,
                        new Action(() =>
                    {
                        // Callback
                        callback.Invoke(response, deferral);
                    }));
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    Console.WriteLine(exception.StackTrace);

                    deferral?.Complete();
                    deferral = null;
                }
            });
        }
Exemplo n.º 2
0
        private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
        {
            CoreWebView2Deferral deferral  = e.GetDeferral();
            BrowserForm          newWindow = new BrowserForm(e, deferral);

            newWindow.Show();
        }
 public void CompleteNewTabInitialize(CoreWebView2 coreWebView)
 {   //When changing WPF Tab Items everything initializes again.
     if (Def != null)
     {
         Args.NewWindow = coreWebView;
         Args.Handled   = true;
         Def.Complete();
         Def = null;
     }
 }
 public void CompleteNewTabInitialize(CoreWebView2 coreWebView)
 {
     if (Def != null)
     {
         Args.NewWindow = coreWebView;
         Args.Handled   = true;
         Def.Complete();
         Def = null;
     }
 }
Exemplo n.º 5
0
        void DeferredCustomClientCertificateSelectionDialog()
        {
            // Safeguarding the handler when unsupported runtime is used.
            try
            {
                if (!_isCustomClientCertificateSelectionDialog)
                {
                    webView.CoreWebView2.ClientCertificateRequested += delegate(
                        object sender, CoreWebView2ClientCertificateRequestedEventArgs args)
                    {
                        // Developer can obtain a deferral for the event so that the WebView2
                        // doesn't examine the properties we set on the event args until
                        // after the deferral completes asynchronously.
                        CoreWebView2Deferral deferral = args.GetDeferral();

                        System.Threading.SynchronizationContext.Current.Post((_) =>
                        {
                            using (deferral)
                            {
                                IReadOnlyList <CoreWebView2ClientCertificate> certificateList = args.MutuallyTrustedCertificates;
                                if (certificateList.Count() > 0)
                                {
                                    // Display custom dialog box for the client certificate selection.
                                    var dialog = new ClientCertificateSelectionDialog(
                                        title: "Select a Certificate for authentication",
                                        host: args.Host,
                                        port: args.Port,
                                        client_cert_list: certificateList);
                                    if (dialog.ShowDialog() == true)
                                    {
                                        // Continue with the selected certificate to respond to the server if `OK` is selected.
                                        args.SelectedCertificate = (CoreWebView2ClientCertificate)dialog.CertificateDataBinding.SelectedItem;
                                    }
                                    // Continue without a certificate to respond to the server if `CANCEL` is selected.
                                    args.Handled = true;
                                }
                                else
                                {
                                    // Continue without a certificate to respond to the server if certificate list is empty.
                                    args.Handled = true;
                                }
                            }
                        }, null);
                    };
                    _isCustomClientCertificateSelectionDialog = true;
                    MessageBox.Show("Custom Client Certificate selection dialog will be used next when WebView2 is making a " +
                                    "request to an HTTP server that needs a client certificate.", "Client certificate selection");
                }
            }
            catch (NotImplementedException exception)
            {
                MessageBox.Show(this, "Custom client certificate selection dialog Failed: " + exception.Message, "Client certificate selection");
            }
        }
Exemplo n.º 6
0
        private async void CoreWebView2_NewWindowRequestedAsync(object sender, CoreWebView2NewWindowRequestedEventArgs e)
        {
            CoreWebView2Deferral def = e.GetDeferral();
            Tab nTab = new();

            MainBrowser.HandleNewTabRequestAsync(nTab);
            await nTab.CreateTabFromNewWindowRequested(CoreWebView2.Environment, true, MainBrowser);

            e.NewWindow = nTab.CoreWebView2;
            e.Handled   = true;
            def.Complete();
        }
Exemplo n.º 7
0
        void DownloadStartingCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            try
            {
                webView.CoreWebView2.DownloadStarting += delegate(
                    object sender, CoreWebView2DownloadStartingEventArgs args)
                {
                    // Developer can obtain a deferral for the event so that the CoreWebView2
                    // doesn't examine the properties we set on the event args until
                    // after the deferral completes asynchronously.
                    CoreWebView2Deferral deferral = args.GetDeferral();

                    // We avoid potential reentrancy from running a message loop in the download
                    // starting event handler by showing our download dialog later when we
                    // complete the deferral asynchronously.
                    System.Threading.SynchronizationContext.Current.Post((_) =>
                    {
                        using (deferral)
                        {
                            // Hide the default download dialog.
                            args.Handled = true;
                            var dialog   = new TextInputDialog(
                                title: "Download Starting",
                                description: "Enter new result file path or select OK to keep default path. Select cancel to cancel the download.",
                                defaultInput: args.ResultFilePath);
                            if (dialog.ShowDialog() == true)
                            {
                                args.ResultFilePath = dialog.Input.Text;
                                UpdateProgress(args.DownloadOperation);
                            }
                            else
                            {
                                args.Cancel = true;
                            }
                        }
                    }, null);
                };
                webView.CoreWebView2.Navigate("https://demo.smartscreen.msft.net/");
            }
            catch (NotImplementedException exception)
            {
                MessageBox.Show(this, "DownloadStarting Failed: " + exception.Message, "Download Starting");
            }
        }
Exemplo n.º 8
0
        private void WebView2Control_CoreWebView2Ready(object sender, EventArgs e)
        {
            HandleResize();

            _webView2Control.Source = new Uri("https://www.bing.com/");

            _webView2Control.CoreWebView2.SourceChanged        += CoreWebView2_SourceChanged;
            _webView2Control.CoreWebView2.HistoryChanged       += CoreWebView2_HistoryChanged;
            _webView2Control.CoreWebView2.DocumentTitleChanged += CoreWebView2_DocumentTitleChanged;
            _webView2Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Image);
            _webView2Control.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
            UpdateTitleWithEvent("CoreWebView2Ready");

            if (_newWindowRequestedEventArgs != null)
            {
                _newWindowRequestedEventArgs.NewWindow = _webView2Control.CoreWebView2;
                _newWindowRequestedEventArgs.Handled   = true;
                _newWindowDeferral.Complete();
                _newWindowRequestedEventArgs = null;
                _newWindowDeferral           = null;
            }
        }
Exemplo n.º 9
0
 public WebView2Manager(WebView2 webview2, CoreWebView2Deferral deferral)
 {
     WebView2 = webview2;
     Deferral = deferral;
     GUID     = Guid.NewGuid().ToString();
 }
Exemplo n.º 10
0
 public BrowserForm(CoreWebView2NewWindowRequestedEventArgs args, CoreWebView2Deferral deferral) : this()
 {
     _newWindowRequestedEventArgs = args;
     _newWindowDeferral           = deferral;
 }
 public void AddNewTab(CoreWebView2NewWindowRequestedEventArgs e)
 {
     Args        = e;
     Def         = e.GetDeferral();
     this.Source = new Uri(e.Uri);
 }