From 495ebe3950ee3e0b3aded96bdec5b690370c8f28 Mon Sep 17 00:00:00 2001 From: lilla28 <36889371+lilla28@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:23:05 +0300 Subject: [PATCH] Added msbuild target to embed and build composeui-fdc3 package to the Shell project, changed project's files to use filescoped namespace, little refactoring around naming variables (#329) --- src/shell/dotnet/Shell/App.xaml.cs | 6 +- src/shell/dotnet/Shell/Fdc3/PreloadFdc3.cs | 14 +-- .../ImageSource/DefaultImageSourcePolicy.cs | 12 ++- .../EnvironmentImageSourcePolicy.cs | 45 +++++----- .../Shell/ImageSource/IImageSourcePolicy.cs | 9 +- .../Shell/ImageSource/ImageSourceProvider.cs | 31 ++++--- src/shell/dotnet/Shell/MainWindow.xaml.cs | 89 ++++++++----------- .../dotnet/Shell/Manifest/ManifestModel.cs | 17 ++-- .../dotnet/Shell/Manifest/ManifestParser.cs | 29 +++--- .../dotnet/Shell/Manifest/ModuleModel.cs | 15 ++-- src/shell/dotnet/Shell/Shell.csproj | 25 ++++-- .../dotnet/Shell/Utilities/ResourceReader.cs | 25 ++---- src/shell/dotnet/Shell/WebWindowOptions.cs | 35 ++++---- 13 files changed, 163 insertions(+), 189 deletions(-) diff --git a/src/shell/dotnet/Shell/App.xaml.cs b/src/shell/dotnet/Shell/App.xaml.cs index b4ed70af0..45e5c8961 100644 --- a/src/shell/dotnet/Shell/App.xaml.cs +++ b/src/shell/dotnet/Shell/App.xaml.cs @@ -22,9 +22,10 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using MorganStanley.ComposeUI.Fdc3.DesktopAgent; using MorganStanley.ComposeUI.Messaging.Server.WebSocket; using Shell.Utilities; +using System.Reflection; +using System.Diagnostics; using Shell.Fdc3; namespace Shell; @@ -180,8 +181,7 @@ private void InjectMessageRouterConfig() private void InjectFdc3() { - string iife = ResourceReader.ReadResource(PreloadFdc3.Fdc3BundleResourceName); - + var iife = ResourceReader.ReadResource(PreloadFdc3.Fdc3BundleResourceName); WebWindow.AddPreloadScript(iife); } } diff --git a/src/shell/dotnet/Shell/Fdc3/PreloadFdc3.cs b/src/shell/dotnet/Shell/Fdc3/PreloadFdc3.cs index e7fe7c895..95733bf10 100644 --- a/src/shell/dotnet/Shell/Fdc3/PreloadFdc3.cs +++ b/src/shell/dotnet/Shell/Fdc3/PreloadFdc3.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Shell.Fdc3; -namespace Shell.Fdc3 +public static class PreloadFdc3 { - public static class PreloadFdc3 - { - public static readonly string Fdc3BundleResourceName = @"Shell.fdc3-iife-bundle.js"; - } + public static readonly string Fdc3BundleResourceName = @"Shell.fdc3-iife-bundle.js"; + } diff --git a/src/shell/dotnet/Shell/ImageSource/DefaultImageSourcePolicy.cs b/src/shell/dotnet/Shell/ImageSource/DefaultImageSourcePolicy.cs index a89694678..d41da355e 100644 --- a/src/shell/dotnet/Shell/ImageSource/DefaultImageSourcePolicy.cs +++ b/src/shell/dotnet/Shell/ImageSource/DefaultImageSourcePolicy.cs @@ -1,13 +1,11 @@ using System; -namespace Shell.ImageSource -{ - public sealed class DefaultImageSourcePolicy : IImageSourcePolicy +namespace Shell.ImageSource; +public sealed class DefaultImageSourcePolicy : IImageSourcePolicy +{ + public bool IsAllowed(Uri uri, Uri appUri) { - public bool IsAllowed(Uri uri, Uri appUri) - { - return uri.Scheme.StartsWith("http") && uri.Host == appUri.Host; - } + return uri.Scheme.StartsWith("http") && uri.Host == appUri.Host; } } diff --git a/src/shell/dotnet/Shell/ImageSource/EnvironmentImageSourcePolicy.cs b/src/shell/dotnet/Shell/ImageSource/EnvironmentImageSourcePolicy.cs index e472b6712..d9df25559 100644 --- a/src/shell/dotnet/Shell/ImageSource/EnvironmentImageSourcePolicy.cs +++ b/src/shell/dotnet/Shell/ImageSource/EnvironmentImageSourcePolicy.cs @@ -1,32 +1,31 @@ using System; using System.Linq; -namespace Shell.ImageSource +namespace Shell.ImageSource; + +public sealed class EnvironmentImageSourcePolicy : IImageSourcePolicy { - public sealed class EnvironmentImageSourcePolicy : IImageSourcePolicy + private const string _allowListEnvVar = "COMPOSE_ALLOWED_IMAGE_SOURCES"; + public bool IsAllowed(Uri uri, Uri appUri) { - private const string _allowListEnvVar = "COMPOSE_ALLOWED_IMAGE_SOURCES"; - public bool IsAllowed(Uri uri, Uri appUri) - { - var allowListString = Environment.GetEnvironmentVariable(_allowListEnvVar); - - // Only allow http or https sources. If no sources are allowed, - if (!uri.Scheme.StartsWith("http")) - { - return false; - } - // If the source host is the same as the app host, allow it. - if (uri.Host == appUri.Host) - { - return true; - } - if (string.IsNullOrEmpty(allowListString)) - { - return false; - } + var allowListString = Environment.GetEnvironmentVariable(_allowListEnvVar); - var allowedSources = allowListString.Split(';'); - return allowedSources.Contains(uri.Host); + // Only allow http or https sources. If no sources are allowed, + if (!uri.Scheme.StartsWith("http")) + { + return false; } + // If the source host is the same as the app host, allow it. + if (uri.Host == appUri.Host) + { + return true; + } + if (string.IsNullOrEmpty(allowListString)) + { + return false; + } + + var allowedSources = allowListString.Split(';'); + return allowedSources.Contains(uri.Host); } } diff --git a/src/shell/dotnet/Shell/ImageSource/IImageSourcePolicy.cs b/src/shell/dotnet/Shell/ImageSource/IImageSourcePolicy.cs index 191bd35d9..80523af10 100644 --- a/src/shell/dotnet/Shell/ImageSource/IImageSourcePolicy.cs +++ b/src/shell/dotnet/Shell/ImageSource/IImageSourcePolicy.cs @@ -1,9 +1,8 @@ using System; -namespace Shell.ImageSource +namespace Shell.ImageSource; + +public interface IImageSourcePolicy { - public interface IImageSourcePolicy - { - bool IsAllowed(Uri uri, Uri appUri); - } + bool IsAllowed(Uri uri, Uri appUri); } diff --git a/src/shell/dotnet/Shell/ImageSource/ImageSourceProvider.cs b/src/shell/dotnet/Shell/ImageSource/ImageSourceProvider.cs index fc404361c..9a6f4c7f3 100644 --- a/src/shell/dotnet/Shell/ImageSource/ImageSourceProvider.cs +++ b/src/shell/dotnet/Shell/ImageSource/ImageSourceProvider.cs @@ -1,28 +1,27 @@ using System; using System.Windows.Media.Imaging; -namespace Shell.ImageSource +namespace Shell.ImageSource; + +public class ImageSourceProvider { - public class ImageSourceProvider + private readonly IImageSourcePolicy _imageSourcePolicy; + public ImageSourceProvider(IImageSourcePolicy imageSourcePolicy) { - IImageSourcePolicy _imageSourcePolicy; - public ImageSourceProvider(IImageSourcePolicy imageSourcePolicy) + _imageSourcePolicy = imageSourcePolicy; + } + + public System.Windows.Media.ImageSource? GetImageSource(Uri uri, Uri appUri) + { + if (!uri.IsAbsoluteUri) { - _imageSourcePolicy = imageSourcePolicy; + uri = new Uri(appUri, uri); } - public System.Windows.Media.ImageSource? GetImageSource(Uri uri, Uri appUri) + if (_imageSourcePolicy.IsAllowed(uri, appUri)) { - if (!uri.IsAbsoluteUri) - { - uri = new Uri(appUri, uri); - } - - if (_imageSourcePolicy.IsAllowed(uri, appUri)) - { - return BitmapFrame.Create(uri); - } - return null; + return BitmapFrame.Create(uri); } + return null; } } diff --git a/src/shell/dotnet/Shell/MainWindow.xaml.cs b/src/shell/dotnet/Shell/MainWindow.xaml.cs index ec991699e..bb1606003 100644 --- a/src/shell/dotnet/Shell/MainWindow.xaml.cs +++ b/src/shell/dotnet/Shell/MainWindow.xaml.cs @@ -15,68 +15,55 @@ using Manifest; using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; using System.Windows.Controls.Ribbon; -namespace Shell +namespace Shell; + +/// +/// Interaction logic for MainWindow.xaml +/// +public partial class MainWindow : RibbonWindow { - /// - /// Interaction logic for MainWindow.xaml - /// - public partial class MainWindow : RibbonWindow - { - internal List webWindows { get; set; } = new List(); - private ManifestModel config; - private ModuleModel[]? modules; + internal List WebWindows { get; set; } = new List(); + private ManifestModel _config; + private ModuleModel[]? _modules; - public MainWindow() - { - InitializeComponent(); + public MainWindow() + { + InitializeComponent(); - config = ManifestParser.OpenManifestFile("exampleManifest.json"); - modules = config.Modules; - DataContext = modules; - } + _config = ManifestParser.OpenManifestFile("exampleManifest.json"); + _modules = _config.Modules; + DataContext = _modules; + } - private void CreateWebWindow(ModuleModel item) + private void CreateWebWindow(ModuleModel item) + { + var options = new WebWindowOptions { - var options = new WebWindowOptions - { - Title = item.AppName, - Url = item.Url, - IconUrl = item.IconUrl - }; + Title = item.AppName, + Url = item.Url, + IconUrl = item.IconUrl + }; - var webWindow = new WebWindow(options); - webWindow.Owner = this; - webWindow.Closed += WebWindowClosed; - webWindows.Add(webWindow); - webWindow.Show(); - } + var webWindow = new WebWindow(options); + webWindow.Owner = this; + webWindow.Closed += WebWindowClosed; + WebWindows.Add(webWindow); + webWindow.Show(); + } - private void WebWindowClosed(object? sender, EventArgs e) - { - webWindows.Remove((WebWindow)sender!); - } + private void WebWindowClosed(object? sender, EventArgs e) + { + WebWindows.Remove((WebWindow)sender!); + } + + private void ShowChild_Click(object sender, RoutedEventArgs e) + { + var context = ((Button)sender).DataContext; - private void ShowChild_Click(object sender, RoutedEventArgs e) - { - var context = ((Button)sender).DataContext; - - CreateWebWindow((ModuleModel)context); - } + CreateWebWindow((ModuleModel)context); } } diff --git a/src/shell/dotnet/Shell/Manifest/ManifestModel.cs b/src/shell/dotnet/Shell/Manifest/ManifestModel.cs index 63979e4b9..a4e119fa0 100644 --- a/src/shell/dotnet/Shell/Manifest/ManifestModel.cs +++ b/src/shell/dotnet/Shell/Manifest/ManifestModel.cs @@ -14,17 +14,16 @@ using System.Text.Json; -namespace Manifest +namespace Manifest; + +internal sealed class ManifestModel { - internal sealed class ManifestModel - { - public ModuleModel[]? Modules { get; set; } + public ModuleModel[]? Modules { get; set; } - public static JsonSerializerOptions JsonSerializerOptions = new() - { - PropertyNameCaseInsensitive = true - }; - } + public static JsonSerializerOptions JsonSerializerOptions = new() + { + PropertyNameCaseInsensitive = true + }; } diff --git a/src/shell/dotnet/Shell/Manifest/ManifestParser.cs b/src/shell/dotnet/Shell/Manifest/ManifestParser.cs index b2314d5ae..4bf749fb7 100644 --- a/src/shell/dotnet/Shell/Manifest/ManifestParser.cs +++ b/src/shell/dotnet/Shell/Manifest/ManifestParser.cs @@ -16,26 +16,25 @@ using System.IO; using System.Text.Json; -namespace Manifest +namespace Manifest; + +internal static class ManifestParser { - internal static class ManifestParser + internal static ManifestModel Manifest { get; set; } + + public static ManifestModel OpenManifestFile(string fileName) { - internal static ManifestModel Manifest { get; set; } + string processPath = Environment.ProcessPath; + string folder = Path.GetDirectoryName(processPath); + string path = Path.Combine(folder, @"Manifest\", fileName); - public static ManifestModel OpenManifestFile(string fileName) + using (FileStream stream = File.Open(path, FileMode.Open)) { - string processPath = Environment.ProcessPath; - string folder = Path.GetDirectoryName(processPath); - string path = Path.Combine(folder, @"Manifest\", fileName); - - using (FileStream stream = File.Open(path, FileMode.Open)) - { - Manifest = JsonSerializer.Deserialize(stream, ManifestModel.JsonSerializerOptions); + Manifest = JsonSerializer.Deserialize(stream, ManifestModel.JsonSerializerOptions); - stream.Close(); - } - - return Manifest; + stream.Close(); } + + return Manifest; } } diff --git a/src/shell/dotnet/Shell/Manifest/ModuleModel.cs b/src/shell/dotnet/Shell/Manifest/ModuleModel.cs index c2751f4ba..fe06c9fdf 100644 --- a/src/shell/dotnet/Shell/Manifest/ModuleModel.cs +++ b/src/shell/dotnet/Shell/Manifest/ModuleModel.cs @@ -14,13 +14,12 @@ using System; -namespace Manifest +namespace Manifest; + +[Serializable] +internal sealed class ModuleModel { - [Serializable] - internal sealed class ModuleModel - { - public string AppName { get; set; } = string.Empty; - public string Url { get; set; } = string.Empty; - public string IconUrl { get; set; } = string.Empty; - } + public string AppName { get; set; } = string.Empty; + public string Url { get; set; } = string.Empty; + public string IconUrl { get; set; } = string.Empty; } diff --git a/src/shell/dotnet/Shell/Shell.csproj b/src/shell/dotnet/Shell/Shell.csproj index 5ab1d6d5e..6fe42ad13 100644 --- a/src/shell/dotnet/Shell/Shell.csproj +++ b/src/shell/dotnet/Shell/Shell.csproj @@ -1,4 +1,4 @@ - + WinExe @@ -47,16 +47,27 @@ - $(ComposeUIRepositoryRoot)/src/shell/js/composeui-fdc3/dist/fdc3-iife-bundle.js + $(ComposeUIRepositoryRoot)\src\shell\js\composeui-fdc3\dist\fdc3-iife-bundle.js - - + - - - + + + + + + + + + + + \ No newline at end of file diff --git a/src/shell/dotnet/Shell/Utilities/ResourceReader.cs b/src/shell/dotnet/Shell/Utilities/ResourceReader.cs index 5c009cc83..aacf2c215 100644 --- a/src/shell/dotnet/Shell/Utilities/ResourceReader.cs +++ b/src/shell/dotnet/Shell/Utilities/ResourceReader.cs @@ -13,28 +13,19 @@ */ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -namespace Shell.Utilities +namespace Shell.Utilities; + +public static class ResourceReader { - public static class ResourceReader + public static string ReadResource(string resourcePath) { - public static string ReadResource(string name) - { - var assembly = Assembly.GetExecutingAssembly(); - string resourcePath = name; + var assembly = Assembly.GetExecutingAssembly(); - using var stream = assembly.GetManifestResourceStream(resourcePath) ?? throw new InvalidOperationException("Resource not found"); - using var reader = new StreamReader(stream); - { - return reader.ReadToEnd(); - } - } + using var stream = assembly.GetManifestResourceStream(resourcePath) ?? throw new InvalidOperationException("Resource not found"); + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); } } diff --git a/src/shell/dotnet/Shell/WebWindowOptions.cs b/src/shell/dotnet/Shell/WebWindowOptions.cs index 49d2c3bd9..0e6e0432a 100644 --- a/src/shell/dotnet/Shell/WebWindowOptions.cs +++ b/src/shell/dotnet/Shell/WebWindowOptions.cs @@ -1,27 +1,26 @@ using System.ComponentModel.DataAnnotations; -namespace Shell +namespace Shell; + +public sealed class WebWindowOptions { - public sealed class WebWindowOptions - { - [Display(Description = "Set the height of the window. Default: 450")] - public double? Height { get; set; } + [Display(Description = "Set the height of the window. Default: 450")] + public double? Height { get; set; } - [Display(Description = $"Set the title of the window. Default: {DefaultTitle}")] - public string? Title { get; set; } + [Display(Description = $"Set the title of the window. Default: {DefaultTitle}")] + public string? Title { get; set; } - [Display(Description = $"Set the url for the web view. Default: {DefaultUrl}")] - public string? Url { get; set; } + [Display(Description = $"Set the url for the web view. Default: {DefaultUrl}")] + public string? Url { get; set; } - [Display(Name = "icon", Description = $"Set the icon url for the window.")] - public string? IconUrl { get; set; } + [Display(Name = "icon", Description = $"Set the icon url for the window.")] + public string? IconUrl { get; set; } - [Display(Description = $"Set the width of the window. Default: 800")] - public double? Width { get; set; } + [Display(Description = $"Set the width of the window. Default: 800")] + public double? Width { get; set; } - public const double DefaultHeight = 450; - public const string DefaultTitle = "Compose Web Container"; - public const string DefaultUrl = "about:blank"; - public const double DefaultWidth = 800; - } + public const double DefaultHeight = 450; + public const string DefaultTitle = "Compose Web Container"; + public const string DefaultUrl = "about:blank"; + public const double DefaultWidth = 800; } \ No newline at end of file