2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Permissions / IsolatedStorageFilePermission.cs
bloba934fd9dc70f92210c409348b3c209bd4fca76cf
1 //
2 // System.Security.Permissions.IsolatedStorageFilePermission.cs
3 //
4 // Author
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
32 namespace System.Security.Permissions {
34 [Serializable]
35 [ComVisible (true)]
36 public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission {
38 // Constructors
40 public IsolatedStorageFilePermission (PermissionState state)
41 : base (state)
45 // Properties
47 // Methods
49 public override IPermission Copy ()
51 IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
52 p.m_userQuota = m_userQuota;
53 p.m_machineQuota = m_machineQuota;
54 p.m_expirationDays = m_expirationDays;
55 p.m_permanentData = m_permanentData;
56 p.m_allowed = m_allowed;
57 return p;
60 public override IPermission Intersect (IPermission target)
62 IsolatedStorageFilePermission isfp = Cast (target);
63 if (isfp == null)
64 return null;
65 if (IsEmpty () && isfp.IsEmpty ())
66 return null;
68 IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
69 p.m_userQuota = (m_userQuota < isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
70 p.m_machineQuota = (m_machineQuota < isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
71 p.m_expirationDays = (m_expirationDays < isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
72 p.m_permanentData = (m_permanentData && isfp.m_permanentData);
73 // UsageAllowed == Unrestricted is a special case handled by the property
74 p.UsageAllowed = (m_allowed < isfp.m_allowed) ? m_allowed : isfp.m_allowed;
75 return p;
78 public override bool IsSubsetOf (IPermission target)
80 IsolatedStorageFilePermission isfp = Cast (target);
81 if (isfp == null)
82 return IsEmpty ();
83 if (isfp.IsUnrestricted ())
84 return true;
86 if (m_userQuota > isfp.m_userQuota)
87 return false;
88 if (m_machineQuota > isfp.m_machineQuota)
89 return false;
90 if (m_expirationDays > isfp.m_expirationDays)
91 return false;
92 if (m_permanentData != isfp.m_permanentData)
93 return false;
94 if (m_allowed > isfp.m_allowed)
95 return false;
96 return true;
99 public override IPermission Union (IPermission target)
101 IsolatedStorageFilePermission isfp = Cast (target);
102 if (isfp == null)
103 return Copy ();
105 IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
106 p.m_userQuota = (m_userQuota > isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
107 p.m_machineQuota = (m_machineQuota > isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
108 p.m_expirationDays = (m_expirationDays > isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
109 p.m_permanentData = (m_permanentData || isfp.m_permanentData);
110 // UsageAllowed == Unrestricted is a special case handled by the property
111 p.UsageAllowed = (m_allowed > isfp.m_allowed) ? m_allowed : isfp.m_allowed;
112 return p;
115 [MonoTODO ("(2.0) new override - something must have been added ???")]
116 [ComVisible (false)]
117 public override SecurityElement ToXml ()
119 return base.ToXml ();
122 // IBuiltInPermission
123 int IBuiltInPermission.GetTokenIndex ()
125 return (int) BuiltInToken.IsolatedStorageFile;
128 // helpers
130 private IsolatedStorageFilePermission Cast (IPermission target)
132 if (target == null)
133 return null;
135 IsolatedStorageFilePermission isfp = (target as IsolatedStorageFilePermission);
136 if (isfp == null) {
137 ThrowInvalidPermission (target, typeof (IsolatedStorageFilePermission));
140 return isfp;