The Labs \ Source Viewer \ SSCLI \ System.Runtime.Hosting \ ApplicationActivator

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //
  16. // ApplicationActivator class is the base class that handles activation of Add-ins.
  17. // There is a single designated instance of the ApplicationActivator in each Appdomain
  18. // to which all Add-in activation calls are routed to. The AppdomainManager for the
  19. // current Appdomain could provide its own custom ApplicationActivator, or an instance
  20. // of the default ApplicationActivator is created.
  21. //
  22. namespace System.Runtime.Hosting
  23. {
  24.     using System.Deployment.Internal.Isolation;
  25.     using System.Deployment.Internal.Isolation.Manifest;
  26.     using System.IO;
  27.     using System.Reflection;
  28.     using System.Runtime.Remoting;
  29.     using System.Security;
  30.     using System.Security.Permissions;
  31.     using System.Security.Policy;
  32.     using System.Threading;
  33.    
  34.     internal sealed class ManifestRunner
  35.     {
  36.         private AppDomain m_domain;
  37.         private string m_path;
  38.         private string[] m_args;
  39.         private ApartmentState m_apt;
  40.         private Assembly m_assembly;
  41.         private int m_runResult;
  42.        
  43.         //
  44.         // We need to assert unmanaged code security permission to be
  45.         // able to call ActivationContext.ApplicationDirectory.
  46.         //
  47.        
  48.         [SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted = true)]
  49.         internal ManifestRunner(AppDomain domain, ActivationContext activationContext)
  50.         {
  51.             m_domain = domain;
  52.            
  53.             string file;
  54.             string parameters;
  55.             CmsUtils.GetEntryPoint(activationContext, out file, out parameters);
  56.             if (parameters == null || parameters.Length == 0)
  57.                 m_args = new string[0];
  58.             else
  59.                 m_args = parameters.Split(' ');
  60.            
  61.             m_apt = ApartmentState.Unknown;
  62.            
  63.             // get the 'merged' application directory path.
  64.             string directoryName = activationContext.ApplicationDirectory;
  65.             m_path = Path.Combine(directoryName, file);
  66.         }
  67.        
  68.         internal Assembly EntryAssembly {
  69.             [FileIOPermissionAttribute(SecurityAction.Assert, Unrestricted = true)]
  70.             [SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted = true)]
  71.             get {
  72.                 if (m_assembly == null)
  73.                     m_assembly = Assembly.LoadFrom(m_path);
  74.                 return m_assembly;
  75.             }
  76.         }
  77.        
  78.         private void NewThreadRunner()
  79.         {
  80.             m_runResult = Run(false);
  81.         }
  82.        
  83.         private int RunInNewThread()
  84.         {
  85.             Thread th = new Thread(new ThreadStart(NewThreadRunner));
  86.             th.SetApartmentState(m_apt);
  87.             th.Start();
  88.             th.Join();
  89.             return m_runResult;
  90.         }
  91.        
  92.         private int Run(bool checkAptModel)
  93.         {
  94.             if (checkAptModel && m_apt != ApartmentState.Unknown) {
  95.                 if (Thread.CurrentThread.GetApartmentState() != ApartmentState.Unknown && Thread.CurrentThread.GetApartmentState() != m_apt)
  96.                     return RunInNewThread();
  97.                 Thread.CurrentThread.SetApartmentState(m_apt);
  98.             }
  99.             return m_domain.nExecuteAssembly(EntryAssembly, m_args);
  100.         }
  101.        
  102.         internal int ExecuteAsAssembly()
  103.         {
  104.             object[] attrs = EntryAssembly.EntryPoint.GetCustomAttributes(typeof(STAThreadAttribute), false);
  105.             if (attrs.Length > 0)
  106.                 m_apt = ApartmentState.STA;
  107.             attrs = EntryAssembly.EntryPoint.GetCustomAttributes(typeof(MTAThreadAttribute), false);
  108.             if (attrs.Length > 0)
  109.                 if (m_apt == ApartmentState.Unknown)
  110.                     m_apt = ApartmentState.MTA;
  111.                 else
  112.                     m_apt = ApartmentState.Unknown;
  113.             return Run(true);
  114.         }
  115.     }
  116.    
  117.     [System.Runtime.InteropServices.ComVisible(true)]
  118.     public class ApplicationActivator
  119.     {
  120.         public ApplicationActivator()
  121.         {
  122.         }
  123.        
  124.         public virtual ObjectHandle CreateInstance(ActivationContext activationContext)
  125.         {
  126.             return CreateInstance(activationContext, null);
  127.         }
  128.        
  129.         public virtual ObjectHandle CreateInstance(ActivationContext activationContext, string[] activationCustomData)
  130.         {
  131.             if (activationContext == null)
  132.                 throw new ArgumentNullException("activationContext");
  133.            
  134.             if (CmsUtils.CompareIdentities(AppDomain.CurrentDomain.ActivationContext, activationContext)) {
  135.                 ManifestRunner runner = new ManifestRunner(AppDomain.CurrentDomain, activationContext);
  136.                 return new ObjectHandle(runner.ExecuteAsAssembly());
  137.             }
  138.            
  139.             AppDomainSetup adSetup = new AppDomainSetup(new ActivationArguments(activationContext, activationCustomData));
  140.             // we inherit the evidence from the calling domain
  141.             return CreateInstanceHelper(adSetup);
  142.         }
  143.        
  144.         protected static ObjectHandle CreateInstanceHelper(AppDomainSetup adSetup)
  145.         {
  146.             if (adSetup.ActivationArguments == null)
  147.                 throw new ArgumentException(Environment.GetResourceString("Arg_MissingActivationArguments"));
  148.            
  149.             adSetup.ActivationArguments.ActivateInstance = true;
  150.             // inherit the caller's domain evidence for the activation.
  151.             Evidence activatorEvidence = AppDomain.CurrentDomain.Evidence;
  152.             // add the application identity as an evidence.
  153.             Evidence appEvidence = CmsUtils.MergeApplicationEvidence(null, adSetup.ActivationArguments.ApplicationIdentity, adSetup.ActivationArguments.ActivationContext, adSetup.ActivationArguments.ActivationData);
  154.            
  155.             HostSecurityManager securityManager = AppDomain.CurrentDomain.HostSecurityManager;
  156.             ApplicationTrust appTrust = securityManager.DetermineApplicationTrust(appEvidence, activatorEvidence, new TrustManagerContext());
  157.             if (appTrust == null || !appTrust.IsApplicationTrustedToRun)
  158.                 throw new PolicyException(Environment.GetResourceString("Policy_NoExecutionPermission"), System.__HResults.CORSEC_E_NO_EXEC_PERM, null);
  159.            
  160.             ObjRef or = AppDomain.nCreateInstance(adSetup.ActivationArguments.ApplicationIdentity.FullName, adSetup, appEvidence, appEvidence == null ? AppDomain.CurrentDomain.InternalEvidence : null, AppDomain.CurrentDomain.GetSecurityDescriptor());
  161.             if (or == null)
  162.                 return null;
  163.             return RemotingServices.Unmarshal(or) as ObjectHandle;
  164.         }
  165.     }
  166. }

Developer Fusion