From 75f3af36389fa200e033dfc9e02caa19283a0626 Mon Sep 17 00:00:00 2001 From: Ognjen Sobajic Date: Tue, 11 Apr 2023 09:31:00 -0700 Subject: [PATCH] Update projects to the latest WebView2 SDK 1.0.1724-prerelease (#180) * Updates for Win32, WPF and WinForms sample apps from 114.0.1777.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.1777-prerelease --------- Co-authored-by: WebView2 Github Bot --- .../ScenarioCookieManagement.cpp | 7 +- .../WebView2APISample/ScenarioDragDrop.cpp | 9 +- .../WebView2APISample.vcxproj | 4 +- .../documentation/Testing-Instructions.md | 18 ++ SampleApps/WebView2APISample/packages.config | 2 +- .../BrowserForm.Designer.cs | 210 ++++++++++++++ .../BrowserForm.cs | 272 ++++++++++++++++++ ...ientCertificateSelectionDialog.Designer.cs | 101 +++++++ .../ClientCertificateSelectionDialog.cs | 64 +++++ .../WebView2WindowsFormsBrowser.csproj | 2 +- .../WebView2WpfBrowser/MainWindow.xaml.cs | 6 +- .../NewWindowOptionsDialog.xaml | 6 +- .../NewWindowOptionsDialog.xaml.cs | 3 + .../WebView2WpfBrowser.csproj | 2 +- 14 files changed, 689 insertions(+), 17 deletions(-) create mode 100644 SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.Designer.cs create mode 100644 SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.cs diff --git a/SampleApps/WebView2APISample/ScenarioCookieManagement.cpp b/SampleApps/WebView2APISample/ScenarioCookieManagement.cpp index 6c4fb3c2..691044b4 100644 --- a/SampleApps/WebView2APISample/ScenarioCookieManagement.cpp +++ b/SampleApps/WebView2APISample/ScenarioCookieManagement.cpp @@ -31,10 +31,9 @@ ScenarioCookieManagement::ScenarioCookieManagement(AppWindow* appWindow, bool is CHECK_FEATURE_RETURN_EMPTY(webView2_13); wil::com_ptr webView2Profile; CHECK_FAILURE(webView2_13->get_Profile(&webView2Profile)); - auto webView2ExperimentalProfile8 = - webView2Profile.try_query(); - CHECK_FEATURE_RETURN_EMPTY(webView2ExperimentalProfile8); - CHECK_FAILURE(webView2ExperimentalProfile8->get_CookieManager(&m_cookieManager)); + auto webView2Profile5 = webView2Profile.try_query(); + CHECK_FEATURE_RETURN_EMPTY(webView2Profile5); + CHECK_FAILURE(webView2Profile5->get_CookieManager(&m_cookieManager)); //! [CookieManagerProfile] } else diff --git a/SampleApps/WebView2APISample/ScenarioDragDrop.cpp b/SampleApps/WebView2APISample/ScenarioDragDrop.cpp index 333792c5..e8045bda 100644 --- a/SampleApps/WebView2APISample/ScenarioDragDrop.cpp +++ b/SampleApps/WebView2APISample/ScenarioDragDrop.cpp @@ -23,10 +23,10 @@ ScenarioDragDrop::ScenarioDragDrop(AppWindow* appWindow) : m_appWindow(appWindow Callback( [this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args) { - wil::com_ptr args2 = + wil::com_ptr args2 = wil::com_ptr(args) - .query(); - wil::com_ptr objectsCollection; + .query(); + wil::com_ptr objectsCollection; args2->get_AdditionalObjects(&objectsCollection); unsigned int length; objectsCollection->get_Count(&length); @@ -38,8 +38,7 @@ ScenarioDragDrop::ScenarioDragDrop(AppWindow* appWindow) : m_appWindow(appWindow wil::com_ptr object; objectsCollection->GetValueAtIndex(i, &object); - wil::com_ptr file = - object.query(); + wil::com_ptr file = object.query(); if (file) { // Add the file to message to be sent back to webview diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index c7e345cf..51e19395 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -382,13 +382,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/SampleApps/WebView2APISample/documentation/Testing-Instructions.md b/SampleApps/WebView2APISample/documentation/Testing-Instructions.md index 2f516ae6..a9f74a43 100644 --- a/SampleApps/WebView2APISample/documentation/Testing-Instructions.md +++ b/SampleApps/WebView2APISample/documentation/Testing-Instructions.md @@ -90,6 +90,7 @@ These are instructions for manually testing all the features of the WebView2 API * [Open Link in New Window From PDF](#Open-Link-in-New-Window-from-PDF) * [WebView Does Not Crash](#WebView-Does-Not-Crash) * [Draggable Regions](#Draggable-Regions) + * [Drag and Drop](#Drag-and-Drop) ## Getting started @@ -1404,3 +1405,20 @@ this the `appRegion` changes would not take place until some document element wa 1. Expected: Sample app will maximize 1. Right click 'Microsoft Edge WebView2' element and select `restore` 1. Expected: Sample app will restore. + +#### Drag and Drop +Test that Drag and Drop is supported in WebView2 using both hosting modes. +1. Launch the sample app. +1. Select text "Runtime version". +1. Click, hold, and drag the selected text to the Query text box. +1. Release mouse over text box to drop text. +1. Expected: "Runtime version" text is inserted into Query text box. +1. Go to `Window -> Close WebView.` +1. Go to `Window -> WebView Creation Mode -> Visual - DComp.` +1. Go to `Window -> Create WebView.` +1. Select text "Runtime version". +1. Click, hold, and drag the selected text to the Query text box. +1. Release mouse over text box to drop text. +1. Expected: "Runtime version" text is inserted into Query text box. +1. Go to `Window -> Close WebView.` +1. Expected: App does not crash. diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index 59696091..8810eb67 100644 --- a/SampleApps/WebView2APISample/packages.config +++ b/SampleApps/WebView2APISample/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs index 9e66dd1d..b56843db 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs +++ b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.Designer.cs @@ -3,6 +3,7 @@ // found in the LICENSE file. using System; +using Microsoft.Web.WebView2.Core; namespace WebView2WindowsFormsBrowser { @@ -53,6 +54,9 @@ private void InitializeComponent() this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.scriptToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.zoomToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.clearBrowsingDataStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.clientCertificateRequestedStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.cookieManagementStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.xToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.xToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.xToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); @@ -97,6 +101,25 @@ private void InitializeComponent() this.injectScriptIntoFrameMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.addInitializeScriptMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeInitializeScriptMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.AuthenticationMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearAllDOMStorageMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearAllProfileMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearAllSiteMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearAutofillMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearBrowsingHistoryMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearCookiesMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearDiskCacheMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ClearDownloadHistoryMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.CustomClientCertificateSelectionMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.DeferredCustomCertificateDialogMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.GetCookiesMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.AddOrUpdateCookieMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.DeleteCookiesMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.DeleteAllCookiesMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.postMessageStringMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.postMessageJsonMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.postMessageStringIframeMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.postMessageJsonIframeMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.webViewLogoBitmap = new System.Drawing.Bitmap(@"assets\AppStartPageBackground.png"); this.webView2Control = new Microsoft.Web.WebView2.WinForms.WebView2(); this.menuStrip1.SuspendLayout(); @@ -384,12 +407,40 @@ private void InitializeComponent() this.injectScriptIntoFrameMenuItem, this.methodCDPToolStripMenuItem, this.taskManagerToolStripMenuItem, + this.postMessageStringMenuItem, + this.postMessageJsonMenuItem, + this.postMessageStringIframeMenuItem, + this.postMessageJsonIframeMenuItem, this.addInitializeScriptMenuItem, this.removeInitializeScriptMenuItem}); this.scriptToolStripMenuItem.Name = "scriptToolStripMenuItem"; this.scriptToolStripMenuItem.Size = new System.Drawing.Size(86, 38); this.scriptToolStripMenuItem.Text = "Script"; // + // clearBrowsingDataStripMenuItem + // + this.clearBrowsingDataStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ClearAllDOMStorageMenuItem, this.ClearAllProfileMenuItem, this.ClearAllSiteMenuItem, this.ClearAutofillMenuItem, this.ClearBrowsingHistoryMenuItem, this.ClearCookiesMenuItem, this.ClearDiskCacheMenuItem, this.ClearDownloadHistoryMenuItem}); + this.clearBrowsingDataStripMenuItem.Name = "clearBrowsingDataStripMenuItem"; + this.clearBrowsingDataStripMenuItem.Size = new System.Drawing.Size(86, 38); + this.clearBrowsingDataStripMenuItem.Text = "Clear Browsing Data"; + // + // clientCertificateRequestedStripMenuItem + // + this.clientCertificateRequestedStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.CustomClientCertificateSelectionMenuItem, this.DeferredCustomCertificateDialogMenuItem}); + this.clientCertificateRequestedStripMenuItem.Name = "clientCertificateRequestedStripMenuItem"; + this.clientCertificateRequestedStripMenuItem.Size = new System.Drawing.Size(86, 38); + this.clientCertificateRequestedStripMenuItem.Text = "Client Certificate Request"; + // + // cookieManagementStripMenuItem + // + this.cookieManagementStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.GetCookiesMenuItem, this.AddOrUpdateCookieMenuItem, this.DeleteCookiesMenuItem, this.DeleteAllCookiesMenuItem}); + this.cookieManagementStripMenuItem.Name = "cookieManagementStripMenuItem"; + this.cookieManagementStripMenuItem.Size = new System.Drawing.Size(86, 38); + this.cookieManagementStripMenuItem.Text = "Cookie Management"; + // // toggleVisibilityMenuItem // this.toggleVisibilityMenuItem.Name = "toggleVisibilityMenuItem"; @@ -512,6 +563,34 @@ private void InitializeComponent() this.removeInitializeScriptMenuItem.Text = "Remove Initialize Script"; this.removeInitializeScriptMenuItem.Click += new System.EventHandler(this.removeInitializeScriptMenuItem_Click); // + // postMessageStringMenuItem + // + this.postMessageStringMenuItem.Name = "postMessageStringMenuItem"; + this.postMessageStringMenuItem.Size = new System.Drawing.Size(359, 44); + this.postMessageStringMenuItem.Text = "Post Message String"; + this.postMessageStringMenuItem.Click += new System.EventHandler(this.postMessageStringMenuItem_Click); + // + // postMessageJsonMenuItem + // + this.postMessageJsonMenuItem.Name = "postMessageJsonMenuItem"; + this.postMessageJsonMenuItem.Size = new System.Drawing.Size(359, 44); + this.postMessageJsonMenuItem.Text = "Post Message JSON"; + this.postMessageJsonMenuItem.Click += new System.EventHandler(this.postMessageJsonMenuItem_Click); + // + // postMessageStringIframeMenuItem + // + this.postMessageStringIframeMenuItem.Name = "postMessageStringIframeMenuItem"; + this.postMessageStringIframeMenuItem.Size = new System.Drawing.Size(359, 44); + this.postMessageStringIframeMenuItem.Text = "Post Message String Iframe"; + this.postMessageStringIframeMenuItem.Click += new System.EventHandler(this.postMessageStringIframeMenuItem_Click); + // + // postMessageJsonIframeMenuItem + // + this.postMessageJsonIframeMenuItem.Name = "postMessageJsonIframeMenuItem"; + this.postMessageJsonIframeMenuItem.Size = new System.Drawing.Size(359, 44); + this.postMessageJsonIframeMenuItem.Text = "Post Message JSON Iframe"; + this.postMessageJsonIframeMenuItem.Click += new System.EventHandler(this.postMessageJsonIframeMenuItem_Click); + // // transparentBackgroundColorMenuItem // this.transparentBackgroundColorMenuItem.Name = "transparentBackgroundColorMenuItem"; @@ -522,6 +601,10 @@ private void InitializeComponent() // scenarioToolStripMenuItem // this.scenarioToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripMenuItem[] { + this.AuthenticationMenuItem, + this.clearBrowsingDataStripMenuItem, + this.clientCertificateRequestedStripMenuItem, + this.cookieManagementStripMenuItem, this.addRemoteObjectMenuItem, this.domContentLoadedMenuItem, this.navigateWithWebResourceRequestMenuItem, @@ -628,6 +711,111 @@ private void InitializeComponent() this.aboutToolStripMenuItem.Size = new System.Drawing.Size(359, 44); this.aboutToolStripMenuItem.Text = "About"; this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); + // + // AuthenticationMenuItem + // + this.AuthenticationMenuItem.Name = "AuthenticationMenuItem"; + this.AuthenticationMenuItem.Size = new System.Drawing.Size(359, 44); + this.AuthenticationMenuItem.Text = "Authentication"; + this.AuthenticationMenuItem.Click += new System.EventHandler(this.AuthenticationMenuItem_Click); + // + // ClearAllDOMStorageMenuItem + // + this.ClearAllDOMStorageMenuItem.Name = "ClearAllDOMStorageMenuItem"; + this.ClearAllDOMStorageMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearAllDOMStorageMenuItem.Text = "All DOM Storage"; + this.ClearAllDOMStorageMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.AllDomStorage)); + // + // ClearAllProfileMenuItem + // + this.ClearAllProfileMenuItem.Name = "ClearAllProfileMenuItem"; + this.ClearAllProfileMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearAllProfileMenuItem.Text = "All Profile"; + this.ClearAllProfileMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.AllProfile)); + // + // ClearAllSiteMenuItem + // + this.ClearAllSiteMenuItem.Name = "ClearAllSiteMenuItem"; + this.ClearAllSiteMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearAllSiteMenuItem.Text = "All Site"; + this.ClearAllSiteMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.AllSite)); + // + // ClearAutofillMenuItem + // + this.ClearAutofillMenuItem.Name = "ClearAutofillMenuItem"; + this.ClearAutofillMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearAutofillMenuItem.Text = "Autofill"; + this.ClearAutofillMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, (CoreWebView2BrowsingDataKinds)(CoreWebView2BrowsingDataKinds.GeneralAutofill | CoreWebView2BrowsingDataKinds.PasswordAutosave))); + // + // ClearBrowsingHistoryMenuItem + // + this.ClearBrowsingHistoryMenuItem.Name = "ClearBrowsingHistoryMenuItem"; + this.ClearBrowsingHistoryMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearBrowsingHistoryMenuItem.Text = "Browsing History"; + this.ClearBrowsingHistoryMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.BrowsingHistory)); + // + // ClearCookiesMenuItem + // + this.ClearCookiesMenuItem.Name = "ClearCookiesMenuItem"; + this.ClearCookiesMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearCookiesMenuItem.Text = "Cookies"; + this.ClearCookiesMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.Cookies)); + // + // ClearDiskCacheMenuItem + // + this.ClearDiskCacheMenuItem.Name = "ClearDiskCacheMenuItem"; + this.ClearDiskCacheMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearDiskCacheMenuItem.Text = "Disk Cache"; + this.ClearDiskCacheMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.DiskCache)); + // + // ClearDownloadHistoryMenuItem + // + this.ClearDownloadHistoryMenuItem.Name = "ClearDownloadHistoryMenuItem"; + this.ClearDownloadHistoryMenuItem.Size = new System.Drawing.Size(359, 44); + this.ClearDownloadHistoryMenuItem.Text = "Download History"; + this.ClearDownloadHistoryMenuItem.Click += new System.EventHandler((sender, e) => ClearBrowsingData(sender, e, CoreWebView2BrowsingDataKinds.DownloadHistory)); + // + // CustomClientCertificateSelectionMenuItem + // + this.CustomClientCertificateSelectionMenuItem.Name = "CustomClientCertificateSelectionMenuItem"; + this.CustomClientCertificateSelectionMenuItem.Size = new System.Drawing.Size(359, 44); + this.CustomClientCertificateSelectionMenuItem.Text = "Custom Client Certificate Selection"; + this.CustomClientCertificateSelectionMenuItem.Click += new System.EventHandler(this.CustomClientCertificateSelectionMenuItem_Click); + // + // DeferredCustomCertificateDialogMenuItem + // + this.DeferredCustomCertificateDialogMenuItem.Name = "DeferredCustomCertificateDialogMenuItem"; + this.DeferredCustomCertificateDialogMenuItem.Size = new System.Drawing.Size(359, 44); + this.DeferredCustomCertificateDialogMenuItem.Text = "Deferred Custom Client Certificate Selection Dialog"; + this.DeferredCustomCertificateDialogMenuItem.Click += new System.EventHandler(this.DeferredCustomCertificateDialogMenuItem_Click); + // + // GetCookiesMenuItem + // + this.GetCookiesMenuItem.Name = "GetCookiesMenuItem"; + this.GetCookiesMenuItem.Size = new System.Drawing.Size(359, 44); + this.GetCookiesMenuItem.Text = "Get Cookies"; + this.GetCookiesMenuItem.Click += new System.EventHandler((sender, e) => GetCookiesMenuItem_Click(sender, e, txtUrl.Text)); + // + // AddOrUpdateCookieMenuItem + // + this.AddOrUpdateCookieMenuItem.Name = "AddOrUpdateCookieMenuItem"; + this.AddOrUpdateCookieMenuItem.Size = new System.Drawing.Size(359, 44); + this.AddOrUpdateCookieMenuItem.Text = "Add Or Update Cookie"; + this.AddOrUpdateCookieMenuItem.Click += new System.EventHandler((sender, e) => AddOrUpdateCookieMenuItem_Click(sender, e, new Uri(txtUrl.Text).Host)); + // + // DeleteCookiesMenuItem + // + this.DeleteCookiesMenuItem.Name = "DeleteCookiesMenuItem"; + this.DeleteCookiesMenuItem.Size = new System.Drawing.Size(359, 44); + this.DeleteCookiesMenuItem.Text = "Delete Cookie"; + this.DeleteCookiesMenuItem.Click += new System.EventHandler((sender, e) => DeleteCookiesMenuItem_Click(sender, e, new Uri(txtUrl.Text).Host)); + // + // DeleteAllCookiesMenuItem + // + this.DeleteAllCookiesMenuItem.Name = "DeleteAllCookiesMenuItem"; + this.DeleteAllCookiesMenuItem.Size = new System.Drawing.Size(359, 44); + this.DeleteAllCookiesMenuItem.Text = "Delete All Cookies"; + this.DeleteAllCookiesMenuItem.Click += new System.EventHandler(this.DeleteAllCookiesMenuItem_Click); // // webView2Control @@ -691,6 +879,9 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem acceleratorKeysEnabledToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem zoomToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem clearBrowsingDataStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem clientCertificateRequestedStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem cookieManagementStripMenuItem; private System.Windows.Forms.ToolStripMenuItem scriptToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem xToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem xToolStripMenuItem1; @@ -736,6 +927,25 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem injectScriptIntoFrameMenuItem; private System.Windows.Forms.ToolStripMenuItem addInitializeScriptMenuItem; private System.Windows.Forms.ToolStripMenuItem removeInitializeScriptMenuItem; + private System.Windows.Forms.ToolStripMenuItem AuthenticationMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearAllDOMStorageMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearAllProfileMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearAllSiteMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearAutofillMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearBrowsingHistoryMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearCookiesMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearDiskCacheMenuItem; + private System.Windows.Forms.ToolStripMenuItem ClearDownloadHistoryMenuItem; + private System.Windows.Forms.ToolStripMenuItem CustomClientCertificateSelectionMenuItem; + private System.Windows.Forms.ToolStripMenuItem DeferredCustomCertificateDialogMenuItem; + private System.Windows.Forms.ToolStripMenuItem GetCookiesMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddOrUpdateCookieMenuItem; + private System.Windows.Forms.ToolStripMenuItem DeleteCookiesMenuItem; + private System.Windows.Forms.ToolStripMenuItem DeleteAllCookiesMenuItem; + private System.Windows.Forms.ToolStripMenuItem postMessageStringMenuItem; + private System.Windows.Forms.ToolStripMenuItem postMessageJsonMenuItem; + private System.Windows.Forms.ToolStripMenuItem postMessageStringIframeMenuItem; + private System.Windows.Forms.ToolStripMenuItem postMessageJsonIframeMenuItem; } } diff --git a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs index 7a6021c3..7923214b 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs +++ b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs @@ -12,6 +12,7 @@ using System.Windows.Forms; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; +using System.Linq; namespace WebView2WindowsFormsBrowser { @@ -84,6 +85,26 @@ CoreWebView2Settings WebViewSettings string _lastInitializeScriptId; List _webViewFrames = new List(); + void WebView_HandleIFrames(object sender, CoreWebView2FrameCreatedEventArgs args) + { + _webViewFrames.Add(args.Frame); + args.Frame.Destroyed += WebViewFrames_DestoryedNestedIFrames; + } + + void WebViewFrames_DestoryedNestedIFrames(object sender, object args) + { + try + { + var frameToRemove = _webViewFrames.SingleOrDefault(r => r.IsDestroyed() == 1); + if (frameToRemove != null) + _webViewFrames.Remove(frameToRemove); + } + catch (InvalidOperationException ex) + { + MessageBox.Show(ex.Message); + } + } + string WebViewFrames_ToString() { string result = ""; @@ -160,6 +181,8 @@ private void WebView2Control_CoreWebView2InitializationCompleted(object sender, this.webView2Control.CoreWebView2.DocumentTitleChanged += CoreWebView2_DocumentTitleChanged; this.webView2Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Image); this.webView2Control.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed; + this.webView2Control.CoreWebView2.FrameCreated += WebView_HandleIFrames; + UpdateTitleWithEvent("CoreWebView2InitializationCompleted succeeded"); EnableButtons(); } @@ -745,6 +768,154 @@ private void aboutToolStripMenuItem_Click(object sender, EventArgs e) MessageBox.Show(this, "WebView2WindowsFormsBrowser, Version 1.0\nCopyright(C) 2023", "About WebView2WindowsFormsBrowser"); } + void AuthenticationMenuItem_Click(object sender, EventArgs e) + { + // + this.webView2Control.CoreWebView2.BasicAuthenticationRequested += delegate (object requestSender, CoreWebView2BasicAuthenticationRequestedEventArgs args) + { + // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo credentials in https://authenticationtest.com")] + args.Response.UserName = "user"; + // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo credentials in https://authenticationtest.com")] + args.Response.Password = "pass"; + }; + this.webView2Control.CoreWebView2.Navigate("https://authenticationtest.com/HTTPAuth"); + // + } + + async void ClearBrowsingData(object target, EventArgs e, CoreWebView2BrowsingDataKinds dataKinds) + { + // Clear the browsing data from the last hour. + await this.webView2Control.CoreWebView2.Profile.ClearBrowsingDataAsync(dataKinds); + MessageBox.Show(this, + "Completed", + "Clear Browsing Data"); + // + } + + void WebView_ClientCertificateRequested(object sender, CoreWebView2ClientCertificateRequestedEventArgs e) + { + IReadOnlyList certificateList = e.MutuallyTrustedCertificates; + if (certificateList.Count() > 0) + { + // There is no significance to the order, picking a certificate arbitrarily. + e.SelectedCertificate = certificateList.LastOrDefault(); + } + e.Handled = true; + } + + private bool _isCustomClientCertificateSelection = false; + void CustomClientCertificateSelectionMenuItem_Click(object sender, EventArgs e) + { + // Safeguarding the handler when unsupported runtime is used. + try + { + if (!_isCustomClientCertificateSelection) + { + this.webView2Control.CoreWebView2.ClientCertificateRequested += WebView_ClientCertificateRequested; + } + else + { + this.webView2Control.CoreWebView2.ClientCertificateRequested -= WebView_ClientCertificateRequested; + } + _isCustomClientCertificateSelection = !_isCustomClientCertificateSelection; + + MessageBox.Show(this, + _isCustomClientCertificateSelection ? "Custom client certificate selection has been enabled" : "Custom client certificate selection has been disabled", + "Custom client certificate selection"); + } + catch (NotImplementedException exception) + { + MessageBox.Show(this, "Custom client certificate selection Failed: " + exception.Message, "Custom client certificate selection"); + } + } + // + // This example hides the default client certificate dialog and shows a custom dialog instead. + // The dialog box displays mutually trusted certificates list and allows the user to select a certificate. + // Selecting `OK` will continue the request with a certificate. + // Selecting `CANCEL` will continue the request without a certificate + private bool _isCustomClientCertificateSelectionDialog = false; + void DeferredCustomCertificateDialogMenuItem_Click(object sender, EventArgs e) + { + // Safeguarding the handler when unsupported runtime is used. + try + { + if (!_isCustomClientCertificateSelectionDialog) + { + this.webView2Control.CoreWebView2.ClientCertificateRequested += delegate ( + object requestSender, 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 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() == DialogResult.OK) + { + // Continue with the selected certificate to respond to the server if `OK` is selected. + args.SelectedCertificate = (CoreWebView2ClientCertificate)dialog.CertificateDataBinding.SelectedItems[0].Tag; + } + } + 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"); + } + } + + async void GetCookiesMenuItem_Click(object sender, EventArgs e, string address) + { + // + List cookieList = await this.webView2Control.CoreWebView2.CookieManager.GetCookiesAsync(address); + StringBuilder cookieResult = new StringBuilder(cookieList.Count + " cookie(s) received from " + address); + for (int i = 0; i < cookieList.Count; ++i) + { + CoreWebView2Cookie cookie = this.webView2Control.CoreWebView2.CookieManager.CreateCookieWithSystemNetCookie(cookieList[i].ToSystemNetCookie()); + cookieResult.Append($"\n{cookie.Name} {cookie.Value} {(cookie.IsSession ? "[session cookie]" : cookie.Expires.ToString("G"))}"); + } + MessageBox.Show(this, cookieResult.ToString(), "GetCookiesAsync"); + // + } + + void AddOrUpdateCookieMenuItem_Click(object sender, EventArgs e, string domain) + { + // + CoreWebView2Cookie cookie = this.webView2Control.CoreWebView2.CookieManager.CreateCookie("CookieName", "CookieValue", domain, "/"); + this.webView2Control.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie); + // + } + + void DeleteAllCookiesMenuItem_Click(object sender, EventArgs e) + { + this.webView2Control.CoreWebView2.CookieManager.DeleteAllCookies(); + } + + void DeleteCookiesMenuItem_Click(object sender, EventArgs e, string domain) + { + this.webView2Control.CoreWebView2.CookieManager.DeleteCookiesWithDomainAndPath("CookieName", domain, "/"); + } + private void showBrowserProcessInfoMenuItem_Click(object sender, EventArgs e) { var browserInfo = this.webView2Control.CoreWebView2.BrowserProcessId; @@ -1133,6 +1304,107 @@ private void removeInitializeScriptMenuItem_Click(object sender, EventArgs e) } } } + + // Prompt the user for a string and then post it as a web message. + private void postMessageStringMenuItem_Click(object sender, EventArgs e) + { + var dialog = new TextInputDialog( + title: "Post Web Message String", + description: "Web message string:\r\nEnter the web message as a string."); + + try + { + if (dialog.ShowDialog() == DialogResult.OK) + { + this.webView2Control.CoreWebView2.PostWebMessageAsString(dialog.inputBox()); + } + } + catch (Exception exception) + { + MessageBox.Show(this, "PostMessageAsString Failed: " + exception.Message, + "Post Message As String"); + } + } + + // Prompt the user for some JSON and then post it as a web message. + private void postMessageJsonMenuItem_Click(object sender, EventArgs e) + { + var dialog = new TextInputDialog( + title: "Post Web Message JSON", + description: "Web message JSON:\r\nEnter the web message as JSON.", + defaultInput: "{\"SetColor\":\"blue\"}"); + + try + { + if (dialog.ShowDialog() == DialogResult.OK) + { + this.webView2Control.CoreWebView2.PostWebMessageAsJson(dialog.inputBox()); + } + } + catch (Exception exception) + { + MessageBox.Show(this, "PostMessageAsJSON Failed: " + exception.Message, + "Post Message As JSON"); + } + } + + // Prompt the user for a string and then post it as a web message to the first iframe. + private void postMessageStringIframeMenuItem_Click(object sender, EventArgs e) + { + var dialog = new TextInputDialog( + title: "Post Web Message String Iframe", + description: "Web message string:\r\nEnter the web message as a string."); + + try + { + if (dialog.ShowDialog() == DialogResult.OK) + { + if (_webViewFrames.Count != 0) + { + _webViewFrames[0].PostWebMessageAsString(dialog.inputBox()); + } + else + { + MessageBox.Show("No iframes found"); + } + } + + } + catch (Exception exception) + { + MessageBox.Show(this, "PostMessageAsStringIframe Failed: " + exception.Message, + "Post Message As String"); + } + } + + // Prompt the user for some JSON and then post it as a web message to the first iframe. + private void postMessageJsonIframeMenuItem_Click(object sender, EventArgs e) + { + var dialog = new TextInputDialog( + title: "Post Web Message JSON Iframe", + description: "Web message JSON:\r\nEnter the web message as JSON.", + defaultInput: "{\"SetColor\":\"blue\"}"); + + try + { + if (dialog.ShowDialog() == DialogResult.OK) + { + if (_webViewFrames.Count != 0) + { + _webViewFrames[0].PostWebMessageAsJson(dialog.inputBox()); + } + else + { + MessageBox.Show("No iframes found"); + } + } + } + catch (Exception exception) + { + MessageBox.Show(this, "PostMessageAsJSONIframe Failed: " + exception.Message, + "Post Message As JSON"); + } + } #endregion private void HandleResize() diff --git a/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.Designer.cs b/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.Designer.cs new file mode 100644 index 00000000..eab8cf06 --- /dev/null +++ b/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.Designer.cs @@ -0,0 +1,101 @@ + +namespace WebView2WindowsFormsBrowser +{ + partial class ClientCertificateSelectionDialog + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.btnOk = new System.Windows.Forms.Button(); + this.btnCancel = new System.Windows.Forms.Button(); + this.txtDescription = new System.Windows.Forms.TextBox(); + this.CertificateDataBinding = new System.Windows.Forms.ListView(); + this.SuspendLayout(); + // + // btnOk + // + this.btnOk.Location = new System.Drawing.Point(464, 295); + this.btnOk.Name = "btnOk"; + this.btnOk.Size = new System.Drawing.Size(75, 23); + this.btnOk.TabIndex = 0; + this.btnOk.Text = "OK"; + this.btnOk.UseVisualStyleBackColor = true; + this.btnOk.Click += new System.EventHandler(this.btnOk_Click); + // + // btnCancel + // + this.btnCancel.Location = new System.Drawing.Point(545, 295); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.Size = new System.Drawing.Size(75, 23); + this.btnCancel.TabIndex = 1; + this.btnCancel.Text = "Cancel"; + this.btnCancel.UseVisualStyleBackColor = true; + this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); + // + // txtDescription + // + this.txtDescription.Location = new System.Drawing.Point(22, 12); + this.txtDescription.Multiline = true; + this.txtDescription.Name = "txtDescription"; + this.txtDescription.ReadOnly = true; + this.txtDescription.Size = new System.Drawing.Size(598, 20); + this.txtDescription.TabIndex = 3; + // + // CertificateDataBinding + // + this.CertificateDataBinding.HideSelection = false; + this.CertificateDataBinding.Location = new System.Drawing.Point(22, 51); + this.CertificateDataBinding.Name = "CertificateDataBinding"; + this.CertificateDataBinding.Size = new System.Drawing.Size(598, 226); + this.CertificateDataBinding.TabIndex = 4; + this.CertificateDataBinding.UseCompatibleStateImageBehavior = false; + // + // ClientCertificateSelectionDialog + // + this.AcceptButton = this.btnOk; + this.CancelButton = this.btnCancel; + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(642, 347); + this.Controls.Add(this.CertificateDataBinding); + this.Controls.Add(this.txtDescription); + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.btnOk); + this.Name = "ClientCertificateSelectionDialog"; + this.Text = "ClientCertificateSelectionDialog"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button btnOk; + private System.Windows.Forms.Button btnCancel; + private System.Windows.Forms.TextBox txtDescription; + public System.Windows.Forms.ListView CertificateDataBinding; + } +} \ No newline at end of file diff --git a/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.cs b/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.cs new file mode 100644 index 00000000..1cc2a670 --- /dev/null +++ b/SampleApps/WebView2WindowsFormsBrowser/ClientCertificateSelectionDialog.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Microsoft.Web.WebView2.Core; + +namespace WebView2WindowsFormsBrowser +{ + public partial class ClientCertificateSelectionDialog : Form + { + public ClientCertificateSelectionDialog( + string title = null, + string host = null, + int port = 0, + IReadOnlyList client_cert_list = null) + { + InitializeComponent(); + if (title != null) + { + Text = title; + } + if (host != null && port > 0) + { + txtDescription.Text = String.Format("Site {0}:{1} needs your credentials:", host, port); + } + if (client_cert_list != null) + { + CertificateDataBinding.View = View.Details; + CertificateDataBinding.FullRowSelect = true; + CertificateDataBinding.Tag = client_cert_list; + for(int i = 0; i < client_cert_list.Count; i++) + { + ListViewItem item = new ListViewItem(client_cert_list[i].Subject); + item.Tag = client_cert_list[i]; + item.SubItems.Add(client_cert_list[i].Issuer); + item.SubItems.Add(client_cert_list[i].ValidFrom.ToString()); + item.SubItems.Add(client_cert_list[i].ValidTo.ToString()); + item.SubItems.Add(client_cert_list[i].Kind.ToString()); + CertificateDataBinding.Items.Add(item); + } + + CertificateDataBinding.Columns.Add("Subject", -2, HorizontalAlignment.Left); + CertificateDataBinding.Columns.Add("Issuer", -2, HorizontalAlignment.Left); + CertificateDataBinding.Columns.Add("Valid From", -2, HorizontalAlignment.Left); + CertificateDataBinding.Columns.Add("Valid To", -2, HorizontalAlignment.Left); + CertificateDataBinding.Columns.Add("Kind", -2, HorizontalAlignment.Left); + } + } + + private void btnOk_Click(object sender, EventArgs e) + { + this.DialogResult = DialogResult.OK; + } + private void btnCancel_Click(object sender, EventArgs e) + { + this.DialogResult = DialogResult.Cancel; + } + } +} diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index 7529d5a1..057cfe6d 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -19,7 +19,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index b6cc0064..984a48f1 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -2365,7 +2365,7 @@ void NewWindowWithOptionsCmdExecuted(object target, ExecutedRoutedEventArgs e) } } - private void ThreadProc(string browserExecutableFolder, string userDataFolder, string language, string additionalBrowserArguments, string profileName, bool? isInPrivateModeEnabled) + private void ThreadProc(string browserExecutableFolder, string userDataFolder, string language, string additionalBrowserArguments, string profileName, bool? isInPrivateModeEnabled, string scriptLocale) { try { @@ -2378,6 +2378,7 @@ private void ThreadProc(string browserExecutableFolder, string userDataFolder, s creationProperties.AdditionalBrowserArguments = additionalBrowserArguments; creationProperties.ProfileName = profileName; creationProperties.IsInPrivateModeEnabled = isInPrivateModeEnabled; + creationProperties.ScriptLocale = scriptLocale; var tempWindow = new MainWindow(creationProperties); tempWindow.Show(); // Causes dispatcher to shutdown when window is closed. @@ -2402,9 +2403,10 @@ void CreateNewThreadCmdExecuted(object target, ExecutedRoutedEventArgs e) string additionalBrowserArguments = webView.CreationProperties.AdditionalBrowserArguments; string profileName = webView.CreationProperties.ProfileName; bool? isInPrivateModeEnabled = webView.CreationProperties.IsInPrivateModeEnabled; + string scriptLocale = webView.CreationProperties.ScriptLocale; Thread newWindowThread = new Thread(() => { - ThreadProc(browserExecutableFolder, userDataFolder, language, additionalBrowserArguments, profileName, isInPrivateModeEnabled); + ThreadProc(browserExecutableFolder, userDataFolder, language, additionalBrowserArguments, profileName, isInPrivateModeEnabled, scriptLocale); }); newWindowThread.SetApartmentState(ApartmentState.STA); newWindowThread.IsBackground = false; diff --git a/SampleApps/WebView2WpfBrowser/NewWindowOptionsDialog.xaml b/SampleApps/WebView2WpfBrowser/NewWindowOptionsDialog.xaml index 3d2da3f2..ed7d7e68 100644 --- a/SampleApps/WebView2WpfBrowser/NewWindowOptionsDialog.xaml +++ b/SampleApps/WebView2WpfBrowser/NewWindowOptionsDialog.xaml @@ -15,7 +15,7 @@ found in the LICENSE file. Height="260" Width="500"> - + + + - + \ No newline at end of file