The Labs \ Source Viewer \ SSCLI \ System.Security.Permissions \ IsolatedStorageFilePermission

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //
  16. // Purpose : This permission is used to controls/administer access to
  17. // IsolatedStorageFile
  18. //
  19. namespace System.Security.Permissions
  20. {
  21.    
  22.     using System.Globalization;
  23.    
  24.     [Serializable()]
  25.     [System.Runtime.InteropServices.ComVisible(true)]
  26.     public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission
  27.     {
  28.         public IsolatedStorageFilePermission(PermissionState state) : base(state)
  29.         {
  30.         }
  31.        
  32.         internal IsolatedStorageFilePermission(IsolatedStorageContainment UsageAllowed, long ExpirationDays, bool PermanentData) : base(UsageAllowed, ExpirationDays, PermanentData)
  33.         {
  34.         }
  35.        
  36.         //------------------------------------------------------
  37.         //
  38.         // IPERMISSION IMPLEMENTATION
  39.         //
  40.         //------------------------------------------------------
  41.        
  42.         public override IPermission Union(IPermission target)
  43.         {
  44.             if (target == null) {
  45.                 return this.Copy();
  46.             }
  47.             else if (!VerifyType(target)) {
  48.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  49.             }
  50.            
  51.             IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;
  52.            
  53.             if (this.IsUnrestricted() || operand.IsUnrestricted()) {
  54.                 return new IsolatedStorageFilePermission(PermissionState.Unrestricted);
  55.             }
  56.             else {
  57.                 IsolatedStorageFilePermission union;
  58.                 union = new IsolatedStorageFilePermission(PermissionState.None);
  59.                 union.m_userQuota = max(m_userQuota, operand.m_userQuota);
  60.                 union.m_machineQuota = max(m_machineQuota, operand.m_machineQuota);
  61.                 union.m_expirationDays = max(m_expirationDays, operand.m_expirationDays);
  62.                 union.m_permanentData = m_permanentData || operand.m_permanentData;
  63.                 union.m_allowed = (IsolatedStorageContainment)max((long)m_allowed, (long)operand.m_allowed);
  64.                 return union;
  65.             }
  66.         }
  67.        
  68.         public override bool IsSubsetOf(IPermission target)
  69.         {
  70.             if (target == null) {
  71.                 return ((m_userQuota == 0) && (m_machineQuota == 0) && (m_expirationDays == 0) && (m_permanentData == false) && (m_allowed == IsolatedStorageContainment.None));
  72.             }
  73.            
  74.             try {
  75.                 IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;
  76.                
  77.                 if (operand.IsUnrestricted())
  78.                     return true;
  79.                
  80.                 return ((operand.m_userQuota >= m_userQuota) && (operand.m_machineQuota >= m_machineQuota) && (operand.m_expirationDays >= m_expirationDays) && (operand.m_permanentData || !m_permanentData) && (operand.m_allowed >= m_allowed));
  81.             }
  82.             catch (InvalidCastException) {
  83.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  84.             }
  85.            
  86.         }
  87.        
  88.         public override IPermission Intersect(IPermission target)
  89.         {
  90.             if (target == null)
  91.                 return null;
  92.             else if (!VerifyType(target)) {
  93.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  94.             }
  95.            
  96.             IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;
  97.            
  98.             if (operand.IsUnrestricted())
  99.                 return Copy();
  100.             else if (IsUnrestricted())
  101.                 return target.Copy();
  102.            
  103.             IsolatedStorageFilePermission intersection;
  104.             intersection = new IsolatedStorageFilePermission(PermissionState.None);
  105.             intersection.m_userQuota = min(m_userQuota, operand.m_userQuota);
  106.             intersection.m_machineQuota = min(m_machineQuota, operand.m_machineQuota);
  107.             intersection.m_expirationDays = min(m_expirationDays, operand.m_expirationDays);
  108.             intersection.m_permanentData = m_permanentData && operand.m_permanentData;
  109.             intersection.m_allowed = (IsolatedStorageContainment)min((long)m_allowed, (long)operand.m_allowed);
  110.            
  111.             if ((intersection.m_userQuota == 0) && (intersection.m_machineQuota == 0) && (intersection.m_expirationDays == 0) && (intersection.m_permanentData == false) && (intersection.m_allowed == IsolatedStorageContainment.None))
  112.                 return null;
  113.            
  114.             return intersection;
  115.         }
  116.        
  117.         public override IPermission Copy()
  118.         {
  119.             IsolatedStorageFilePermission copy;
  120.             copy = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
  121.             if (!IsUnrestricted()) {
  122.                 copy.m_userQuota = m_userQuota;
  123.                 copy.m_machineQuota = m_machineQuota;
  124.                 copy.m_expirationDays = m_expirationDays;
  125.                 copy.m_permanentData = m_permanentData;
  126.                 copy.m_allowed = m_allowed;
  127.             }
  128.             return copy;
  129.         }
  130.        
  131.        
  132.         /// <internalonly/>
  133.         int IBuiltInPermission.GetTokenIndex()
  134.         {
  135.             return IsolatedStorageFilePermission.GetTokenIndex();
  136.         }
  137.        
  138.         static internal int GetTokenIndex()
  139.         {
  140.             return BuiltInPermissionIndex.IsolatedStorageFilePermissionIndex;
  141.         }
  142.        
  143.         //------------------------------------------------------
  144.         //
  145.         // IsolatedStoragePermission OVERRIDES
  146.         //
  147.         //------------------------------------------------------
  148.        
  149.         [System.Runtime.InteropServices.ComVisible(false)]
  150.         public override SecurityElement ToXml()
  151.         {
  152.             return base.ToXml("System.Security.Permissions.IsolatedStorageFilePermission");
  153.         }
  154.     }
  155. }

Developer Fusion