Skip to content

Commit

Permalink
Remove XBAP-related dead code from BrowserInteropHelper/SafeSecurityH…
Browse files Browse the repository at this point in the history
…elper (#9855)

* Remove XBAP-related dead code from BrowserInteropHelper

* Remove IHTMLWindow4 interface import

* Remove IsHostedInIEorWebOC and related code (as it is always false)

* Remove HostHtmlDocumentServiceProvider as it is always NULL

* Hollow out DynamicScriptObject as instances were provided via BrowserInteropHelper
  • Loading branch information
h3xds1nz authored Nov 8, 2024
1 parent 24105c1 commit c0b716b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 821 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@

//
// Description: Implements Avalon BrowserInteropHelper class, which helps
// interop with the browser
// interop with the browser. Deprecated as XBAP is not supported.
//

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Security;
using System.Diagnostics;
using MS.Internal;
using MS.Win32;
using System.Windows.Input;
using MS.Internal;
using MS.Internal.AppModel;
using MS.Internal.Interop;
using System.Threading;

using SafeSecurityHelper=MS.Internal.PresentationFramework.SafeSecurityHelper;

namespace System.Windows.Interop
{
Expand All @@ -38,20 +26,7 @@ static BrowserInteropHelper()
/// <summary>
/// Returns the IOleClientSite interface
/// </summary>
/// <remarks>
/// Callers must have UnmanagedCode permission to call this API.
/// </remarks>
public static object ClientSite
{
get
{

object oleClientSite = null;


return oleClientSite;
}
}
public static object ClientSite => null;

/// <summary>
/// Gets a script object that provides access to the HTML window object,
Expand All @@ -71,12 +46,6 @@ public static object ClientSite
/// That's why they are still separate. Also, this one is public.
/// </remarks>
public static bool IsBrowserHosted => false;

internal static HostingFlags HostingFlags
{
get { return _hostingFlags; }
set { _hostingFlags = value; }
}

/// <summary>
/// Returns the Uri used to launch the application.
Expand All @@ -97,21 +66,7 @@ internal static bool IsViewer
{
get
{
Application app = Application.Current;
return app != null && app.MimeType == MimeType.Markup;
}
}


/// <summary>
/// Returns true if the host browser is IE or the WebOC hosted in a standalone application.
/// Also returns false if not browser-hosted.
/// </summary>
internal static bool IsHostedInIEorWebOC
{
get
{
return (HostingFlags & HostingFlags.hfHostedInIEorWebOC) != 0;
return Application.Current?.MimeType == MimeType.Markup;
}
}

Expand All @@ -132,115 +87,7 @@ internal static bool IsInitialViewerNavigation
}
}

/// <summary>
/// Retrieves the IServiceProvider object for the browser we're hosted in.
/// This is used for IDispatchEx operations in the script interop feature.
/// Critical: Returns critical type UnsafeNativeMethods.IServiceProvider
/// </summary>
internal static UnsafeNativeMethods.IServiceProvider HostHtmlDocumentServiceProvider
{
get
{
// HostHtmlDocumentServiceProvider is used by DynamicScriptObject for IDispatchEx
// operations in case of IE. During initialization we get the document node from
// the DOM to obtain the required service provider. During this short timeframe
// it's valid to have a null-value returned here. In all other cases, we make sure
// not to run with a null service provider for sake of security.
// See InitializeHostHtmlDocumentServiceProvider as well.
/* TODO: straighten this with browser flags to check only when hosted in IE */
Invariant.Assert(!(_initializedHostScript && _hostHtmlDocumentServiceProvider == null));

return _hostHtmlDocumentServiceProvider;
}
}

private static void InitializeHostHtmlDocumentServiceProvider(DynamicScriptObject scriptObject)
{
// The service provider is used for Internet Explorer IDispatchEx use.
if ( IsHostedInIEorWebOC
&& scriptObject.ScriptObject is UnsafeNativeMethods.IHTMLWindow4
&& _hostHtmlDocumentServiceProvider == null)
{
// We use the IDispatch infrastructure to gain access to the document DOM node where
// the IServiceProvider lives that was recommended to us by IE people. Notice during
// this call the HostHtmlDocumentServiceProvider property here is still null, so this
// first request is made (and succeeds properly) without the service provider in place.
object document;
bool foundDoc = scriptObject.TryFindMemberAndInvokeNonWrapped("document",
NativeMethods.DISPATCH_PROPERTYGET,
true /* cache DISPID, why not? */,
null, /* arguments */
out document);

// The fact the host script is required to be a IHTMLWindow4 here ensures there is
// document property and because we're dealing with IE, we know it has a service
// provider on it.
Invariant.Assert(foundDoc);
_hostHtmlDocumentServiceProvider = (UnsafeNativeMethods.IServiceProvider)document;

// See HostHtmlDocumentServiceProvider property get accessor for more information on the use of
// this field to ensure we got a valid service provider.
_initializedHostScript = true;
}
}

private static void HostFilterInput(ref MSG msg, ref bool handled)
{
WindowMessage message = (WindowMessage)msg.message;
// The host gets to see input, keyboard and mouse messages.
if (message == WindowMessage.WM_INPUT ||
(message >= WindowMessage.WM_KEYFIRST && message <= WindowMessage.WM_IME_KEYLAST) ||
(message >= WindowMessage.WM_MOUSEFIRST && message <= WindowMessage.WM_MOUSELAST))
{
if (MS.Win32.NativeMethods.S_OK == ForwardTranslateAccelerator(ref msg, false))
{
handled = true;
}
}
}

/// <summary> This hook gets a "last chance" to handle a key. Such applicaton-unhandled
/// keys are forwarded to the browser frame.
/// </summary>
internal static IntPtr PostFilterInput(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (!handled)
{
if ((WindowMessage)msg >= WindowMessage.WM_KEYFIRST && (WindowMessage)msg <= WindowMessage.WM_IME_KEYLAST)
{
MSG m = new MSG(hwnd, msg, wParam, lParam, SafeNativeMethods.GetMessageTime(), 0, 0);
if (MS.Win32.NativeMethods.S_OK == ForwardTranslateAccelerator(ref m, true))
{
handled = true;
}
}
}
return IntPtr.Zero;
}

internal static void InitializeHostFilterInput()
{
ComponentDispatcher.ThreadFilterMessage +=
new ThreadMessageEventHandler(HostFilterInput);
}

private static void EnsureScriptInteropAllowed()
{
if (_isScriptInteropDisabled == null)
{
_isScriptInteropDisabled = SafeSecurityHelper.IsFeatureDisabled(SafeSecurityHelper.KeyToRead.ScriptInteropDisable);
}
}

private static HostingFlags _hostingFlags;
private static bool _isInitialViewerNavigation;
private static bool? _isScriptInteropDisabled;

private static UnsafeNativeMethods.IServiceProvider _hostHtmlDocumentServiceProvider;
private static bool _initializedHostScript;

[DllImport(ExternDll.PresentationHostDll, EntryPoint="ForwardTranslateAccelerator")]
private static extern int ForwardTranslateAccelerator(ref MSG pMsg, bool appUnhandled);
}
}

Loading

0 comments on commit c0b716b

Please sign in to comment.