summaryrefslogtreecommitdiff
path: root/src/vm/clrprivbinderwinrt.h
blob: da6f0d53f783c2c46813444a024e81513beaace4 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// 


// 
// Contains the types that implement code:ICLRPrivBinder and code:ICLRPrivAssembly for WinRT binding.
// 
//=============================================================================================

#pragma once

#include "holder.h"
#include "internalunknownimpl.h"
#include "clrprivbinding.h"
#include "clrprivruntimebinders.h"
#include "clrprivbinderutil.h"
#include "clrprivtypecachewinrt.h"
#include "clr_std/utility"
#include "winrt/windowsstring.h"
#include "appxutil.h"

#include "coreclr/corebindresult.h"

// IBindResult maps directly to its one and only implementation on CoreCLR.
typedef CoreBindResult IBindResult;

//=====================================================================================================================
// Forward declarations
class CLRPrivBinderWinRT;
class CLRPrivAssemblyWinRT;
class BINDER_SPACE::ApplicationContext;
class BINDER_SPACE::Assembly;

typedef DPTR(CLRPrivBinderWinRT)     PTR_CLRPrivBinderWinRT;
typedef DPTR(CLRPrivAssemblyWinRT)   PTR_CLRPrivAssemblyWinRT;

BOOL 
IsWindowsNamespace(const char * wszNamespace);

