2007-02-18 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / UIPermission.cs
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.
28 //
29
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32
33 namespace System.Security.Permissions {
34
35 #if NET_2_0
36         [ComVisible (true)]
37 #endif
38         [Serializable]
39         public sealed class UIPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
40
41                 private UIPermissionWindow _window;             // Note: this (looks like) but isn't a [Flags]
42                 private UIPermissionClipboard _clipboard;       // Note: this (looks like) but isn't a [Flags]
43
44                 private const int version = 1;
45
46                 // Constructors
47
48                 public UIPermission (PermissionState state) 
49                 {
50                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
51                                 _clipboard = UIPermissionClipboard.AllClipboard;
52                                 _window = UIPermissionWindow.AllWindows;
53                         }
54                 }
55
56                 public UIPermission (UIPermissionClipboard clipboardFlag) 
57                 {
58                         // reuse validation by the Clipboard property
59                         Clipboard = clipboardFlag;
60                 }
61
62                 public UIPermission (UIPermissionWindow windowFlag) 
63                 {
64                         // reuse validation by the Window property
65                         Window = windowFlag;
66                 }
67
68                 public UIPermission (UIPermissionWindow windowFlag, UIPermissionClipboard clipboardFlag) 
69                 {
70                         // reuse validation by the Clipboard and Window properties
71                         Clipboard = clipboardFlag;
72                         Window = windowFlag;
73                 }
74
75                 // Properties
76
77                 public UIPermissionClipboard Clipboard {
78                         get { return _clipboard; }
79                         set {
80                                 if (!Enum.IsDefined (typeof (UIPermissionClipboard), value)) {
81                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
82                                         throw new ArgumentException (msg, "UIPermissionClipboard");
83                                 }
84                                 _clipboard = value;
85                         }
86                 }
87
88                 public UIPermissionWindow Window { 
89                         get { return _window; }
90                         set {
91                                 if (!Enum.IsDefined (typeof (UIPermissionWindow), value)) {
92                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
93                                         throw new ArgumentException (msg, "UIPermissionWindow");
94                                 }
95                                 _window = value;
96                         }
97                 }
98
99                 // Methods
100
101                 public override IPermission Copy () 
102                 {
103                         return new UIPermission (_window, _clipboard);
104                 }
105
106                 public override void FromXml (SecurityElement esd) 
107                 {
108                         // General validation in CodeAccessPermission
109                         CheckSecurityElement (esd, "esd", version, version);
110                         // Note: we do not (yet) care about the return value 
111                         // as we only accept version 1 (min/max values)
112
113                         if (IsUnrestricted (esd)) {
114                                 _window = UIPermissionWindow.AllWindows;
115                                 _clipboard = UIPermissionClipboard.AllClipboard;
116                         }
117                         else {
118                                 string w = esd.Attribute ("Window");
119                                 if (w == null)
120                                         _window = UIPermissionWindow.NoWindows;
121                                 else
122                                         _window = (UIPermissionWindow) Enum.Parse (typeof (UIPermissionWindow), w);
123
124                                 string c = esd.Attribute ("Clipboard");
125                                 if (c == null)
126                                         _clipboard = UIPermissionClipboard.NoClipboard;
127                                 else
128                                         _clipboard = (UIPermissionClipboard) Enum.Parse (typeof (UIPermissionClipboard), c);
129                         }
130                 }
131
132                 public override IPermission Intersect (IPermission target) 
133                 {
134                         UIPermission uip = Cast (target);
135                         if (uip == null)
136                                 return null;
137
138                         // there are not [Flags] so we can't use boolean operators
139                         UIPermissionWindow w = ((_window < uip._window) ? _window : uip._window);
140                         UIPermissionClipboard c = ((_clipboard < uip._clipboard) ? _clipboard : uip._clipboard);
141
142                         if (IsEmpty (w, c))
143                                 return null;
144
145                         return new UIPermission (w, c);
146                 }
147
148                 public override bool IsSubsetOf (IPermission target) 
149                 {
150                         UIPermission uip = Cast (target);
151                         if (uip == null)
152                                 return IsEmpty (_window, _clipboard);
153                         if (uip.IsUnrestricted ())
154                                 return true;
155
156                         // there are not [Flags] so we can't use boolean operators
157                         return ((_window <= uip._window) && (_clipboard <= uip._clipboard));
158                 }
159
160                 public bool IsUnrestricted () 
161                 {
162                         return ((_window == UIPermissionWindow.AllWindows) &&
163                                 (_clipboard == UIPermissionClipboard.AllClipboard));
164                 }
165
166                 public override SecurityElement ToXml () 
167                 {
168                         SecurityElement e = Element (version);
169
170                         if (_window == UIPermissionWindow.AllWindows && _clipboard == UIPermissionClipboard.AllClipboard) {
171                                 e.AddAttribute ("Unrestricted", "true");
172                         }
173                         else {
174                                 if (_window != UIPermissionWindow.NoWindows)
175                                         e.AddAttribute ("Window", _window.ToString ());
176
177                                 if (_clipboard != UIPermissionClipboard.NoClipboard)
178                                         e.AddAttribute ("Clipboard", _clipboard.ToString ());
179                         }
180                         return e;
181                 }
182
183                 public override IPermission Union (IPermission target)
184                 {
185                         UIPermission uip = Cast (target);
186                         if (uip == null)
187                                 return Copy ();
188
189                         // there are not [Flags] so we can't use boolean operators
190                         UIPermissionWindow w = ((_window > uip._window) ? _window : uip._window);
191                         UIPermissionClipboard c = ((_clipboard > uip._clipboard) ? _clipboard : uip._clipboard);
192
193                         if (IsEmpty (w, c))
194                                 return null;
195
196                         return new UIPermission (w, c);
197                 }
198
199                 // IBuiltInPermission
200                 int IBuiltInPermission.GetTokenIndex ()
201                 {
202                         return (int) BuiltInToken.UI;
203                 }
204
205                 // helpers
206
207                 private bool IsEmpty (UIPermissionWindow w, UIPermissionClipboard c)
208                 {
209                         return ((w == UIPermissionWindow.NoWindows) && (c == UIPermissionClipboard.NoClipboard));
210                 }
211
212                 private UIPermission Cast (IPermission target)
213                 {
214                         if (target == null)
215                                 return null;
216
217                         UIPermission uip = (target as UIPermission);
218                         if (uip == null) {
219                                 ThrowInvalidPermission (target, typeof (UIPermission));
220                         }
221
222                         return uip;
223                 }
224         }
225 }