From 0a9036e54fc5c609b5a7c5b1405d6473cae5a968 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 11 Oct 2024 18:45:00 +0200 Subject: [PATCH 1/3] Rmove OleCmdHelper/CommandWithArgument classes (XBAP) --- .../MS/Internal/AppModel/OleCmdHelper.cs | 340 ------------------ .../PresentationFramework.csproj | 1 - 2 files changed, 341 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/OleCmdHelper.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/OleCmdHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/OleCmdHelper.cs deleted file mode 100644 index 14b406d4af1..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/OleCmdHelper.cs +++ /dev/null @@ -1,340 +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: -// This is a helper class used for interop to process the -// IOleCommandTarget calls in browser hosting scenario -// - -using System.Collections; -using System.Runtime.InteropServices; - -using System.Windows.Threading; -using System.Windows; -using System.Windows.Input; -using System.Windows.Controls; - -using MS.Internal.Documents; // DocumentApplicationDocumentViewer -using MS.Internal.KnownBoxes; -using MS.Win32; - -namespace MS.Internal.AppModel -{ - #region OleCmdHelper class - // - // OleCmd helper class for processing IOleCommandTarget calls in browser hosting scenario - // - internal sealed class OleCmdHelper : MarshalByRefObject - { - internal const int - OLECMDERR_E_NOTSUPPORTED = unchecked((int)0x80040100), - OLECMDERR_E_DISABLED = unchecked((int)0x80040101), - OLECMDERR_E_UNKNOWNGROUP = unchecked((int)0x80040104); - internal const uint CommandUnsupported = 0; - internal const uint CommandEnabled = (uint)(UnsafeNativeMethods.OLECMDF.OLECMDF_ENABLED | UnsafeNativeMethods.OLECMDF.OLECMDF_SUPPORTED); - internal const uint CommandDisabled = (uint)UnsafeNativeMethods.OLECMDF.OLECMDF_SUPPORTED; - - // IMPORTANT: Keep this in sync with wcp\host\inc\hostservices.idl - internal static readonly Guid CGID_ApplicationCommands = new Guid(0xebbc8a63, 0x8559, 0x4892, 0x97, 0xa8, 0x31, 0xe9, 0xb0, 0xe9, 0x85, 0x91); - internal static readonly Guid CGID_EditingCommands = new Guid(0xc77ce45, 0xd1c, 0x4f2a, 0xb2, 0x93, 0xed, 0xd5, 0xe2, 0x7e, 0xba, 0x47); - - internal OleCmdHelper() - { - } - - /// - /// The native code passes queries here only for the recognized command groups: - /// standard (NULL), ApplicaitonCommands, EditingCommands. - /// - internal void QueryStatus(Guid guidCmdGroup, uint cmdId, ref uint flags) - { - /***IMPORTANT: - Make sure to return allowed and appropriate values according to the specification of - IOleCommandTarget::QueryStatus(). In particular: - - OLECMDF_SUPPORTED without OLECMDF_ENABLED should not be blindly returned for - unrecognized commands. - - Some code in IE treats OLECMDERR_E_xxx differently from generic failures. - - E_NOTIMPL is not an acceptable return value. - */ - - if (Application.Current == null || Application.IsShuttingDown == true) - { - Marshal.ThrowExceptionForHR(NativeMethods.E_FAIL); - } - - // Get values from mapping here else mark them as disabled ==> - // i.e "supported but not enabled" and is the equivalent of disabled since - // there is no explicit "disabled" OLECMD flag - - IDictionary oleCmdMappingTable = GetOleCmdMappingTable(guidCmdGroup); - if (oleCmdMappingTable == null) - { - Marshal.ThrowExceptionForHR(OleCmdHelper.OLECMDERR_E_UNKNOWNGROUP); - } - CommandWithArgument command = oleCmdMappingTable[cmdId] as CommandWithArgument; - if (command == null) - { - flags = CommandUnsupported; - return; - } - // Go through the Dispatcher in order to use its SynchronizationContext and also - // so that any application exception caused during event routing is reported via - // Dispatcher.UnhandledException. - // The above code is not in the callback, because it throws, and we don't want the - // application to get these exceptions. (The COM Interop layer turns them into HRESULTs.) - bool enabled = (bool)Application.Current.Dispatcher.Invoke( - DispatcherPriority.Send, new DispatcherOperationCallback(QueryEnabled), command); - flags = enabled ? CommandEnabled : CommandDisabled; - } - - private object QueryEnabled(object command) - { - if (Application.Current.MainWindow == null) - return false; - IInputElement target = FocusManager.GetFocusedElement(Application.Current.MainWindow); - if (target == null) - { - // This will always succeed because Window is IInputElement - target = (IInputElement)Application.Current.MainWindow; - } - return BooleanBoxes.Box(((CommandWithArgument)command).QueryEnabled(target, null)); - } - - /// - /// The native code passes here only commands of the recognized command groups: - /// standard (NULL), ApplicaitonCommands, EditingCommands. - /// - internal void ExecCommand(Guid guidCmdGroup, uint commandId, object arg) - { - if (Application.Current == null || Application.IsShuttingDown == true) - { - Marshal.ThrowExceptionForHR(NativeMethods.E_FAIL); - } - - int hresult = (int)Application.Current.Dispatcher.Invoke( - DispatcherPriority.Send, - new DispatcherOperationCallback(ExecCommandCallback), - new object[] { guidCmdGroup, commandId, arg }); - // Note: ExecCommandCallback() returns an HRESULT instead of throwing for the reason - // explained in QueryStatus(). - if (hresult < 0) - { - Marshal.ThrowExceptionForHR(hresult); - } - } - - private object ExecCommandCallback(object arguments) - { - object[] args = (object[])arguments; - Invariant.Assert(args.Length == 3); - Guid guidCmdGroup = (Guid)args[0]; - uint commandId = (uint)args[1]; - object arg = args[2]; - - IDictionary oleCmdMappingTable = GetOleCmdMappingTable(guidCmdGroup); - if (oleCmdMappingTable == null) - return OLECMDERR_E_UNKNOWNGROUP; - CommandWithArgument command = oleCmdMappingTable[commandId] as CommandWithArgument; - if (command == null) - return OLECMDERR_E_NOTSUPPORTED; - - if (Application.Current.MainWindow == null) - return OLECMDERR_E_DISABLED; - IInputElement target = FocusManager.GetFocusedElement(Application.Current.MainWindow); - if (target == null) - { - // This will always succeed because Window is IInputElement - target = (IInputElement)Application.Current.MainWindow; - } - return command.Execute(target, arg) ? NativeMethods.S_OK : OLECMDERR_E_DISABLED; - } - - private IDictionary GetOleCmdMappingTable(Guid guidCmdGroup) - { - IDictionary mappingTable = null; - - if (guidCmdGroup.Equals(CGID_ApplicationCommands)) - { - EnsureApplicationCommandsTable(); - mappingTable = _applicationCommandsMappingTable; - } - else if (guidCmdGroup.Equals(Guid.Empty)) - { - EnsureOleCmdMappingTable(); - mappingTable = _oleCmdMappingTable; - } - else if (guidCmdGroup.Equals(CGID_EditingCommands)) - { - EnsureEditingCommandsTable(); - mappingTable = _editingCommandsMappingTable; - } - - return mappingTable; - } - private void EnsureOleCmdMappingTable() - { - if (_oleCmdMappingTable == null) - { - _oleCmdMappingTable = new SortedList(10); - - //Add applevel commands here - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_SAVE, new CommandWithArgument(ApplicationCommands.Save)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_SAVEAS, new CommandWithArgument(ApplicationCommands.SaveAs)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_PRINT, new CommandWithArgument(ApplicationCommands.Print)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_CUT, new CommandWithArgument(ApplicationCommands.Cut)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_COPY, new CommandWithArgument(ApplicationCommands.Copy)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_PASTE, new CommandWithArgument(ApplicationCommands.Paste)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_PROPERTIES, new CommandWithArgument(ApplicationCommands.Properties)); - - //Set the Enabled property of Stop and Refresh commands correctly - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_REFRESH, new CommandWithArgument(NavigationCommands.Refresh)); - _oleCmdMappingTable.Add((uint)UnsafeNativeMethods.OLECMDID.OLECMDID_STOP, new CommandWithArgument(NavigationCommands.BrowseStop)); - } - } - - private void EnsureApplicationCommandsTable() - { - if (_applicationCommandsMappingTable == null) - { - /* we want to possible add 26 entries, so the capacity should be - * 26/0.72 = 19 for default of 1.0 load factor*/ - _applicationCommandsMappingTable = new Hashtable(19); - - //Add applevel commands here - // Note: The keys are added as uint type so that the default container comparer works - // when we try to look up a command by a uint cmdid. - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Cut, new CommandWithArgument(ApplicationCommands.Cut)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Copy, new CommandWithArgument(ApplicationCommands.Copy)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Paste, new CommandWithArgument(ApplicationCommands.Paste)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_SelectAll, new CommandWithArgument(ApplicationCommands.SelectAll)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Find, new CommandWithArgument(ApplicationCommands.Find)); - - // Add standard navigation commands - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Refresh, new CommandWithArgument(NavigationCommands.Refresh)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Stop, new CommandWithArgument(NavigationCommands.BrowseStop)); - - // add document viewer commands - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Digitalsignatures_SignDocument, new CommandWithArgument(DocumentApplicationDocumentViewer.Sign)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Digitalsignatures_RequestSignature, new CommandWithArgument(DocumentApplicationDocumentViewer.RequestSigners)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Digitalsignatures_ViewSignature, new CommandWithArgument(DocumentApplicationDocumentViewer.ShowSignatureSummary)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Permission_Set, new CommandWithArgument(DocumentApplicationDocumentViewer.ShowRMPublishingUI)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Permission_View, new CommandWithArgument(DocumentApplicationDocumentViewer.ShowRMPermissions)); - _applicationCommandsMappingTable.Add((uint)AppCommands.Edit_Permission_Restrict, new CommandWithArgument(DocumentApplicationDocumentViewer.ShowRMCredentialManager)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_In, new CommandWithArgument(NavigationCommands.IncreaseZoom)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_Out, new CommandWithArgument(NavigationCommands.DecreaseZoom)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_400, new CommandWithArgument(NavigationCommands.Zoom, 400)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_250, new CommandWithArgument(NavigationCommands.Zoom, 250)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_150, new CommandWithArgument(NavigationCommands.Zoom, 150)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_100, new CommandWithArgument(NavigationCommands.Zoom, 100)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_75, new CommandWithArgument(NavigationCommands.Zoom, 75)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_50, new CommandWithArgument(NavigationCommands.Zoom, 50)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_25, new CommandWithArgument(NavigationCommands.Zoom, 25)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_PageWidth, new CommandWithArgument(DocumentViewer.FitToWidthCommand)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_WholePage, new CommandWithArgument(DocumentViewer.FitToHeightCommand)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_TwoPages, new CommandWithArgument(DocumentViewer.FitToMaxPagesAcrossCommand, 2)); - _applicationCommandsMappingTable.Add((uint)AppCommands.View_Zoom_Thumbnails, new CommandWithArgument(DocumentViewer.ViewThumbnailsCommand)); - } - } - - private void EnsureEditingCommandsTable() - { - if (_editingCommandsMappingTable == null) - { - _editingCommandsMappingTable = new SortedList(2); - // Note: The keys are added as uint type so that the default container comparer works - // when we try to look up a command by a uint cmdid. - _editingCommandsMappingTable.Add((uint)EditingCommandIds.Backspace, - new CommandWithArgument(System.Windows.Documents.EditingCommands.Backspace)); - _editingCommandsMappingTable.Add((uint)EditingCommandIds.Delete, - new CommandWithArgument(System.Windows.Documents.EditingCommands.Delete)); - } - } - - private SortedList _oleCmdMappingTable; - private Hashtable _applicationCommandsMappingTable; - private SortedList _editingCommandsMappingTable; - } - #endregion OleCmdHelper class - - #region CommandAndArgument class - - /// - /// This wrapper class helps store default arguments for commands. - /// The primary scenario for this class is the Zoom command where we - /// have multiple menu items and want to fire a single event with an - /// argument. We cannot attach an argument value to the native menu - /// item so when we do the translation we add it. - /// - internal class CommandWithArgument - { - public CommandWithArgument(RoutedCommand command) : this(command, null) - { } - - public CommandWithArgument(RoutedCommand command, object argument) - { - _command = command; - _argument = argument; - } - - public bool Execute(IInputElement target, object argument) - { - if (argument == null) - { - argument = _argument; - } - - // ISecureCommand is used to enforce user-initiated invocation. Cut, Copy and Paste - // are marked as such. See ApplicationCommands.GetRequiredPermissions. - if (_command is ISecureCommand) - { - bool unused; - if (_command.CriticalCanExecute(argument, target, /* trusted: */ true, out unused)) - { - _command.ExecuteCore(argument, target, /* userInitiated: */ true); - return true; - } - return false; - } - if (_command.CanExecute(argument, target)) - { - _command.Execute(argument, target); - return true; - } - return false; - } - - - public bool QueryEnabled(IInputElement target, object argument) - { - if (argument == null) - { - argument = _argument; - } - - // ISecureCommand is used to enforce user-initiated invocation. Cut, Copy and Paste - // are marked as such. See ApplicationCommands.GetRequiredPermissions. - if (_command is ISecureCommand) - { - bool unused; - return _command.CriticalCanExecute(argument, target, /* trusted: */ true, out unused); - } - return _command.CanExecute(argument, target); - } - - public RoutedCommand Command - { - get - { - return _command; - } - } - - private object _argument; - - private RoutedCommand _command; - } - - #endregion -} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj index 5b90421c0a6..a16304803fb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj @@ -119,7 +119,6 @@ - From 4fcce717564942623857633826c29d3b7c636769 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 11 Oct 2024 18:46:33 +0200 Subject: [PATCH 2/3] Remove obsolete enums from IBrowserHostServices --- .../Internal/AppModel/IBrowserHostServices.cs | 60 ------------------- 1 file changed, 60 deletions(-) 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 index 4723b17883d..387755512d8 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/IBrowserHostServices.cs @@ -61,64 +61,4 @@ internal enum MimeType Application = 2, Markup = 3 } - - //********************************************************************************************// - // IMPORTANT: IMPORTANT: IMPORTANT: IMPORTANT: // - //********************************************************************************************// - //Start with 8001 , the enum defined on the managed world starts with 8001 as well - //KEEP THESE IN SYNC - //The ApplicationCommands enums in wcp\host\inc\hostservices.idl and IBrowserHostServices.cs - //and the menuIDs wcp\host\docobj\resource.hxx and resources.rc - // - internal enum AppCommands - { - Edit_Cut = 8001, - Edit_Copy, - Edit_Paste, - Edit_SelectAll, - Edit_Find, - - Edit_Digitalsignatures, - Edit_Digitalsignatures_SignDocument, - Edit_Digitalsignatures_RequestSignature, - Edit_Digitalsignatures_ViewSignature, - - Edit_Permission, - Edit_Permission_Set, - Edit_Permission_View, - Edit_Permission_Restrict, - - View_StatusBar, - View_Stop, - View_Refresh, - View_FullScreen, - - View_Zoom, - View_Zoom_In, - View_Zoom_Out, - View_Zoom_400, - View_Zoom_250, - View_Zoom_150, - View_Zoom_100, - View_Zoom_75, - View_Zoom_50, - View_Zoom_25, - View_Zoom_PageWidth, - View_Zoom_WholePage, - View_Zoom_TwoPages, - View_Zoom_Thumbnails, - } - - internal enum AppMenus - { - EditMenu = 0x3020, - ViewMenu = 0x3040 - } - - //***Keep in sync with host\Inc\HostServices.idl. - internal enum EditingCommandIds : uint - { - Backspace = 1, - Delete = 2 - }; } From bfa87124dd834af315a99c02293e1215ee0d7a73 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 11 Oct 2024 19:21:37 +0200 Subject: [PATCH 3/3] Remove ISecureCommand / SecureUICommand dead code --- .../PresentationCore/PresentationCore.csproj | 2 -- .../Windows/Input/Command/ISecureCommand.cs | 31 ---------------- .../Windows/Input/Command/SecureUICommand.cs | 35 ------------------- 3 files changed, 68 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/ISecureCommand.cs delete mode 100644 src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/SecureUICommand.cs diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj index df8afb8ed48..643b9b3f32b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj @@ -612,7 +612,6 @@ - @@ -628,7 +627,6 @@ - diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/ISecureCommand.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/ISecureCommand.cs deleted file mode 100644 index b29e4dc692a..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/ISecureCommand.cs +++ /dev/null @@ -1,31 +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: ISecureCommand enables a command to specify that calls -// must have a specific permission to modify the bindings -// associated with that command. That permission will -// then be asserted when the command is invoked in a user -// interactive (trusted) way. -// -// - -using System.ComponentModel; -using MS.Internal.PresentationCore; - -namespace System.Windows.Input -{ - /// - /// ISecureCommand enables a command to specify that calls - /// must have a specific permission to modify the bindings - /// associated with that command. That permission will - /// then be asserted when the command is invoked in a user - /// interactive (trusted) way. - /// - [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=" + BuildInfo.WCP_VERSION + ", Culture=neutral, PublicKeyToken=" + BuildInfo.WCP_PUBLIC_KEY_TOKEN + ", Custom=null")] - internal interface ISecureCommand : ICommand - { - } -} diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/SecureUICommand.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/SecureUICommand.cs deleted file mode 100644 index 8b3f210b055..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/SecureUICommand.cs +++ /dev/null @@ -1,35 +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: The Command class is used by the developer to define the intent of the User Action -// This also serves the purpose of identifying commands or to compare identities of -// InputBindings and CommandBindings -// - -using System.ComponentModel; -using MS.Internal.PresentationCore; - -namespace System.Windows.Input -{ - /// - /// Command - /// - [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=" + BuildInfo.WCP_VERSION + ", Culture=neutral, PublicKeyToken=" + BuildInfo.WCP_PUBLIC_KEY_TOKEN + ", Custom=null")] - internal class SecureUICommand : RoutedUICommand, ISecureCommand - { - /// - /// Creates a new secure command, requiring the specified permissions. Used to delay initialization of Text and InputGestureCollection to time of first use. - /// - /// Name of the Command Property/Field for Serialization - /// Type that is registering the property - /// Idenfier assigned by the owning type. - internal SecureUICommand(string name, Type ownerType, byte commandId) - : base(name, ownerType, commandId) - { - } - } - }