private static IDataObject GetDataObjectInternal()
        {
            IDataObject    dataObject;
            IComDataObject oleDataObject;

            // Retry OLE operations several times as mitigation for clipboard locking issues in TS sessions.
            // See Dev10 bug 616223 and VSWhidbey bug 476911.

            int i = OleRetryCount;

            while (true)
            {
                oleDataObject = null;
                int hr = OleServicesContext.CurrentOleServicesContext.OleGetClipboard(ref oleDataObject);

                if (NativeMethods.Succeeded(hr))
                {
                    break;
                }

                if (--i == 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                Thread.Sleep(OleRetryDelay);
            }

            if (oleDataObject is IDataObject)
            {
                dataObject = (IDataObject)oleDataObject;
            }
            else if (oleDataObject != null)
            {
                dataObject = new DataObject(oleDataObject);
            }
            else
            {
                dataObject = null;
            }
            // We make this check outside of the loop independant of whether the data is ole data object or IDataObject
            // Although one is unable to create an OleDataObject in partial trust we still need to ensure that if he did
            // we strip the formats we care about by wrapping in ConstrainedDataObject
            if (dataObject != null)
            {
                // this is the case we are concerend about where content comes from partial trust into full trust
                // in the case where data contained is in one of the two formats: XAML or ApplicationTrust we return a wrapper
                // that blocks access to these
                if (IsDataObjectFromLessPriviligedApplicationDomain(dataObject) &&
                    (dataObject.GetDataPresent(DataFormats.Xaml, /*autoConvert:*/ false) ||
                     dataObject.GetDataPresent(DataFormats.ApplicationTrust, /*autoConvert:*/ false)))
                {
                    // in this case we set the data object to be a wrapper data object that blocks off
                    // xaml or application trust formats if they exist
                    dataObject = new ConstrainedDataObject(dataObject);
                }
            }
            return(dataObject);
        }
Exemple #2
0
        private static IDataObject GetDataObjectInternal()
        {
            IDataObject    dataObject;
            IComDataObject oleDataObject;

            // Retry OLE operations several times as mitigation for clipboard locking issues in TS sessions.

            int i = OleRetryCount;

            while (true)
            {
                oleDataObject = null;
                int hr = OleServicesContext.CurrentOleServicesContext.OleGetClipboard(ref oleDataObject);

                if (NativeMethods.Succeeded(hr))
                {
                    break;
                }

                if (--i == 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                Thread.Sleep(OleRetryDelay);
            }

            if (oleDataObject is IDataObject && !Marshal.IsComObject(oleDataObject))
            {
                dataObject = (IDataObject)oleDataObject;
            }
            else if (oleDataObject != null)
            {
                // Wrap any COM objects or objects that don't implement <see cref="T:System.Windows.IDataObject"/>.
                // In the case of COM objects, this protects us from a <see cref="T:System.InvalidOperationException"/> from the marshaler
                // when calling <see cref="M:System.Windows.IDataObject.GetData(T:System.Type)"/> due to <see cref="T:System.Type"/>
                // not being marked with the <see cref="T:System.Runtime.InteropServices.COMVisibleAttribute"/>.
                dataObject = new DataObject(oleDataObject);
            }
            else
            {
                dataObject = null;
            }
            // We make this check outside of the loop independant of whether the data is ole data object or IDataObject
            // Although one is unable to create an OleDataObject in partial trust we still need to ensure that if he did
            // we strip the formats we care about by wrapping in ConstrainedDataObject
            if (dataObject != null)
            {
                // this is the case we are concerend about where content comes from partial trust into full trust
                // in the case where data contained is in one of the two formats: XAML or ApplicationTrust we return a wrapper
                // that blocks access to these
                if (IsDataObjectFromLessPriviligedApplicationDomain(dataObject) &&
                    (dataObject.GetDataPresent(DataFormats.Xaml, /*autoConvert:*/ false) ||
                     dataObject.GetDataPresent(DataFormats.ApplicationTrust, /*autoConvert:*/ false)))
                {
                    // in this case we set the data object to be a wrapper data object that blocks off
                    // xaml or application trust formats if they exist
                    dataObject = new ConstrainedDataObject(dataObject);
                }
            }
            return(dataObject);
        }