Skip to content

Blazor API Review: IUriHelper #12425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Blazor
{
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public static partial class JSInteropMethods
{
[Microsoft.JSInterop.JSInvokableAttribute("NotifyLocationChanged")]
public static void NotifyLocationChanged(string uri, bool isInterceptedLink) { }
}
}
namespace Microsoft.AspNetCore.Blazor.Hosting
{
public static partial class BlazorWebAssemblyHost
Expand Down Expand Up @@ -68,18 +77,6 @@ protected override void HandleException(System.Exception exception) { }
protected override System.Threading.Tasks.Task UpdateDisplayAsync(in Microsoft.AspNetCore.Components.Rendering.RenderBatch batch) { throw null; }
}
}
namespace Microsoft.AspNetCore.Blazor.Services
{
public partial class WebAssemblyUriHelper : Microsoft.AspNetCore.Components.UriHelperBase
{
internal WebAssemblyUriHelper() { }
public static readonly Microsoft.AspNetCore.Blazor.Services.WebAssemblyUriHelper Instance;
protected override void EnsureInitialized() { }
protected override void NavigateToCore(string uri, bool forceLoad) { }
[Microsoft.JSInterop.JSInvokableAttribute("NotifyLocationChanged")]
public static void NotifyLocationChanged(string newAbsoluteUri, bool isInterceptedLink) { }
}
}
namespace Microsoft.AspNetCore.Components.Builder
{
public static partial class ComponentsApplicationBuilderExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ private void CreateServiceProvider()
services.AddSingleton<IWebAssemblyHost, WebAssemblyHost>();
services.AddSingleton<IJSRuntime>(WebAssemblyJSRuntime.Instance);
services.AddSingleton<IComponentContext, WebAssemblyComponentContext>();
services.AddSingleton<IUriHelper>(WebAssemblyUriHelper.Instance);
services.AddSingleton<NavigationManager>(WebAssemblyNavigationManager.Instance);
services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
services.AddSingleton<ILoggerFactory, WebAssemblyLoggerFactory>();
services.AddSingleton<HttpClient>(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
var uriHelper = s.GetRequiredService<IUriHelper>();
var navigationManager = s.GetRequiredService<NavigationManager>();
return new HttpClient
{
BaseAddress = new Uri(WebAssemblyUriHelper.Instance.GetBaseUri())
BaseAddress = new Uri(navigationManager.BaseUri)
};
});

Expand Down
26 changes: 26 additions & 0 deletions src/Components/Blazor/Blazor/src/JSInteropMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel;
using Microsoft.AspNetCore.Blazor.Services;
using Microsoft.JSInterop;

