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

minor cleanup and improved a message in clipboard APIs #12759

Merged
merged 3 commits into from
Jan 17, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/System.Windows.Forms/src/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@

// Ideally these should be different exceptions, but leaving them as shipped for compatibility
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.DataObject.Composition.NativeToWinFormsAdapter.ReadByteStreamFromHGLOBAL(Windows.Win32.Foundation.HGLOBAL,System.Boolean@)~System.IO.MemoryStream")]
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.DataObject.Composition.NativeToRuntimeAdapter.System#Runtime#InteropServices#ComTypes#IDataObject#EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR)~System.Runtime.InteropServices.ComTypes.IEnumFORMATETC")]
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.DataObject.Composition.NativeToRuntimeAdapter.EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR)~System.Runtime.InteropServices.ComTypes.IEnumFORMATETC")]
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.Clipboard.SetDataObject(System.Object,System.Boolean,System.Int32,System.Int32)")]
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.ComponentModel.Com2Interop.Com2TypeInfoProcessor.ProcessFunctions(Windows.Win32.System.Com.ITypeInfo*,System.Collections.Generic.IDictionary{System.String,System.Windows.Forms.ComponentModel.Com2Interop.Com2TypeInfoProcessor.PropertyInfo},System.Int32,System.Int32,System.Boolean@)")]
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Forms.Control.ActiveXImpl.ThrowHr(Windows.Win32.Foundation.HRESULT)")]
Expand Down
3 changes: 3 additions & 0 deletions src/System.Windows.Forms/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7042,4 +7042,7 @@ Stack trace where the illegal operation occurred was:
<data name="ClipboardOrDragDrop_CannotJsonSerializeDataObject">
<value>An object of type 'DataObject' will serialize as empty. Use 'DataObject.SetDataAsJson' APIs on your object to JSON-serialize the data within your object, then use the '{0}' API with your object.</value>
</data>
<data name="NotSupportedExceptionOnClipboard">
<value>Clipboard contains a 'NotSupportedException' that indicates that write operation failed. '{0}'.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,14 @@ public static bool ContainsText(TextDataFormat format)
{
data = default;
resolver.OrThrowIfNull();
if (!DataObject.IsValidFormatAndType<T>(format)
|| GetDataObject() is not { } dataObject)
{
// Invalid format or no object on the clipboard at all.
return false;
}

return GetTypedDataObject<T>(format, out ITypedDataObject? typed)
&& typed.TryGetData(format, resolver, autoConvert: false, out data);
return dataObject.TryGetData(format, resolver, autoConvert: false, out data);
}

/// <inheritdoc cref="TryGetData{T}(string, Func{TypeName, Type}, out T)"/>
Expand All @@ -373,30 +378,14 @@ public static bool ContainsText(TextDataFormat format)
[NotNullWhen(true), MaybeNullWhen(false)] out T data)
{
data = default;

return GetTypedDataObject<T>(format, out ITypedDataObject? typed) && typed.TryGetData(format, out data);
}

private static bool GetTypedDataObject<T>(
string format,
[NotNullWhen(true), MaybeNullWhen(false)] out ITypedDataObject typed)
{
typed = default;
if (!DataObject.IsValidFormatAndType<T>(format)
|| GetDataObject() is not { } dataObject)
{
// Invalid format or no object on the clipboard at all.
return false;
}

if (dataObject is not ITypedDataObject typedDataObject)
{
throw new NotSupportedException(
string.Format(SR.ITypeDataObject_Not_Implemented, dataObject.GetType().FullName));
}

typed = typedDataObject;
return true;
return dataObject.TryGetData(format, out data);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ record = stream.Decode(out recordMap);
&& record.TryGetNotSupportedException(out object? @object)
&& @object is NotSupportedException exception)
{
throw new NotSupportedException(exception.Message);
throw new NotSupportedException(string.Format(SR.NotSupportedExceptionOnClipboard, exception.Message));
}

return null;
Expand All @@ -135,7 +135,7 @@ record = stream.Decode(out recordMap);
typeof(T).FullName));
}

// NRBF deserializer is more secure than the BinaryFormatter is:
// NRBF deserializer fixes some known BinaryFormatter issues:
// 1. Doesn't allow arrays that have a non-zero base index (can't create these in C# or VB)
// 2. Only allows IObjectReference types that contain primitives (to avoid observable cycle
// dependencies to indeterminate state)
Expand Down
Loading