1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// ============================================================
5//
6// ApplicationContext.hpp
7//
8
9
10//
11// Defines the ApplicationContext class
12//
13// ============================================================
14
15#ifndef __BINDER__APPLICATION_CONTEXT_HPP__
16#define __BINDER__APPLICATION_CONTEXT_HPP__
17
18#include "bindertypes.hpp"
19#include "failurecache.hpp"
20#include "assemblyidentitycache.hpp"
21#ifdef FEATURE_VERSIONING_LOG
22#include "bindinglog.hpp"
23#endif // FEATURE_VERSIONING_LOG
24#include "stringarraylist.h"
25
26namespace BINDER_SPACE
27{
28 //=============================================================================================
29 // Data structures for Simple Name -> File Name hash
30 struct FileNameMapEntry
31 {
32 LPWSTR m_wszFileName;
33 };
34
35 class FileNameHashTraits : public NoRemoveSHashTraits< DefaultSHashTraits< FileNameMapEntry > >
36 {
37 public:
38 typedef PCWSTR key_t;
39 static const FileNameMapEntry Null() { FileNameMapEntry e; e.m_wszFileName = nullptr; return e; }
40 static bool IsNull(const FileNameMapEntry & e) { return e.m_wszFileName == nullptr; }
41 static key_t GetKey(const FileNameMapEntry & e)
42 {
43 key_t key;
44 key = e.m_wszFileName;
45 return key;
46 }
47 static count_t Hash(const key_t &str) { return HashiString(str); }
48 static BOOL Equals(const key_t &lhs, const key_t &rhs) { LIMITED_METHOD_CONTRACT; return (_wcsicmp(lhs, rhs) == 0); }
49 };
50
51 typedef SHash<FileNameHashTraits> TpaFileNameHash;
52
53 // Entry in SHash table that maps namespace to list of files
54 struct SimpleNameToFileNameMapEntry
55 {
56 LPWSTR m_wszSimpleName;
57 LPWSTR m_wszILFileName;
58 LPWSTR m_wszNIFileName;
59 };
60
61 // SHash traits for Namespace -> FileNameList hash
62 class SimpleNameToFileNameMapTraits : public NoRemoveSHashTraits< DefaultSHashTraits< SimpleNameToFileNameMapEntry > >
63 {
64 public:
65 typedef PCWSTR key_t;
66 static const SimpleNameToFileNameMapEntry Null() { SimpleNameToFileNameMapEntry e; e.m_wszSimpleName = nullptr; return e; }
67 static bool IsNull(const SimpleNameToFileNameMapEntry & e) { return e.m_wszSimpleName == nullptr; }
68 static key_t GetKey(const SimpleNameToFileNameMapEntry & e)
69 {
70 key_t key;
71 key = e.m_wszSimpleName;
72 return key;
73 }
74 static count_t Hash(const key_t &str) { return HashiString(str); }
75 static BOOL Equals(const key_t &lhs, const key_t &rhs) { LIMITED_METHOD_CONTRACT; return (_wcsicmp(lhs, rhs) == 0); }
76
77 void OnDestructPerEntryCleanupAction(const SimpleNameToFileNameMapEntry & e)
78 {
79 if (e.m_wszILFileName == nullptr && e.m_wszNIFileName == nullptr)
80 {
81 // Don't delete simple name here since it's a filename only entry and will be cleaned up
82 // by the SimpleName -> FileName entry which reuses the same filename pointer.
83 return;
84 }
85
86 if (e.m_wszSimpleName != nullptr)
87 {
88 delete [] e.m_wszSimpleName;
89 }
90 if (e.m_wszILFileName != nullptr)
91 {
92 delete [] e.m_wszILFileName;
93 }
94 if (e.m_wszNIFileName != nullptr)
95 {
96 delete [] e.m_wszNIFileName;
97 }
98 }
99 static const bool s_DestructPerEntryCleanupAction = true;
100 };
101
102 typedef SHash<SimpleNameToFileNameMapTraits> SimpleNameToFileNameMap;
103
104 class ApplicationContext
105 : public IUnknown
106 {
107 public:
108 // IUnknown methods
109 STDMETHOD(QueryInterface)(REFIID riid,
110 void **ppv);
111 STDMETHOD_(ULONG, AddRef)();
112 STDMETHOD_(ULONG, Release)();
113
114 // ApplicationContext methods
115 ApplicationContext();
116 virtual ~ApplicationContext();
117 HRESULT Init();
118
119 inline SString &GetApplicationName();
120 inline DWORD GetAppDomainId();
121 inline void SetAppDomainId(DWORD dwAppDomainId);
122
123 HRESULT SetupBindingPaths(/* in */ SString &sTrustedPlatformAssemblies,
124 /* in */ SString &sPlatformResourceRoots,
125 /* in */ SString &sAppPaths,
126 /* in */ SString &sAppNiPaths,
127 /* in */ BOOL fAcquireLock);
128
129 HRESULT GetAssemblyIdentity(/* in */ LPCSTR szTextualIdentity,
130 /* in */ AssemblyIdentityUTF8 **ppAssemblyIdentity);
131
132 // Getters/Setter
133 inline ExecutionContext *GetExecutionContext();
134 inline InspectionContext *GetInspectionContext();
135 inline FailureCache *GetFailureCache();
136 inline HRESULT AddToFailureCache(SString &assemblyNameOrPath,
137 HRESULT hrBindResult);
138 inline StringArrayList *GetAppPaths();
139 inline SimpleNameToFileNameMap *GetTpaList();
140 inline TpaFileNameHash *GetTpaFileNameList();
141 inline StringArrayList *GetPlatformResourceRoots();
142 inline StringArrayList *GetAppNiPaths();
143
144 // Using a host-configured Trusted Platform Assembly list
145 bool IsTpaListProvided();
146 inline CRITSEC_COOKIE GetCriticalSectionCookie();
147 inline LONG GetVersion();
148 inline void IncrementVersion();
149
150#ifdef FEATURE_VERSIONING_LOG
151 inline BindingLog *GetBindingLog();
152 inline void ClearBindingLog();
153#endif // FEATURE_VERSIONING_LOG
154
155 protected:
156 LONG m_cRef;
157 Volatile<LONG> m_cVersion;
158 SString m_applicationName;
159 DWORD m_dwAppDomainId;
160 ExecutionContext *m_pExecutionContext;
161 InspectionContext *m_pInspectionContext;
162 FailureCache *m_pFailureCache;
163 CRITSEC_COOKIE m_contextCS;
164#ifdef FEATURE_VERSIONING_LOG
165 BindingLog m_bindingLog;
166#endif // FEATURE_VERSIONING_LOG
167
168 AssemblyIdentityCache m_assemblyIdentityCache;
169
170 StringArrayList m_platformResourceRoots;
171 StringArrayList m_appPaths;
172 StringArrayList m_appNiPaths;
173
174 SimpleNameToFileNameMap * m_pTrustedPlatformAssemblyMap;
175 TpaFileNameHash * m_pFileNameHash;
176
177 bool m_fCanExplicitlyBindToNativeImages;
178public:
179 inline void SetExplicitBindToNativeImages(bool fCanExplicitlyBindToNativeImages)
180 {
181 m_fCanExplicitlyBindToNativeImages = fCanExplicitlyBindToNativeImages;
182 }
183
184 inline bool CanExplicitlyBindToNativeImages()
185 {
186 return m_fCanExplicitlyBindToNativeImages;
187 }
188protected:
189 };
190
191#include "applicationcontext.inl"
192
193};
194
195#endif
196