namespace Microsoft.AspNetCore.Blazor
{
/// <summary>
/// Contains methods called by interop. Intended for framework use only, not supported for use in application
/// code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class JSInteropMethods
{
/// <summary>
/// For framework use only.
/// </summary>
[JSInvokable(nameof(NotifyLocationChanged))]
public static void NotifyLocationChanged(string uri, bool isInterceptedLink)
{
WebAssemblyNavigationManager.Instance.SetLocation(uri, isInterceptedLink);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Routing;
using Interop = Microsoft.AspNetCore.Components.Web.BrowserUriHelperInterop;
using Interop = Microsoft.AspNetCore.Components.Web.BrowserNavigationManagerInterop;

namespace Microsoft.AspNetCore.Blazor.Services
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Components;
using Interop = Microsoft.AspNetCore.Components.Web.BrowserNavigationManagerInterop;

namespace Microsoft.AspNetCore.Blazor.Services
{
/// <summary>
/// Default client-side implementation of <see cref="NavigationManager"/>.
/// </summary>
internal class WebAssemblyNavigationManager : NavigationManager
{
/// <summary>
/// Gets the instance of <see cref="WebAssemblyNavigationManager"/>.
/// </summary>
public static readonly WebAssemblyNavigationManager Instance = new WebAssemblyNavigationManager();

// For simplicity we force public consumption of the BrowserNavigationManager through
// a singleton. Only a single instance can be updated by the browser through
// interop. We can construct instances for testing.
internal WebAssemblyNavigationManager()
{
}

protected override void EnsureInitialized()
{
// As described in the comment block above, BrowserNavigationManager is only for
// client-side (Mono) use, so it's OK to rely on synchronicity here.
var baseUri = WebAssemblyJSRuntime.Instance.Invoke<string>(Interop.GetBaseUri);
var uri = WebAssemblyJSRuntime.Instance.Invoke<string>(Interop.GetLocationHref);
Initialize(baseUri, uri);
}

public void SetLocation(string uri, bool isInterceptedLink)
{
Uri = uri;
NotifyLocationChanged(isInterceptedLink);
}

/// <inheritdoc />
protected override void NavigateToCore(string uri, bool forceLoad)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}

WebAssemblyJSRuntime.Instance.Invoke<object>(Interop.NavigateTo, uri, forceLoad);
}
}
}
85 changes: 0 additions & 85 deletions src/Components/Blazor/Blazor/src/Services/WebAssemblyUriHelper.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,6 @@ public sealed partial class InjectAttribute : System.Attribute
{
public InjectAttribute() { }
}
public partial interface IUriHelper
{
event System.EventHandler<Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs> OnLocationChanged;
string GetAbsoluteUri();
string GetBaseUri();
void NavigateTo(string uri);
void NavigateTo(string uri, bool forceLoad);
System.Uri ToAbsoluteUri(string href);
string ToBaseRelativePath(string baseUri, string locationAbsolute);
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public sealed partial class LayoutAttribute : System.Attribute
{
Expand All @@ -302,6 +292,20 @@ public partial class NavigationException : System.Exception
public NavigationException(string uri) { }
public string Location { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
}
public abstract partial class NavigationManager
{
protected NavigationManager() { }
public string BaseUri { get { throw null; } protected set { } }
public string Uri { get { throw null; } protected set { } }
public event System.EventHandler<Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs> LocationChanged { add { } remove { } }
protected virtual void EnsureInitialized() { }
protected void Initialize(string baseUri, string uri) { }
public void NavigateTo(string uri, bool forceLoad = false) { }
protected abstract void NavigateToCore(string uri, bool forceLoad);
protected void NotifyLocationChanged(bool isInterceptedLink) { }
public System.Uri ToAbsoluteUri(string relativeUri) { throw null; }
public string ToBaseRelativePath(string uri) { throw null; }
}
public abstract partial class OwningComponentBase : Microsoft.AspNetCore.Components.ComponentBase, System.IDisposable
{
protected OwningComponentBase() { }
Expand Down Expand Up @@ -383,23 +387,6 @@ public sealed partial class RouteAttribute : System.Attribute
public RouteAttribute(string template) { }
public string Template { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
}
public abstract partial class UriHelperBase : Microsoft.AspNetCore.Components.IUriHelper
{
protected UriHelperBase() { }
public event System.EventHandler<Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs> OnLocationChanged { add { } remove { } }
protected virtual void EnsureInitialized() { }
public string GetAbsoluteUri() { throw null; }
public virtual string GetBaseUri() { throw null; }
public virtual void InitializeState(string uriAbsolute, string baseUriAbsolute) { }
public void NavigateTo(string uri) { }
public void NavigateTo(string uri, bool forceLoad) { }
protected abstract void NavigateToCore(string uri, bool forceLoad);
protected void SetAbsoluteBaseUri(string baseUri) { }
protected void SetAbsoluteUri(string uri) { }
public System.Uri ToAbsoluteUri(string href) { throw null; }
public string ToBaseRelativePath(string baseUri, string locationAbsolute) { throw null; }
protected void TriggerOnLocationChanged(bool isinterceptedLink) { }
}
}
namespace Microsoft.AspNetCore.Components.CompilerServices
{
Expand Down Expand Up @@ -637,16 +624,17 @@ public enum RenderTreeFrameType : short
}
namespace Microsoft.AspNetCore.Components.Routing
{
public partial interface IHostEnvironmentNavigationManager
{
void Initialize(string baseUri, string uri);
}
public partial interface INavigationInterception
{
System.Threading.Tasks.Task EnableNavigationInterceptionAsync();
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct LocationChangedEventArgs
public partial class LocationChangedEventArgs : System.EventArgs
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public LocationChangedEventArgs(string location, bool isNavigationIntercepted) { throw null; }
public LocationChangedEventArgs(string location, bool isNavigationIntercepted) { }
public bool IsNavigationIntercepted { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public string Location { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
}
Expand Down
64 changes: 0 additions & 64 deletions src/Components/Components/src/IUriHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Components/Components/src/NavigationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.AspNetCore.Components
{
/// <summary>
/// Exception thrown when an <see cref="IUriHelper"/> is not able to navigate to a different url.
/// Exception thrown when an <see cref="NavigationManager"/> is not able to navigate to a different url.
/// </summary>
public class NavigationException : Exception
{
Expand Down
Loading