3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
9 namespace System.Security.Permissions {
13 using System.Security;
14 using System.Security.Util;
15 using System.Globalization;
18 [System.Runtime.InteropServices.ComVisible(true)]
19 public enum IsolatedStorageContainment {
21 DomainIsolationByUser = 0x10,
22 ApplicationIsolationByUser = 0x15,
23 AssemblyIsolationByUser = 0x20,
24 DomainIsolationByMachine = 0x30,
25 AssemblyIsolationByMachine = 0x40,
26 ApplicationIsolationByMachine = 0x45,
27 DomainIsolationByRoamingUser = 0x50,
28 AssemblyIsolationByRoamingUser = 0x60,
29 ApplicationIsolationByRoamingUser = 0x65,
30 AdministerIsolatedStorageByUser = 0x70,
31 //AdministerIsolatedStorageByMachine = 0x80,
32 UnrestrictedIsolatedStorage = 0xF0
38 [SecurityPermissionAttribute( SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true )]
40 [System.Runtime.InteropServices.ComVisible(true)]
41 abstract public class IsolatedStoragePermission
42 : CodeAccessPermission, IUnrestrictedPermission
45 //------------------------------------------------------
49 //------------------------------------------------------
52 internal long m_userQuota;
54 internal long m_machineQuota;
56 internal long m_expirationDays;
58 internal bool m_permanentData;
60 internal IsolatedStorageContainment m_allowed;
62 //------------------------------------------------------
66 //------------------------------------------------------
68 protected IsolatedStoragePermission(PermissionState state)
70 if (state == PermissionState.Unrestricted)
72 m_userQuota = Int64.MaxValue;
73 m_machineQuota = Int64.MaxValue;
74 m_expirationDays = Int64.MaxValue ;
75 m_permanentData = true;
76 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
78 else if (state == PermissionState.None)
83 m_permanentData = false;
84 m_allowed = IsolatedStorageContainment.None;
88 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
92 internal IsolatedStoragePermission(IsolatedStorageContainment UsageAllowed,
93 long ExpirationDays, bool PermanentData)
96 m_userQuota = 0; // typical demand won't include quota
97 m_machineQuota = 0; // typical demand won't include quota
98 m_expirationDays = ExpirationDays;
99 m_permanentData = PermanentData;
100 m_allowed = UsageAllowed;
103 internal IsolatedStoragePermission(IsolatedStorageContainment UsageAllowed,
104 long ExpirationDays, bool PermanentData, long UserQuota)
108 m_userQuota = UserQuota;
109 m_expirationDays = ExpirationDays;
110 m_permanentData = PermanentData;
111 m_allowed = UsageAllowed;
115 //------------------------------------------------------
117 // PUBLIC ACCESSOR METHODS
119 //------------------------------------------------------
122 public long UserQuota {
132 internal long MachineQuota {
134 m_machineQuota = value;
137 return m_machineQuota;
140 internal long ExpirationDays {
142 m_expirationDays = value;
145 return m_expirationDays;
148 internal bool PermanentData {
150 m_permanentData = value;
153 return m_permanentData;
158 public IsolatedStorageContainment UsageAllowed {
168 //------------------------------------------------------
170 // CODEACCESSPERMISSION IMPLEMENTATION
172 //------------------------------------------------------
174 public bool IsUnrestricted()
176 return m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage;
180 //------------------------------------------------------
184 //------------------------------------------------------
185 internal static long min(long x,long y) {return x>y?y:x;}
186 internal static long max(long x,long y) {return x<y?y:x;}
188 #if FEATURE_CAS_POLICY
189 //------------------------------------------------------
191 // PUBLIC ENCODING METHODS
193 //------------------------------------------------------
195 private const String _strUserQuota = "UserQuota";
196 private const String _strMachineQuota = "MachineQuota";
197 private const String _strExpiry = "Expiry";
198 private const String _strPermDat = "Permanent";
200 public override SecurityElement ToXml()
202 return ToXml ( this.GetType().FullName );
205 internal SecurityElement ToXml(String permName)
207 SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, permName );
208 if (!IsUnrestricted())
210 esd.AddAttribute( "Allowed", Enum.GetName( typeof( IsolatedStorageContainment ), m_allowed ) );
213 esd.AddAttribute(_strUserQuota, (m_userQuota).ToString(CultureInfo.InvariantCulture)) ;
215 if (m_machineQuota>0)
217 esd.AddAttribute(_strMachineQuota, (m_machineQuota).ToString(CultureInfo.InvariantCulture)) ;
219 if (m_expirationDays>0)
221 esd.AddAttribute( _strExpiry, (m_expirationDays).ToString(CultureInfo.InvariantCulture)) ;
225 esd.AddAttribute(_strPermDat, (m_permanentData).ToString()) ;
230 esd.AddAttribute( "Unrestricted", "true" );
236 public override void FromXml(SecurityElement esd)
238 CodeAccessPermission.ValidateElement( esd, this );
240 m_allowed = IsolatedStorageContainment.None; // default if no match
242 if (XMLUtil.IsUnrestricted(esd))
244 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
248 String allowed = esd.Attribute( "Allowed" );
251 m_allowed = (IsolatedStorageContainment)Enum.Parse( typeof( IsolatedStorageContainment ), allowed );
254 if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
256 m_userQuota = Int64.MaxValue;
257 m_machineQuota = Int64.MaxValue;
258 m_expirationDays = Int64.MaxValue ;
259 m_permanentData = true;
264 param = esd.Attribute (_strUserQuota) ;
265 m_userQuota = param != null ? Int64.Parse(param, CultureInfo.InvariantCulture) : 0 ;
266 param = esd.Attribute (_strMachineQuota) ;
267 m_machineQuota = param != null ? Int64.Parse(param, CultureInfo.InvariantCulture) : 0 ;
268 param = esd.Attribute (_strExpiry) ;
269 m_expirationDays = param != null ? Int64.Parse(param, CultureInfo.InvariantCulture) : 0 ;
270 param = esd.Attribute (_strPermDat) ;
271 m_permanentData = param != null ? (Boolean.Parse(param)) : false ;
274 #endif // FEATURE_CAS_POLICY