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