mediapermission.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / System / Security / permissions / mediapermission.cs / 1305600 / mediapermission.cs

                            //---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// Description: 
//              The MediaPermission controls the ability to create rich Media in Avalon. 
//
// 
// History:
//  06/07/05: marka     Created.
//---------------------------------------------------------------------------
 
using System;
using System.Security; 
using System.Security.Permissions; 
using System.IO;
using System.Runtime.Serialization; 
using System.Collections;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Windows; 
using MS.Internal.WindowsBase;
 
namespace System.Security.Permissions 
{
 
    ///
    ///     Enum of audio permission levels.
    ///
    public enum MediaPermissionAudio 
    {
        ///  
        ///     NoAudio - no sound allowed to play. 
        /// 
        NoAudio, 

        /// 
        ///     SiteOfOriginAudio - only allow audio from site of origin.
        ///  
        SiteOfOriginAudio,
 
        ///  
        ///     SafeAudio - allowed to play audio with some restrictions.
        ///  
        SafeAudio,

        /// 
        ///     Allowed to play audio with no restrictions 
        /// 
        AllAudio 
 
    }
 
    ///
    ///     Enum of video permission levels.
    ///
    public enum MediaPermissionVideo 
    {
        ///  
        ///     NoVideo - no video allowed to play. 
        /// 
        NoVideo, 

        /// 
        ///     SiteOfOriginVideo - only allow video from site of origin.
        ///  
        SiteOfOriginVideo,
 
        ///  
        ///     SafeVideo - allowed to play video with some restrictions.
        ///  
        SafeVideo,

        /// 
        ///     allowed to play video with no restrictions 
        /// 
        AllVideo, 
 
    }
 
    ///
    ///     Enum of image permission levels.
    ///
    public enum MediaPermissionImage 
    {
        ///  
        ///     NoImage - no images allowed to display 
        /// 
        NoImage, 

        /// 
        ///     SiteOfOriginImage -only allow image from site of origin.
        ///  
        SiteOfOriginImage,
 
        ///  
        ///     SafeImage - allowed to display images with some restrictions.
        ///                 Only certified codecs allowed. 
        /// 
        SafeImage,

        ///  
        ///     Allowed to display images with no restrictions.
        ///  
        AllImage, 

    } 

    ///
    ///              The MediaPermission controls the ability for richMedia to work in partial trust.
    /// 
    ///              There are 3 enum values that control the type of media that can work.
    /// 
    ///              MediaPermissionAudio - controls the level of audio support. 
    ///              MediaPermissionVideo - controls the level of video supported.
    ///              MeidaPermissionImage - controls the level of image display supported. 
    ///
    [Serializable()]
    sealed public class MediaPermission : CodeAccessPermission, IUnrestrictedPermission
    { 
        //-----------------------------------------------------
        // 
        //  Constructors 
        //
        //----------------------------------------------------- 

        #region Constructors

        /// 
        ///     MediaPermission ctor.
        /// 
        public MediaPermission() 
        {
            InitDefaults(); 
        }

        ///
        ///     MediaPermission ctor. 
        ///
        public MediaPermission(PermissionState state) 
        { 
            if (state == PermissionState.Unrestricted)
            { 
                _mediaPermissionAudio = MediaPermissionAudio.AllAudio;
                _mediaPermissionVideo = MediaPermissionVideo.AllVideo;
                _mediaPermissionImage = MediaPermissionImage.AllImage;
            } 
            else if (state == PermissionState.None)
            { 
                _mediaPermissionAudio = MediaPermissionAudio.NoAudio; 
                _mediaPermissionVideo = MediaPermissionVideo.NoVideo;
                _mediaPermissionImage = MediaPermissionImage.NoImage; 
            }
            else
            {
                throw new ArgumentException( SR.Get(SRID.InvalidPermissionState) ); 
            }
        } 
 
        ///
        ///     MediaPermission ctor. 
        ///
        public MediaPermission(MediaPermissionAudio permissionAudio )
        {
            VerifyMediaPermissionAudio( permissionAudio ) ; 
            InitDefaults();
 
            _mediaPermissionAudio = permissionAudio ; 
        }
 
        ///
        ///     MediaPermission ctor.
        ///
        public MediaPermission(MediaPermissionVideo permissionVideo ) 
        {
            VerifyMediaPermissionVideo( permissionVideo ) ; 
            InitDefaults(); 

            _mediaPermissionVideo = permissionVideo ; 
        }

