Skip to content
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

Remove obsolete/dead code overloads and options from MimeObjectFactory #10227

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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,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.

Expand All @@ -22,15 +22,10 @@ namespace MS.Internal.AppModel
// delgate to close stream.
internal static class AppModelKnownContentFactory
{
// <summary>
// Creates an object instance from a Baml stream and it's Uri
// </summary>
internal static object BamlConverter(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter)
{
return BamlConverterCore(stream, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, false);
}

internal static object BamlConverterCore(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
/// <summary>
/// Creates an object instance from a Baml stream and its Uri
/// </summary>
internal static object BamlConverterCore(Stream stream, Uri baseUri, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
{
asyncObjectConverter = null;
if (isUnsafe)
Expand Down Expand Up @@ -61,15 +56,10 @@ internal static object BamlConverterCore(Stream stream, Uri baseUri, bool canUse
return Application.LoadBamlStreamWithSyncInfo(stream, pc);
}

// <summary>
// Creates an object instance from a Xaml stream and it's Uri
// </summary>
internal static object XamlConverter(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter)
{
return XamlConverterCore(stream, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, false);
}

internal static object XamlConverterCore(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
/// <summary>
/// Creates an object instance from a Xaml stream and its Uri
/// </summary>
internal static object XamlConverterCore(Stream stream, Uri baseUri, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
{
asyncObjectConverter = null;

Expand Down Expand Up @@ -123,22 +113,13 @@ private static void OnParserComplete(object sender, AsyncCompletedEventArgs args
}
}

internal static object HtmlXappConverter(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter)
{
return HtmlXappConverterCore(stream, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, false);
}

internal static object HtmlXappConverterCore(Stream stream, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
internal static object HtmlXappConverterCore(Stream stream, Uri baseUri, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
{
asyncObjectConverter = null;
if (isUnsafe)
{
throw new InvalidOperationException(SR.Format(SR.BamlIsNotSupportedOutsideOfApplicationResources));
}
if (canUseTopLevelBrowser)
{
return null;
}

if (string.Equals(baseUri.Scheme, BaseUriHelper.PackAppBaseUri.Scheme, StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// 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:
Expand All @@ -13,68 +12,35 @@

namespace MS.Internal.AppModel
{
internal delegate object StreamToObjectFactoryDelegate(Stream s, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter);
internal delegate object StreamToObjectFactoryDelegateCore(Stream s, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe);
internal delegate object StreamToObjectFactoryDelegateCore(Stream s, Uri baseUri, bool sandboxExternalContent, bool allowAsync,
bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe);

internal static class MimeObjectFactory
{
//------------------------------------------------------
//
// Internal Static Methods
//
//------------------------------------------------------

#region internal static methods

// The delegate that we are calling is responsible for closing the stream
internal static object GetObjectAndCloseStream(Stream s, ContentType contentType, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter)
{
return GetObjectAndCloseStreamCore(s, contentType, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, false);
}

internal static object GetObjectAndCloseStreamCore(Stream s, ContentType contentType, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
/// <summary>
/// Stores content type along with its factory callback.
/// </summary>
private static readonly Dictionary<ContentType, StreamToObjectFactoryDelegateCore> s_objectConvertersCore = new(9, new ContentType.WeakComparer());

/// <remarks>
/// The delegate that we are calling is responsible for closing the stream
/// </remarks>
internal static object GetObjectAndCloseStreamCore(Stream s, ContentType contentType, Uri baseUri, bool sandboxExternalContent, bool allowAsync,
bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe)
{
object objToReturn = null;
asyncObjectConverter = null;

if (contentType != null)
{
StreamToObjectFactoryDelegateCore d;
if (_objectConvertersCore.TryGetValue(contentType, out d))
{
objToReturn = d(s, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, isUnsafe);
}
}
if (contentType is not null && s_objectConvertersCore.TryGetValue(contentType, out StreamToObjectFactoryDelegateCore callback))
return callback(s, baseUri, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter, isUnsafe);

return objToReturn;
asyncObjectConverter = null;
return null;
}
// The delegate registered here will be responsible for closing the stream passed to it.

/// <remarks>
/// The delegate registered here will be responsible for closing the stream passed to it.
/// </remarks>
internal static void RegisterCore(ContentType contentType, StreamToObjectFactoryDelegateCore method)
{
_objectConvertersCore[contentType] = method;
s_objectConvertersCore[contentType] = method;
}

// The delegate registered here will be responsible for closing the stream passed to it.
internal static void Register(ContentType contentType, StreamToObjectFactoryDelegate method)
{
StreamToObjectFactoryDelegateCore methodCore = new StreamToObjectFactoryDelegateCore((Stream s, Uri baseUri, bool canUseTopLevelBrowser, bool sandboxExternalContent, bool allowAsync, bool isJournalNavigation, out XamlReader asyncObjectConverter, bool isUnsafe) => method(s, baseUri, canUseTopLevelBrowser, sandboxExternalContent, allowAsync, isJournalNavigation, out asyncObjectConverter));
RegisterCore(contentType, methodCore);
}

#endregion


//------------------------------------------------------
//
// Private Members
//
//------------------------------------------------------

#region private members

private static readonly Dictionary<ContentType, StreamToObjectFactoryDelegate> _objectConverters = new Dictionary<ContentType, StreamToObjectFactoryDelegate>(9, new ContentType.WeakComparer());
private static readonly Dictionary<ContentType, StreamToObjectFactoryDelegateCore> _objectConvertersCore = new Dictionary<ContentType, StreamToObjectFactoryDelegateCore>(9, new ContentType.WeakComparer());

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -2003,7 +2003,6 @@ private static void ApplicationInit()
MimeObjectFactory.RegisterCore(MimeTypeMapper.HtmMime, htmlxappFactoryDelegate);
MimeObjectFactory.RegisterCore(MimeTypeMapper.HtmlMime, htmlxappFactoryDelegate);
MimeObjectFactory.RegisterCore(MimeTypeMapper.XbapMime, htmlxappFactoryDelegate);

}

// This function returns the resource stream including resource and content file.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -2925,12 +2925,7 @@ private void GetObjectFromResponse(WebRequest request, WebResponse response, Uri
_webResponse = response;
_asyncObjectConverter = null;


// canUseTopLevelBrowserForHTMLRendering will be true for TopLevel navigation away from browser hosted app. If that is the case
// o will be null.
// We don't support browser hosting since .NET Core 3.0, so therefore canUseTopLevelBrowserForHTMLRendering = false
bool canUseTopLevelBrowserForHTMLRendering = false;
Object o = MimeObjectFactory.GetObjectAndCloseStreamCore(bindStream, contentType, destinationUri, canUseTopLevelBrowserForHTMLRendering, sandBoxContent, true /*allowAsync*/, IsJournalNavigation(navigateInfo), out _asyncObjectConverter, IsUnsafe);
Object o = MimeObjectFactory.GetObjectAndCloseStreamCore(bindStream, contentType, destinationUri, sandBoxContent, allowAsync: true, IsJournalNavigation(navigateInfo), out _asyncObjectConverter, IsUnsafe);

if (o != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -215,11 +215,7 @@ public Uri Source
// MimeObjectFactory.GetObjectAndCloseStream will try to find the object converter basing on the mime type.
// It can be a sync/async converter. It's the converter's responsiblity to close the stream.
// If it fails to find a convert, this call will return null.
System.Windows.Markup.XamlReader asyncObjectConverter;
ResourceDictionary loadedRD = MimeObjectFactory.GetObjectAndCloseStreamCore(s, contentType, uri, false, false, false /*allowAsync*/, false /*isJournalNavigation*/, out asyncObjectConverter, IsUnsafe)
as ResourceDictionary;

if (loadedRD == null)
if (MimeObjectFactory.GetObjectAndCloseStreamCore(s, contentType, uri, false, allowAsync: false, false, out _, IsUnsafe) is not ResourceDictionary loadedRD)
{
throw new InvalidOperationException(SR.Format(SR.ResourceDictionaryLoadFromFailure, _source.ToString()));
}
Expand Down