summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ICustomPropertyProvider.cs
blob: 3bbde35a3cfc7452ac4518e3c4ab99f31d3ed209 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// 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.

//

using System;
using System.StubHelpers;
using System.Reflection;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Security;

namespace System.Runtime.InteropServices.WindowsRuntime
{

    //
    // ICustomProperty Implementation helpers
    //
    internal static class ICustomPropertyProviderImpl
    {
        //
        // Creates a ICustomProperty implementation for Jupiter
        // Called from ICustomPropertyProvider_GetProperty from within runtime
        //
        static internal ICustomProperty CreateProperty(object target, string propertyName)
        {
            Contract.Requires(target != null);
            Contract.Requires(propertyName != null);

            IGetProxyTarget proxy = target as IGetProxyTarget;
            if (proxy != null) 
                target = proxy.GetTarget();

            // Only return public instance/static properties
            PropertyInfo propertyInfo = target.GetType().GetProperty(
                propertyName,
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);

            if (propertyInfo == null)
                return null;
            else
                return new CustomPropertyImpl(propertyInfo);
        }

        //
        // Creates a ICustomProperty implementation for Jupiter
        // Called from ICustomPropertyProvider_GetIndexedProperty from within runtime
        //               
        static internal unsafe ICustomProperty CreateIndexedProperty(object target, string propertyName, TypeNameNative *pIndexedParamType)
        {
            Contract.Requires(target != null);
            Contract.Requires(propertyName != null);

            Type indexedParamType = null;
            SystemTypeMarshaler.ConvertToManaged(pIndexedParamType, ref indexedParamType);

            return CreateIndexedProperty(target, propertyName, indexedParamType);        
        }

        static internal ICustomProperty CreateIndexedProperty(object target, string propertyName, Type indexedParamType)
        {
            Contract.Requires(target != null);
            Contract.Requires(propertyName != null);

            IGetProxyTarget proxy = target as IGetProxyTarget;
            if (proxy != null) 
                target = proxy.GetTarget();

            // Only return public instance/static properties
            PropertyInfo propertyInfo = target.GetType().GetProperty(
                propertyName,
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public,
                null,                                                                   // default binder
                null,                                                                   // ignore return type
                new Type[] { indexedParamType },                                        // indexed parameter type
                null                                                                    // ignore type modifier
                );

            if (propertyInfo == null)
                return null;
            else
                return new CustomPropertyImpl(propertyInfo);
        }

        static internal unsafe void GetType(object target, TypeNameNative *pIndexedParamType)
        {            
            IGetProxyTarget proxy = target as IGetProxyTarget;
            if (proxy != null) 
                target = proxy.GetTarget();

            SystemTypeMarshaler.ConvertToNative(target.GetType(), pIndexedParamType);
        }        
    }

    [Flags]
    enum InterfaceForwardingSupport
    {
        None                        = 0,
        IBindableVector             = 0x1,              // IBindableVector -> IBindableVector
        IVector                     = 0x2,              // IBindableVector -> IVector<T>
        IBindableVectorView         = 0x4,              // IBindableVectorView -> IBindableVectorView
        IVectorView                 = 0x8,              // IBindableVectorView -> IVectorView<T>
        IBindableIterableOrIIterable= 0x10              // IBindableIterable -> IBindableIterable/IIterable<T>
    }

    //
    // Interface for data binding code (CustomPropertyImpl) to retreive the target object
    // See CustomPropertyImpl.InvokeInternal for details
    //
    internal interface IGetProxyTarget
    {
        object GetTarget();            
    }
    