        ///
        ///     MediaPermission ctor. 
        ///
        public MediaPermission(MediaPermissionImage permissionImage ) 
        { 
            VerifyMediaPermissionImage( permissionImage );
            InitDefaults(); 

            _mediaPermissionImage = permissionImage ;
        }
 
        ///
        ///     MediaPermission ctor. 
        /// 
        public MediaPermission(MediaPermissionAudio permissionAudio,
                                      MediaPermissionVideo permissionVideo, 
                                      MediaPermissionImage permissionImage )
        {
            VerifyMediaPermissionAudio( permissionAudio );
            VerifyMediaPermissionVideo( permissionVideo ); 
            VerifyMediaPermissionImage( permissionImage );
 
            _mediaPermissionAudio = permissionAudio ; 
            _mediaPermissionVideo = permissionVideo ;
            _mediaPermissionImage = permissionImage ; 
        }

        #endregion Constructors
 
        //------------------------------------------------------
        // 
        //  Public Methods 
        //
        //----------------------------------------------------- 

        #region Public Methods

 
        //
        // IUnrestrictedPermission implementation 
        // 

        /// 
        ///     Is this an unrestricted permisison ?
        ///
        public bool IsUnrestricted()
        { 
            return EqualsLevel( MediaPermissionAudio.AllAudio ,
                                 MediaPermissionVideo.AllVideo, 
                                 MediaPermissionImage.AllImage ) ; 
        }
 
        //
        // CodeAccessPermission implementation
        //
 

        /// 
        ///     Is this a subsetOf the target ? 
        ///
        public override bool IsSubsetOf(IPermission target) 
        {
            if (target == null)
            {
                return EqualsLevel( MediaPermissionAudio.NoAudio, 
                                     MediaPermissionVideo.NoVideo,
                                     MediaPermissionImage.NoImage ) ; 
            } 

            MediaPermission operand = target as MediaPermission ; 
            if ( operand != null )
            {
                return ( ( this._mediaPermissionAudio <= operand._mediaPermissionAudio) &&
                          ( this._mediaPermissionVideo <= operand._mediaPermissionVideo ) && 
                          ( this._mediaPermissionImage <= operand._mediaPermissionImage ) ) ;
            } 
            else 
            {
                throw new ArgumentException(SR.Get(SRID.TargetNotMediaPermissionLevel)); 
            }
        }

        /// 
        ///     Return the intersection with the target
        /// 
        public override IPermission Intersect(IPermission target) 
        {
            if (target == null) 
            {
                return null;
            }
 
            MediaPermission operand = target as MediaPermission ;
            if ( operand != null ) 
            { 
                //
                // Construct a permission that is the aggregate of the 
                // least priveleged level of the 3 enums.
                //
                MediaPermissionAudio audioIntersectLevel = _mediaPermissionAudio < operand._mediaPermissionAudio
                                                                ? _mediaPermissionAudio : operand._mediaPermissionAudio; 

                MediaPermissionVideo videoIntersectLevel = _mediaPermissionVideo < operand._mediaPermissionVideo 
                                                                ? _mediaPermissionVideo : operand._mediaPermissionVideo; 

                MediaPermissionImage imageIntersectLevel = _mediaPermissionImage < operand._mediaPermissionImage 
                                                                ? _mediaPermissionImage : operand._mediaPermissionImage ;

                if ( ( audioIntersectLevel == MediaPermissionAudio.NoAudio ) &&
                     ( videoIntersectLevel == MediaPermissionVideo.NoVideo ) && 
                     ( imageIntersectLevel == MediaPermissionImage.NoImage ) )
 
                { 
                    return null;
                } 
                else
                {
                    return new MediaPermission( audioIntersectLevel, videoIntersectLevel, imageIntersectLevel ) ;
                } 
            }
            else 
            { 
                throw new ArgumentException(SR.Get(SRID.TargetNotMediaPermissionLevel));
            } 
        }

        ///
        ///     Return the Union with the target 
        ///
        public override IPermission Union(IPermission target) 
        { 
            if (target == null)
            { 
                return this.Copy();
            }

            MediaPermission operand = target as MediaPermission ; 
            if ( operand != null )
            { 
                // 
                // Construct a permission that is the aggregate of the
                // most priveleged level of the 3 enums. 
                //
                MediaPermissionAudio audioUnionLevel = _mediaPermissionAudio > operand._mediaPermissionAudio
                                                                ? _mediaPermissionAudio : operand._mediaPermissionAudio;
 
                MediaPermissionVideo videoUnionLevel = _mediaPermissionVideo > operand._mediaPermissionVideo
                                                                ? _mediaPermissionVideo : operand._mediaPermissionVideo; 
 
                MediaPermissionImage imageUnionLevel = _mediaPermissionImage > operand._mediaPermissionImage
                                                                ? _mediaPermissionImage : operand._mediaPermissionImage ; 

                if ( ( audioUnionLevel == MediaPermissionAudio.NoAudio ) &&
                     ( videoUnionLevel == MediaPermissionVideo.NoVideo ) &&
                     ( imageUnionLevel == MediaPermissionImage.NoImage ) ) 
                {
                    return null; 
                } 
                else
                { 
                    return new MediaPermission( audioUnionLevel, videoUnionLevel, imageUnionLevel ) ;
                }
            }
            else 
            {
                throw new ArgumentException(SR.Get(SRID.TargetNotMediaPermissionLevel)); 
            } 
        }
 
