Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.PlatformServices/Reactive/Internal/PhoneShellThunks.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if WINDOWSPHONE7
4 #if !DEBUG_NO_AGENT_SUPPORT
5 //
6 // The Windows Phone Marketplace Test Kit disallows usage of types
7 // in the Microsoft.Phone.Shell namespace, determined by static code
8 // analysis, for background agents.
9 //
10 // However, with a null check for PhoneApplicationService.Current,
11 // we can safely use this the lifecycle events for dormant state
12 // transitions. In a background agent this property will be null;
13 // trying to create an instance of PhoneApplicationService throws
14 // a ComException and is required to set the Current property.
15 //
16 // In order to access the PhoneApplicationService functionality for
17 // non-background agent assemblies, we build a late bound wrapper
18 // around the APIs.
19 //
20 // See appplat\src\Frameworks\Microsoft\Phone\PhoneApplicationService.cs
21 // for implementation details of the class we use.
22 //
23 namespace System.Reactive.PlatformServices.Phone.Shell
24 {
25 using System;
26 using System.Collections.Generic;
27 using System.Linq.Expressions;
28 using System.Reflection;
29 using Microsoft.Phone;
30
31 class PhoneApplicationService
32 {
33 private static readonly object s_gate = new object();
34 internal static readonly Assembly s_phshAsm;
35 private static readonly Type s_pasType;
36 private static readonly PropertyInfo s_curProp;
37 private static readonly EventInfo s_actdEvt;
38 private static readonly EventInfo s_deacEvt;
39
40 private readonly object _target;
41
42 static PhoneApplicationService()
43 {
44 s_phshAsm = typeof(BackgroundAgent).Assembly; // Has to be in Microsoft.Phone.dll.
45 s_pasType = s_phshAsm.GetType("Microsoft.Phone.Shell.PhoneApplicationService");
46 s_curProp = s_pasType.GetProperty("Current", BindingFlags.Public | BindingFlags.Static);
47 s_actdEvt = s_curProp.PropertyType.GetEvent("Activated", BindingFlags.Public | BindingFlags.Instance);
48 s_deacEvt = s_curProp.PropertyType.GetEvent("Deactivated", BindingFlags.Public | BindingFlags.Instance);
49 }
50
51 private PhoneApplicationService(object target)
52 {
53 _target = target;
54 }
55
56 private static PhoneApplicationService s_current;
57
58 public static PhoneApplicationService Current
59 {
60 get
61 {
62 lock (s_gate)
63 {
64 if (s_current == null)
65 {
66 var current = s_curProp.GetValue(null, null);
67 if (current == null)
68 return null;
69
70 //
71 // Current provides a singleton. The constructor
72 // of PhoneApplicationService guarantees this,
73 // throwing an InvalidOperationException if more
74 // than one instance is created.
75 //
76 s_current = new PhoneApplicationService(current);
77 }
78 }
79
80 return s_current;
81 }
82 }
83
84 private Dictionary<object, object> _actdHandlers = new Dictionary<object, object>();
85
86 public event EventHandler<ActivatedEventArgs> Activated
87 {
88 add
89 {
90 AddHandler<ActivatedEventArgs>(s_actdEvt, _actdHandlers, value);
91 }
92
93 remove
94 {
95 RemoveHandler(s_actdEvt, _actdHandlers, value);
96 }
97 }
98
99 private Dictionary<object, object> _deacHandlers = new Dictionary<object, object>();
100
101 public event EventHandler<DeactivatedEventArgs> Deactivated
102 {
103 add
104 {
105 AddHandler<DeactivatedEventArgs>(s_deacEvt, _deacHandlers, value);
106 }
107
108 remove
109 {
110 RemoveHandler(s_deacEvt, _deacHandlers, value);
111 }
112 }
113
114 private void AddHandler<TEventArgs>(EventInfo evt, Dictionary<object, object> map, object handler)
115 where TEventArgs : EventArgs
116 {
117 var h = GetHandler<TEventArgs>(evt, handler);
118 var add = evt.GetAddMethod();
119
120 lock (s_gate)
121 {
122 map.Add(handler, h);
123 add.Invoke(_target, new object[] { h });
124 }
125 }
126
127 private void RemoveHandler(EventInfo evt, Dictionary<object, object> map, object handler)
128 {
129 var rem = evt.GetRemoveMethod();
130
131 lock (s_gate)
132 {
133 var h = default(object);
134 if (map.TryGetValue(handler, out h))
135 {
136 //
137 // We assume only one handler will be attached to
138 // the event, hence we shouldn't worry about having
139 // multiple delegate instances with the same target
140 // being attached. This guarantee is made by the
141 // reference counting in HostLifecycleService.
142 //
143 // The use of a dictionary allows for reuse with
144 // multiple distinct handlers going forward.
145 //
146 map.Remove(handler);
147 rem.Invoke(_target, new object[] { h });
148 }
149 }
150 }
151
152 private static object GetHandler<TEventArgsThunk>(EventInfo evt, object call)
153 where TEventArgsThunk : EventArgs
154 {
155 var ht = evt.EventHandlerType;
156 var hp = ht.GetMethod("Invoke").GetParameters();
157
158 var po = Expression.Parameter(hp[0].ParameterType, hp[0].Name);
159 var pe = Expression.Parameter(hp[1].ParameterType, hp[1].Name);
160
161 var h = Expression.Lambda(
162 ht,
163 Expression.Invoke(
164 Expression.Constant(call),
165 po,
166 Expression.New(
167 typeof(TEventArgsThunk).GetConstructor(new[] { typeof(object) }),
168 pe
169 )
170 ),
171 po,
172 pe
173 );
174
175 return h.Compile();
176 }
177 }
178
179 class ActivatedEventArgs : EventArgs
180 {
181 private static readonly Type s_aeaType;
182 private static readonly PropertyInfo s_aipProp;
183
184 private readonly object _target;
185
186 static ActivatedEventArgs()
187 {
188 s_aeaType = PhoneApplicationService.s_phshAsm.GetType("Microsoft.Phone.Shell.ActivatedEventArgs");
189 s_aipProp = s_aeaType.GetProperty("IsApplicationInstancePreserved", BindingFlags.Public | BindingFlags.Instance);
190 }
191
192 public ActivatedEventArgs(object target)
193 {
194 _target = target;
195 }
196
197 public bool IsApplicationInstancePreserved
198 {
199 get
200 {
201 return (bool)s_aipProp.GetValue(_target, null);
202 }
203 }
204 }
205
206 class DeactivatedEventArgs : EventArgs
207 {
208 public DeactivatedEventArgs(object target)
209 {
210 }
211 }
212 }
213 #endif
214 #endif