2 // System.Security.Permissions.IsolatedStoragePermission.cs
5 // Piers Haken <piersh@friskit.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 {
37 [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
41 public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission {
43 private const int version = 1;
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;
52 protected IsolatedStoragePermission (PermissionState state)
54 public IsolatedStoragePermission (PermissionState state)
57 if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
58 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
62 public long UserQuota {
63 get { return m_userQuota; }
64 set { m_userQuota = value; }
67 public IsolatedStorageContainment UsageAllowed {
68 get { return m_allowed; }
70 if (!Enum.IsDefined (typeof (IsolatedStorageContainment), value)) {
71 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
72 throw new ArgumentException (msg, "IsolatedStorageContainment");
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;
85 public bool IsUnrestricted ()
87 return IsolatedStorageContainment.UnrestrictedIsolatedStorage == m_allowed;
90 public override SecurityElement ToXml ()
92 SecurityElement se = Element (version);
94 if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
95 se.AddAttribute ("Unrestricted", "true");
97 se.AddAttribute ("Allowed", m_allowed.ToString ());
99 se.AddAttribute ("UserQuota", m_userQuota.ToString ());
105 public override void FromXml (SecurityElement esd)
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)
114 m_expirationDays = 0;
115 m_permanentData = false;
116 m_allowed = IsolatedStorageContainment.None;
118 if (IsUnrestricted (esd)) {
119 UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
121 string a = esd.Attribute ("Allowed");
123 UsageAllowed = (IsolatedStorageContainment) Enum.Parse (
124 typeof (IsolatedStorageContainment), a);
126 a = esd.Attribute ("UserQuota");
129 Int64.Parse (a, true, out m_userQuota, out exc);
136 internal bool IsEmpty ()
138 // should we include internals ? or just publics ?
139 return ((m_userQuota == 0) && (m_allowed == IsolatedStorageContainment.None));