merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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 #if NET_2_0
39         [ComVisible (true)]
40 #endif
41         public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission {
42
43                 private const int version = 1;
44
45                 internal long m_userQuota;
46                 internal long m_machineQuota;
47                 internal long m_expirationDays;
48                 internal bool m_permanentData;
49                 internal IsolatedStorageContainment m_allowed;
50
51 #if NET_2_0
52                 protected IsolatedStoragePermission (PermissionState state)
53 #else
54                 public IsolatedStoragePermission (PermissionState state)
55 #endif
56                 {
57                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
58                                 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
59                         }
60                 }
61
62                 public long UserQuota {
63                         get { return m_userQuota; }
64                         set { m_userQuota = value; }
65                 }
66
67                 public IsolatedStorageContainment UsageAllowed {
68                         get { return m_allowed; }
69                         set {
70                                 if (!Enum.IsDefined (typeof (IsolatedStorageContainment), value)) {
71                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
72                                         throw new ArgumentException (msg, "IsolatedStorageContainment");
73                                 }
74                                 m_allowed = value;
75                                 if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage) {
76                                         m_userQuota = Int64.MaxValue;
77                                         m_machineQuota = Int64.MaxValue;
78                                         m_expirationDays = Int64.MaxValue ;
79                                         m_permanentData = true;
80                                 }
81                         }
82                 }
83
84
85                 public bool IsUnrestricted ()
86                 {
87                         return IsolatedStorageContainment.UnrestrictedIsolatedStorage == m_allowed;
88                 }
89
90                 public override SecurityElement ToXml ()
91                 {
92                         SecurityElement se = Element (version);
93
94                         if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
95                                 se.AddAttribute ("Unrestricted", "true");
96                         else {
97                                 se.AddAttribute ("Allowed", m_allowed.ToString ());
98                                 if (m_userQuota > 0)
99                                         se.AddAttribute ("UserQuota", m_userQuota.ToString ());
100                         }
101                         
102                         return se;
103                 }
104
105                 public override void FromXml (SecurityElement esd)
106                 {
107                         // General validation in CodeAccessPermission
108                         CheckSecurityElement (esd, "esd", version, version);
109                         // Note: we do not (yet) care about the return value 
110                         // as we only accept version 1 (min/max values)
111
112                         m_userQuota = 0;
113                         m_machineQuota = 0;
114                         m_expirationDays = 0;
115                         m_permanentData = false;
116                         m_allowed = IsolatedStorageContainment.None;
117
118                         if (IsUnrestricted (esd)) {
119                                 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
120                         } else {
121                                 string a = esd.Attribute ("Allowed");
122                                 if (a != null) {
123                                         UsageAllowed = (IsolatedStorageContainment) Enum.Parse (
124                                                 typeof (IsolatedStorageContainment), a);
125                                 }
126                                 a = esd.Attribute ("UserQuota");
127                                 if (a != null) {
128                                         Exception exc;
129                                         Int64.Parse (a, true, out m_userQuota, out exc);
130                                 }
131                         }
132                 }
133
134                 // helpers
135
136                 internal bool IsEmpty ()
137                 {
138                         // should we include internals ? or just publics ?
139                         return ((m_userQuota == 0) && (m_allowed == IsolatedStorageContainment.None));
140                 }
141         }
142 }