2 // System.Security.Permissions.HostProtectionPermission.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Globalization;
33 namespace System.Security.Permissions {
36 internal sealed class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
38 private const int version = 1;
40 private HostProtectionResource _resources;
44 public HostProtectionPermission (PermissionState state)
46 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
47 _resources = HostProtectionResource.All;
49 _resources = HostProtectionResource.None;
52 public HostProtectionPermission (HostProtectionResource resources)
54 // reuse validation by the Flags property
55 Resources = _resources;
58 public HostProtectionResource Resources {
59 get { return _resources; }
61 if (!Enum.IsDefined (typeof (HostProtectionResource), value)) {
62 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
63 throw new ArgumentException (msg, "HostProtectionResource");
69 public override IPermission Copy ()
71 return new HostProtectionPermission (_resources);
74 public override IPermission Intersect (IPermission target)
76 HostProtectionPermission hpp = Cast (target);
80 if (this.IsUnrestricted () && hpp.IsUnrestricted ())
81 return new HostProtectionPermission (PermissionState.Unrestricted);
82 if (this.IsUnrestricted ())
84 if (hpp.IsUnrestricted ())
86 return new HostProtectionPermission (_resources & hpp._resources);
89 public override IPermission Union (IPermission target)
91 HostProtectionPermission hpp = Cast (target);
95 if (this.IsUnrestricted () || hpp.IsUnrestricted ())
96 return new HostProtectionPermission (PermissionState.Unrestricted);
98 return new HostProtectionPermission (_resources | hpp._resources);
101 public override bool IsSubsetOf (IPermission target)
103 HostProtectionPermission hpp = Cast (target);
105 return (_resources == HostProtectionResource.None);
107 if (hpp.IsUnrestricted ())
109 if (this.IsUnrestricted ())
112 return ((_resources & ~hpp._resources) == 0);
115 public override void FromXml (SecurityElement e)
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)
122 _resources = (HostProtectionResource) Enum.Parse (
123 typeof (HostProtectionResource), e.Attribute ("Resources"));
126 public override SecurityElement ToXml ()
128 SecurityElement e = Element (version);
129 e.AddAttribute ("Resources", _resources.ToString ());
133 // IUnrestrictedPermission
134 public bool IsUnrestricted ()
136 return (_resources == HostProtectionResource.All);
139 // IBuiltInPermission
140 int IBuiltInPermission.GetTokenIndex ()
142 return (int) BuiltInToken.HostProtection;
147 private HostProtectionPermission Cast (IPermission target)
152 HostProtectionPermission hpp = (target as HostProtectionPermission);
154 ThrowInvalidPermission (target, typeof (HostProtectionPermission));