2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Permissions / HostProtectionPermission.cs
1 //
2 // System.Security.Permissions.HostProtectionPermission.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System.Globalization;
32
33 namespace System.Security.Permissions {
34         
35         [Serializable]
36         internal sealed class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
37
38                 private const int version = 1;
39
40                 private HostProtectionResource _resources;
41
42                 // constructors
43
44                 public HostProtectionPermission (PermissionState state)
45                 {
46                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
47                                 _resources = HostProtectionResource.All;
48                         else
49                                 _resources = HostProtectionResource.None;
50                 }
51
52                 public HostProtectionPermission (HostProtectionResource resources) 
53                 {
54                         // reuse validation by the Flags property
55                         Resources = _resources;
56                 }
57
58                 public HostProtectionResource Resources {
59                         get { return _resources; }
60                         set {
61                                 if (!Enum.IsDefined (typeof (HostProtectionResource), value)) {
62                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
63                                         throw new ArgumentException (msg, "HostProtectionResource");
64                                 }
65                                 _resources = value;
66                         }
67                 }
68
69                 public override IPermission Copy () 
70                 {
71                         return new HostProtectionPermission (_resources);
72                 }
73
74                 public override IPermission Intersect (IPermission target) 
75                 {
76                         HostProtectionPermission hpp = Cast (target);
77                         if (hpp == null)
78                                 return null;
79
80                         if (this.IsUnrestricted () && hpp.IsUnrestricted ())
81                                 return new HostProtectionPermission (PermissionState.Unrestricted);
82                         if (this.IsUnrestricted ())
83                                 return hpp.Copy ();
84                         if (hpp.IsUnrestricted ())
85                                 return this.Copy ();
86                         return new HostProtectionPermission (_resources & hpp._resources);
87                 }
88
89                 public override IPermission Union (IPermission target) 
90                 {
91                         HostProtectionPermission hpp = Cast (target);
92                         if (hpp == null)
93                                 return this.Copy ();
94
95                         if (this.IsUnrestricted () || hpp.IsUnrestricted ())
96                                 return new HostProtectionPermission (PermissionState.Unrestricted);
97                         
98                         return new HostProtectionPermission (_resources | hpp._resources);
99                 }
100
101                 public override bool IsSubsetOf (IPermission target) 
102                 {
103                         HostProtectionPermission hpp = Cast (target);
104                         if (hpp == null) 
105                                 return (_resources == HostProtectionResource.None);
106
107                         if (hpp.IsUnrestricted ())
108                                 return true;
109                         if (this.IsUnrestricted ())
110                                 return false;
111
112                         return ((_resources & ~hpp._resources) == 0);
113                 }
114
115                 public override void FromXml (SecurityElement e) 
116                 {
117                         // General validation in CodeAccessPermission
118                         CheckSecurityElement (e, "e", version, version);
119                         // Note: we do not (yet) care about the return value 
120                         // as we only accept version 1 (min/max values)
121
122                         _resources = (HostProtectionResource) Enum.Parse (
123                                 typeof (HostProtectionResource), e.Attribute ("Resources"));
124                 }
125
126                 public override SecurityElement ToXml () 
127                 {
128                         SecurityElement e = Element (version);
129                         e.AddAttribute ("Resources", _resources.ToString ());
130                         return e;
131                 }
132
133                 // IUnrestrictedPermission
134                 public bool IsUnrestricted () 
135                 {
136                         return (_resources == HostProtectionResource.All);
137                 }
138
139                 // IBuiltInPermission
140                 int IBuiltInPermission.GetTokenIndex ()
141                 {
142                         return (int) BuiltInToken.HostProtection;
143                 }
144
145                 // helpers
146
147                 private HostProtectionPermission Cast (IPermission target)
148                 {
149                         if (target == null)
150                                 return null;
151
152                         HostProtectionPermission hpp = (target as HostProtectionPermission);
153                         if (hpp == null) {
154                                 ThrowInvalidPermission (target, typeof (HostProtectionPermission));
155                         }
156
157                         return hpp;
158                 }
159         }
160 }
161
162 #endif