handle IsTerminating.
[mono.git] / mcs / class / corlib / System.Security.Permissions / IsolatedStoragePermission.cs
1 //
2 // System.Security.Permissions.IsolatedStoragePermission.cs
3 //
4 // Authors:
5 //      Piers Haken <piersh@friskit.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc.                        http://www.ximian.com
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Permissions {
35
36         [Serializable]
37         [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
38         [ComVisible (true)]
39         public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission {
40
41                 private const int version = 1;
42
43                 internal long m_userQuota;
44                 internal long m_machineQuota;
45                 internal long m_expirationDays;
46                 internal bool m_permanentData;
47                 internal IsolatedStorageContainment m_allowed;
48
49                 protected IsolatedStoragePermission (PermissionState state)
50                 {
51                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
52                                 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
53                         }
54                 }
55
56                 public long UserQuota {
57                         get { return m_userQuota; }
58                         set { m_userQuota = value; }
59                 }
60
61                 public IsolatedStorageContainment UsageAllowed {
62                         get { return m_allowed; }
63                         set {
64                                 if (!Enum.IsDefined (typeof (IsolatedStorageContainment), value)) {
65                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
66                                         throw new ArgumentException (msg, "IsolatedStorageContainment");
67                                 }
68                                 m_allowed = value;
69                                 if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage) {
70                                         m_userQuota = Int64.MaxValue;
71                                         m_machineQuota = Int64.MaxValue;
72                                         m_expirationDays = Int64.MaxValue ;
73                                         m_permanentData = true;
74                                 }
75                         }
76                 }
77
78
79                 public bool IsUnrestricted ()
80                 {
81                         return IsolatedStorageContainment.UnrestrictedIsolatedStorage == m_allowed;
82                 }
83
84                 public override SecurityElement ToXml ()
85                 {
86                         SecurityElement se = Element (version);
87
88                         if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
89                                 se.AddAttribute ("Unrestricted", "true");
90                         else {
91                                 se.AddAttribute ("Allowed", m_allowed.ToString ());
92                                 if (m_userQuota > 0)
93                                         se.AddAttribute ("UserQuota", m_userQuota.ToString ());
94                         }
95                         
96                         return se;
97                 }
98
99                 public override void FromXml (SecurityElement esd)
100                 {
101                         // General validation in CodeAccessPermission
102                         CheckSecurityElement (esd, "esd", version, version);
103                         // Note: we do not (yet) care about the return value 
104                         // as we only accept version 1 (min/max values)
105
106                         m_userQuota = 0;
107                         m_machineQuota = 0;
108                         m_expirationDays = 0;
109                         m_permanentData = false;
110                         m_allowed = IsolatedStorageContainment.None;
111
112                         if (IsUnrestricted (esd)) {
113                                 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
114                         } else {
115                                 string a = esd.Attribute ("Allowed");
116                                 if (a != null) {
117                                         UsageAllowed = (IsolatedStorageContainment) Enum.Parse (
118                                                 typeof (IsolatedStorageContainment), a);
119                                 }
120                                 a = esd.Attribute ("UserQuota");
121                                 if (a != null) {
122                                         Exception exc;
123                                         Int64.Parse (a, true, out m_userQuota, out exc);
124                                 }
125                         }
126                 }
127
128                 // helpers
129
130                 internal bool IsEmpty ()
131                 {
132                         // should we include internals ? or just publics ?
133                         return ((m_userQuota == 0) && (m_allowed == IsolatedStorageContainment.None));
134                 }
135         }
136 }