2010-01-25 Atsushi Enomoto <atsushi@ximian.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         [ComVisible (true)]
36         [Serializable]
37         public sealed class UIPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
38
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]
41
42                 private const int version = 1;
43
44                 // Constructors
45
46                 public UIPermission (PermissionState state) 
47                 {
48                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
49                                 _clipboard = UIPermissionClipboard.AllClipboard;
50                                 _window = UIPermissionWindow.AllWindows;
51                         }
52                 }
53
54                 public UIPermission (UIPermissionClipboard clipboardFlag) 
55                 {
56                         // reuse validation by the Clipboard property
57                         Clipboard = clipboardFlag;
58                 }
59
60                 public UIPermission (UIPermissionWindow windowFlag) 
61                 {
62                         // reuse validation by the Window property
63                         Window = windowFlag;
64                 }
65
66                 public UIPermission (UIPermissionWindow windowFlag, UIPermissionClipboard clipboardFlag) 
67                 {
68                         // reuse validation by the Clipboard and Window properties
69                         Clipboard = clipboardFlag;
70                         Window = windowFlag;
71                 }
72
73                 // Properties
74
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");
81                                 }
82                                 _clipboard = value;
83                         }
84                 }
85
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");
92                                 }
93                                 _window = value;
94                         }
95                 }
96
97                 // Methods
98
99                 public override IPermission Copy () 
100                 {
101                         return new UIPermission (_window, _clipboard);
102                 }
103
104                 public override void FromXml (SecurityElement esd) 
105                 {
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)
110
111                         if (IsUnrestricted (esd)) {
112                                 _window = UIPermissionWindow.AllWindows;
113                                 _clipboard = UIPermissionClipboard.AllClipboard;
114                         }
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);
121
122                                 string c = esd.Attribute ("Clipboard");
123                                 if (c == null)
124                                         _clipboard = UIPermissionClipboard.NoClipboard;
125                                 else
126                                         _clipboard = (UIPermissionClipboard) Enum.Parse (typeof (UIPermissionClipboard), c);
127                         }
128                 }
129
130                 public override IPermission Intersect (IPermission target) 
131                 {
132                         UIPermission uip = Cast (target);
133                         if (uip == null)
134                                 return null;
135
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);
139
140                         if (IsEmpty (w, c))
141                                 return null;
142
143                         return new UIPermission (w, c);
144                 }
145
146                 public override bool IsSubsetOf (IPermission target) 
147                 {
148                         UIPermission uip = Cast (target);
149                         if (uip == null)
150                                 return IsEmpty (_window, _clipboard);
151                         if (uip.IsUnrestricted ())
152                                 return true;
153
154                         // there are not [Flags] so we can't use boolean operators
155                         return ((_window <= uip._window) && (_clipboard <= uip._clipboard));
156                 }
157
158                 public bool IsUnrestricted () 
159                 {
160                         return ((_window == UIPermissionWindow.AllWindows) &&
161                                 (_clipboard == UIPermissionClipboard.AllClipboard));
162                 }
163
164                 public override SecurityElement ToXml () 
165                 {
166                         SecurityElement e = Element (version);
167
168                         if (_window == UIPermissionWindow.AllWindows && _clipboard == UIPermissionClipboard.AllClipboard) {
169                                 e.AddAttribute ("Unrestricted", "true");
170                         }
171                         else {
172                                 if (_window != UIPermissionWindow.NoWindows)
173                                         e.AddAttribute ("Window", _window.ToString ());
174
175                                 if (_clipboard != UIPermissionClipboard.NoClipboard)
176                                         e.AddAttribute ("Clipboard", _clipboard.ToString ());
177                         }
178                         return e;
179                 }
180
181                 public override IPermission Union (IPermission target)
182                 {
183                         UIPermission uip = Cast (target);
184                         if (uip == null)
185                                 return Copy ();
186
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);
190
191                         if (IsEmpty (w, c))
192                                 return null;
193
194                         return new UIPermission (w, c);
195                 }
196
197                 // IBuiltInPermission
198                 int IBuiltInPermission.GetTokenIndex ()
199                 {
200                         return (int) BuiltInToken.UI;
201                 }
202
203                 // helpers
204
205                 private bool IsEmpty (UIPermissionWindow w, UIPermissionClipboard c)
206                 {
207                         return ((w == UIPermissionWindow.NoWindows) && (c == UIPermissionClipboard.NoClipboard));
208                 }
209
210                 private UIPermission Cast (IPermission target)
211                 {
212                         if (target == null)
213                                 return null;
214
215                         UIPermission uip = (target as UIPermission);
216                         if (uip == null) {
217                                 ThrowInvalidPermission (target, typeof (UIPermission));
218                         }
219
220                         return uip;
221                 }
222         }
223 }