2009-12-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Permissions / HostProtectionPermission.cs
blobf1c71d797ea3836e150b5c5f64d6b58cb7df7752
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.
29 using System.Globalization;
31 namespace System.Security.Permissions {
33 [Serializable]
34 internal sealed class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
36 private const int version = 1;
38 private HostProtectionResource _resources;
40 // constructors
42 public HostProtectionPermission (PermissionState state)
44 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
45 _resources = HostProtectionResource.All;
46 else
47 _resources = HostProtectionResource.None;
50 public HostProtectionPermission (HostProtectionResource resources)
52 // reuse validation by the Flags property
53 Resources = _resources;
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");
63 _resources = value;
67 public override IPermission Copy ()
69 return new HostProtectionPermission (_resources);
72 public override IPermission Intersect (IPermission target)
74 HostProtectionPermission hpp = Cast (target);
75 if (hpp == null)
76 return null;
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);
87 public override IPermission Union (IPermission target)
89 HostProtectionPermission hpp = Cast (target);
90 if (hpp == null)
91 return this.Copy ();
93 if (this.IsUnrestricted () || hpp.IsUnrestricted ())
94 return new HostProtectionPermission (PermissionState.Unrestricted);
96 return new HostProtectionPermission (_resources | hpp._resources);
99 public override bool IsSubsetOf (IPermission target)
101 HostProtectionPermission hpp = Cast (target);
102 if (hpp == null)
103 return (_resources == HostProtectionResource.None);
105 if (hpp.IsUnrestricted ())
106 return true;
107 if (this.IsUnrestricted ())
108 return false;
110 return ((_resources & ~hpp._resources) == 0);
113 public override void FromXml (SecurityElement e)
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)
120 _resources = (HostProtectionResource) Enum.Parse (
121 typeof (HostProtectionResource), e.Attribute ("Resources"));
124 public override SecurityElement ToXml ()
126 SecurityElement e = Element (version);
127 e.AddAttribute ("Resources", _resources.ToString ());
128 return e;
131 // IUnrestrictedPermission
132 public bool IsUnrestricted ()
134 return (_resources == HostProtectionResource.All);
137 // IBuiltInPermission
138 int IBuiltInPermission.GetTokenIndex ()
140 return (int) BuiltInToken.HostProtection;
143 // helpers
145 private HostProtectionPermission Cast (IPermission target)
147 if (target == null)
148 return null;
150 HostProtectionPermission hpp = (target as HostProtectionPermission);
151 if (hpp == null) {
152 ThrowInvalidPermission (target, typeof (HostProtectionPermission));
155 return hpp;