From 265c39c39e39b95864cb7e2ccb1c0aa8fbd7646d Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 16:15:30 +0100 Subject: [PATCH 1/7] Remove IBrowserHostServices as its not used anymore --- .../Internal/AppModel/IBrowserHostServices.cs | 51 ------------------- .../PresentationFramework.csproj | 1 - 2 files changed, 52 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs deleted file mode 100644 index 9546a114d4e..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// -// Description: -// Interop services between windows client applications -// and the browser host. This interface is implemented on -// the client application end to support services to the -// unmanaged docobj hosted in the browser -// -// ***********************IMPORTANT************************** -// -// If you change any of the interface definitions here -// make sure you also change the interface definitions -// in the unmanaged side. (wcp\host\inc\hostservices.idl) -// If you are not sure about how to define it -// here, TEMPORARILY mark the interface as -// ComVisible in the managed side, use tlbexp to generate -// a typelibrary from the managed dll and copy the method -// signatures from there. REMEMBER to remove the ComVisible -// in the managed code when you are done. -// Defining the interfaces at both ends prevents us from -// publicly exposing these interfaces to the outside world. -// In order for marshaling to work correctly, the vtable -// and data types should match EXACTLY in both the managed -// and unmanaged worlds -// - -namespace MS.Internal.AppModel -{ - //********************************************************************************************// - // IMPORTANT: IMPORTANT: IMPORTANT: IMPORTANT: // - //********************************************************************************************// - // If you change or update this interface, make sure you update the definitions in - // wcp\host\inc\hostservices.idl - - [Flags] - enum HostingFlags - { - hfHostedInIE = 1, // Not mutually exclusive! See master definition in the IDL file. - hfHostedInWebOC = 2,// - hfHostedInIEorWebOC = 3, - hfHostedInMozilla = 4, - hfHostedInFrame = 8, // hosted in an HTML frame or iframe element or WebOC in the IE process - hfIsBrowserLowIntegrityProcess = 0x10, - hfInDebugMode = 0x20 - }; - - -} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj index 9e9473e6c93..11a3f87dde2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj @@ -111,7 +111,6 @@ - From 1d2502d41859d0c7a1f240ad39bdde08e71c69d1 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 16:16:35 +0100 Subject: [PATCH 2/7] Remove ProgressPage as its no longer used --- .../MS/Internal/AppModel/ProgressPage.cs | 125 ------------------ .../PresentationFramework.csproj | 1 - 2 files changed, 126 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ProgressPage.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ProgressPage.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ProgressPage.cs deleted file mode 100644 index 3be58d9322b..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ProgressPage.cs +++ /dev/null @@ -1,125 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// -// Description: -// Deployment progress page. This is primarily a proxy to the native progress page, which supersedes -// the managed one from up to v3.5. See Host\DLL\ProgressPage.hxx for details. -// - -using System.Runtime.InteropServices; -using System.Windows.Interop; -using System.Windows.Threading; - -// Disambiguate MS.Internal.HRESULT... -using HR = MS.Internal.Interop.HRESULT; - -namespace MS.Internal.AppModel -{ - [ComImport, Guid("1f681651-1024-4798-af36-119bbe5e5665")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - interface INativeProgressPage - { - [PreserveSig] - HR Show(); - [PreserveSig] - HR Hide(); - [PreserveSig] - HR ShowProgressMessage(string message); - [PreserveSig] - HR SetApplicationName(string appName); - [PreserveSig] - HR SetPublisherName(string publisherName); - [PreserveSig] - HR OnDownloadProgress(ulong bytesDownloaded, ulong bytesTotal); - }; - - /// - /// IProgressPage is public. It was introduced for the Media Center integration, which is now considered - /// deprecated, but we have to support it at least for as long as we keep doing in-place upgrades. - /// - interface IProgressPage2 : IProgressPage - { - void ShowProgressMessage(string message); - }; - - class NativeProgressPageProxy : IProgressPage2 - { - internal NativeProgressPageProxy(INativeProgressPage npp) - { - _npp = npp; - } - - public void ShowProgressMessage(string message) - { - // Ignore the error code. This page is transient and it's not the end of the world if this doesn't show up. - HR hr = _npp.ShowProgressMessage(message); - } - - public Uri DeploymentPath - { - set { } - get { throw new System.NotImplementedException(); } - } - - /// - /// The native progress page sends a stop/cancel request to its host object, which then calls - /// IBrowserHostServices.ExecCommand(OLECMDID_STOP). - /// - public DispatcherOperationCallback StopCallback - { - set { } - get { throw new System.NotImplementedException(); } - } - - /// - /// The native progress page sends a Refresh request to its host object, which then calls - /// IBrowserHostServices.ExecCommand(OLECMDID_REFRESH). - /// - public System.Windows.Threading.DispatcherOperationCallback RefreshCallback - { - set { } - get { return null; } - } - - public string ApplicationName - { - set - { - // Ignore the error code. This page is transient and it's not the end of the world if this doesn't show up. - HR hr = _npp.SetApplicationName(value); - } - - get { throw new System.NotImplementedException(); } - } - - /// - /// Critical: Calls a SUC'd COM interface method. - /// TreatAsSafe: 1) The publisher name is coming from the manifest, so it could be anything. - /// This means the input doesn't need to be trusted. - /// 2) Setting arbitrary application/publisher can be considered spoofing, but a malicious website - /// could fake the whole progress page and still achieve the same. - public string PublisherName - { - set - { - // Ignore the error code. This page is transient and it's not the end of the world if this doesn't show up. - HR hr = _npp.SetPublisherName(value); - } - - get { throw new System.NotImplementedException(); } - } - - /// - /// Critical: Calls a SUC'd COM interface method. - /// TreatAsSafe: Sending even arbitrary progress updates not considered harmful. - public void UpdateProgress(long bytesDownloaded, long bytesTotal) - { - // Ignore the error code. This page is transient and it's not the end of the world if this doesn't show up. - HR hr = _npp.OnDownloadProgress((ulong)bytesDownloaded, (ulong)bytesTotal); - } - - INativeProgressPage _npp; - }; -} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj index 11a3f87dde2..c35491ad95c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj @@ -118,7 +118,6 @@ - From fa0d84d1e8a4fbc3653313bc6e4e45de505709a5 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 16:40:15 +0100 Subject: [PATCH 3/7] Clean up AppSecurityManager --- .../Internal/AppModel/AppSecurityManager.cs | 88 ++++++------------- .../Windows/Navigation/NavigationService.cs | 10 +-- 2 files changed, 28 insertions(+), 70 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/AppSecurityManager.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/AppSecurityManager.cs index 056f01c8329..cd187b6d548 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/AppSecurityManager.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/AppSecurityManager.cs @@ -1,26 +1,15 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// -// Description: AppSecurityManager class. -// -//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -// IMPORTANT: We are creating an instance of IInternetSecurityManager here. This -// is currently also done in the CustomCredentialPolicy at the Core level. Any -// modification to either of these classes--especially concerning MapUrlToZone-- -// should be considered for both classes. -//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -// using MS.Win32; using System.Windows; using MS.Internal.Utility; +using System.ComponentModel; using System.Runtime.InteropServices; namespace MS.Internal.AppModel { - enum LaunchResult + internal enum LaunchResult { Launched, NotLaunched, @@ -29,50 +18,29 @@ enum LaunchResult internal static class AppSecurityManager { - /// - /// Safely launch the browser if you can. - /// If you can't demand unmanaged code permisison. - /// - /// originatingUri = the current uri - /// destinationUri = the uri you are going to. - /// - internal static void SafeLaunchBrowserDemandWhenUnsafe(Uri originatingUri, Uri destinationUri, bool fIsTopLevel) + /// + /// Launch the browser if you can. + /// + internal static void SafeLaunchBrowserDemandWhenUnsafe(Uri destinationUri, bool fIsTopLevel) { - LaunchResult launched = LaunchResult.NotLaunched; - - launched = SafeLaunchBrowserOnlyIfPossible(originatingUri, destinationUri, fIsTopLevel); - if (launched == LaunchResult.NotLaunched) + if (SafeLaunchBrowserOnlyIfPossible(destinationUri, fIsTopLevel) is LaunchResult.NotLaunched) { - UnsafeLaunchBrowser(destinationUri); + ShellExecuteDefaultBrowser(destinationUri); } } - /// + /// /// Safely launch the browser if it's possible to do so in partial trust /// Returns enum indicating whether we safely launched ( or at least think we did). /// /// This function is appropriate for use when we launch the browser from partial trust /// ( as it doesn't perform demands for the "unsafe" cases ) /// - internal static LaunchResult SafeLaunchBrowserOnlyIfPossible(Uri originatingUri, Uri destinationUri, bool fIsTopLevel) + internal static LaunchResult SafeLaunchBrowserOnlyIfPossible(Uri destinationUri, bool fIsTopLevel) { - return SafeLaunchBrowserOnlyIfPossible(originatingUri, destinationUri, null, fIsTopLevel); - } - - /// - /// Safely launch the browser if it's possible to do so in partial trust - /// Returns enum indicating whether we safely launched ( or at least think we did). - /// Html target names can be passed in with this. - /// This function is appropriate for use when we launch the browser from partial trust - /// ( as it doesn't perform demands for the "unsafe" cases ) - /// - internal static LaunchResult SafeLaunchBrowserOnlyIfPossible(Uri originatingUri, Uri destinationUri, string targetName, bool fIsTopLevel) - { - LaunchResult launched = LaunchResult.NotLaunched; - - bool isKnownScheme = (Object.ReferenceEquals(destinationUri.Scheme, Uri.UriSchemeHttp)) || - (Object.ReferenceEquals(destinationUri.Scheme, Uri.UriSchemeHttps)) || - destinationUri.IsFile; + bool isKnownScheme = object.ReferenceEquals(destinationUri.Scheme, Uri.UriSchemeHttp) || + object.ReferenceEquals(destinationUri.Scheme, Uri.UriSchemeHttps) || + destinationUri.IsFile; bool fIsMailTo = string.Equals(destinationUri.Scheme, Uri.UriSchemeMailto, StringComparison.OrdinalIgnoreCase); @@ -89,26 +57,19 @@ internal static LaunchResult SafeLaunchBrowserOnlyIfPossible(Uri originatingUri, { // Shell-Exec the browser to the mailto url. // assumed safe - because we're only allowing this for mailto urls. - // - UnsafeNativeMethods.ShellExecute(new HandleRef(null, IntPtr.Zero), /*hwnd*/ - null, /*operation*/ - BindUriHelper.UriToString(destinationUri), /*file*/ - null, /*parameters*/ - null, /*directory*/ - 0); /*nShowCmd*/ - launched = LaunchResult.Launched; + UnsafeNativeMethods.ShellExecute(hwnd: new HandleRef(null, IntPtr.Zero), + lpOperation: null, + lpFile: BindUriHelper.UriToString(destinationUri), + lpParameters: null, + lpDirectory: null, + nShowCmd: 0); + + return LaunchResult.Launched; } } - return launched; - } - - // This invokes the browser unsafely. - // Whoever is calling this function should do the right demands. - internal static void UnsafeLaunchBrowser(Uri uri, string targetFrame = null) - { - ShellExecuteDefaultBrowser(uri); + return LaunchResult.NotLaunched; } /// @@ -132,10 +93,11 @@ the key used is (supposedly) HKCR\htmlfile\shell\opennew\ddeexec, and its value sei.fMask |= UnsafeNativeMethods.ShellExecuteFlags.SEE_MASK_CLASSNAME; sei.lpClass = ".htm"; // The default browser is looked up by this. } + sei.lpFile = uri.ToString(); // It's safe to use Uri.ToString since there's an inheritance demand on it that prevents spoofing by subclasses. + if (!UnsafeNativeMethods.ShellExecuteEx(sei)) - throw new InvalidOperationException(SR.FailToLaunchDefaultBrowser, - new System.ComponentModel.Win32Exception(/*uses the last Win32 error*/)); + throw new InvalidOperationException(SR.FailToLaunchDefaultBrowser, new Win32Exception(/*uses the last Win32 error*/)); } } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs index 5057aa7a370..e4eb1112580 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationService.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -2774,13 +2774,9 @@ private WebRequest CreateWebRequest(Uri resolvedDestinationUri, NavigateInfo nav } catch (NotSupportedException) { - LaunchResult launched = LaunchResult.NotLaunched; - // Not supported exceptions are thrown for mailto: which we want to support. // So we detect mailto: here. - launched = AppSecurityManager.SafeLaunchBrowserOnlyIfPossible(CurrentSource, resolvedDestinationUri, IsTopLevelContainer); - - if (launched == LaunchResult.NotLaunched) + if (AppSecurityManager.SafeLaunchBrowserOnlyIfPossible(resolvedDestinationUri, IsTopLevelContainer) is LaunchResult.NotLaunched) throw; } catch (SecurityException) @@ -3016,7 +3012,7 @@ private void DelegateToBrowser(bool isPack, Uri destinationUri) destinationUri.ToString()); } - AppSecurityManager.SafeLaunchBrowserDemandWhenUnsafe(CurrentSource, destinationUri, IsTopLevelContainer); + AppSecurityManager.SafeLaunchBrowserDemandWhenUnsafe(destinationUri, IsTopLevelContainer); } finally { From da1b5e9f434d51f21f845edaa1d98fbe2f7dbd21 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 17:04:04 +0100 Subject: [PATCH 4/7] Remove unused method from CAS days --- .../MS/Internal/Controls/WebBrowserEvent.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs index b7a00e4194d..b3a3f349ab6 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Controls/WebBrowserEvent.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -149,15 +149,6 @@ public void BeforeNavigate2(object pDisp, ref object url, ref object flags, ref } } - /// - /// Determines whether a URI has a recognized and allowed URI scheme that we shouldn't block on navigation - /// attempts from the browser, regardless of whether we are partial trust or not. - /// - private static bool IsAllowedScriptScheme(Uri uri) - { - return uri != null && (uri.Scheme == "javascript" || uri.Scheme == "vbscript"); - } - /// /// Critical: This code extracts the IWebBrowser2, IHTMLDocument interface. /// TreatAsSafe: This does not expose the interface. From 21c5b935ec6934c348f16645edb53b71b809a839 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 17:40:27 +0100 Subject: [PATCH 5/7] Clean up SiteOfOriginContainer, seal the class --- .../AppModel/SiteOfOriginContainer.cs | 168 +++--------------- .../MS/internal/AppModel/SiteOfOriginPart.cs | 20 +-- 2 files changed, 35 insertions(+), 153 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs index a8de3ef10d1..ed9758bb60e 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs @@ -1,19 +1,8 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. -// -// -// Description: -// SiteOfOriginContainer is an implementation of the abstract Package class. -// It contains nontrivial overrides for GetPartCore and Exists. -// Many of the methods on Package are not applicable to loading application -// resources, so the SiteOfOriginContainer implementations of these methods throw -// the NotSupportedException. -// - -using System.IO.Packaging; using System.IO; +using System.IO.Packaging; using System.Windows.Navigation; namespace MS.Internal.AppModel @@ -25,71 +14,46 @@ namespace MS.Internal.AppModel /// so the SiteOfOriginContainer implementations of these methods throw /// the NotSupportedException. /// - internal class SiteOfOriginContainer : System.IO.Packaging.Package + internal sealed class SiteOfOriginContainer : Package { - //------------------------------------------------------ - // - // Static Methods - // - //------------------------------------------------------ + private static readonly BooleanSwitch s_traceSwitch = new("SiteOfOrigin", "SiteOfOriginContainer and SiteOfOriginPart trace messages"); - #region Static Methods + internal static bool TraceSwitchEnabled + { + get => s_traceSwitch.Enabled; + set => s_traceSwitch.Enabled = value; + } + /// + /// Retrieves the site of origin, derived from . + /// internal static Uri SiteOfOrigin { get { // Calling FixFileUri because BaseDirectory will be a c:\\ style path - Uri siteOfOrigin = BaseUriHelper.FixFileUri(new Uri(System.AppDomain.CurrentDomain.BaseDirectory)); + Uri siteOfOrigin = BaseUriHelper.FixFileUri(new Uri(AppDomain.CurrentDomain.BaseDirectory)); #if DEBUG - if (_traceSwitch.Enabled) - System.Diagnostics.Trace.TraceInformation( - DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + - Environment.CurrentManagedThreadId + - ": SiteOfOriginContainer: returning site of origin " + siteOfOrigin); + if (TraceSwitchEnabled) + { + Trace.TraceInformation($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.Millisecond} {Environment.CurrentManagedThreadId}" + + $": SiteOfOriginContainer: returning site of origin {siteOfOrigin}"); + } #endif return siteOfOrigin; } } - - #endregion - - //------------------------------------------------------ - // - // Public Constructors - // - //------------------------------------------------------ - - #region Public Constructors /// /// Default Constructor /// internal SiteOfOriginContainer() : base(FileAccess.Read) - { + { } - #endregion - - //------------------------------------------------------ - // - // Public Properties - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Public Methods - // - //------------------------------------------------------ - - #region Public Methods - /// - /// If this were to be implemented for http site of origin, - /// it will require a server round trip. + /// If this were to be implemented for http site of origin, it will require a server round trip. /// /// /// @@ -98,76 +62,6 @@ public override bool PartExists(Uri uri) return true; } - #endregion - - //------------------------------------------------------ - // - // Public Events - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Internal Constructors - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Internal Properties - // - //------------------------------------------------------ - - #region Internal Properties - - internal static bool TraceSwitchEnabled - { - get - { - return _traceSwitch.Enabled; - } - set - { - _traceSwitch.Enabled = value; - } - } - - internal static System.Diagnostics.BooleanSwitch _traceSwitch = - new System.Diagnostics.BooleanSwitch("SiteOfOrigin", "SiteOfOriginContainer and SiteOfOriginPart trace messages"); - - #endregion - - //------------------------------------------------------ - // - // Internal Methods - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Internal Events - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Protected Constructors - // - //------------------------------------------------------ - // None - - //------------------------------------------------------ - // - // Protected Methods - // - //------------------------------------------------------ - - #region Protected Methods - /// /// This method creates a SiteOfOriginPart which will create a WebRequest /// to access files at the site of origin. @@ -177,25 +71,15 @@ internal static bool TraceSwitchEnabled protected override PackagePart GetPartCore(Uri uri) { #if DEBUG - if (_traceSwitch.Enabled) - System.Diagnostics.Trace.TraceInformation( - DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + - Environment.CurrentManagedThreadId + - ": SiteOfOriginContainer: Creating SiteOfOriginPart for Uri " + uri); + if (TraceSwitchEnabled) + { + Trace.TraceInformation($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.Millisecond} {Environment.CurrentManagedThreadId}" + + $": SiteOfOriginContainer: Creating SiteOfOriginPart for Uri {uri}"); + } #endif return new SiteOfOriginPart(this, uri); } - #endregion - - - //------------------------------------------------------ - // - // Uninteresting (but required) overrides - // - //------------------------------------------------------ - #region Uninteresting (but required) overrides - protected override PackagePart CreatePartCore(Uri uri, string contentType, CompressionOption compressionOption) { return null; @@ -215,7 +99,5 @@ protected override void FlushCore() { throw new NotSupportedException(); } - - #endregion } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginPart.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginPart.cs index ce9d5072428..91cd0e03912 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginPart.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginPart.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -46,7 +46,7 @@ internal SiteOfOriginPart(Package container, Uri uri) : protected override Stream GetStreamCore(FileMode mode, FileAccess access) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -58,7 +58,7 @@ protected override Stream GetStreamCore(FileMode mode, FileAccess access) protected override string GetContentTypeCore() { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -86,7 +86,7 @@ private Stream GetStreamAndSetContentType(bool onlyNeedContentType) if (onlyNeedContentType && _contentType != MS.Internal.ContentType.Empty) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -102,7 +102,7 @@ private Stream GetStreamAndSetContentType(bool onlyNeedContentType) if (_cacheStream != null) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -116,7 +116,7 @@ private Stream GetStreamAndSetContentType(bool onlyNeedContentType) if (_absoluteLocation == null) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -129,7 +129,7 @@ private Stream GetStreamAndSetContentType(bool onlyNeedContentType) } #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -155,7 +155,7 @@ private Stream GetStreamAndSetContentType(bool onlyNeedContentType) private Stream HandleFileSource(bool onlyNeedContentType) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -179,7 +179,7 @@ private Stream HandleWebSource(bool onlyNeedContentType) Stream responseStream = response.GetResponseStream(); #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + @@ -189,7 +189,7 @@ private Stream HandleWebSource(bool onlyNeedContentType) if (_contentType == MS.Internal.ContentType.Empty) { #if DEBUG - if (SiteOfOriginContainer._traceSwitch.Enabled) + if (SiteOfOriginContainer.TraceSwitchEnabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + Environment.CurrentManagedThreadId + From 4f7bf569eb47cee82cfe38f4fa90d70946dc530f Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 17:41:05 +0100 Subject: [PATCH 6/7] Use AppContext.BaseDirectory over AppDomain.CurrentDomain.BaseDirectory for clarity --- .../MS/internal/AppModel/SiteOfOriginContainer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs index ed9758bb60e..c6690d24868 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs @@ -25,14 +25,14 @@ internal static bool TraceSwitchEnabled } /// - /// Retrieves the site of origin, derived from . + /// Retrieves the site of origin, derived from . /// internal static Uri SiteOfOrigin { get { // Calling FixFileUri because BaseDirectory will be a c:\\ style path - Uri siteOfOrigin = BaseUriHelper.FixFileUri(new Uri(AppDomain.CurrentDomain.BaseDirectory)); + Uri siteOfOrigin = BaseUriHelper.FixFileUri(new Uri(AppContext.BaseDirectory)); #if DEBUG if (TraceSwitchEnabled) { From 07e62a34ee87f7de15e17f3bedfce76b3edf64c0 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 3 Jan 2025 18:03:37 +0100 Subject: [PATCH 7/7] Final comments --- .../MS/internal/AppModel/SiteOfOriginContainer.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs index c6690d24868..de27d5edc0c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs @@ -18,6 +18,9 @@ internal sealed class SiteOfOriginContainer : Package { private static readonly BooleanSwitch s_traceSwitch = new("SiteOfOrigin", "SiteOfOriginContainer and SiteOfOriginPart trace messages"); + /// + /// Represents the debug trace for and . + /// internal static bool TraceSwitchEnabled { get => s_traceSwitch.Enabled; @@ -55,8 +58,7 @@ internal SiteOfOriginContainer() : base(FileAccess.Read) /// /// If this were to be implemented for http site of origin, it will require a server round trip. /// - /// - /// + /// Regardless of input, always returns . public override bool PartExists(Uri uri) { return true;