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