summaryrefslogtreecommitdiff
path: root/src/vm/clrprivbinderappx.h
blob: 1cc29d3ed271c1d72944765dcf761c51ecea7415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//



#pragma once

#include "holder.h"
#include "internalunknownimpl.h"
#include "shash.h"
#include "fusion.h"
#include "clrprivbinding.h"
#include "clrprivruntimebinders.h"
#include "clrprivbinderfusion.h"
#include "clrprivbinderwinrt.h"

//=====================================================================================================================
// Forward declarations
class CLRPrivBinderAppX;
class CLRPrivAssemblyAppX;

class DomainAssembly;

// Forward declaration of helper class used in native image binding.
class CLRPrivAssemblyAppX_NIWrapper;

typedef DPTR(CLRPrivBinderAppX) PTR_CLRPrivBinderAppX;

//=====================================================================================================================
class CLRPrivBinderAppX : 
    public IUnknownCommon<ICLRPrivBinder, IBindContext, ICLRPrivWinRtTypeBinder>
{
    friend class CLRPrivAssemblyAppX;

public:
    //=============================================================================================
    // ICLRPrivBinder methods

    // Implements code:ICLRPrivBinder::BindAssemblyByName
    STDMETHOD(BindAssemblyByName)(
        IAssemblyName * pAssemblyName,
        ICLRPrivAssembly ** ppAssembly);

    // Implements code:ICLRPrivBinder::VerifyBind
    STDMETHOD(VerifyBind)(
        IAssemblyName *pAssemblyName,
        ICLRPrivAssembly *pAssembly,
        ICLRPrivAssemblyInfo *pAssemblyInfo);

    // Implements code:ICLRPrivBinder::GetBinderFlags
    STDMETHOD(GetBinderFlags)(
        DWORD *pBinderFlags)
    {
        LIMITED_METHOD_CONTRACT;

        if (pBinderFlags == NULL)
            return E_INVALIDARG;

        *pBinderFlags = m_pParentBinder != NULL ? BINDER_DESIGNER_BINDING_CONTEXT : BINDER_NONE;
        return S_OK;
    }

    // Implements code:ICLRPrivBinder::GetBinderID
    STDMETHOD(GetBinderID)(
        UINT_PTR *pBinderId);
    
    STDMETHOD(FindAssemblyBySpec)(
        LPVOID pvAppDomain,
        LPVOID pvAssemblySpec,
        HRESULT * pResult,
        ICLRPrivAssembly ** ppAssembly);

    //=============================================================================================
    // ICLRPrivWinRtTypeBinder methods
    
    // Implements code:ICLRPrivWinRtTypeBinder::FindAssemblyForWinRtTypeIfLoaded
    STDMETHOD_(void *, FindAssemblyForWinRtTypeIfLoaded)(
        void *  pAppDomain, 
        LPCUTF8 szNamespace, 
        LPCUTF8 szClassName);
    
    //=============================================================================================
    // IBindContext methods

    // Implements code:IBindContext::PreBind
    STDMETHOD(PreBind)(
        IAssemblyName  *pIAssemblyName,
        DWORD           dwPreBindFlags,
        IBindResult   **ppIBindResult);

    // Implements code:IBindContext::IsDefaultContext
    STDMETHOD(IsDefaultContext)();

    //=============================================================================================
    // Class methods

    //---------------------------------------------------------------------------------------------
    static
    CLRPrivBinderAppX * GetOrCreateBinder();
    
    static 
    PTR_CLRPrivBinderAppX GetBinderOrNull()
    {
        LIMITED_METHOD_DAC_CONTRACT;
        return s_pSingleton;
    }
    
    static 
    CLRPrivBinderAppX * CreateParentedBinder(
        ICLRPrivBinder *        pParentBinder, 
        CLRPrivTypeCacheWinRT * pWinRtTypeCache, 
        LPCWSTR *               rgwzAltPath, 
        UINT                    cAltPaths,
        BOOL                    fCanUseNativeImages);
    
    //---------------------------------------------------------------------------------------------
    ~CLRPrivBinderAppX();

    //---------------------------------------------------------------------------------------------
    enum AppXBindFlags
    {
        ABF_BindIL      = 1,
        ABF_BindNI      = 2,
        ABF_Default     = ABF_BindIL | ABF_BindNI,
    };

    //---------------------------------------------------------------------------------------------
    CLRPrivBinderFusion * GetFusionBinder()
    {
        LIMITED_METHOD_CONTRACT;
        return m_pFusionBinder;
    }
    
    PTR_CLRPrivBinderWinRT GetWinRtBinder()
    {
        LIMITED_METHOD_DAC_CONTRACT;
        return m_pWinRTBinder;
    }
    
private:
    //---------------------------------------------------------------------------------------------
    // Binds within AppX packages only. BindAssemblyByName takes care of delegating to Fusion
    // when needed.
    HRESULT BindAppXAssemblyByNameWorker(
        IAssemblyName * pIAssemblyName,
        DWORD dwAppXBindFlags,
        CLRPrivAssemblyAppX ** ppAssembly);

    //---------------------------------------------------------------------------------------------
    // Binds within AppX packages only. BindAssemblyByName takes care of delegating to Fusion
    // when needed.
    HRESULT BindAppXAssemblyByName(
        IAssemblyName * pIAssemblyName,
        DWORD dwAppXBindFlags,
        ICLRPrivAssembly ** ppPrivAssembly);

    //---------------------------------------------------------------------------------------------
    // Binds within AppX packages only. PreBindAssemblyByName takes care of delegating to Fusion
    // when needed.
    HRESULT PreBindAppXAssemblyByName(
        IAssemblyName * pIAssemblyName,
        DWORD dwAppXBindFlags,
        IBindResult ** ppIBindResult);

    //---------------------------------------------------------------------------------------------
    UINT_PTR GetBinderID();

    //---------------------------------------------------------------------------------------------
    CLRPrivBinderAppX(LPCWSTR *rgwzAltPath, UINT cAltPaths);

    //---------------------------------------------------------------------------------------------
    HRESULT CheckGetAppXRT();

    //---------------------------------------------------------------------------------------------
    HRESULT CacheBindResult(
        CLRPrivBinderUtil::AssemblyIdentity *   pIdentity,
        HRESULT                                 hrResult);
    
    //---------------------------------------------------------------------------------------------
    CLRPrivBinderFusion::BindingScope GetFusionBindingScope();

    //---------------------------------------------------------------------------------------------
    Crst              m_MapReadLock;
    Crst              m_MapWriteLock;

    //---------------------------------------------------------------------------------------------
    // Identity to CLRPrivBinderAppX map
    struct NameToAssemblyMapTraits : public StringSHashTraits<CLRPrivAssemblyAppX, WCHAR, CaseInsensitiveStringCompareHash<WCHAR> >
    {
        static LPCWSTR GetKey(CLRPrivAssemblyAppX *pAssemblyAppX);
    };
    typedef SHash<NameToAssemblyMapTraits> NameToAssemblyMap;

    NameToAssemblyMap m_NameToAssemblyMap;

    //---------------------------------------------------------------------------------------------
    // Binding record map, used by cache lookup requests.
    struct BindingRecord
    {
        // This stores the result of the original bind request.
        HRESULT hr;
    };

    struct BindingRecordMapTraits : public MapSHashTraits<CLRPrivBinderUtil::AssemblyIdentity*, BindingRecord>
    {
        typedef MapSHashTraits<CLRPrivBinderUtil::AssemblyIdentity*, BindingRecord> base_t;
        typedef base_t::element_t element_t;
        typedef base_t::count_t count_t;
        typedef base_t::key_t;

        static count_t Hash(key_t k) 
        {
            return HashiString(k->Name);
        }

        static BOOL Equals(key_t k1, key_t k2)
        {
            return SString::_wcsicmp(k1->Name, k2->Name) == 0;
        }

        static const bool s_DestructPerEntryCleanupAction = true;
        static inline void OnDestructPerEntryCleanupAction(element_t const & e)
        {
            delete [] e.Key();
        }
    };
    typedef SHash<BindingRecordMapTraits> BindingRecordMap;

    BindingRecordMap m_BindingRecordMap;

    //---------------------------------------------------------------------------------------------
    NewArrayHolder< NewArrayHolder<WCHAR> > m_rgAltPathsHolder;
    NewArrayHolder< WCHAR* >                m_rgAltPaths;
    UINT                                    m_cAltPaths;

#ifdef FEATURE_FUSION
    BOOL                                    m_fCanUseNativeImages;
    ReleaseHolder<IILFingerprintFactory>    m_pFingerprintFactory;
#endif

    //---------------------------------------------------------------------------------------------
    // ParentBinder is set only in designer binding context (forms a chain of binders)
    ReleaseHolder<ICLRPrivBinder>      m_pParentBinder;
    
    ReleaseHolder<CLRPrivBinderFusion> m_pFusionBinder;
    PTR_CLRPrivBinderWinRT             m_pWinRTBinder;
    
    //---------------------------------------------------------------------------------------------
    //static CLRPrivBinderAppX * s_pSingleton;
    SPTR_DECL(CLRPrivBinderAppX, s_pSingleton);

    //---------------------------------------------------------------------------------------------
    // Cache the binding scope in the constructor so that there is no need to call into a WinRT
    // API in a GC_NOTRIGGER scope later on.
    CLRPrivBinderFusion::BindingScope   m_fusionBindingScope;
};  // class CLRPrivBinderAppX


