[bcl] Update Reference Source to .NET Framework 4.6.2
[mono.git] / mcs / class / referencesource / mscorlib / system / deployment / cmsutils.cs
1 using Microsoft.Win32;
2 using System;
3 using System.Deployment.Internal;
4 using System.Deployment.Internal.Isolation;
5 using System.Deployment.Internal.Isolation.Manifest;
6 using System.IO;
7 using System.Globalization;
8 using System.Runtime.Hosting;
9 using System.Runtime.InteropServices;
10 using System.Security.Permissions;
11 using System.Security.Policy;
12 using System.Text;
13 using System.Runtime.Versioning;
14 using System.Diagnostics.Contracts;
15
16 namespace System.Deployment.Internal.Isolation.Manifest {
17     [System.Security.SecuritySafeCritical]  // auto-generated
18     [SecurityPermissionAttribute(SecurityAction.Assert, Flags = SecurityPermissionFlag.UnmanagedCode)]
19     internal static class CmsUtils {
20
21         internal static void GetEntryPoint (ActivationContext activationContext, out string fileName, out string parameters) {
22             parameters = null;
23             fileName = null;
24
25             ICMS appManifest = activationContext.ApplicationComponentManifest;
26             if (appManifest == null || appManifest.EntryPointSection == null)
27                 throw new ArgumentException(Environment.GetResourceString("Argument_NoMain"));
28
29             IEnumUnknown refEnum = (IEnumUnknown) appManifest.EntryPointSection._NewEnum;
30             uint count = 0;
31             Object[] entries = new Object[1];
32             // Look for the first entry point. ClickOnce semantic validation ensures exactly one entry point is present.
33             if (refEnum.Next(1, entries, ref count) == 0 && count == 1) {
34                 IEntryPointEntry iref= (IEntryPointEntry) entries[0];
35                 EntryPointEntry reference = iref.AllData;
36                 if (reference.CommandLine_File != null && reference.CommandLine_File.Length > 0) {
37                     fileName = reference.CommandLine_File;
38                 } else {
39                     // Locate the dependent assembly that is being refered to. Well-formed manifests should have an identity.
40                     IAssemblyReferenceEntry refEntry = null;
41                     object assemblyObj = null;
42                     if (reference.Identity != null) {
43                         ((ISectionWithReferenceIdentityKey)appManifest.AssemblyReferenceSection).Lookup(reference.Identity, out assemblyObj);
44                         refEntry = (IAssemblyReferenceEntry) assemblyObj;
45                         fileName = refEntry.DependentAssembly.Codebase;
46                     }
47                 }
48                 parameters = reference.CommandLine_Parameters;
49             }
50         }
51
52         internal static IAssemblyReferenceEntry[] GetDependentAssemblies(ActivationContext activationContext)
53         {
54             IAssemblyReferenceEntry[] entries = null;
55             ICMS appManifest = activationContext.ApplicationComponentManifest;
56             if (appManifest == null)
57                 return null;
58             
59             ISection dependencySection =  appManifest.AssemblyReferenceSection;
60             uint count = (dependencySection != null) ? dependencySection.Count : 0;
61             if (count > 0)
62             {
63                 uint fetched = 0;
64                 entries = new IAssemblyReferenceEntry[count];
65                 IEnumUnknown dependencyEnum = (IEnumUnknown)dependencySection._NewEnum;
66                 int hr = dependencyEnum.Next(count, entries, ref fetched);
67                 if (fetched != count || hr < 0)
68                     return null; //
69             }
70             return entries;
71         }
72                     
73
74         [ResourceExposure(ResourceScope.Machine)]
75         [ResourceConsumption(ResourceScope.Machine)]
76         internal static string GetEntryPointFullPath (ActivationArguments activationArguments) {
77             return GetEntryPointFullPath(activationArguments.ActivationContext);
78         }
79
80         [ResourceExposure(ResourceScope.Machine)]
81         [ResourceConsumption(ResourceScope.Machine)]
82         internal static string GetEntryPointFullPath (ActivationContext activationContext)
83         {
84             string file, parameters;
85             GetEntryPoint(activationContext, out file, out parameters);
86
87             if (!string.IsNullOrEmpty(file))
88             {
89                 string directoryName = activationContext.ApplicationDirectory;
90                 if (directoryName == null || directoryName.Length == 0)
91                 {
92                     // If we were passed a relative path, assume the app base is the current working directory
93                     directoryName = Directory.UnsafeGetCurrentDirectory();
94                 }
95
96                 file = Path.Combine(directoryName, file);
97             }
98
99             return file;
100         }
101
102         internal static bool CompareIdentities (ActivationContext activationContext1, ActivationContext activationContext2) {
103             if (activationContext1 == null || activationContext2 == null)
104                 return activationContext1 == activationContext2;
105             return IsolationInterop.AppIdAuthority.AreDefinitionsEqual(0, activationContext1.Identity.Identity, activationContext2.Identity.Identity);
106         }
107
108         internal static bool CompareIdentities (ApplicationIdentity applicationIdentity1, ApplicationIdentity applicationIdentity2, ApplicationVersionMatch versionMatch) {
109             if (applicationIdentity1 == null || applicationIdentity2 == null)
110                 return applicationIdentity1 == applicationIdentity2;
111             uint flags;
112             switch (versionMatch) {
113             case ApplicationVersionMatch.MatchExactVersion:
114                 flags = 0;
115                 break;
116             case ApplicationVersionMatch.MatchAllVersions:
117                 flags = (uint) IAPPIDAUTHORITY_ARE_DEFINITIONS_EQUAL_FLAGS.IAPPIDAUTHORITY_ARE_DEFINITIONS_EQUAL_FLAG_IGNORE_VERSION;
118                 break;
119             default:
120                 throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)versionMatch), "versionMatch");
121             }
122             return IsolationInterop.AppIdAuthority.AreDefinitionsEqual(flags, applicationIdentity1.Identity, applicationIdentity2.Identity);
123         }
124
125         internal static string GetFriendlyName (ActivationContext activationContext) {
126             ICMS deplManifest = activationContext.DeploymentComponentManifest;
127             IMetadataSectionEntry metadataSectionEntry = (IMetadataSectionEntry) deplManifest.MetadataSectionEntry;
128             IDescriptionMetadataEntry descriptionMetadataEntry = metadataSectionEntry.DescriptionData;
129             string friendlyName = String.Empty;
130             if (descriptionMetadataEntry != null) {
131                 DescriptionMetadataEntry entry = descriptionMetadataEntry.AllData;
132                 friendlyName = (entry.Publisher != null ? String.Format("{0} {1}", entry.Publisher, entry.Product) : entry.Product);
133             }
134             return friendlyName;
135         }
136
137         [ResourceExposure(ResourceScope.Machine)]
138         [ResourceConsumption(ResourceScope.Machine)]
139         internal static void CreateActivationContext (string fullName, string[] manifestPaths, bool useFusionActivationContext, out ApplicationIdentity applicationIdentity, out ActivationContext activationContext) {
140             applicationIdentity = new ApplicationIdentity(fullName);
141             activationContext = null;
142             if (useFusionActivationContext) {
143                 if (manifestPaths != null)
144                     activationContext = new ActivationContext(applicationIdentity, manifestPaths);
145                 else
146                     activationContext = new ActivationContext(applicationIdentity);
147             }
148         }
149
150         //
151         // Helper method to create an application evidence used in app model activation.
152         // There are basically 2 cases where this method is called:
153         //   a) It is called in CreateInstanceHelper. In this case, it gathers 
154         //      the application evidence passed to the CreateDomainHelper call.
155         //   b) It is also called in the server domain. In that case, the domain could
156         //      be either the default domain (in which case the input evidence is null)
157         //      or a domain created via CreateDomainHelper in which case the application
158         //      evidence already contains the application identity and possibly the activation
159         //      context.
160         //
161
162         internal static Evidence MergeApplicationEvidence (Evidence evidence, ApplicationIdentity applicationIdentity, ActivationContext activationContext, string[] activationData)
163         {
164             return MergeApplicationEvidence(evidence,
165                                             applicationIdentity,
166                                             activationContext,
167                                             activationData,
168                                             null);
169         }
170
171         internal static Evidence MergeApplicationEvidence(Evidence evidence,
172                                                           ApplicationIdentity applicationIdentity,
173                                                           ActivationContext activationContext,
174                                                           string[] activationData,
175                                                           ApplicationTrust applicationTrust)
176         {
177             Evidence appEvidence = new Evidence();
178
179             ActivationArguments activationArgs = (activationContext == null ? new ActivationArguments(applicationIdentity, activationData) : new ActivationArguments(activationContext, activationData));
180             appEvidence = new Evidence();
181             appEvidence.AddHostEvidence(activationArgs);
182
183             if (applicationTrust != null)
184                 appEvidence.AddHostEvidence(applicationTrust);
185
186             if (activationContext != null)
187             {
188                 Evidence asiEvidence = new ApplicationSecurityInfo(activationContext).ApplicationEvidence;
189                 if (asiEvidence != null)
190                     appEvidence.MergeWithNoDuplicates(asiEvidence);
191             }
192
193             if (evidence != null)
194                 appEvidence.MergeWithNoDuplicates(evidence);
195
196             return appEvidence;
197         }
198     }
199 }
200