2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Security.Permissions / UIPermission.cs
blob9c40dd8b928cfe231ebbe91baac84fa9438ee121
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-2005 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;
31 using System.Runtime.InteropServices;
33 namespace System.Security.Permissions {
35 [ComVisible (true)]
36 [Serializable]
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;
44 // Constructors
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
63 Window = windowFlag;
66 public UIPermission (UIPermissionWindow windowFlag, UIPermissionClipboard clipboardFlag)
68 // reuse validation by the Clipboard and Window properties
69 Clipboard = clipboardFlag;
70 Window = windowFlag;
73 // Properties
75 public UIPermissionClipboard Clipboard {
76 get { return _clipboard; }
77 set {
78 if (!Enum.IsDefined (typeof (UIPermissionClipboard), value)) {
79 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
80 throw new ArgumentException (msg, "UIPermissionClipboard");
82 _clipboard = value;
86 public UIPermissionWindow Window {
87 get { return _window; }
88 set {
89 if (!Enum.IsDefined (typeof (UIPermissionWindow), value)) {
90 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
91 throw new ArgumentException (msg, "UIPermissionWindow");
93 _window = value;
97 // Methods
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;
115 else {
116 string w = esd.Attribute ("Window");
117 if (w == null)
118 _window = UIPermissionWindow.NoWindows;
119 else
120 _window = (UIPermissionWindow) Enum.Parse (typeof (UIPermissionWindow), w);
122 string c = esd.Attribute ("Clipboard");
123 if (c == null)
124 _clipboard = UIPermissionClipboard.NoClipboard;
125 else
126 _clipboard = (UIPermissionClipboard) Enum.Parse (typeof (UIPermissionClipboard), c);
130 public override IPermission Intersect (IPermission target)
132 UIPermission uip = Cast (target);
133 if (uip == null)
134 return null;
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);
140 if (IsEmpty (w, c))
141 return null;
143 return new UIPermission (w, c);
146 public override bool IsSubsetOf (IPermission target)
148 UIPermission uip = Cast (target);
149 if (uip == null)
150 return IsEmpty (_window, _clipboard);
151 if (uip.IsUnrestricted ())
152 return true;
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");
171 else {
172 if (_window != UIPermissionWindow.NoWindows)
173 e.AddAttribute ("Window", _window.ToString ());
175 if (_clipboard != UIPermissionClipboard.NoClipboard)
176 e.AddAttribute ("Clipboard", _clipboard.ToString ());
178 return e;
181 public override IPermission Union (IPermission target)
183 UIPermission uip = Cast (target);
184 if (uip == null)
185 return Copy ();
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);
191 if (IsEmpty (w, c))
192 return null;
194 return new UIPermission (w, c);
197 // IBuiltInPermission
198 int IBuiltInPermission.GetTokenIndex ()
200 return (int) BuiltInToken.UI;
203 // helpers
205 private bool IsEmpty (UIPermissionWindow w, UIPermissionClipboard c)
207 return ((w == UIPermissionWindow.NoWindows) && (c == UIPermissionClipboard.NoClipboard));
210 private UIPermission Cast (IPermission target)
212 if (target == null)
213 return null;
215 UIPermission uip = (target as UIPermission);
216 if (uip == null) {
217 ThrowInvalidPermission (target, typeof (UIPermission));
220 return uip;