Merge pull request #3647 from BrzVlad/fix-sgen-internal-alloc
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationTrustCollection.cs
1 //
2 // System.Security.Policy.ApplicationTrustCollection class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 namespace System.Security.Policy {
35
36         [ComVisible (true)]
37         public sealed class ApplicationTrustCollection : ICollection, IEnumerable {
38
39                 private ArrayList _list;
40
41                 internal ApplicationTrustCollection ()
42                 {
43                         _list = new ArrayList ();
44                 }
45
46                 // constants (from beta1 - still useful ?)
47
48 //              public const string ApplicationTrustProperty = "ApplicationTrust";
49 //              public const string InstallReferenceIdentifier = "{3f471841-eef2-47d6-89c0-d028f03a4ad5}";
50
51                 // properties
52
53                 public int Count {
54                         get { return _list.Count; }
55                 }
56
57                 public bool IsSynchronized {
58                         get { return false; }   // always false
59                 }
60
61                 public object SyncRoot {
62                         get { return this; }    // self
63                 }
64
65                 public ApplicationTrust this [int index] {
66                         get { return (ApplicationTrust) _list [index]; }
67                 }
68
69                 public ApplicationTrust this [string appFullName] {
70                         get {
71                                 for (int i=0; i < _list.Count; i++) {
72                                         ApplicationTrust at = (_list [i] as ApplicationTrust);
73                                         if (at.ApplicationIdentity.FullName == appFullName)
74                                                 return at;
75                                 }
76                                 return null;
77                         }
78                 }
79
80                 // methods
81
82                 public int Add (ApplicationTrust trust)
83                 {
84                         if (trust == null)
85                                 throw new ArgumentNullException ("trust");
86                         if (trust.ApplicationIdentity == null) {
87                                 throw new ArgumentException (Locale.GetText (
88                                         "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
89                         }
90
91                         return _list.Add (trust);
92                 }
93
94                 public void AddRange (ApplicationTrust[] trusts)
95                 {
96                         if (trusts == null)
97                                 throw new ArgumentNullException ("trusts");
98
99                         foreach (ApplicationTrust t in trusts) {
100                                 if (t.ApplicationIdentity == null) {
101                                         throw new ArgumentException (Locale.GetText (
102                                                 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
103                                 }
104                                 _list.Add (t);
105                         }
106                 }
107
108                 public void AddRange (ApplicationTrustCollection trusts)
109                 {
110                         if (trusts == null)
111                                 throw new ArgumentNullException ("trusts");
112
113                         foreach (ApplicationTrust t in trusts) {
114                                 if (t.ApplicationIdentity == null) {
115                                         throw new ArgumentException (Locale.GetText (
116                                                 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
117                                 }
118                                 _list.Add (t);
119                         }
120                 }
121
122                 public void Clear ()
123                 {
124                         _list.Clear ();
125                 }
126
127                 public void CopyTo (ApplicationTrust[] array, int index)
128                 {
129                         _list.CopyTo (array, index);
130                 }
131
132                 void ICollection.CopyTo (Array array, int index)
133                 {
134                         _list.CopyTo (array, index);
135                 }
136
137                 public ApplicationTrustCollection Find (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
138                 {
139                         if (applicationIdentity == null)
140                                 throw new ArgumentNullException ("applicationIdentity");
141
142                         string fullname = applicationIdentity.FullName;
143
144                         switch (versionMatch) {
145                         case ApplicationVersionMatch.MatchAllVersions:
146                                 int pos = fullname.IndexOf (", Version=");
147                                 if (pos >= 0)
148                                         fullname = fullname.Substring (0, pos);
149                                 break;
150                         case ApplicationVersionMatch.MatchExactVersion:
151                                 break;
152                         default:
153                                 throw new ArgumentException ("versionMatch");
154                         }
155
156                         ApplicationTrustCollection coll = new ApplicationTrustCollection ();
157                         foreach (ApplicationTrust t in _list) {
158                                 if (t.ApplicationIdentity.FullName.StartsWith (fullname)) {
159                                         coll.Add (t);
160                                 }
161                         }
162
163                         return coll;
164                 }
165
166                 public ApplicationTrustEnumerator GetEnumerator ()
167                 {
168                         return new ApplicationTrustEnumerator (this);
169                 }
170
171                 IEnumerator IEnumerable.GetEnumerator ()
172                 {
173                         return (IEnumerator) new ApplicationTrustEnumerator (this);
174                 }
175
176                 public void Remove (ApplicationTrust trust)
177                 {
178                         if (trust == null)
179                                 throw new ArgumentNullException ("trust");
180                         if (trust.ApplicationIdentity == null) {
181                                 throw new ArgumentException (Locale.GetText (
182                                         "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
183                         }
184
185                         RemoveAllInstances (trust);
186                 }
187
188                 public void Remove (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
189                 {
190                         ApplicationTrustCollection coll = Find (applicationIdentity, versionMatch);
191                         foreach (ApplicationTrust t in coll) {
192                                 RemoveAllInstances (t);
193                         }
194                 }
195
196                 public void RemoveRange (ApplicationTrust[] trusts)
197                 {
198                         if (trusts == null)
199                                 throw new ArgumentNullException ("trusts");
200
201                         foreach (ApplicationTrust t in trusts) {
202                                 RemoveAllInstances (t);
203                         }
204                 }
205
206                 public void RemoveRange (ApplicationTrustCollection trusts)
207                 {
208                         if (trusts == null)
209                                 throw new ArgumentNullException ("trusts");
210
211                         foreach (ApplicationTrust t in trusts) {
212                                 RemoveAllInstances (t);
213                         }
214                 }
215
216                 // helpers
217
218                 internal void RemoveAllInstances (ApplicationTrust trust)
219                 {
220                         for (int i=_list.Count - 1; i >= 0; i--) {
221                                 if (trust.Equals (_list [i]))
222                                         _list.RemoveAt (i);
223                         }
224                 }
225         }
226 }
227