//=====================================================================================================================
//=====================================================================================================================
//=====================================================================================================================
class CLRPrivBinderWinRT : 
    public IUnknownCommon<ICLRPrivBinder
    >
{
    friend class CLRPrivAssemblyWinRT;
    
public:
    //=============================================================================================
    // Options of namespace resolution
    enum NamespaceResolutionKind
    {
        NamespaceResolutionKind_WindowsAPI,             // Using RoResolveNamespace Win8 API
        NamespaceResolutionKind_DesignerResolveEvent    // Using DesignerNamespaceResolve event
    };
    
private:
    //=============================================================================================
    // Data structures for Namespace -> FileNameList map (as returned by RoResolveNamespace API)
    
    // Entry in SHash table that maps namespace to list of files
    struct NamespaceToFileNameListMapEntry
    {
        PTR_WSTR                           m_wszNamespace;
        CLRPrivBinderUtil::PTR_WStringList m_pFileNameList;
    };
    
    // SHash traits for Namespace -> FileNameList hash
    class NamespaceToFileNameListMapTraits : public NoRemoveSHashTraits< DefaultSHashTraits< NamespaceToFileNameListMapEntry > >
    {
    public:
        typedef PCWSTR key_t;
        static const NamespaceToFileNameListMapEntry Null() { NamespaceToFileNameListMapEntry e; e.m_wszNamespace = PTR_WSTR(nullptr); return e; }
        static bool IsNull(const NamespaceToFileNameListMapEntry & e) { return e.m_wszNamespace == nullptr; }
        static PCWSTR GetKey(const NamespaceToFileNameListMapEntry & e) { return e.m_wszNamespace; }
        static count_t Hash(PCWSTR str) { return HashString(str); }
        static BOOL Equals(PCWSTR lhs, PCWSTR rhs) { LIMITED_METHOD_CONTRACT; return (wcscmp(lhs, rhs) == 0); }
        
        void OnDestructPerEntryCleanupAction(const NamespaceToFileNameListMapEntry & e)
        {
            delete [] e.m_wszNamespace;
            CLRPrivBinderUtil::WStringList_Delete(e.m_pFileNameList);
        }
        static const bool s_DestructPerEntryCleanupAction = true;
    };

    typedef SHash<NamespaceToFileNameListMapTraits> NamespaceToFileNameListMap;
    
    //=============================================================================================
    // Data structure for FileName -> CLRPrivAssemblyWinRT * map
    
    struct FileNameToAssemblyWinRTMapEntry
    {
        PTR_CWSTR                m_wszFileName;   // File name (owned by m_pAssembly)
        PTR_CLRPrivAssemblyWinRT m_pAssembly;
    };
    
    class FileNameToAssemblyWinRTMapTraits : public DefaultSHashTraits<FileNameToAssemblyWinRTMapEntry>
    {
    public:
        typedef PCWSTR key_t;
        static const FileNameToAssemblyWinRTMapEntry Null() { FileNameToAssemblyWinRTMapEntry e; e.m_wszFileName = PTR_CWSTR(nullptr); return e; }
        static bool IsNull(const FileNameToAssemblyWinRTMapEntry &e) { return e.m_wszFileName == PTR_CWSTR(nullptr); }
        static const FileNameToAssemblyWinRTMapEntry Deleted() { FileNameToAssemblyWinRTMapEntry e; e.m_wszFileName = (PTR_CWSTR)-1; return e; }
        static bool IsDeleted(const FileNameToAssemblyWinRTMapEntry & e) { return dac_cast<TADDR>(e.m_wszFileName) == (TADDR)-1; }
        static PCWSTR GetKey(const FileNameToAssemblyWinRTMapEntry & e) { return e.m_wszFileName; }
        static count_t Hash(PCWSTR str) { return HashString(str); }
        static BOOL Equals(PCWSTR lhs, PCWSTR rhs) { LIMITED_METHOD_CONTRACT; return (wcscmp(lhs, rhs) == 0); }
    };
    
    typedef SHash<FileNameToAssemblyWinRTMapTraits> FileNameToAssemblyWinRTMap;
    
public:
    //=============================================================================================
    // ICLRPrivBinder interface methods

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

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

    STDMETHOD(GetLoaderAllocator)(
        LPVOID * pLoaderAllocator)
    {
        return E_FAIL;
    }


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

    CLRPrivBinderWinRT(
        ICLRPrivBinder *        pParentBinder, 
        CLRPrivTypeCacheWinRT * pWinRtTypeCache,
        LPCWSTR *               rgwzAltPath, 
        UINT                    cAltPaths, 
        NamespaceResolutionKind fNamespaceResolutionKind,
        BOOL                    fCanUseNativeImages);
    
    static 
    CLRPrivBinderWinRT * GetOrCreateBinder(
        CLRPrivTypeCacheWinRT * pWinRtTypeCache, 
        NamespaceResolutionKind fNamespaceResolutionKind);
    
    ~CLRPrivBinderWinRT();

    // Binds WinRT assemblies only.
    HRESULT BindWinRTAssemblyByName(
        IAssemblyName * pIAssemblyName,
        CLRPrivAssemblyWinRT ** ppAssembly);

    // Binds WinRT assemblies only.
    HRESULT BindWinRTAssemblyByName(
        IAssemblyName * pIAssemblyName,
        ICLRPrivAssembly ** ppPrivAssembly);

    // Binds WinRT assemblies only.
    HRESULT BindWinRTAssemblyByName(
        IAssemblyName * pIAssemblyName,
        IBindResult ** ppIBindResult);

    HRESULT GetAssemblyAndTryFindNativeImage(SString &sWinmdFilename, LPCWSTR pwzSimpleName, BINDER_SPACE::Assembly ** ppAssembly);
    // On Phone the application's APP_PATH CoreCLR hosting config property is used as the app
    // package graph for RoResolveNamespace to find 3rd party WinMDs.  This method wires up
    // the app paths so the WinRT binder will find 3rd party WinMDs.
    HRESULT SetApplicationContext(BINDER_SPACE::ApplicationContext *pApplicationContext, LPCWSTR pwzAppLocalWinMD);
    // Finds assembly with WinRT type if it is already loaded
    // Note: This method could implement interface code:ICLRPrivWinRtTypeBinder if it is ever needed
    PTR_Assembly FindAssemblyForTypeIfLoaded(
        PTR_AppDomain pAppDomain, 
        LPCUTF8       szNamespace, 
        LPCUTF8       szClassName);


private:
    //=============================================================================================
    // Accessors for FileName -> CLRPrivAssemblyWinRT * map
    
    ReleaseHolder<CLRPrivAssemblyWinRT> FindAssemblyByFileName(
        PCWSTR wzsFileName);
    
    ReleaseHolder<CLRPrivAssemblyWinRT> AddFileNameToAssemblyMapping(
        PCWSTR                 wszFileName,
        CLRPrivAssemblyWinRT * pAssembly);
    
    void RemoveFileNameToAssemblyMapping(
        PCWSTR wszFileName);
    
    //=============================================================================================
    // Internal methods
    
    // Returns list of file names from code:m_NamespaceToFileNameListMap for the namespace
    HRESULT GetFileNameListForNamespace(LPCWSTR wszNamespace, CLRPrivBinderUtil::WStringList ** ppFileNameList);
    
    // Adds (thread-safe) list of file names to code:m_NamespaceToFileNameListMap for the namespace.
    // Returns TRUE if the list was added to the cache.
    BOOL AddFileNameListForNamespace(
        LPCWSTR                           wszNamespace, 
        CLRPrivBinderUtil::WStringList *  pFileNameList, 
        CLRPrivBinderUtil::WStringList ** ppFileNameList);
    

private:
    //=============================================================================================
    
    // Namespace -> FileName list map ... items are never removed
    NamespaceToFileNameListMap m_NamespaceToFileNameListMap;
    // FileName -> CLRPrivAssemblyWinRT * map ... items can be removed when CLRPrivAssemblyWinRT dies
    FileNameToAssemblyWinRTMap m_FileNameToAssemblyMap;

    // Lock for the above maps
    CrstExplicitInit m_MapsLock;
    // Lock for adding into the above maps, in addition to the read-lock above
    CrstExplicitInit m_MapsAddLock;

    //=============================================================================================
    
    PTR_CLRPrivTypeCacheWinRT m_pTypeCache;
    
    // The kind of namespace resolution (RoResolveNamespace Win8 API or DesignerNamespaceResolve event)
    NamespaceResolutionKind m_fNamespaceResolutionKind;
    
    static CLRPrivBinderWinRT * s_pSingleton;
    
    // Parent binder used to delegate bind requests up the binder hierarchy.
    ICLRPrivBinder * m_pParentBinder;
    
#ifndef CROSSGEN_COMPILE
    // Alternative paths for use with RoGetNamespace api
    CLRPrivBinderUtil::HSTRINGArrayHolder m_rgAltPaths;
#endif



    BINDER_SPACE::ApplicationContext * m_pApplicationContext;
    NewArrayHolder<WCHAR> m_appLocalWinMDPath;


};  // class CLRPrivBinderWinRT


