Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / isolatedstoragepermission.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 namespace System.Security.Permissions {
10     
11     using System;
12     using System.IO;
13     using System.Security;
14     using System.Security.Util;
15     using System.Globalization;
16
17     [Serializable]
18 [System.Runtime.InteropServices.ComVisible(true)]
19     public enum IsolatedStorageContainment {
20         None                                    = 0x00,
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
33     };
34
35     
36     [Serializable]
37 #if !FEATURE_CORECLR
38     [SecurityPermissionAttribute( SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true )]
39 #endif
40     [System.Runtime.InteropServices.ComVisible(true)]
41     abstract public class IsolatedStoragePermission
42            : CodeAccessPermission, IUnrestrictedPermission
43     {
44
45         //------------------------------------------------------
46         //
47         // PRIVATE STATE DATA
48         //
49         //------------------------------------------------------
50         
51         /// <internalonly/>
52         internal long m_userQuota;
53         /// <internalonly/>
54         internal long m_machineQuota;
55         /// <internalonly/>
56         internal long m_expirationDays;
57         /// <internalonly/>
58         internal bool m_permanentData;
59         /// <internalonly/>
60         internal IsolatedStorageContainment m_allowed;
61     
62         //------------------------------------------------------
63         //
64         // CONSTRUCTORS
65         //
66         //------------------------------------------------------
67     
68         protected IsolatedStoragePermission(PermissionState state)
69         {
70             if (state == PermissionState.Unrestricted)
71             {
72                 m_userQuota = Int64.MaxValue;
73                 m_machineQuota = Int64.MaxValue;
74                 m_expirationDays = Int64.MaxValue ;
75                 m_permanentData = true;
76                 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
77             }
78             else if (state == PermissionState.None)
79             {
80                 m_userQuota = 0;
81                 m_machineQuota = 0;
82                 m_expirationDays = 0;
83                 m_permanentData = false;
84                 m_allowed = IsolatedStorageContainment.None;
85             }
86             else
87             {
88                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
89             }
90         }
91     
92         internal IsolatedStoragePermission(IsolatedStorageContainment UsageAllowed, 
93             long ExpirationDays, bool PermanentData)
94
95         {
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;
101         }
102     
103         internal IsolatedStoragePermission(IsolatedStorageContainment UsageAllowed, 
104             long ExpirationDays, bool PermanentData, long UserQuota)
105
106         {
107                 m_machineQuota = 0;
108                 m_userQuota = UserQuota;
109                 m_expirationDays = ExpirationDays;
110                 m_permanentData = PermanentData;
111                 m_allowed = UsageAllowed;
112         }
113     
114         
115         //------------------------------------------------------
116         //
117         // PUBLIC ACCESSOR METHODS
118         //
119         //------------------------------------------------------
120         
121         // properties
122         public long UserQuota {
123             set{
124                 m_userQuota = value;
125             }
126             get{
127                 return m_userQuota;
128             }
129         }
130
131 #if false
132         internal long MachineQuota {
133             set{
134                 m_machineQuota = value;
135             }
136             get{
137                 return m_machineQuota;
138             }
139         }
140         internal long ExpirationDays {
141             set{
142                 m_expirationDays = value;
143             }
144             get{
145                 return m_expirationDays;
146             }
147         }
148         internal bool PermanentData {
149             set{
150                 m_permanentData = value;
151             }
152             get{
153                 return m_permanentData;
154             }
155         }
156 #endif
157
158         public IsolatedStorageContainment UsageAllowed {
159             set{
160                 m_allowed = value;
161             }
162             get{
163                 return m_allowed;
164             }
165         }
166
167     
168         //------------------------------------------------------
169         //
170         // CODEACCESSPERMISSION IMPLEMENTATION
171         //
172         //------------------------------------------------------
173         
174         public bool IsUnrestricted()
175         {
176             return m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage;
177         }
178         
179     
180         //------------------------------------------------------
181         //
182         // INTERNAL METHODS
183         //
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;}
187
188 #if FEATURE_CAS_POLICY
189         //------------------------------------------------------
190         //
191         // PUBLIC ENCODING METHODS
192         //
193         //------------------------------------------------------
194         
195         private const String _strUserQuota   = "UserQuota";
196         private const String _strMachineQuota   = "MachineQuota";
197         private const String _strExpiry  = "Expiry";
198         private const String _strPermDat = "Permanent";
199
200         public override SecurityElement ToXml()
201         {
202             return ToXml ( this.GetType().FullName );
203         }
204     
205         internal SecurityElement ToXml(String permName)
206         {
207             SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, permName );
208             if (!IsUnrestricted())
209             {
210                 esd.AddAttribute( "Allowed", Enum.GetName( typeof( IsolatedStorageContainment ), m_allowed ) );
211                 if (m_userQuota>0)
212                 {
213                     esd.AddAttribute(_strUserQuota, (m_userQuota).ToString(CultureInfo.InvariantCulture)) ;
214                 }
215                 if (m_machineQuota>0)
216                 {
217                     esd.AddAttribute(_strMachineQuota, (m_machineQuota).ToString(CultureInfo.InvariantCulture)) ;
218                 }
219                 if (m_expirationDays>0)
220                 {
221                     esd.AddAttribute( _strExpiry, (m_expirationDays).ToString(CultureInfo.InvariantCulture)) ;
222                 }
223                 if (m_permanentData)
224                 {
225                     esd.AddAttribute(_strPermDat, (m_permanentData).ToString()) ;
226                 }
227             }
228             else
229             {
230                 esd.AddAttribute( "Unrestricted", "true" );
231             }
232             return esd;
233         }
234     
235
236         public override void FromXml(SecurityElement esd)
237         {
238             CodeAccessPermission.ValidateElement( esd, this );
239
240             m_allowed = IsolatedStorageContainment.None;    // default if no match
241
242             if (XMLUtil.IsUnrestricted(esd))
243             {
244                 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
245             }
246             else
247             {
248                 String allowed = esd.Attribute( "Allowed" );
249
250                 if (allowed != null)
251                     m_allowed = (IsolatedStorageContainment)Enum.Parse( typeof( IsolatedStorageContainment ), allowed );
252             }
253                     
254             if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
255             {
256                 m_userQuota = Int64.MaxValue;
257                 m_machineQuota = Int64.MaxValue;
258                 m_expirationDays = Int64.MaxValue ;
259                 m_permanentData = true;
260             }
261             else 
262             {
263                 String param;
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 ;
272             }
273         }
274 #endif // FEATURE_CAS_POLICY
275     }
276 }