2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Permissions / IsolatedStoragePermission.cs
blob3e5d958ddc50c04d3380048ff21ebe40d010bcfe
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)
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.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Permissions {
36 [Serializable]
37 [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
38 [ComVisible (true)]
39 public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission {
41 private const int version = 1;
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;
49 protected IsolatedStoragePermission (PermissionState state)
51 if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
52 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
56 public long UserQuota {
57 get { return m_userQuota; }
58 set { m_userQuota = value; }
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");
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;
79 public bool IsUnrestricted ()
81 return IsolatedStorageContainment.UnrestrictedIsolatedStorage == m_allowed;
84 public override SecurityElement ToXml ()
86 SecurityElement se = Element (version);
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 ());
96 return se;
99 public override void FromXml (SecurityElement esd)
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)
106 m_userQuota = 0;
107 m_machineQuota = 0;
108 m_expirationDays = 0;
109 m_permanentData = false;
110 m_allowed = IsolatedStorageContainment.None;
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);
120 a = esd.Attribute ("UserQuota");
121 if (a != null) {
122 Exception exc;
123 Int64.Parse (a, true, out m_userQuota, out exc);
128 // helpers
130 internal bool IsEmpty ()
132 // should we include internals ? or just publics ?
133 return ((m_userQuota == 0) && (m_allowed == IsolatedStorageContainment.None));