Skip to content

Commit ef40046

Browse files
authored
Create WebView2 abstraction for sharing code between ASP.NET Core and .NET MAUI (#33125)
Removed WebView2-specific code from WebView2 package and replaced with abstractions. The WebView2 references moved to the WPF and WinForms projects. This enables the .NET MAUI project to use the WinUI version of WebView2.
1 parent d96c3bf commit ef40046

27 files changed

+1029
-66
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
5+
{
6+
/// <summary>
7+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
8+
/// of the Blazor framework. These types will change in a future release.
9+
///
10+
/// Specifies the web resource request contexts.
11+
/// </summary>
12+
public enum CoreWebView2WebResourceContextWrapper
13+
{
14+
/// <summary>
15+
/// Specifies all resources.
16+
/// </summary>
17+
All,
18+
/// <summary>
19+
/// Specifies a document resources.
20+
/// </summary>
21+
Document,
22+
/// <summary>
23+
/// Specifies a CSS resources.
24+
/// </summary>
25+
Stylesheet,
26+
/// <summary>
27+
/// Specifies an image resources.
28+
/// </summary>
29+
Image,
30+
/// <summary>
31+
/// Specifies another media resource such as a video.
32+
/// </summary>
33+
Media,
34+
/// <summary>
35+
/// Specifies a font resource.
36+
/// </summary>
37+
Font,
38+
/// <summary>
39+
/// Specifies a script resource.
40+
/// </summary>
41+
Script,
42+
/// <summary>
43+
/// Specifies an XML HTTP request.
44+
/// </summary>
45+
XmlHttpRequest,
46+
/// <summary>
47+
/// Specifies a Fetch API communication.
48+
/// </summary>
49+
Fetch,
50+
/// <summary>
51+
/// Specifies a TextTrack resource.
52+
/// </summary>
53+
TextTrack,
54+
/// <summary>
55+
/// Specifies an EventSource API communication.
56+
/// </summary>
57+
EventSource,
58+
/// <summary>
59+
/// Specifies a WebSocket API communication.
60+
/// </summary>
61+
Websocket,
62+
/// <summary>
63+
/// Specifies a Web App Manifest.
64+
/// </summary>
65+
Manifest,
66+
/// <summary>
67+
/// Specifies a Signed HTTP Exchange.
68+
/// </summary>
69+
SignedExchange,
70+
/// <summary>
71+
/// Specifies a Ping request.
72+
/// </summary>
73+
Ping,
74+
/// <summary>
75+
/// Specifies a CSP Violation Report.
76+
/// </summary>
77+
CspViolationReport,
78+
/// <summary>
79+
/// Specifies an other resource.
80+
/// </summary>
81+
Other
82+
}
83+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
5+
{
6+
/// <summary>
7+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
8+
/// of the Blazor framework. These types will change in a future release.
9+
///
10+
/// Event args for the AcceleratorKeyPressed event.
11+
/// </summary>
12+
public interface ICoreWebView2AcceleratorKeyPressedEventArgsWrapper
13+
{
14+
/// <summary>
15+
/// Gets the Win32 virtual key code of the key that was pressed or released.
16+
/// </summary>
17+
uint VirtualKey { get; }
18+
19+
/// <summary>
20+
/// Gets the LPARAM value that accompanied the window message.
21+
/// </summary>
22+
int KeyEventLParam { get; }
23+
24+
/// <summary>
25+
/// Indicates whether the AcceleratorKeyPressed event is handled by host.
26+
/// </summary>
27+
/// <remarks>
28+
/// If set to <c>true</c> then this prevents the WebView from performing the default action for this accelerator key. Otherwise the WebView will perform the default action for the accelerator key.
29+
/// </remarks>
30+
bool Handled { get; set; }
31+
}
32+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
5+
{
6+
/// <summary>
7+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
8+
/// of the Blazor framework. These types will change in a future release.
9+
///
10+
/// Defines properties that enable, disable, or modify WebView features.
11+
/// </summary>
12+
/// <remarks>
13+
/// Setting changes made after <see cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.NavigationStarting" /> event do not apply until the next top-level navigation.
14+
/// </remarks>
15+
public interface ICoreWebView2SettingsWrapper
16+
{
17+
/// <summary>
18+
/// Determines whether running JavaScript is enabled in all future navigations in the WebView.
19+
/// </summary>
20+
/// <remarks>
21+
/// This only affects scripts in the document. Scripts injected with <see cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.ExecuteScriptAsync(System.String)" /> runs even if script is disabled. The default value is <c>true</c>.
22+
/// </remarks>
23+
public bool IsScriptEnabled { get; set; }
24+
25+
/// <summary>
26+
/// Determines whether communication from the host to the top-level HTML document of the WebView is allowed.
27+
/// </summary>
28+
/// <remarks>
29+
/// This is used when loading a new HTML document. If set to <c>true</c>, communication from the host to the top-level HTML document of the WebView is allowed using <see cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsJson(System.String)" />, <see cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsString(System.String)" />, and message event of <c>window.chrome.webview</c>. Communication from the top-level HTML document of the WebView to the host is allowed using <c>window.chrome.webview.postMessage</c> function and the <see cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.WebMessageReceived" /> event. If set to <c>false</c>, then communication is disallowed. <see cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsJson(System.String)" /> and <see cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsString(System.String)" /> fail and <c>window.chrome.webview.postMessage</c> fails by throwing an instance of an Error object. The default value is <c>true</c>.
30+
/// </remarks>
31+
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsJson(System.String)" />
32+
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.PostWebMessageAsString(System.String)" />
33+
/// <seealso cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.WebMessageReceived" />
34+
public bool IsWebMessageEnabled { get; set; }
35+
36+
/// <summary>
37+
/// Determines whether WebView renders the default Javascript dialog box.
38+
/// </summary>
39+
/// <remarks>
40+
/// This is used when loading a new HTML document. If set to <c>false</c>, WebView does not render the default JavaScript dialog box (specifically those displayed by the JavaScript alert, confirm, prompt functions and <c>beforeunload</c> event). Instead, WebView raises <see cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.ScriptDialogOpening" /> event that contains all of the information for the dialog and allow the host app to show a custom UI. The default value is <c>true</c>.
41+
/// </remarks>
42+
/// <seealso cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.ScriptDialogOpening" />
43+
public bool AreDefaultScriptDialogsEnabled { get; set; }
44+
45+
/// <summary>
46+
/// Determines whether the status bar is displayed.
47+
/// </summary>
48+
/// <remarks>
49+
/// The status bar is usually displayed in the lower left of the WebView and shows things such as the URI of a link when the user hovers over it and other information. The default value is <c>true</c>.
50+
/// </remarks>
51+
public bool IsStatusBarEnabled { get; set; }
52+
53+
/// <summary>
54+
/// Determines whether the user is able to use the context menu or keyboard shortcuts to open the DevTools window.
55+
/// </summary>
56+
/// <remarks>
57+
/// The default value is <c>true</c>.
58+
/// </remarks>
59+
public bool AreDevToolsEnabled { get; set; }
60+
61+
/// <summary>
62+
/// Determines whether the default context menus are shown to the user in WebView.
63+
/// </summary>
64+
/// <remarks>
65+
/// The default value is <c>true</c>.
66+
/// </remarks>
67+
public bool AreDefaultContextMenusEnabled { get; set; }
68+
69+
/// <summary>
70+
/// Determines whether host objects are accessible from the page in WebView.
71+
/// </summary>
72+
/// <remarks>
73+
/// The default value is <c>true</c>.
74+
/// </remarks>
75+
public bool AreHostObjectsAllowed { get; set; }
76+
77+
/// <summary>
78+
/// Determines whether the user is able to impact the zoom of the WebView.
79+
/// </summary>
80+
/// <remarks>
81+
/// When disabled, the user is not able to zoom using Ctrl++, Ctr+-, or Ctrl+mouse wheel, but the zoom is set using <see cref="P:Microsoft.Web.WebView2.Core.CoreWebView2Controller.ZoomFactor" /> property. The default value is <c>true</c>.
82+
/// </remarks>
83+
public bool IsZoomControlEnabled { get; set; }
84+
85+
/// <summary>
86+
/// Determines whether to disable built in error page for navigation failure and render process failure.
87+
/// </summary>
88+
/// <remarks>
89+
/// When disabled, blank page is displayed when related error happens. The default value is <c>true</c>.
90+
/// </remarks>
91+
public bool IsBuiltInErrorPageEnabled { get; set; }
92+
}
93+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
5+
{
6+
/// <summary>
7+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
8+
/// of the Blazor framework. These types will change in a future release.
9+
///
10+
/// An HTTP request used with the <see cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.WebResourceRequested" /> event.
11+
/// </summary>
12+
public interface ICoreWebView2WebResourceRequestWrapper
13+
{
14+
/// <summary>
15+
/// Gets or sets the request URI.
16+
/// </summary>
17+
string Uri { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the HTTP request method.
21+
/// </summary>
22+
string Method { get; set; }
23+
24+
///// <summary>
25+
///// Gets or sets the HTTP request message body as stream.
26+
///// </summary>
27+
///// <remarks>
28+
///// POST data should be here. If a stream is set, which overrides the message body, the stream must have all the content data available by the time the <see cref="E:Microsoft.Web.WebView2.Core.CoreWebView2.WebResourceRequested" /> event deferral of this request is completed. Stream should be agile or be created from a background STA to prevent performance impact to the UI thread. <c>null</c> means no content data.
29+
///// </remarks>
30+
///// <seealso cref="T:System.IO.Stream" />
31+
//Stream Content { get; set; }
32+
33+
///// <summary>
34+
///// Gets the mutable HTTP request headers.
35+
///// </summary>
36+
//ICoreWebView2HttpRequestHeadersWrapper Headers { get; }
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
6+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
7+
{
8+
/// <summary>
9+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
10+
/// of the Blazor framework. These types will change in a future release.
11+
///
12+
/// Event args for the WebResourceRequested event.
13+
/// </summary>
14+
public interface ICoreWebView2WebResourceRequestedEventArgsWrapper
15+
{
16+
/// <summary>
17+
/// Gets the web resource request.
18+
/// </summary>
19+
/// <remarks>
20+
/// The request object may be missing some headers that are added by network stack at a later time.
21+
/// </remarks>
22+
ICoreWebView2WebResourceRequestWrapper Request { get; }
23+
24+
/// <summary>
25+
/// Gets the web resource request context.
26+
/// </summary>
27+
CoreWebView2WebResourceContextWrapper ResourceContext { get; }
28+
29+
/// <summary>
30+
/// Set the response content for this web resource.
31+
/// </summary>
32+
/// <param name="content"></param>
33+
/// <param name="statusCode"></param>
34+
/// <param name="statusMessage"></param>
35+
/// <param name="headerString"></param>
36+
void SetResponse(Stream content, int statusCode, string statusMessage, string headerString);
37+
}
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.AspNetCore.Components.WebView.WebView2
8+
{
9+
/// <summary>
10+
/// Types in the Microsoft.AspNetCore.Components.WebView.WebView2 are not recommended for use outside
11+
/// of the Blazor framework. These types will change in a future release.
12+
///
13+
/// Defines an abstraction for different WebView2 implementations on different platforms.
14+
/// </summary>
15+
public interface ICoreWebView2Wrapper
16+
{
17+
/// <summary>
18+
/// Gets the <see cref="ICoreWebView2SettingsWrapper" /> object contains various modifiable settings for the running WebView.
19+
/// </summary>
20+
public ICoreWebView2SettingsWrapper Settings { get; }
21+
22+
/// <summary>
23+
/// Adds the provided JavaScript to a list of scripts that should be run after the global object has been created, but before the HTML document has been parsed and before any other script included by the HTML document is run.
24+
/// </summary>
25+
/// <param name="javaScript">The JavaScript code to be run.</param>
26+
Task AddScriptToExecuteOnDocumentCreatedAsync(string javaScript);
27+
28+
29+
/// <summary>
30+
/// WebMessageReceived is raised when the IsWebMessageEnabled setting is set and the top-level document of the WebView runs <c>window.chrome.webview.postMessage</c>.
31+
/// </summary>
32+
Action AddWebMessageReceivedHandler(Action<WebMessageReceivedEventArgs> messageReceivedHandler);
33+
34+
/// <summary>
35+
/// Adds a URI and resource context filter to the WebResourceRequested event.
36+
/// </summary>
37+
/// <param name="uri">An URI to be added to the WebResourceRequested event.</param>
38+
/// <param name="resourceContext">A resource context filter to be added to the WebResourceRequested event.</param>
39+
void AddWebResourceRequestedFilter(string uri, CoreWebView2WebResourceContextWrapper resourceContext);
40+
41+
/// <summary>
42+
/// WebResourceRequested is raised when the WebView is performing a URL request to a matching URL and resource context filter that was added with AddWebResourceRequestedFilter />.
43+
/// </summary>
44+
Action AddWebResourceRequestedHandler(EventHandler<ICoreWebView2WebResourceRequestedEventArgsWrapper> webResourceRequestedHandler);
45+
46+
/// <summary>
47+
/// Posts a message that is a simple <see cref="T:System.String" /> rather than a JSON string representation of a JavaScript object.
48+
/// </summary>
49+
/// <param name="webMessageAsString">The web message to be posted to the top level document in this WebView.</param>
50+
void PostWebMessageAsString(string webMessageAsString);
51+
}
52+
}

0 commit comments

Comments
 (0)