Ejemplo n.º 1
0
        public EdgeHtmlWebview(WebviewBridge bridge)
        {
            this.bridge = bridge ?? throw new ArgumentNullException(nameof(bridge));

            var version = Native.GetOsVersion();

            supportsInitializeScript = version.MajorVersion >= 10 && version.BuildNumber >= 17763;

            var process = new WebViewControlProcess();
            var bounds  = new global::Windows.Foundation.Rect(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

            webview = process.CreateWebViewControlAsync(Handle.ToInt64(), bounds)
                      .AsTask()
                      .RunSyncWithPump();

            UpdateSize();

            if (supportsInitializeScript)
            {
                string initScript = Resources.GetInitScript("Windows");
                webview.AddInitializeScript(initScript);
            }

            webview.ScriptNotify += Webview_ScriptNotify;

            webview.NavigationStarting  += Webview_NavigationStarting;
            webview.NavigationCompleted += Webview_NavigationCompleted;
            Layout += (s, e) => UpdateSize();
        }
Ejemplo n.º 2
0
        private void Init()
        {
            var process = new WebViewControlProcess();
            var bounds  = new Rect(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

            webview = process.CreateWebViewControlAsync(Handle.ToInt64(), bounds)
                      .AsTask()
                      .ConfigureAwait(false)
                      .GetAwaiter()
                      .GetResult();

            UpdateSize();

            webview.DefaultBackgroundColor         = ParseColor(config.BackgroundColor);
            webview.Settings.IsScriptNotifyAllowed = config.EnableScriptInterface;
            if (config.EnableScriptInterface)
            {
                webview.ScriptNotify += Webview_ScriptNotify;

                // TODO: needs Win10 1809 - 10.0.17763.0
                // webview.AddInitializeScript(initScript);
            }

            webview.NavigationCompleted += Webview_NavigationCompleted;
        }
Ejemplo n.º 3
0
        private async void Form1_Load(object sender, EventArgs e)
        {
            var options = new WebViewControlProcessOptions
            {
                // Enable private network access to enable Enterprise SSO
                PrivateNetworkClientServerCapability = WebViewControlProcessCapabilityState.Enabled
            };
            var process = new WebViewControlProcess(options);
            var control = await process.CreateWebViewControlAsync(
                (long)this.Handle,
                new Windows.Foundation.Rect(0.0f, 0.0f, Width, Height));

            this.Layout += (o, a) =>
            {
                // This event is raised once at startup with the AffectedControl and AffectedProperty properties
                // on the LayoutEventArgs as null.
                if (a.AffectedControl != null && a.AffectedProperty != null)
                {
                    // Ensure that the affected property is the Bounds property to the control
                    if (a.AffectedProperty == nameof(this.Bounds))
                    {
                        // In a typical control the DisplayRectangle is the interior canvas of the control
                        // and in a scrolling control the DisplayRectangle would be larger than the ClientRectangle.
                        // However, that is abstracted from us in WebView so we need to synchronize the ClientRectangle
                        // and permit WebView to handle scrolling based on the new viewport
                        var rect = new Rect(
                            this.ClientRectangle.X,
                            this.ClientRectangle.Y,
                            this.ClientRectangle.Width,
                            this.ClientRectangle.Height);

                        control.Bounds = rect;
                    }
                }
            };

            control.ContainsFullScreenElementChanged += (o, a) =>
            {
                void EnterFullScreen()
                {
                    this.WindowState     = FormWindowState.Normal;
                    this.FormBorderStyle = FormBorderStyle.None;
                    this.WindowState     = FormWindowState.Maximized;
                }

                void LeaveFullScreen()
                {
                    this.FormBorderStyle = FormBorderStyle.Sizable;
                    this.WindowState     = FormWindowState.Normal;
                }

                // Toggle
                this.isFullScreen = !this.isFullScreen;

                if (this.isFullScreen)
                {
                    EnterFullScreen();
                }
                else
                {
                    LeaveFullScreen();
                }
            };

            control.ScriptNotify += (o, a) => { MessageBox.Show(a.Value, a.Uri?.ToString() ?? string.Empty); };

            control.NavigationCompleted += (o, a) =>
            {
                this.Text = o.DocumentTitle;
                if (!a.IsSuccess)
                {
                    MessageBox.Show(
                        $"Could not navigate to {a.Uri}",
                        $"Error: {a.WebErrorStatus}",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            };

            control.NavigationStarting += (o, a) =>
            {
                this.Text = "Navigating " + (a.Uri?.ToString() ?? string.Empty);
            };

            control.PermissionRequested += (o, a) =>
            {
                if (a.PermissionRequest.State == WebViewControlPermissionState.Allow)
                {
                    return;
                }

                var msg = $"Allow {a.PermissionRequest.Uri.Host} to access {a.PermissionRequest.PermissionType}?";

                var response = MessageBox.Show(msg, "Permission Request", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

                if (response == DialogResult.Yes)
                {
                    if (a.PermissionRequest.State == WebViewControlPermissionState.Defer)
                    {
                        o.GetDeferredPermissionRequestById(a.PermissionRequest.Id, out var permission);
                        permission?.Allow();
                    }
                    else
                    {
                        a.PermissionRequest.Allow();
                    }
                }
                else
                {
                    if (a.PermissionRequest.State == WebViewControlPermissionState.Defer)
                    {
                        o.GetDeferredPermissionRequestById(a.PermissionRequest.Id, out var permission);
                        permission?.Deny();
                    }
                    else
                    {
                        a.PermissionRequest.Deny();
                    }
                }
            };

            control.Navigate(new Uri("http://bing.com"));
        }
Ejemplo n.º 4
0
        public static async Task <WebAuthenticationResult> AuthenticateAsync(
            Uri requestUri, Uri callbackUri, Window parentWindow)
        {
            var            webViewControlProcess = new WebViewControlProcess();
            WebViewControl webViewControl        = null;

            var webViewWindow = new WebAuthenticationWindow();

            webViewWindow.Owner = parentWindow;

            // Response details
            string responseData = "";
            uint   responseCode = 0;
            var    status       = WebAuthenticationStatus.Success;

            webViewWindow.Loaded += async(_, __) =>
            {
                await Task.Delay(200);

                webViewControl = await webViewControlProcess.CreateWebViewControlAsync(
                    new WindowInteropHelper(webViewWindow).Handle.ToInt64(),
                    new Windows.Foundation.Rect(0, 0, webViewWindow.ActualWidth, webViewWindow.ActualHeight));

                webViewControl.NavigationStarting += (s, e) =>
                {
                    // Check for the Uri first -- localhost will give a 404
                    // but we might already have the data we want
                    if (e.Uri.ToString().StartsWith(callbackUri.ToString()))
                    {
                        responseData = e.Uri.ToString();
                        webViewWindow.DialogResult = true;
                    }
                };

                webViewControl.NavigationCompleted += (s, e) =>
                {
                    if (!e.IsSuccess)
                    {
                        webViewWindow.DialogResult = false;
                        responseCode = (uint)e.WebErrorStatus;
                    }
                };

                webViewControl.Navigate(requestUri);
            };

            var dialogResult = await ShowDialogAsync(webViewWindow);

            if (dialogResult.HasValue)
            {
                status = dialogResult.Value ? WebAuthenticationStatus.Success : WebAuthenticationStatus.Error;
            }
            else
            {
                status = WebAuthenticationStatus.Canceled;
            }

            webViewControlProcess.Terminate();

            return(new WebAuthenticationResult(responseData, responseCode, status));
        }