//=====================================================================================================================
//=====================================================================================================================
//=====================================================================================================================
class CLRPrivAssemblyWinRT :
    public IUnknownCommon<ICLRPrivAssembly, ICLRPrivAssemblyID_WinRT>
{
    friend class CLRPrivBinderWinRT;
    
public:
    //=============================================================================================
    // Class methods
    
    CLRPrivAssemblyWinRT(
        CLRPrivBinderWinRT *                         pBinder, 
        CLRPrivBinderUtil::CLRPrivResourcePathImpl * pResourceIL,
        IBindResult *                                pIBindResult,
        BOOL                                         fShareable);
    
    ~CLRPrivAssemblyWinRT();

    HRESULT GetIBindResult(
        IBindResult ** ppIBindResult);
    
    static HRESULT GetIBindResult(
        ICLRPrivAssembly * pPrivAssembly, 
        IBindResult **     ppIBindResult);
    
    //=============================================================================================
    // IUnknown interface methods
    
    // Implements interface method code:IUnknown::Release.
    // Overridden to implement self-removal from assembly map code:CLRPrivBinderWinRT::m_FileNameToAssemblyMap.
    STDMETHOD_(ULONG, Release)();
    
    //=============================================================================================
    // ICLRPrivBinder interface methods
    
    // Implements interface method code:ICLRPrivBinder::BindAssemblyByName.
    STDMETHOD(BindAssemblyByName)(
        IAssemblyName * pAssemblyName,
        ICLRPrivAssembly ** ppAssembly)
    {
        STATIC_CONTRACT_WRAPPER;
        return m_pBinder->BindAssemblyByName(pAssemblyName, ppAssembly);
    }
    
    // Implements interface method code:ICLRPrivBinder::GetBinderID.
    STDMETHOD(GetBinderID)(
        UINT_PTR * pBinderId)
    {
        STATIC_CONTRACT_WRAPPER;
        return m_pBinder->GetBinderID(pBinderId);
    }
    
    STDMETHOD(GetLoaderAllocator)(
        LPVOID * pLoaderAllocator)
    {
        WRAPPER_NO_CONTRACT;
        return m_pBinder->GetLoaderAllocator(pLoaderAllocator);
    }

    //=============================================================================================
    // ICLRPrivAssembly interface methods
    
    // Implements interface method code:ICLRPrivAssembly::IsShareable.
    STDMETHOD(IsShareable)(
        BOOL * pbIsShareable);
    
    // Implements interface method code:ICLRPrivAssembly::GetAvailableImageTypes.
    STDMETHOD(GetAvailableImageTypes)(
        LPDWORD pdwImageTypes);
    
    // Implements interface method code:ICLRPrivAssembly::GetImageResource.
    STDMETHOD(GetImageResource)(
        DWORD               dwImageType, 
        DWORD *             pdwImageType, 
        ICLRPrivResource ** ppIResource);

    void SetFallbackBinder(ICLRPrivBinder* fallbackBinder)
    {
        m_FallbackBinder = clr::SafeAddRef(fallbackBinder);
    }

    ICLRPrivBinder* GetFallbackBinder()
    {
        return m_FallbackBinder;
    }
    
private:
    //=============================================================================================
    
    HRESULT EnsureAvailableImageTypes();

    ReleaseHolder<CLRPrivBinderWinRT> m_pBinder;
    ReleaseHolder<CLRPrivBinderUtil::CLRPrivResourcePathImpl> m_pResourceIL;
    // This cannot be a holder as there can be a race to assign to it.
    ICLRPrivResource * m_pIResourceNI;
    ReleaseHolder<IBindResult> m_pIBindResult;
    BOOL m_fShareable;
    Volatile<DWORD> m_dwImageTypes;
    ReleaseHolder<ICLRPrivBinder> m_FallbackBinder;
};  // class CLRPrivAssemblyWinRT