    //
    // Proxy that supports data binding on another object
    //
    // This serves two purposes:
    //
    // 1. Delegate data binding interfaces to another object
    // Note that this proxy implements the native interfaces directly to avoid unnecessary overhead 
    // (such as the adapter code that addresses behavior differences between IBindableVector & List
    // as well as simplify forwarding code (except for IEnumerable)
    //
    // 2. ICLRServices.CreateManagedReference will hand out ICCW* of a new instance of this object
    // and will hold the other object alive
    //
    //
    internal class ICustomPropertyProviderProxy<T1, T2> : IGetProxyTarget,
                                                          ICustomQueryInterface,
                                                          IEnumerable,          // IBindableIterable -> IBindableIterable/IIterable<T>
                                                          IBindableVector,      // IBindableVector -> IBindableVector/IVector<T>
                                                          IBindableVectorView   // IBindableVectorView -> IBindableVectorView/IVectorView<T>
    {
        private object _target;
        private InterfaceForwardingSupport _flags;

        internal ICustomPropertyProviderProxy(object target, InterfaceForwardingSupport flags)
        {
            _target = target;
            _flags = flags;
        }

        //
        // Creates a new instance of ICustomPropertyProviderProxy<T1, T2> and assign appropriate
        // flags
        //
        internal static object CreateInstance(object target)
        {
            InterfaceForwardingSupport supportFlags = InterfaceForwardingSupport.None;

            //
            // QI and figure out the right flags
            //
            if (target as IList != null)
                supportFlags |= InterfaceForwardingSupport.IBindableVector;

            // NOTE: We need to use the directed type here
            // If we use IVector_Raw<T1> here, it derives from a different IIterable<T> which the runtime
            // doesn't recognize, and therefore IEnumerable cast won't be able to take advantage of this QI
            if (target as IList<T1> != null)
                supportFlags |= InterfaceForwardingSupport.IVector;
            
            if (target as IBindableVectorView != null)
                supportFlags |= InterfaceForwardingSupport.IBindableVectorView;

            // NOTE: We need to use the redirected type here
            // If we use IVector_Raw<T1> here, it derives from a different IIterable<T> which the runtime
            // doesn't recognize, and therefore IEnumerable cast won't be able to take advantage of this QI
            if (target as IReadOnlyList<T2> != null)
                supportFlags |= InterfaceForwardingSupport.IVectorView;

            // Verify IEnumerable last because the first few QIs might succeed and we need
            // IEnumerable cast to use that cache (instead of having ICustomPropertyProvider to
            // forward it manually)
            // For example, if we try to shoot in the dark by trying IVector<IInspectable> and it 
            // succeeded, IEnumerable needs to know that
            if (target as IEnumerable != null)
                supportFlags |= InterfaceForwardingSupport.IBindableIterableOrIIterable;
            
            return new ICustomPropertyProviderProxy<T1, T2>(target, supportFlags);                
        }


        //
        // override ToString() to make sure callers get correct IStringable.ToString() behavior in native code
        //
        public override string ToString()
        {
            return WindowsRuntime.IStringableHelper.ToString(_target);
        }

        //
        // IGetProxyTarget - unwraps the target object and use it for data binding
        // 
        object IGetProxyTarget.GetTarget()
        {
            return _target;
        }

        // 
        // ICustomQueryInterface methods
        //    
        public CustomQueryInterfaceResult GetInterface([In]ref Guid iid, out IntPtr ppv)
        {
            ppv = IntPtr.Zero;

            if (iid == typeof(IBindableIterable).GUID)
            {
                // Reject the QI if target doesn't implement IEnumerable
                if ((_flags & (InterfaceForwardingSupport.IBindableIterableOrIIterable)) == 0)
                    return CustomQueryInterfaceResult.Failed;                    
            }

            if (iid == typeof(IBindableVector).GUID)
            {
                // Reject the QI if target doesn't implement IBindableVector/IVector
                if ((_flags & (InterfaceForwardingSupport.IBindableVector | InterfaceForwardingSupport.IVector)) == 0)
                    return CustomQueryInterfaceResult.Failed;                    
            }

            if (iid == typeof(IBindableVectorView).GUID)
            {
                // Reject the QI if target doesn't implement IBindableVectorView/IVectorView
                if ((_flags & (InterfaceForwardingSupport.IBindableVectorView | InterfaceForwardingSupport.IVectorView)) == 0)
                    return CustomQueryInterfaceResult.Failed;                
            }
            
            return CustomQueryInterfaceResult.NotHandled;
        }
    
        //
        // IEnumerable methods
        //
        public IEnumerator GetEnumerator()
        {
            return ((IEnumerable)_target).GetEnumerator();
        }

        //
        // IBindableVector implementation (forwards to IBindableVector / IVector<T>)
        //        
        [Pure]
        object IBindableVector.GetAt(uint index)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                return bindableVector.GetAt(index);
            }
            else
            {
                // IBindableVector -> IVector<T>
                return GetVectorOfT().GetAt(index);
            }            
        }
        
        [Pure]
        uint IBindableVector.Size 
        { 
            get
            {            
                IBindableVector bindableVector = GetIBindableVectorNoThrow();
                if (bindableVector != null)
                {
                    // IBindableVector -> IBindableVector
                    return bindableVector.Size;
                }
                else
                {
                    // IBindableVector -> IVector<T>
                    return GetVectorOfT().Size;
                }            
            }
        }
        
        [Pure]
        IBindableVectorView IBindableVector.GetView()
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                return bindableVector.GetView();
            }
            else
            {
                // IBindableVector -> IVector<T>
                return new IVectorViewToIBindableVectorViewAdapter<T1>(GetVectorOfT().GetView());
            }        
        }

        private sealed class IVectorViewToIBindableVectorViewAdapter<T> : IBindableVectorView
        {
            private IVectorView<T> _vectorView;

            public IVectorViewToIBindableVectorViewAdapter(IVectorView<T> vectorView)
            { 
                this._vectorView = vectorView; 
            }

            [Pure]
            object IBindableVectorView.GetAt(uint index)
            {
                return _vectorView.GetAt(index);
            }
            
            [Pure]
            uint IBindableVectorView.Size
            { 
                get
                {
                    return _vectorView.Size;
                }
            }
            
            [Pure]
            bool IBindableVectorView.IndexOf(object value, out uint index)
            {
                return _vectorView.IndexOf(ConvertTo<T>(value), out index);
            }

            IBindableIterator IBindableIterable.First()
            {
                return new IteratorOfTToIteratorAdapter<T>(_vectorView.First());
            }

        }         
        
        [Pure]
        bool IBindableVector.IndexOf(object value, out uint index)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                return bindableVector.IndexOf(value, out index);
            }
            else
            {
                // IBindableVector -> IVector<T>
                return GetVectorOfT().IndexOf(ConvertTo<T1>(value), out index);
            }            
        }
        
        void IBindableVector.SetAt(uint index, object value)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.SetAt(index, value);
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().SetAt(index, ConvertTo<T1>(value));
            }            
        }
        
        void IBindableVector.InsertAt(uint index, object value)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.InsertAt(index, value);
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().InsertAt(index, ConvertTo<T1>(value));
            }            
        }
        
        void IBindableVector.RemoveAt(uint index)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.RemoveAt(index);
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().RemoveAt(index);
            }            
        }
        
        void IBindableVector.Append(object value)
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.Append(value);
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().Append(ConvertTo<T1>(value));
            }            
        }        
        
        void IBindableVector.RemoveAtEnd()
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.RemoveAtEnd();
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().RemoveAtEnd();
            }            
        }
        
        void IBindableVector.Clear()
        {
            IBindableVector bindableVector = GetIBindableVectorNoThrow();
            if (bindableVector != null)
            {
                // IBindableVector -> IBindableVector
                bindableVector.Clear();
            }
            else
            {
                // IBindableVector -> IVector<T>
                GetVectorOfT().Clear();
            }            
        }

        private IBindableVector GetIBindableVectorNoThrow()
        {
            if ((_flags & InterfaceForwardingSupport.IBindableVector) != 0)
                return JitHelpers.UnsafeCast<IBindableVector>(_target);
            else
                return null;
        }

        private IVector_Raw<T1> GetVectorOfT()
        {
            if ((_flags & InterfaceForwardingSupport.IVector) != 0)
                return JitHelpers.UnsafeCast<IVector_Raw<T1>>(_target);
            else
                throw new InvalidOperationException();  // We should not go down this path, unless Jupiter pass this out to managed code
                                                        // and managed code use reflection to do the cast
        }
        
        //
        // IBindableVectorView implementation (forwarding to IBindableVectorView or IVectorView<T>)
        //
        [Pure]
        object IBindableVectorView.GetAt(uint index)
        {
            IBindableVectorView bindableVectorView = GetIBindableVectorViewNoThrow();
            if (bindableVectorView != null)
                return bindableVectorView.GetAt(index);
            else
                return GetVectorViewOfT().GetAt(index);
        }
        
        [Pure]
        uint IBindableVectorView.Size
        { 
            get
            {
                IBindableVectorView bindableVectorView = GetIBindableVectorViewNoThrow();
                if (bindableVectorView != null)
                    return bindableVectorView.Size;
                else
                    return GetVectorViewOfT().Size;
            }
        }
        
        [Pure]
        bool IBindableVectorView.IndexOf(object value, out uint index)
        {
            IBindableVectorView bindableVectorView = GetIBindableVectorViewNoThrow();
            if (bindableVectorView != null)
                return bindableVectorView.IndexOf(value, out index);
            else
                return GetVectorViewOfT().IndexOf(ConvertTo<T2>(value), out index);
        }

        IBindableIterator IBindableIterable.First()
        {
            IBindableVectorView bindableVectorView = GetIBindableVectorViewNoThrow();
            if (bindableVectorView != null)
                return bindableVectorView.First();
            else
                return new IteratorOfTToIteratorAdapter<T2>(GetVectorViewOfT().First());
        }

        private sealed class IteratorOfTToIteratorAdapter<T> : IBindableIterator
        {
            private IIterator<T> _iterator;

            public IteratorOfTToIteratorAdapter(IIterator<T> iterator)
            { this._iterator = iterator; }

            public bool HasCurrent { get { return _iterator.HasCurrent; } }
            public object Current  { get { return (object)_iterator.Current; } }
            public bool MoveNext() { return _iterator.MoveNext(); }
        }         

        private IBindableVectorView GetIBindableVectorViewNoThrow()
        {
            if ((_flags & InterfaceForwardingSupport.IBindableVectorView) != 0)
                return JitHelpers.UnsafeCast<IBindableVectorView>(_target);
            else
                return null;
        }

        private IVectorView<T2> GetVectorViewOfT()
        {
            if ((_flags & InterfaceForwardingSupport.IVectorView) != 0)
                return JitHelpers.UnsafeCast<IVectorView<T2>>(_target);
            else
                throw new InvalidOperationException();  // We should not go down this path, unless Jupiter pass this out to managed code
                                                        // and managed code use reflection to do the cast
        }

        //
        // Convert to type T
        //
        private static T ConvertTo<T>(object value)
        {
            // Throw ArgumentNullException if value is null (otherwise we'll throw NullReferenceException
            // when casting value to T)
            ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(value, ExceptionArgument.value);
        
            // No coersion support needed. If we need coersion later, this is the place
            return (T) value;
        }
    }
}