Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / policy / sitemembershipcondition.cs
1 using System.Diagnostics.Contracts;
2 // ==++==
3 // 
4 //   Copyright (c) Microsoft Corporation.  All rights reserved.
5 // 
6 // ==--==
7 //  SiteMembershipCondition.cs
8 // 
9 // <OWNER>Microsoft</OWNER>
10 //
11 //  Implementation of membership condition for zones
12 //
13
14 namespace System.Security.Policy {
15     
16     using System;
17     using SecurityManager = System.Security.SecurityManager;
18     using SiteString = System.Security.Util.SiteString;
19     using PermissionSet = System.Security.PermissionSet;
20     using SecurityElement = System.Security.SecurityElement;
21     using System.Collections;
22     using System.Globalization;
23
24     [Serializable]
25 [System.Runtime.InteropServices.ComVisible(true)]
26     sealed public class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition, IReportMatchMembershipCondition
27     {
28         
29         //------------------------------------------------------
30         //
31         // PRIVATE STATE DATA
32         //
33         //------------------------------------------------------
34         
35         private SiteString m_site;
36         private SecurityElement m_element;
37         
38         //------------------------------------------------------
39         //
40         // PUBLIC CONSTRUCTORS
41         //
42         //------------------------------------------------------
43     
44         internal SiteMembershipCondition()
45         {
46             m_site = null;
47         }
48         
49         public SiteMembershipCondition( String site )
50         {
51             if (site == null)
52                 throw new ArgumentNullException( "site" );
53             Contract.EndContractBlock();
54         
55             m_site = new SiteString( site );
56         }
57       
58         //------------------------------------------------------
59         //
60         // PUBLIC ACCESSOR METHODS
61         //
62         //------------------------------------------------------
63     
64
65         public String Site
66         {
67             set
68             {
69                 if (value == null)
70                     throw new ArgumentNullException( "value" );
71                 Contract.EndContractBlock();
72             
73                 m_site = new SiteString( value );
74             }
75         
76             get
77             {
78                 if (m_site == null && m_element != null)
79                     ParseSite();
80
81                 if (m_site != null)
82                     return m_site.ToString();
83                 else
84                     return "";
85             }
86         }
87                
88         //------------------------------------------------------
89         //
90         // IMEMBERSHIPCONDITION IMPLEMENTATION
91         //
92         //------------------------------------------------------
93     
94         public bool Check( Evidence evidence )
95         {
96             object usedEvidence = null;
97             return (this as IReportMatchMembershipCondition).Check(evidence, out usedEvidence);
98         }
99
100         bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
101         {
102             usedEvidence = null;
103
104             if (evidence == null)
105                 return false;
106
107             Site site = evidence.GetHostEvidence<Site>();
108             if (site != null)
109             {
110                 if (m_site == null && m_element != null)
111                 {
112                     ParseSite();
113                 }
114        
115                 if (site.GetSiteString().IsSubsetOf(this.m_site))
116                 {
117                     usedEvidence = site;
118                     return true;
119                 }
120             }
121             return false;
122         }
123         
124         public IMembershipCondition Copy()
125         {
126             if (m_site == null && m_element != null)
127                 ParseSite();
128                         
129             return new SiteMembershipCondition( m_site.ToString() );
130         }
131         
132         
133         public SecurityElement ToXml()
134         {
135             return ToXml( null );
136         }
137     
138         public void FromXml( SecurityElement e )
139         {
140             FromXml( e, null );
141         }
142         
143         public SecurityElement ToXml( PolicyLevel level )
144         {
145             if (m_site == null && m_element != null)
146                 ParseSite();
147                         
148             SecurityElement root = new SecurityElement( "IMembershipCondition" );
149             System.Security.Util.XMLUtil.AddClassAttribute( root, this.GetType(), "System.Security.Policy.SiteMembershipCondition" );
150             // If you hit this assert then most likely you are trying to change the name of this class. 
151             // This is ok as long as you change the hard coded string above and change the assert below.
152             Contract.Assert( this.GetType().FullName.Equals( "System.Security.Policy.SiteMembershipCondition" ), "Class name changed!" );
153
154             root.AddAttribute( "version", "1" );
155             
156             if (m_site != null)
157                 root.AddAttribute( "Site", m_site.ToString() );
158             
159             return root;
160         }
161     
162         public void FromXml( SecurityElement e, PolicyLevel level  )
163         {
164             if (e == null)
165                 throw new ArgumentNullException("e");
166         
167             if (!e.Tag.Equals( "IMembershipCondition" ))
168             {
169                 throw new ArgumentException( Environment.GetResourceString( "Argument_MembershipConditionElement" ) );
170             }
171             Contract.EndContractBlock();
172             
173             lock (this)
174             {
175                 m_site = null;
176                 m_element = e;
177             }
178         }
179             
180         private void ParseSite()
181         {   
182             lock (this)
183             {
184                 if (m_element == null)
185                     return;
186
187                 String elSite = m_element.Attribute( "Site" );
188                 if (elSite == null)
189                     throw new ArgumentException( Environment.GetResourceString( "Argument_SiteCannotBeNull" ) );
190                 else
191                     m_site = new SiteString( elSite );
192                 m_element = null;
193             }
194         }
195         
196         public override bool Equals( Object o )
197         {
198             SiteMembershipCondition that = (o as SiteMembershipCondition);
199             
200             if (that != null)
201             {
202                 if (this.m_site == null && this.m_element != null)
203                     this.ParseSite();
204                 if (that.m_site == null && that.m_element != null)
205                     that.ParseSite();
206                 
207                 if( Equals (this.m_site, that.m_site ))
208                 {
209                     return true;
210                 }
211             }
212             return false;
213         }
214         
215         public override int GetHashCode()
216         {
217             if (m_site == null && m_element != null)
218                 ParseSite();
219             
220             if (m_site != null)
221             {
222                 return m_site.GetHashCode();
223             }
224             else
225             {
226                 return typeof( SiteMembershipCondition ).GetHashCode();
227             }
228         }
229         
230         public override String ToString()
231         {
232             if (m_site == null && m_element != null)
233                 ParseSite();
234         
235             if (m_site != null)
236                 return String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Site_ToStringArg" ), m_site );
237             else
238                 return Environment.GetResourceString( "Site_ToString" );
239         }
240     }
241 }