2 // System.Security.Permissions.UIPermission.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Globalization
;
31 using System
.Runtime
.InteropServices
;
33 namespace System
.Security
.Permissions
{
37 public sealed class UIPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
39 private UIPermissionWindow _window
; // Note: this (looks like) but isn't a [Flags]
40 private UIPermissionClipboard _clipboard
; // Note: this (looks like) but isn't a [Flags]
42 private const int version
= 1;
46 public UIPermission (PermissionState state
)
48 if (CheckPermissionState (state
, true) == PermissionState
.Unrestricted
) {
49 _clipboard
= UIPermissionClipboard
.AllClipboard
;
50 _window
= UIPermissionWindow
.AllWindows
;
54 public UIPermission (UIPermissionClipboard clipboardFlag
)
56 // reuse validation by the Clipboard property
57 Clipboard
= clipboardFlag
;
60 public UIPermission (UIPermissionWindow windowFlag
)
62 // reuse validation by the Window property
66 public UIPermission (UIPermissionWindow windowFlag
, UIPermissionClipboard clipboardFlag
)
68 // reuse validation by the Clipboard and Window properties
69 Clipboard
= clipboardFlag
;
75 public UIPermissionClipboard Clipboard
{
76 get { return _clipboard; }
78 if (!Enum
.IsDefined (typeof (UIPermissionClipboard
), value)) {
79 string msg
= String
.Format (Locale
.GetText ("Invalid enum {0}"), value);
80 throw new ArgumentException (msg
, "UIPermissionClipboard");
86 public UIPermissionWindow Window
{
87 get { return _window; }
89 if (!Enum
.IsDefined (typeof (UIPermissionWindow
), value)) {
90 string msg
= String
.Format (Locale
.GetText ("Invalid enum {0}"), value);
91 throw new ArgumentException (msg
, "UIPermissionWindow");
99 public override IPermission
Copy ()
101 return new UIPermission (_window
, _clipboard
);
104 public override void FromXml (SecurityElement esd
)
106 // General validation in CodeAccessPermission
107 CheckSecurityElement (esd
, "esd", version
, version
);
108 // Note: we do not (yet) care about the return value
109 // as we only accept version 1 (min/max values)
111 if (IsUnrestricted (esd
)) {
112 _window
= UIPermissionWindow
.AllWindows
;
113 _clipboard
= UIPermissionClipboard
.AllClipboard
;
116 string w
= esd
.Attribute ("Window");
118 _window
= UIPermissionWindow
.NoWindows
;
120 _window
= (UIPermissionWindow
) Enum
.Parse (typeof (UIPermissionWindow
), w
);
122 string c
= esd
.Attribute ("Clipboard");
124 _clipboard
= UIPermissionClipboard
.NoClipboard
;
126 _clipboard
= (UIPermissionClipboard
) Enum
.Parse (typeof (UIPermissionClipboard
), c
);
130 public override IPermission
Intersect (IPermission target
)
132 UIPermission uip
= Cast (target
);
136 // there are not [Flags] so we can't use boolean operators
137 UIPermissionWindow w
= ((_window
< uip
._window
) ? _window
: uip
._window
);
138 UIPermissionClipboard c
= ((_clipboard
< uip
._clipboard
) ? _clipboard
: uip
._clipboard
);
143 return new UIPermission (w
, c
);
146 public override bool IsSubsetOf (IPermission target
)
148 UIPermission uip
= Cast (target
);
150 return IsEmpty (_window
, _clipboard
);
151 if (uip
.IsUnrestricted ())
154 // there are not [Flags] so we can't use boolean operators
155 return ((_window
<= uip
._window
) && (_clipboard
<= uip
._clipboard
));
158 public bool IsUnrestricted ()
160 return ((_window
== UIPermissionWindow
.AllWindows
) &&
161 (_clipboard
== UIPermissionClipboard
.AllClipboard
));
164 public override SecurityElement
ToXml ()
166 SecurityElement e
= Element (version
);
168 if (_window
== UIPermissionWindow
.AllWindows
&& _clipboard
== UIPermissionClipboard
.AllClipboard
) {
169 e
.AddAttribute ("Unrestricted", "true");
172 if (_window
!= UIPermissionWindow
.NoWindows
)
173 e
.AddAttribute ("Window", _window
.ToString ());
175 if (_clipboard
!= UIPermissionClipboard
.NoClipboard
)
176 e
.AddAttribute ("Clipboard", _clipboard
.ToString ());
181 public override IPermission
Union (IPermission target
)
183 UIPermission uip
= Cast (target
);
187 // there are not [Flags] so we can't use boolean operators
188 UIPermissionWindow w
= ((_window
> uip
._window
) ? _window
: uip
._window
);
189 UIPermissionClipboard c
= ((_clipboard
> uip
._clipboard
) ? _clipboard
: uip
._clipboard
);
194 return new UIPermission (w
, c
);
197 // IBuiltInPermission
198 int IBuiltInPermission
.GetTokenIndex ()
200 return (int) BuiltInToken
.UI
;
205 private bool IsEmpty (UIPermissionWindow w
, UIPermissionClipboard c
)
207 return ((w
== UIPermissionWindow
.NoWindows
) && (c
== UIPermissionClipboard
.NoClipboard
));
210 private UIPermission
Cast (IPermission target
)
215 UIPermission uip
= (target
as UIPermission
);
217 ThrowInvalidPermission (target
, typeof (UIPermission
));