Merge pull request #839 from glinos/master
[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 using System.Globalization;
30
31 namespace System.Security.Permissions {
32         
33         [Serializable]
34         internal sealed class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
35
36                 private const int version = 1;
37
38                 private HostProtectionResource _resources;
39
40                 // constructors
41
42                 public HostProtectionPermission (PermissionState state)
43                 {
44                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
45                                 _resources = HostProtectionResource.All;
46                         else
47                                 _resources = HostProtectionResource.None;
48                 }
49
50                 public HostProtectionPermission (HostProtectionResource resources) 
51                 {
52                         // reuse validation by the Flags property
53                         Resources = _resources;
54                 }
55
56                 public HostProtectionResource Resources {
57                         get { return _resources; }
58                         set {
59                                 if (!Enum.IsDefined (typeof (HostProtectionResource), value)) {
60                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
61                                         throw new ArgumentException (msg, "HostProtectionResource");
62                                 }
63                                 _resources = value;
64                         }
65                 }
66
67                 public override IPermission Copy () 
68                 {
69                         return new HostProtectionPermission (_resources);
70                 }
71
72                 public override IPermission Intersect (IPermission target) 
73                 {
74                         HostProtectionPermission hpp = Cast (target);
75                         if (hpp == null)
76                                 return null;
77
78                         if (this.IsUnrestricted () && hpp.IsUnrestricted ())
79                                 return new HostProtectionPermission (PermissionState.Unrestricted);
80                         if (this.IsUnrestricted ())
81                                 return hpp.Copy ();
82                         if (hpp.IsUnrestricted ())
83                                 return this.Copy ();
84                         return new HostProtectionPermission (_resources & hpp._resources);
85                 }
86
87                 public override IPermission Union (IPermission target) 
88                 {
89                         HostProtectionPermission hpp = Cast (target);
90                         if (hpp == null)
91                                 return this.Copy ();
92
93                         if (this.IsUnrestricted () || hpp.IsUnrestricted ())
94                                 return new HostProtectionPermission (PermissionState.Unrestricted);
95                         
96                         return new HostProtectionPermission (_resources | hpp._resources);
97                 }
98
99                 public override bool IsSubsetOf (IPermission target) 
100                 {
101                         HostProtectionPermission hpp = Cast (target);
102                         if (hpp == null) 
103                                 return (_resources == HostProtectionResource.None);
104
105                         if (hpp.IsUnrestricted ())
106                                 return true;
107                         if (this.IsUnrestricted ())
108                                 return false;
109
110                         return ((_resources & ~hpp._resources) == 0);
111                 }
112
113                 public override void FromXml (SecurityElement e) 
114                 {
115                         // General validation in CodeAccessPermission
116                         CheckSecurityElement (e, "e", version, version);
117                         // Note: we do not (yet) care about the return value 
118                         // as we only accept version 1 (min/max values)
119
120                         _resources = (HostProtectionResource) Enum.Parse (
121                                 typeof (HostProtectionResource), e.Attribute ("Resources"));
122                 }
123
124                 public override SecurityElement ToXml () 
125                 {
126                         SecurityElement e = Element (version);
127                         e.AddAttribute ("Resources", _resources.ToString ());
128                         return e;
129                 }
130
131                 // IUnrestrictedPermission
132                 public bool IsUnrestricted () 
133                 {
134                         return (_resources == HostProtectionResource.All);
135                 }
136
137                 // IBuiltInPermission
138                 int IBuiltInPermission.GetTokenIndex ()
139                 {
140                         return (int) BuiltInToken.HostProtection;
141                 }
142
143                 // helpers
144
145                 private HostProtectionPermission Cast (IPermission target)
146                 {
147                         if (target == null)
148                                 return null;
149
150                         HostProtectionPermission hpp = (target as HostProtectionPermission);
151                         if (hpp == null) {
152                                 ThrowInvalidPermission (target, typeof (HostProtectionPermission));
153                         }
154
155                         return hpp;
156                 }
157         }
158 }
159