//=====================================================================================================================
class CLRPrivAssemblyAppX :
    public IUnknownCommon<ICLRPrivAssembly>
{
    friend class CLRPrivBinderAppX;

public:
    //---------------------------------------------------------------------------------------------
    CLRPrivAssemblyAppX(
        CLRPrivBinderUtil::AssemblyIdentity * pIdentity,
        CLRPrivBinderAppX *pBinder,
        ICLRPrivResource *pIResourceIL,
        IBindResult * pIBindResult);

    //---------------------------------------------------------------------------------------------
    ~CLRPrivAssemblyAppX();

    //---------------------------------------------------------------------------------------------
    // Implements code:IUnknown::Release
    STDMETHOD_(ULONG, Release)();

    //---------------------------------------------------------------------------------------------
    LPCWSTR GetSimpleName() const;

    //=============================================================================================
    // ICLRPrivBinder interface methods
    
    // Implements code:ICLRPrivBinder::BindAssemblyByName
    STDMETHOD(BindAssemblyByName)(
        IAssemblyName * pAssemblyName,
        ICLRPrivAssembly ** ppAssembly);

    // Implements code:ICLRPrivBinder::VerifyBind
    STDMETHOD(VerifyBind)(
        IAssemblyName *pAssemblyName,
        ICLRPrivAssembly *pAssembly,
        ICLRPrivAssemblyInfo *pAssemblyInfo)
    {
        STANDARD_BIND_CONTRACT;

        HRESULT hr = S_OK;

        VALIDATE_PTR_RET(pAssemblyName);
        VALIDATE_PTR_RET(pAssembly);
        VALIDATE_PTR_RET(pAssemblyInfo);

        // Re-initialize the assembly identity with full identity contained in metadata.
        IfFailRet(m_pIdentity->Initialize(pAssemblyInfo));

        return m_pBinder->VerifyBind(pAssemblyName, pAssembly, pAssemblyInfo);
    }

    // Implements code:ICLRPrivBinder::GetBinderFlags
    STDMETHOD(GetBinderFlags)(
        DWORD *pBinderFlags)
    {
        LIMITED_METHOD_CONTRACT;
        return m_pBinder->GetBinderFlags(pBinderFlags);
    }

    // Implements code:ICLRPrivBinder::GetBinderID
    STDMETHOD(GetBinderID)(
        UINT_PTR *pBinderId);

    // Implements code:ICLRPrivBinder::FindAssemblyBySpec
    STDMETHOD(FindAssemblyBySpec)(
        LPVOID pvAppDomain,
        LPVOID pvAssemblySpec,
        HRESULT * pResult,
        ICLRPrivAssembly ** ppAssembly)
    { STATIC_CONTRACT_WRAPPER; return m_pBinder->FindAssemblyBySpec(pvAppDomain, pvAssemblySpec, pResult, ppAssembly); }

    //=============================================================================================
    // ICLRPrivAssembly interface methods
    
    // Implements code:ICLRPrivAssembly::IsShareable
    STDMETHOD(IsShareable)(
        BOOL * pbIsShareable);

    // Implements code:ICLRPrivAssembly::GetAvailableImageTypes
    STDMETHOD(GetAvailableImageTypes)(
        LPDWORD pdwImageTypes);

    // Implements code:ICLRPrivAssembly::GetImageResource
    STDMETHOD(GetImageResource)(
        DWORD dwImageType,
        DWORD *pdwImageType,
        ICLRPrivResource ** ppIResource);

    //---------------------------------------------------------------------------------------------
    HRESULT GetIBindResult(
        IBindResult ** ppIBindResult);

private:
    CLRPrivBinderUtil::AssemblyIdentity * m_pIdentity;
    
    ReleaseHolder<CLRPrivBinderAppX>      m_pBinder;

    ReleaseHolder<ICLRPrivResource>       m_pIResourceIL;
    // This cannot be a holder as there can be a race to assign to it.
    ICLRPrivResource *                    m_pIResourceNI;

    ReleaseHolder<IBindResult>            m_pIBindResult;
};