2 // System.AppDomainSetup.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Fix serialization compatibility with MS.NET.
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections.Generic;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Security;
39 using System.Runtime.Serialization.Formatters.Binary;
41 using System.Runtime.Hosting;
42 using System.Security.Policy;
47 [ClassInterface (ClassInterfaceType.None)]
49 [StructLayout (LayoutKind.Sequential)]
51 public sealed class AppDomainSetup
53 public sealed class AppDomainSetup : IAppDomainSetup
56 string application_base;
57 string application_name;
59 string configuration_file;
62 string private_bin_path;
63 string private_bin_path_probe;
64 string shadow_copy_directories;
65 string shadow_copy_files;
66 bool publisher_policy;
67 private bool path_changed;
68 private LoaderOptimization loader_optimization;
69 bool disallow_binding_redirects;
70 bool disallow_code_downloads;
72 private ActivationArguments _activationArguments;
73 AppDomainInitializer domain_initializer;
75 ApplicationTrust application_trust;
76 string [] domain_initializer_args;
78 bool disallow_appbase_probe;
79 byte [] configuration_bytes;
81 byte [] serialized_non_primitives;
83 public AppDomainSetup ()
87 internal AppDomainSetup (AppDomainSetup setup)
89 application_base = setup.application_base;
90 application_name = setup.application_name;
91 cache_path = setup.cache_path;
92 configuration_file = setup.configuration_file;
93 dynamic_base = setup.dynamic_base;
94 license_file = setup.license_file;
95 private_bin_path = setup.private_bin_path;
96 private_bin_path_probe = setup.private_bin_path_probe;
97 shadow_copy_directories = setup.shadow_copy_directories;
98 shadow_copy_files = setup.shadow_copy_files;
99 publisher_policy = setup.publisher_policy;
100 path_changed = setup.path_changed;
101 loader_optimization = setup.loader_optimization;
102 disallow_binding_redirects = setup.disallow_binding_redirects;
103 disallow_code_downloads = setup.disallow_code_downloads;
104 _activationArguments = setup._activationArguments;
105 domain_initializer = setup.domain_initializer;
106 application_trust = setup.application_trust;
107 domain_initializer_args = setup.domain_initializer_args;
108 disallow_appbase_probe = setup.disallow_appbase_probe;
109 configuration_bytes = setup.configuration_bytes;
112 public AppDomainSetup (ActivationArguments activationArguments)
114 _activationArguments = activationArguments;
117 public AppDomainSetup (ActivationContext activationContext)
119 _activationArguments = new ActivationArguments (activationContext);
122 static string GetAppBase (string appBase)
127 int len = appBase.Length;
128 if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
129 appBase = appBase.Substring (7);
130 if (Path.DirectorySeparatorChar != '/')
131 appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
132 if (Environment.IsRunningOnWindows) {
133 // Under Windows prepend "//" to indicate it's a local file
134 appBase = "//" + appBase;
137 appBase = Path.GetFullPath (appBase);
143 public string ApplicationBase {
144 get { return GetAppBase (application_base); }
145 set { application_base = value; }
148 public string ApplicationName {
150 return application_name;
153 application_name = value;
157 public string CachePath {
166 public string ConfigurationFile {
168 if (configuration_file == null)
170 if (Path.IsPathRooted(configuration_file))
171 return configuration_file;
172 if (ApplicationBase == null)
173 throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
174 return Path.Combine(ApplicationBase, configuration_file);
177 configuration_file = value;
181 public bool DisallowPublisherPolicy {
183 return publisher_policy;
186 publisher_policy = value;
190 public string DynamicBase {
192 if (dynamic_base == null)
195 if (Path.IsPathRooted (dynamic_base))
198 if (ApplicationBase == null)
199 throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
201 return Path.Combine (ApplicationBase, dynamic_base);
204 if (application_name == null)
205 throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
206 uint id = (uint) application_name.GetHashCode ();
207 dynamic_base = Path.Combine (value, id.ToString("x"));
211 public string LicenseFile {
216 license_file = value;
220 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
221 public LoaderOptimization LoaderOptimization {
223 return loader_optimization;
226 loader_optimization = value;
230 public string PrivateBinPath {
232 return private_bin_path;
235 private_bin_path = value;
240 public string PrivateBinPathProbe {
242 return private_bin_path_probe;
245 private_bin_path_probe = value;
250 public string ShadowCopyDirectories {
252 return shadow_copy_directories;
255 shadow_copy_directories = value;
259 public string ShadowCopyFiles {
261 return shadow_copy_files;
264 shadow_copy_files = value;
268 public bool DisallowBindingRedirects {
270 return disallow_binding_redirects;
273 disallow_binding_redirects = value;
277 public bool DisallowCodeDownload {
279 return disallow_code_downloads;
282 disallow_code_downloads = value;
286 public ActivationArguments ActivationArguments {
288 if (_activationArguments != null)
289 return _activationArguments;
290 DeserializeNonPrimitives ();
291 return _activationArguments;
293 set { _activationArguments = value; }
296 [MonoLimitation ("it needs to be invoked within the created domain")]
297 public AppDomainInitializer AppDomainInitializer {
299 if (domain_initializer != null)
300 return domain_initializer;
301 DeserializeNonPrimitives ();
302 return domain_initializer;
304 set { domain_initializer = value; }
307 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
308 public string [] AppDomainInitializerArguments {
309 get { return domain_initializer_args; }
310 set { domain_initializer_args = value; }
313 [MonoNotSupported ("This property exists but not considered.")]
314 public ApplicationTrust ApplicationTrust {
316 if (application_trust != null)
317 return application_trust;
318 DeserializeNonPrimitives ();
319 if (application_trust == null)
320 application_trust = new ApplicationTrust ();
321 return application_trust;
323 set { application_trust = value; }
326 [MonoNotSupported ("This property exists but not considered.")]
327 public bool DisallowApplicationBaseProbing {
328 get { return disallow_appbase_probe; }
329 set { disallow_appbase_probe = value; }
332 [MonoNotSupported ("This method exists but not considered.")]
333 public byte [] GetConfigurationBytes ()
335 return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
338 [MonoNotSupported ("This method exists but not considered.")]
339 public void SetConfigurationBytes (byte [] value)
341 configuration_bytes = value;
344 private void DeserializeNonPrimitives ()
347 if (serialized_non_primitives == null)
350 BinaryFormatter bf = new BinaryFormatter ();
351 MemoryStream ms = new MemoryStream (serialized_non_primitives);
353 object [] arr = (object []) bf.Deserialize (ms);
355 _activationArguments = (ActivationArguments) arr [0];
356 domain_initializer = (AppDomainInitializer) arr [1];
357 application_trust = (ApplicationTrust) arr [2];
359 serialized_non_primitives = null;
363 internal void SerializeNonPrimitives ()
365 object [] arr = new object [3];
367 arr [0] = _activationArguments;
368 arr [1] = domain_initializer;
369 arr [2] = application_trust;
371 BinaryFormatter bf = new BinaryFormatter ();
372 MemoryStream ms = new MemoryStream ();
374 bf.Serialize (ms, arr);
376 serialized_non_primitives = ms.ToArray ();
380 [MonoTODO ("not implemented, does not throw because it's used in testing moonlight")]
381 public void SetCompatibilitySwitches (IEnumerable<string> switches)