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)]
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; }
64 if (!Enum
.IsDefined (typeof (IsolatedStorageContainment
), value)) {
65 string msg
= String
.Format (Locale
.GetText ("Invalid enum {0}"), value);
66 throw new ArgumentException (msg
, "IsolatedStorageContainment");
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");
91 se
.AddAttribute ("Allowed", m_allowed
.ToString ());
93 se
.AddAttribute ("UserQuota", m_userQuota
.ToString ());
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)
108 m_expirationDays
= 0;
109 m_permanentData
= false;
110 m_allowed
= IsolatedStorageContainment
.None
;
112 if (IsUnrestricted (esd
)) {
113 UsageAllowed
= IsolatedStorageContainment
.UnrestrictedIsolatedStorage
;
115 string a
= esd
.Attribute ("Allowed");
117 UsageAllowed
= (IsolatedStorageContainment
) Enum
.Parse (
118 typeof (IsolatedStorageContainment
), a
);
120 a
= esd
.Attribute ("UserQuota");
123 Int64
.Parse (a
, true, out m_userQuota
, out exc
);
130 internal bool IsEmpty ()
132 // should we include internals ? or just publics ?
133 return ((m_userQuota
== 0) && (m_allowed
== IsolatedStorageContainment
.None
));