-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathCacheRequest.cs
204 lines (177 loc) · 5.18 KB
/
CacheRequest.cs
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
// (c) Copyright Microsoft, 2012.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections;
using System.Diagnostics;
using UIAComWrapperInternal;
using UIAutomationClient = Interop.UIAutomationClient;
namespace System.Windows.Automation
{
public sealed class CacheRequest
{
private UIAutomationClient.IUIAutomationCacheRequest _obj;
private object _lock;
private int _cRef;
[ThreadStatic]
private static Stack _cacheStack;
internal static readonly CacheRequest DefaultCacheRequest = new CacheRequest();
internal CacheRequest(UIAutomationClient.IUIAutomationCacheRequest obj)
{
Debug.Assert(obj != null);
this._obj = obj;
this._lock = new object();
}
public CacheRequest()
{
this._obj = Automation.Factory.CreateCacheRequest();
this._lock = new object();
}
public IDisposable Activate()
{
this.Push();
return new CacheRequestActivation(this);
}
public void Add(AutomationPattern pattern)
{
Utility.ValidateArgumentNonNull(pattern, "pattern");
lock (this._lock)
{
this.CheckAccess();
this._obj.AddPattern(pattern.Id);
}
}
public void Add(AutomationProperty property)
{
Utility.ValidateArgumentNonNull(property, "property");
lock (this._lock)
{
this.CheckAccess();
this._obj.AddProperty(property.Id);
}
}
private void CheckAccess()
{
if ((this._cRef != 0) || (this == DefaultCacheRequest))
{
throw new InvalidOperationException("Can't modify an active cache request");
}
}
public CacheRequest Clone()
{
return new CacheRequest(this._obj.Clone());
}
public void Pop()
{
if (((_cacheStack == null) || (_cacheStack.Count == 0)) || (_cacheStack.Peek() != this))
{
throw new InvalidOperationException("Only the top cache request can be popped");
}
_cacheStack.Pop();
lock (this._lock)
{
this._cRef--;
}
}
public void Push()
{
if (_cacheStack == null)
{
_cacheStack = new Stack();
}
_cacheStack.Push(this);
lock (this._lock)
{
this._cRef++;
}
}
public AutomationElementMode AutomationElementMode
{
get
{
return (AutomationElementMode)this._obj.AutomationElementMode;
}
set
{
lock (this._lock)
{
this.CheckAccess();
this._obj.AutomationElementMode = (UIAutomationClient.AutomationElementMode)value;
}
}
}
public static CacheRequest Current
{
get
{
if ((_cacheStack != null) && (_cacheStack.Count != 0))
{
return (CacheRequest)_cacheStack.Peek();
}
return DefaultCacheRequest;
}
}
internal static UIAutomationClient.IUIAutomationCacheRequest CurrentNativeCacheRequest
{
get
{
return CacheRequest.Current.NativeCacheRequest;
}
}
public Condition TreeFilter
{
get
{
return Condition.Wrap(this._obj.TreeFilter);
}
set
{
Utility.ValidateArgumentNonNull(value, "TreeFilter");
lock (this._lock)
{
this.CheckAccess();
this._obj.TreeFilter = value.NativeCondition;
}
}
}
public TreeScope TreeScope
{
get
{
return (TreeScope)this._obj.TreeScope;
}
set
{
lock (this._lock)
{
this.CheckAccess();
this._obj.TreeScope = (UIAutomationClient.TreeScope)value;
}
}
}
internal UIAutomationClient.IUIAutomationCacheRequest NativeCacheRequest
{
get
{
return this._obj;
}
}
}
internal class CacheRequestActivation : IDisposable
{
private CacheRequest _request;
internal CacheRequestActivation(CacheRequest request)
{
this._request = request;
}
public void Dispose()
{
if (this._request != null)
{
this._request.Pop();
this._request = null;
}
}
}
}