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.
29 using System
.Globalization
;
31 namespace System
.Security
.Permissions
{
34 internal sealed class HostProtectionPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
36 private const int version
= 1;
38 private HostProtectionResource _resources
;
42 public HostProtectionPermission (PermissionState state
)
44 if (CheckPermissionState (state
, true) == PermissionState
.Unrestricted
)
45 _resources
= HostProtectionResource
.All
;
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; }
59 if (!Enum
.IsDefined (typeof (HostProtectionResource
), value)) {
60 string msg
= String
.Format (Locale
.GetText ("Invalid enum {0}"), value);
61 throw new ArgumentException (msg
, "HostProtectionResource");
67 public override IPermission
Copy ()
69 return new HostProtectionPermission (_resources
);
72 public override IPermission
Intersect (IPermission target
)
74 HostProtectionPermission hpp
= Cast (target
);
78 if (this.IsUnrestricted () && hpp
.IsUnrestricted ())
79 return new HostProtectionPermission (PermissionState
.Unrestricted
);
80 if (this.IsUnrestricted ())
82 if (hpp
.IsUnrestricted ())
84 return new HostProtectionPermission (_resources
& hpp
._resources
);
87 public override IPermission
Union (IPermission target
)
89 HostProtectionPermission hpp
= Cast (target
);
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
);
103 return (_resources
== HostProtectionResource
.None
);
105 if (hpp
.IsUnrestricted ())
107 if (this.IsUnrestricted ())
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 ());
131 // IUnrestrictedPermission
132 public bool IsUnrestricted ()
134 return (_resources
== HostProtectionResource
.All
);
137 // IBuiltInPermission
138 int IBuiltInPermission
.GetTokenIndex ()
140 return (int) BuiltInToken
.HostProtection
;
145 private HostProtectionPermission
Cast (IPermission target
)
150 HostProtectionPermission hpp
= (target
as HostProtectionPermission
);
152 ThrowInvalidPermission (target
, typeof (HostProtectionPermission
));