Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / mscorlib / system / security / policy / site.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 // 
8
9 //
10 //  Site.cs
11 //
12 //  Site is an IIdentity representing internet sites.
13 //
14
15 using System;
16 using System.Diagnostics.Contracts;
17 using System.Globalization;
18 using System.Security.Permissions;
19 using System.Security.Util;
20
21 namespace System.Security.Policy
22 {
23     [Serializable]
24     [System.Runtime.InteropServices.ComVisible(true)]
25     public sealed class Site : EvidenceBase, IIdentityPermissionFactory
26     {
27         private SiteString m_name;
28
29         public Site(String name)
30         {
31             if (name == null)
32                 throw new ArgumentNullException("name");
33             Contract.EndContractBlock();
34
35             m_name = new SiteString( name );
36         }
37
38         private Site(SiteString name)
39         {
40             Contract.Assert(name != null);
41             m_name = name;
42         }
43
44         public static Site CreateFromUrl( String url )
45         {
46             return new Site(ParseSiteFromUrl(url));
47         }
48
49         private static SiteString ParseSiteFromUrl( String name )
50         {
51             URLString urlString = new URLString( name );
52
53             if (String.Compare( urlString.Scheme, "file", StringComparison.OrdinalIgnoreCase) == 0)
54                 throw new ArgumentException( Environment.GetResourceString( "Argument_InvalidSite" ) );
55
56             return new SiteString( new URLString( name ).Host );
57         }
58
59         public String Name
60         {
61             get { return m_name.ToString(); }
62         }
63
64         internal SiteString GetSiteString()
65         {
66             return m_name;
67         }
68
69         public IPermission CreateIdentityPermission( Evidence evidence )
70         {
71             return new SiteIdentityPermission( Name );
72         }
73
74         public override bool Equals(Object o)
75         {
76             Site other = o as Site;
77             if (other == null)
78             {
79                 return false;
80             }
81
82             return String.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase);
83         }
84
85         public override int GetHashCode()
86         {
87             return Name.GetHashCode();
88         }
89
90         public override EvidenceBase Clone()
91         {
92             return new Site(m_name);
93         }
94
95         public Object Copy()
96         {
97             return Clone();
98         }
99
100 #if FEATURE_CAS_POLICY
101         internal SecurityElement ToXml()
102         {
103             SecurityElement elem = new SecurityElement( "System.Security.Policy.Site" );
104             // If you hit this assert then most likely you are trying to change the name of this class. 
105             // This is ok as long as you change the hard coded string above and change the assert below.
106             Contract.Assert( this.GetType().FullName.Equals( "System.Security.Policy.Site" ), "Class name changed!" );
107
108             elem.AddAttribute( "version", "1" );
109             
110             if(m_name != null)
111                 elem.AddChild( new SecurityElement( "Name", m_name.ToString() ) );
112                 
113             return elem;
114         }
115 #endif // FEATURE_CAS_POLICY
116
117 #if FEATURE_CAS_POLICY
118         public override String ToString()
119         {
120             return ToXml().ToString();
121         }
122 #endif // FEATURE_CAS_POLICY
123
124         // INormalizeForIsolatedStorage is not implemented for startup perf
125         // equivalent to INormalizeForIsolatedStorage.Normalize()
126         internal Object Normalize()
127         {
128             return m_name.ToString().ToUpper(CultureInfo.InvariantCulture);
129         }
130     }
131 }