        ///
        ///     Copy this permission.
        ///
        public override IPermission Copy() 
        {
            return new MediaPermission( 
                                        this._mediaPermissionAudio, 
                                        this._mediaPermissionVideo,
                                        this._mediaPermissionImage ); 
        }

         ///
        ///     Return an XML instantiation of this permisson. 
        ///
        public override SecurityElement ToXml() 
        { 
            SecurityElement securityElement = new SecurityElement("IPermission");
            securityElement.AddAttribute("class", this.GetType().AssemblyQualifiedName); 
            securityElement.AddAttribute("version", "1");

            if (IsUnrestricted())
            { 
                securityElement.AddAttribute("Unrestricted", Boolean.TrueString);
            } 
            else 
            {
                securityElement.AddAttribute("Audio", _mediaPermissionAudio.ToString()); 
                securityElement.AddAttribute("Video", _mediaPermissionVideo.ToString());
                securityElement.AddAttribute("Image", _mediaPermissionImage.ToString());
            }
 
            return securityElement;
        } 
 
        ///
        ///     Create a permission from XML 
        ///
        public override void FromXml(SecurityElement securityElement)
        {
            if (securityElement == null) 
            {
                throw new ArgumentNullException("securityElement"); 
            } 

 
            String className = securityElement.Attribute("class");

            if (className == null || className.IndexOf(this.GetType().FullName, StringComparison.Ordinal) == -1)
            { 
                throw new ArgumentNullException("securityElement");
            } 
 
            String unrestricted = securityElement.Attribute("Unrestricted");
            if (unrestricted != null && Boolean.Parse(unrestricted)) 
            {
                _mediaPermissionAudio = MediaPermissionAudio.AllAudio ;
                _mediaPermissionVideo = MediaPermissionVideo.AllVideo ;
                _mediaPermissionImage = MediaPermissionImage.AllImage; 
                return;
            } 
 
            InitDefaults();
 
            String audio = securityElement.Attribute("Audio");

            if (audio != null)
            { 
                _mediaPermissionAudio  = (MediaPermissionAudio)Enum.Parse(typeof(MediaPermissionAudio), audio );
            } 
            else 
            {
                throw new ArgumentException(SR.Get(SRID.BadXml,"audio"));     // bad XML 
            }

            String video = securityElement.Attribute("Video");
 
            if (video != null)
            { 
                _mediaPermissionVideo = (MediaPermissionVideo)Enum.Parse(typeof(MediaPermissionVideo), video ); 
            }
            else 
            {
                throw new ArgumentException(SR.Get(SRID.BadXml,"video"));     // bad XML
            }
 
            String image = securityElement.Attribute("Image");
 
            if (image != null) 
            {
                _mediaPermissionImage = (MediaPermissionImage)Enum.Parse(typeof(MediaPermissionImage), image ); 
            }
            else
            {
                throw new ArgumentException(SR.Get(SRID.BadXml,"image"));     // bad XML 
            }
 
        } 

 
        #endregion Public Methods

        //------------------------------------------------------
        // 
        //  Public Properties
        // 
        //------------------------------------------------------ 

        #region Public Properties 

        ///
        ///     Current value of allowed audio permission level
        /// 
        public MediaPermissionAudio Audio
        { 
            get 
            {
                return _mediaPermissionAudio ; 
            }
        }

        /// 
        ///     Current value of allowed video permission level
        /// 
        public MediaPermissionVideo Video 
        {
            get 
            {
                return _mediaPermissionVideo ;
            }
        } 

        /// 
        ///     Current value of allowed image permission level 
        ///
        public MediaPermissionImage Image 
        {
            get
            {
                return _mediaPermissionImage ; 
            }
        } 
 
        #endregion Public Properties
 
        //-----------------------------------------------------
        //
        //  Internal Methods
        // 
        //------------------------------------------------------
 
        #region Internal Methods 

