Skip to content

Commit 02f3dbe

Browse files
Refactor WebViewManager API to have JS component config passed in (#35261)
1 parent 3f62031 commit 02f3dbe

File tree

6 files changed

+17
-20
lines changed

6 files changed

+17
-20
lines changed

src/Components/WebView/Samples/PhotinoPlatform/src/BlazorWindow.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using Microsoft.AspNetCore.Components.Web;
78
using Microsoft.Extensions.FileProviders;
89
using PhotinoNET;
910

@@ -43,8 +44,9 @@ public BlazorWindow(
4344
var fileProvider = new PhysicalFileProvider(contentRootDir);
4445

4546
var dispatcher = new PhotinoDispatcher(_window);
46-
_manager = new PhotinoWebViewManager(_window, services, dispatcher, new Uri(PhotinoWebViewManager.AppBaseUri), fileProvider, hostPageRelativePath);
47-
RootComponents = new BlazorWindowRootComponents(_manager);
47+
var jsComponents = new JSComponentConfigurationStore();
48+
_manager = new PhotinoWebViewManager(_window, services, dispatcher, new Uri(PhotinoWebViewManager.AppBaseUri), fileProvider, jsComponents, hostPageRelativePath);
49+
RootComponents = new BlazorWindowRootComponents(_manager, jsComponents);
4850
}
4951

5052
/// <summary>

src/Components/WebView/Samples/PhotinoPlatform/src/BlazorWindowRootComponents.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ public sealed class BlazorWindowRootComponents : IJSComponentConfiguration
1212
{
1313
private readonly PhotinoWebViewManager _manager;
1414

15-
internal BlazorWindowRootComponents(PhotinoWebViewManager manager)
15+
internal BlazorWindowRootComponents(PhotinoWebViewManager manager, JSComponentConfigurationStore jsComponents)
1616
{
1717
_manager = manager;
18-
JSComponents = manager.JSComponentConfiguration;
18+
JSComponents = jsComponents;
1919
}
2020

21-
/// <inheritdoc />
2221
public JSComponentConfigurationStore JSComponents { get; }
2322

2423
/// <summary>

src/Components/WebView/Samples/PhotinoPlatform/src/PhotinoWebViewManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.InteropServices;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Components.Web;
910
using Microsoft.Extensions.FileProviders;
1011
using PhotinoNET;
1112

@@ -26,8 +27,8 @@ internal class PhotinoWebViewManager : WebViewManager
2627
internal static readonly string AppBaseUri
2728
= $"{BlazorAppScheme}://0.0.0.0/";
2829

29-
public PhotinoWebViewManager(PhotinoWindow window, IServiceProvider provider, Dispatcher dispatcher, Uri appBaseUri, IFileProvider fileProvider, string hostPageRelativePath)
30-
: base(provider, dispatcher, appBaseUri, fileProvider, hostPageRelativePath)
30+
public PhotinoWebViewManager(PhotinoWindow window, IServiceProvider provider, Dispatcher dispatcher, Uri appBaseUri, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath)
31+
: base(provider, dispatcher, appBaseUri, fileProvider, jsComponents, hostPageRelativePath)
3132
{
3233
_window = window ?? throw new ArgumentNullException(nameof(window));
3334
_window.WebMessageReceived += (sender, message) =>

src/Components/WebView/WebView/src/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
#nullable enable
1+
#nullable enable
22
Microsoft.AspNetCore.Components.WebView.WebViewManager
33
Microsoft.AspNetCore.Components.WebView.WebViewManager.AddRootComponentAsync(System.Type! componentType, string! selector, Microsoft.AspNetCore.Components.ParameterView parameters) -> System.Threading.Tasks.Task!
44
Microsoft.AspNetCore.Components.WebView.WebViewManager.Dispatcher.get -> Microsoft.AspNetCore.Components.Dispatcher!
55
Microsoft.AspNetCore.Components.WebView.WebViewManager.DisposeAsync() -> System.Threading.Tasks.ValueTask
6-
Microsoft.AspNetCore.Components.WebView.WebViewManager.JSComponentConfiguration.get -> Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore!
76
Microsoft.AspNetCore.Components.WebView.WebViewManager.MessageReceived(System.Uri! sourceUri, string! message) -> void
87
Microsoft.AspNetCore.Components.WebView.WebViewManager.Navigate(string! url) -> void
98
Microsoft.AspNetCore.Components.WebView.WebViewManager.RemoveRootComponentAsync(string! selector) -> System.Threading.Tasks.Task!
109
Microsoft.AspNetCore.Components.WebView.WebViewManager.TryGetResponseContent(string! uri, bool allowFallbackOnHostPage, out int statusCode, out string! statusMessage, out System.IO.Stream! content, out System.Collections.Generic.IDictionary<string!, string!>! headers) -> bool
11-
Microsoft.AspNetCore.Components.WebView.WebViewManager.WebViewManager(System.IServiceProvider! provider, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, System.Uri! appBaseUri, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, string! hostPageRelativePath) -> void
10+
Microsoft.AspNetCore.Components.WebView.WebViewManager.WebViewManager(System.IServiceProvider! provider, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, System.Uri! appBaseUri, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents, string! hostPageRelativePath) -> void
1211
Microsoft.Extensions.DependencyInjection.ComponentsWebViewServiceCollectionExtensions
1312
abstract Microsoft.AspNetCore.Components.WebView.WebViewManager.NavigateCore(System.Uri! absoluteUri) -> void
1413
abstract Microsoft.AspNetCore.Components.WebView.WebViewManager.SendMessage(string! message) -> void

src/Components/WebView/WebView/src/WebViewManager.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class WebViewManager : IAsyncDisposable
2828
private readonly IpcReceiver _ipcReceiver;
2929
private readonly Uri _appBaseUri;
3030
private readonly StaticContentProvider _staticContentProvider;
31+
private readonly JSComponentConfigurationStore _jsComponents;
3132
private readonly Dictionary<string, RootComponent> _rootComponentsBySelector = new();
3233

3334
// Each time a web page connects, we establish a new per-page context
@@ -41,13 +42,15 @@ public abstract class WebViewManager : IAsyncDisposable
4142
/// <param name="dispatcher">A <see cref="Dispatcher"/> instance that can marshal calls to the required thread or sync context.</param>
4243
/// <param name="appBaseUri">The base URI for the application. Since this is a webview, the base URI is typically on a private origin such as http://0.0.0.0/ or app://example/</param>
4344
/// <param name="fileProvider">Provides static content to the webview.</param>
45+
/// <param name="jsComponents">Describes configuration for adding, removing, and updating root components from JavaScript code.</param>
4446
/// <param name="hostPageRelativePath">Path to the host page within the <paramref name="fileProvider"/>.</param>
45-
public WebViewManager(IServiceProvider provider, Dispatcher dispatcher, Uri appBaseUri, IFileProvider fileProvider, string hostPageRelativePath)
47+
public WebViewManager(IServiceProvider provider, Dispatcher dispatcher, Uri appBaseUri, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath)
4648
{
4749
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
4850
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
4951
_appBaseUri = EnsureTrailingSlash(appBaseUri ?? throw new ArgumentNullException(nameof(appBaseUri)));
5052
fileProvider = StaticWebAssetsLoader.UseStaticWebAssets(fileProvider);
53+
_jsComponents = jsComponents;
5154
_staticContentProvider = new StaticContentProvider(fileProvider, appBaseUri, hostPageRelativePath);
5255
_ipcSender = new IpcSender(_dispatcher, SendMessage);
5356
_ipcReceiver = new IpcReceiver(AttachToPageAsync);
@@ -58,13 +61,6 @@ public WebViewManager(IServiceProvider provider, Dispatcher dispatcher, Uri appB
5861
/// </summary>
5962
public Dispatcher Dispatcher => _dispatcher;
6063

61-
/// <summary>
62-
/// Gets an object that can be used to configure support for adding, updating, and removing
63-
/// components from JavaScript code. This object should be made available to application code
64-
/// during the host building phase so that it can be populated.
65-
/// </summary>
66-
public JSComponentConfigurationStore JSComponentConfiguration { get; } = new();
67-
6864
/// <summary>
6965
/// Instructs the web view to navigate to the specified location, bypassing any
7066
/// client-side routing.
@@ -198,7 +194,7 @@ internal async Task AttachToPageAsync(string baseUrl, string startUrl)
198194

199195
var serviceScope = _provider.CreateAsyncScope();
200196

201-
_currentPageContext = new PageContext(_dispatcher, serviceScope, _ipcSender, JSComponentConfiguration, baseUrl, startUrl);
197+
_currentPageContext = new PageContext(_dispatcher, serviceScope, _ipcSender, _jsComponents, baseUrl, startUrl);
202198

203199
// Add any root components that were registered before the page attached. We don't await any of the
204200
// returned render tasks so that the components can be processed in parallel.

src/Components/WebView/WebView/test/Infrastructure/TestWebViewManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestWebViewManager : WebViewManager
1313
private readonly List<string> _sentIpcMessages = new();
1414

1515
public TestWebViewManager(IServiceProvider provider, IFileProvider fileProvider)
16-
: base(provider, Dispatcher.CreateDefault(), AppBaseUri, fileProvider, hostPageRelativePath: "index.html")
16+
: base(provider, Dispatcher.CreateDefault(), AppBaseUri, fileProvider, new(), hostPageRelativePath: "index.html")
1717
{
1818
}
1919

0 commit comments

Comments
 (0)