Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / namedpermissionset.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //  NamedPermissionSet.cs
7 // 
8 // <OWNER>Microsoft</OWNER>
9 //
10 //  Extends PermissionSet to allow an associated name and description
11 //
12
13 namespace System.Security {
14     
15     using System;
16     using System.Security.Util;
17     using System.Security.Permissions;
18     using System.Runtime.Serialization;
19     using System.Diagnostics.Contracts;
20
21 #if !FEATURE_CAS_POLICY
22     using Microsoft.Win32;
23     using System.Collections;
24     using System.Globalization;
25     using System.IO;
26     using System.Reflection;
27     using System.Runtime.InteropServices;
28     using System.Runtime.Remoting;
29     using System.Runtime.Versioning;
30     using System.Text;
31     
32 #else // FEATURE_CAS_POLICY
33     
34     using System.Threading;
35
36 #endif // FEATURE_CAS_POLICY
37     
38     [Serializable]
39 [System.Runtime.InteropServices.ComVisible(true)]
40     public sealed class NamedPermissionSet : PermissionSet
41     {
42 #if FEATURE_CAS_POLICY
43         // The name of this PermissionSet
44         private String m_name;
45         
46         // The description of this PermissionSet
47         private String m_description;
48         [OptionalField(VersionAdded = 2)]                
49         internal String m_descrResource;
50
51         internal NamedPermissionSet()
52             : base()
53         {
54         }
55         
56         public NamedPermissionSet( String name )
57             : base()
58         {
59             CheckName( name );
60             m_name = name;
61         }
62         
63         public NamedPermissionSet( String name, PermissionState state)
64             : base( state )
65         {
66             CheckName( name );
67             m_name = name;
68         }
69         
70         
71         public NamedPermissionSet( String name, PermissionSet permSet )
72             : base( permSet )
73         {
74             CheckName( name );
75             m_name = name;
76         }
77
78         public NamedPermissionSet( NamedPermissionSet permSet )
79             : base( permSet )
80         {
81             m_name = permSet.m_name;
82             m_description = permSet.Description;
83         }
84
85         internal NamedPermissionSet(SecurityElement permissionSetXml)
86             : base(PermissionState.None)
87         {
88             Contract.Assert(permissionSetXml != null);
89             FromXml(permissionSetXml);
90         }
91
92         public String Name {
93             get { return m_name; }
94             set { CheckName( value ); m_name = value; }
95         }
96     
97         private static void CheckName( String name )
98         {
99             if (name == null || name.Equals( "" ))
100                 throw new ArgumentException( Environment.GetResourceString( "Argument_NPMSInvalidName" ));
101             Contract.EndContractBlock();
102         }
103         
104         public String Description {
105             get
106             {
107                 if(m_descrResource != null)
108                 {
109                     m_description = Environment.GetResourceString(m_descrResource);
110                     m_descrResource = null;
111                 }
112                 return m_description;
113             }
114
115             set
116             {
117                 m_description = value;
118                 m_descrResource = null;
119             }
120         }
121         
122         public override PermissionSet Copy()
123         {
124             return new NamedPermissionSet( this );
125         }
126         
127         public NamedPermissionSet Copy( String name )
128         {
129             NamedPermissionSet set = new NamedPermissionSet( this );
130             set.Name = name;
131             return set;
132         }
133         
134         public override SecurityElement ToXml()
135         {
136             SecurityElement elem = base.ToXml("System.Security.NamedPermissionSet");
137             // If you hit this assert then most likely you are trying to change the name of this class. 
138             // This is ok as long as you change the hard coded string above and change the assert below.
139             Contract.Assert( this.GetType().FullName.Equals( "System.Security.NamedPermissionSet" ), "Class name changed!" );
140
141             if (m_name != null && !m_name.Equals( "" ))
142             {
143                 elem.AddAttribute( "Name", SecurityElement.Escape( m_name ) );
144             }
145             
146             if (Description != null && !Description.Equals( "" ))
147             {
148                 elem.AddAttribute( "Description", SecurityElement.Escape( Description ) );
149             }
150             
151             return elem;
152         }
153         
154         public override void FromXml( SecurityElement et )
155         {
156             FromXml( et, false, false );
157         }
158
159         internal override void FromXml( SecurityElement et, bool allowInternalOnly, bool ignoreTypeLoadFailures )
160         {
161             if (et == null)
162                 throw new ArgumentNullException( "et" );
163             Contract.EndContractBlock();
164
165             String elem;
166
167             elem = et.Attribute( "Name" );
168             m_name = elem == null ? null : elem;
169
170             elem = et.Attribute( "Description" );
171             m_description = (elem == null ? "" : elem);
172             m_descrResource = null;
173
174             base.FromXml( et, allowInternalOnly, ignoreTypeLoadFailures );
175         }
176
177         internal void FromXmlNameOnly( SecurityElement et )
178         {
179             // This function gets only the name for the permission set, ignoring all other info.
180
181             String elem;
182
183             elem = et.Attribute( "Name" );
184             m_name = (elem == null ? null : elem);
185         }
186
187         // NamedPermissionSet Equals should have the exact semantic as PermissionSet.
188         // We explicitly override them here to make sure that no one accidently
189         // changes this.
190
191         [System.Runtime.InteropServices.ComVisible(false)]
192         public override bool Equals( Object obj )
193         {
194             return base.Equals( obj );
195         }
196
197         [System.Runtime.InteropServices.ComVisible(false)]
198         public override int GetHashCode()
199         {
200             return base.GetHashCode();
201         }
202
203         private static Object s_InternalSyncObject;
204         private static Object InternalSyncObject {
205             get {
206                 if (s_InternalSyncObject == null) {
207                     Object o = new Object();
208                     Interlocked.CompareExchange(ref s_InternalSyncObject, o, null);
209                 }
210                 return s_InternalSyncObject;
211             }
212         }
213 #else // FEATURE_CAS_POLICY
214
215         internal static PermissionSet GetBuiltInSet(string name) {
216             // Used by PermissionSetAttribute to create one of the built-in,
217             // immutable permission sets.
218         
219             if (name == null)
220                 return null;
221             else if (name.Equals("FullTrust"))
222                 return CreateFullTrustSet();
223             else if (name.Equals("Nothing"))
224                 return CreateNothingSet();
225             else if (name.Equals("Execution"))
226                 return CreateExecutionSet();
227             else if (name.Equals("SkipVerification"))
228                 return CreateSkipVerificationSet();
229             else if (name.Equals("Internet"))
230                 return CreateInternetSet();
231             else
232                 return null;
233         }
234
235         private static PermissionSet CreateFullTrustSet() {
236             return new PermissionSet(PermissionState.Unrestricted);
237         }
238
239         private static PermissionSet CreateNothingSet() {
240             return new PermissionSet(PermissionState.None);
241         }
242
243         private static PermissionSet CreateExecutionSet() {
244             PermissionSet permSet = new PermissionSet(PermissionState.None);
245 #pragma warning disable 618
246             permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
247 #pragma warning restore 618
248             return permSet;
249         }
250
251         private static PermissionSet CreateSkipVerificationSet() {
252             PermissionSet permSet = new PermissionSet(PermissionState.None);
253 #pragma warning disable 618
254             permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.SkipVerification));
255 #pragma warning restore 618
256             return permSet;
257         }
258
259         private static PermissionSet CreateInternetSet() {
260             PermissionSet permSet = new PermissionSet(PermissionState.None);
261             permSet.AddPermission(new FileDialogPermission(FileDialogPermissionAccess.Open));
262 #pragma warning disable 618
263             permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
264 #pragma warning restore 618
265             permSet.AddPermission(new UIPermission(UIPermissionWindow.SafeTopLevelWindows, UIPermissionClipboard.OwnClipboard));
266             return permSet;
267             
268
269         }
270 #endif // !FEATURE_CAS_POLICY
271     }
272 }