**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Permissions / UIPermission.cs
blob955ede71aedbc18a714961e81ce99da27205a6bb
1 //
2 // System.Security.Permissions.UIPermission.cs
3 //
4 // Author
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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;
32 namespace System.Security.Permissions {
34 [Serializable]
35 public sealed class UIPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
37 private UIPermissionWindow _window; // Note: this (looks like) but isn't a [Flags]
38 private UIPermissionClipboard _clipboard; // Note: this (looks like) but isn't a [Flags]
40 private const int version = 1;
42 // Constructors
44 public UIPermission (PermissionState state)
46 if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
47 _clipboard = UIPermissionClipboard.AllClipboard;
48 _window = UIPermissionWindow.AllWindows;
52 public UIPermission (UIPermissionClipboard clipboardFlag)
54 // reuse validation by the Clipboard property
55 Clipboard = clipboardFlag;
58 public UIPermission (UIPermissionWindow windowFlag)
60 // reuse validation by the Window property
61 Window = windowFlag;
64 public UIPermission (UIPermissionWindow windowFlag, UIPermissionClipboard clipboardFlag)
66 // reuse validation by the Clipboard and Window properties
67 Clipboard = clipboardFlag;
68 Window = windowFlag;
71 // Properties
73 public UIPermissionClipboard Clipboard {
74 get { return _clipboard; }
75 set {
76 if (!Enum.IsDefined (typeof (UIPermissionClipboard), value)) {
77 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
78 throw new ArgumentException (msg, "UIPermissionClipboard");
80 _clipboard = value;
84 public UIPermissionWindow Window {
85 get { return _window; }
86 set {
87 if (!Enum.IsDefined (typeof (UIPermissionWindow), value)) {
88 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
89 throw new ArgumentException (msg, "UIPermissionWindow");
91 _window = value;
95 // Methods
97 public override IPermission Copy ()
99 return new UIPermission (_window, _clipboard);
102 public override void FromXml (SecurityElement esd)
104 // General validation in CodeAccessPermission
105 CheckSecurityElement (esd, "esd", version, version);
106 // Note: we do not (yet) care about the return value
107 // as we only accept version 1 (min/max values)
109 if (IsUnrestricted (esd)) {
110 _window = UIPermissionWindow.AllWindows;
111 _clipboard = UIPermissionClipboard.AllClipboard;
113 else {
114 string w = esd.Attribute ("Window");
115 if (w == null)
116 _window = UIPermissionWindow.NoWindows;
117 else
118 _window = (UIPermissionWindow) Enum.Parse (typeof (UIPermissionWindow), w);
120 string c = esd.Attribute ("Clipboard");
121 if (c == null)
122 _clipboard = UIPermissionClipboard.NoClipboard;
123 else
124 _clipboard = (UIPermissionClipboard) Enum.Parse (typeof (UIPermissionClipboard), c);
128 public override IPermission Intersect (IPermission target)
130 UIPermission uip = Cast (target);
131 if (uip == null)
132 return null;
134 // there are not [Flags] so we can't use boolean operators
135 UIPermissionWindow w = ((_window < uip._window) ? _window : uip._window);
136 UIPermissionClipboard c = ((_clipboard < uip._clipboard) ? _clipboard : uip._clipboard);
138 if (IsEmpty (w, c))
139 return null;
141 return new UIPermission (w, c);
144 public override bool IsSubsetOf (IPermission target)
146 UIPermission uip = Cast (target);
147 if (uip == null)
148 return IsEmpty (_window, _clipboard);
149 if (uip.IsUnrestricted ())
150 return true;
152 // there are not [Flags] so we can't use boolean operators
153 return ((_window <= uip._window) && (_clipboard <= uip._clipboard));
156 public bool IsUnrestricted ()
158 return ((_window == UIPermissionWindow.AllWindows) &&
159 (_clipboard == UIPermissionClipboard.AllClipboard));
162 public override SecurityElement ToXml ()
164 SecurityElement e = Element (version);
166 if (_window == UIPermissionWindow.AllWindows && _clipboard == UIPermissionClipboard.AllClipboard) {
167 e.AddAttribute ("Unrestricted", "true");
169 else {
170 if (_window != UIPermissionWindow.NoWindows)
171 e.AddAttribute ("Window", _window.ToString ());
173 if (_clipboard != UIPermissionClipboard.NoClipboard)
174 e.AddAttribute ("Clipboard", _clipboard.ToString ());
176 return e;
179 public override IPermission Union (IPermission target)
181 UIPermission uip = Cast (target);
182 if (uip == null)
183 return Copy ();
185 // there are not [Flags] so we can't use boolean operators
186 UIPermissionWindow w = ((_window > uip._window) ? _window : uip._window);
187 UIPermissionClipboard c = ((_clipboard > uip._clipboard) ? _clipboard : uip._clipboard);
189 if (IsEmpty (w, c))
190 return null;
192 return new UIPermission (w, c);
195 // IBuiltInPermission
196 int IBuiltInPermission.GetTokenIndex ()
198 return (int) BuiltInToken.UI;
201 // helpers
203 private bool IsEmpty (UIPermissionWindow w, UIPermissionClipboard c)
205 return ((w == UIPermissionWindow.NoWindows) && (c == UIPermissionClipboard.NoClipboard));
208 private UIPermission Cast (IPermission target)
210 if (target == null)
211 return null;
213 UIPermission uip = (target as UIPermission);
214 if (uip == null) {
215 ThrowInvalidPermission (target, typeof (UIPermission));
218 return uip;