        internal static void VerifyMediaPermissionAudio(MediaPermissionAudio level) 
        {
            if (level < MediaPermissionAudio.NoAudio || level > MediaPermissionAudio.AllAudio )
            {
                throw new ArgumentException(SR.Get(SRID.InvalidPermissionLevel)); 
            }
        } 
 
        internal static void VerifyMediaPermissionVideo(MediaPermissionVideo level)
        { 
            if (level < MediaPermissionVideo.NoVideo || level > MediaPermissionVideo.AllVideo )
            {
                throw new ArgumentException(SR.Get(SRID.InvalidPermissionLevel));
            } 
        }
 
        internal static void VerifyMediaPermissionImage(MediaPermissionImage level) 
        {
            if (level < MediaPermissionImage.NoImage || level > MediaPermissionImage.AllImage ) 
            {
                throw new ArgumentException(SR.Get(SRID.InvalidPermissionLevel));
            }
        } 

 
        #endregion Internal Methods 

        //----------------------------------------------------- 
        //
        //  Private Methods
        //
        //----------------------------------------------------- 

        #region Private Methods 
 
        private void InitDefaults()
        { 
            _mediaPermissionAudio = MediaPermissionAudio.SafeAudio;
            _mediaPermissionVideo = MediaPermissionVideo.SafeVideo;
            _mediaPermissionImage = MediaPermissionImage.SafeImage;
        } 

        /// 
        ///     Private helper to compare the level of the 3 enum fields. 
        ///
        private bool EqualsLevel( MediaPermissionAudio audioLevel, 
                                  MediaPermissionVideo videoLevel,
                                  MediaPermissionImage imageLevel )
        {
            return ( ( _mediaPermissionAudio == audioLevel ) && 
                      ( _mediaPermissionVideo == videoLevel ) &&
                      ( _mediaPermissionImage == imageLevel ) ) ; 
        } 

        #endregion Private Methods 

        //
        // Private fields:
        // 

 
        private MediaPermissionAudio _mediaPermissionAudio ; 
        private MediaPermissionVideo _mediaPermissionVideo ;
        private MediaPermissionImage _mediaPermissionImage ; 


    }
 

    /// 
    /// Imperative attribute to create a MediaPermission. 
    ///
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false )] 
    sealed public class MediaPermissionAttribute : CodeAccessSecurityAttribute
    {
        //-----------------------------------------------------
        // 
        //  Constructors
        // 
        //------------------------------------------------------ 

        #region Constructors 

        ///
        /// Imperative attribute to create a MediaPermission.
        /// 
        public MediaPermissionAttribute(SecurityAction action) : base(action)
        { 
 
        }
 
        #endregion Constructors


        //----------------------------------------------------- 
        //
        //  Public Methods 
        // 
        //------------------------------------------------------
 
        #region Public Methods

        ///
        /// Create a MediaPermisison. 
        ///
        public override IPermission CreatePermission() 
        { 
            if (Unrestricted)
            { 
                return new MediaPermission(PermissionState.Unrestricted);
            }

            else 
            {
                return new MediaPermission( _mediaPermissionAudio, 
                                             _mediaPermissionVideo, 
                                             _mediaPermissionImage );
            } 
        }

        #endregion Public Methods
 
        //------------------------------------------------------
        // 
        //  Public Properties 
        //
        //----------------------------------------------------- 

        #region Public Properties

        /// 
        /// Current audio level.
        /// 
        public MediaPermissionAudio Audio 
        {
            get 
            {
                return _mediaPermissionAudio ;
            }
 
            set
            { 
                MediaPermission.VerifyMediaPermissionAudio(value); 
                _mediaPermissionAudio = value;
            } 
        }

        ///
        /// Current Video level. 
        ///
        public MediaPermissionVideo Video 
        { 
            get
            { 
                return _mediaPermissionVideo ;
            }

            set 
            {
                MediaPermission.VerifyMediaPermissionVideo(value); 
                _mediaPermissionVideo = value; 
            }
        } 

        ///
        /// Current Image level.
        /// 
        public MediaPermissionImage Image
        { 
            get 
            {
                return _mediaPermissionImage ; 
            }

            set
            { 
                MediaPermission.VerifyMediaPermissionImage(value);
                _mediaPermissionImage = value; 
            } 
        }
 
        #endregion Public Properties

        //
        // Private fields: 
        //
 
        private MediaPermissionAudio _mediaPermissionAudio ; 
        private MediaPermissionVideo _mediaPermissionVideo ;
        private MediaPermissionImage _mediaPermissionImage ; 


    }
 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK