IA2Support.cpp
10.6 KB
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
/*
This file is a part of the NVDA project.
URL: http://www.nvda-project.org/
Copyright 2006-2010 NVDA contributers.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2.0, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This license can be found at:
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
#include <cstdio>
#include <cwchar>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <objbase.h>
#include <ia2.h>
#include "nvdaControllerInternal.h"
#include <common/log.h>
#include "nvdaHelperRemote.h"
#include "dllmain.h"
#include "inProcess.h"
#include "nvdaInProcUtils.h"
#include "IA2Support.h"
#define APPLICATION_USER_MODEL_ID_MAX_LENGTH 131
typedef LONG(WINAPI *GetCurrentApplicationUserModelId_funcType)(UINT32*,PWSTR);
typedef ULONG(*LPFNDLLCANUNLOADNOW)();
#pragma data_seg(".ia2SupportShared")
wchar_t IA2DllPath[MAX_PATH]={0};
IID ia2Iids[]={
IID_IAccessible2,
IID_IAccessibleAction,
IID_IAccessibleApplication,
IID_IAccessibleComponent,
IID_IAccessibleEditableText,
IID_IAccessibleHyperlink,
IID_IAccessibleHypertext,
IID_IAccessibleImage,
IID_IAccessibleRelation,
IID_IAccessibleTable,
IID_IAccessibleTable2,
IID_IAccessibleTableCell,
IID_IAccessibleText,
IID_IAccessibleValue,
};
#pragma data_seg()
#pragma comment(linker, "/section:.ia2SupportShared,rws")
#define IAccessible2ProxyIID IID_IAccessible2
IID _ia2PSClsidBackups[ARRAYSIZE(ia2Iids)]={0};
bool isIA2Installed=FALSE;
HINSTANCE IA2DllHandle=0;
DWORD IA2RegCooky=0;
HANDLE IA2UIThreadHandle=NULL;
DWORD IA2UIThreadID=0;
HANDLE IA2UIThreadUninstalledEvent=NULL;
UINT wm_uninstallIA2Support=0;
bool isIA2Initialized=FALSE;
bool isIA2SupportDisabled=false;
bool installIA2Support() {
LPFNGETCLASSOBJECT IA2Dll_DllGetClassObject;
int i;
int res;
if(isIA2Installed) return FALSE;
if((IA2DllHandle=CoLoadLibrary(IA2DllPath,FALSE))==NULL) {
LOG_ERROR(L"CoLoadLibrary failed");
return FALSE;
}
IA2Dll_DllGetClassObject=(LPFNGETCLASSOBJECT)GetProcAddress(static_cast<HMODULE>(IA2DllHandle),"DllGetClassObject");
nhAssert(IA2Dll_DllGetClassObject); //IAccessible2 proxy dll must have this function
IUnknown* ia2ClassObjPunk=NULL;
if((res=IA2Dll_DllGetClassObject(IAccessible2ProxyIID,IID_IUnknown,(LPVOID*)&ia2ClassObjPunk))!=S_OK) {
LOG_ERROR(L"Error calling DllGetClassObject, code "<<res);
CoFreeLibrary(IA2DllHandle);
IA2DllHandle=0;
return FALSE;
}
if((res=CoRegisterClassObject(IAccessible2ProxyIID,ia2ClassObjPunk,CLSCTX_INPROC_SERVER,REGCLS_MULTIPLEUSE,(LPDWORD)&IA2RegCooky))!=S_OK) {
LOG_DEBUGWARNING(L"Error registering class object, code "<<res);
ia2ClassObjPunk->Release();
CoFreeLibrary(IA2DllHandle);
IA2DllHandle=0;
return FALSE;
}
ia2ClassObjPunk->Release();
for(i=0;i<ARRAYSIZE(ia2Iids);++i) {
CoGetPSClsid(ia2Iids[i],&(_ia2PSClsidBackups[i]));
CoRegisterPSClsid(ia2Iids[i],IAccessible2ProxyIID);
}
isIA2Installed=TRUE;
return TRUE;
}
bool uninstallIA2Support() {
int i;
LPFNDLLCANUNLOADNOW IA2Dll_DllCanUnloadNow;
if(!isIA2Installed)
return FALSE;
for(i=0;i<ARRAYSIZE(ia2Iids);++i) {
CoRegisterPSClsid(ia2Iids[i],_ia2PSClsidBackups[i]);
}
CoRevokeClassObject(IA2RegCooky);
IA2Dll_DllCanUnloadNow=(LPFNDLLCANUNLOADNOW)GetProcAddress(static_cast<HMODULE>(IA2DllHandle),"DllCanUnloadNow");
nhAssert(IA2Dll_DllCanUnloadNow); //IAccessible2 proxy dll must have this function
if(IA2Dll_DllCanUnloadNow()==S_OK) {
CoFreeLibrary(IA2DllHandle);
}
IA2DllHandle=0;
isIA2Installed=FALSE;
return TRUE;
}
bool IA2Support_initialize() {
nhAssert(!isIA2Initialized);
wsprintf(IA2DllPath,L"%s\\IAccessible2Proxy.dll",dllDirectory);
isIA2Initialized=TRUE;
return TRUE;
}
void CALLBACK IA2Support_winEventProcHook(HWINEVENTHOOK hookID, DWORD eventID, HWND hwnd, long objectID, long childID, DWORD threadID, DWORD time) {
if (eventID != EVENT_SYSTEM_FOREGROUND && eventID != EVENT_OBJECT_FOCUS)
return;
if (installIA2Support()) {
IA2UIThreadHandle=OpenThread(SYNCHRONIZE,false,threadID);
IA2UIThreadID=threadID;
// IA2 support successfully installed, so this hook isn't needed anymore.
unregisterWinEventHook(IA2Support_winEventProcHook);
}
}
LRESULT CALLBACK IA2Support_uninstallerHook(int code, WPARAM wParam, LPARAM lParam) {
MSG* pmsg=(MSG*)lParam;
if(pmsg->message==wm_uninstallIA2Support) {
uninstallIA2Support();
SetEvent(IA2UIThreadUninstalledEvent);
}
return 0;
}
void IA2Support_inProcess_initialize() {
if (isIA2Installed||isIA2SupportDisabled)
return;
// #5417: disable IAccessible2 support for suspendable processes to work around a deadlock in NVDAHelperRemote (specifically seen in Win10 searchUI)
HMODULE kernel32Handle=LoadLibrary(L"kernel32.dll");
if(!kernel32Handle) {
LOG_ERROR(L"Can't load kernel32.dll");
return;
}
GetCurrentApplicationUserModelId_funcType GetCurrentApplicationUserModelId_fp=(GetCurrentApplicationUserModelId_funcType)GetProcAddress(kernel32Handle,"GetCurrentApplicationUserModelId");
if(GetCurrentApplicationUserModelId_fp) {
UINT32 bufSize=APPLICATION_USER_MODEL_ID_MAX_LENGTH+1;
wchar_t* buf=(wchar_t*)malloc(bufSize*sizeof(wchar_t));
LONG res=GetCurrentApplicationUserModelId_fp(&bufSize,buf);
if(res==ERROR_SUCCESS) {
isIA2SupportDisabled=true;
LOG_DEBUGWARNING(L"disabling IA2 support");
} else if(res==ERROR_INSUFFICIENT_BUFFER) {
LOG_ERROR(L"string not long enough");
}
free(buf);
}
FreeLibrary(kernel32Handle);
if(isIA2SupportDisabled) return;
// Try to install IA2 support on focus/foreground changes.
// This hook will be unregistered by the callback once IA2 support is successfully installed.
registerWinEventHook(IA2Support_winEventProcHook);
}
void IA2Support_inProcess_terminate() {
// This will do nothing if the hook isn't registered.
unregisterWinEventHook(IA2Support_winEventProcHook);
if(!isIA2Installed||!IA2UIThreadHandle) {
return;
}
//Check if the UI thread is still alive, if not there's nothing for us to do
if(WaitForSingleObject(IA2UIThreadHandle,0)==0) {
return;
}
//Instruct the UI thread to uninstall IA2
IA2UIThreadUninstalledEvent=CreateEvent(NULL,true,false,NULL);
registerWindowsHook(WH_GETMESSAGE,IA2Support_uninstallerHook);
wm_uninstallIA2Support=RegisterWindowMessage(L"wm_uninstallIA2Support");
PostThreadMessage(IA2UIThreadID,wm_uninstallIA2Support,0,0);
HANDLE waitHandles[2]={IA2UIThreadUninstalledEvent,IA2UIThreadHandle};
int res=WaitForMultipleObjects(2,waitHandles,false,10000);
if(res!=WAIT_OBJECT_0&&res!=WAIT_OBJECT_0+1) {
LOG_DEBUGWARNING(L"WaitForMultipleObjects returned "<<res);
}
unregisterWindowsHook(WH_GETMESSAGE,IA2Support_uninstallerHook);
CloseHandle(IA2UIThreadUninstalledEvent);
CloseHandle(IA2UIThreadHandle);
}
const long FINDCONTENTDESCENDANT_FIRST=0;
const long FINDCONTENTDESCENDANT_CARET=1;
const long FINDCONTENTDESCENDANT_LAST=2;
const long FINDCONTENTDESCENDANT_SELECTIONSTART=3;
const long FINDCONTENTDESCENDANT_SELECTIONEND=4;
bool findContentDescendant(IAccessible2* pacc2, long what, long* descendantID, long* descendantOffset) {
bool foundDescendant=false;
IAccessibleText* paccText=NULL;
pacc2->QueryInterface(IID_IAccessibleText,(void**)&paccText);
if(paccText) {
long offset=-1;
switch(what) {
case FINDCONTENTDESCENDANT_FIRST:
offset=0;
break;
case FINDCONTENTDESCENDANT_CARET:
paccText->get_caretOffset(&offset);
break;
case FINDCONTENTDESCENDANT_LAST:
paccText->get_nCharacters(&offset);
// If there is no text, last is still valid but should just use 0.
if (offset > 0)
--offset;
break;
case FINDCONTENTDESCENDANT_SELECTIONSTART:
case FINDCONTENTDESCENDANT_SELECTIONEND:
long nSelections=0;
paccText->get_nSelections(&nSelections);
if(nSelections==0) {
offset=-1;
} else {
long startOffset=0;
long endOffset=0;
paccText->get_selection(0,&startOffset,&endOffset);
offset=(what==FINDCONTENTDESCENDANT_SELECTIONSTART)?startOffset:endOffset-1;
}
break;
}
paccText->Release();
if(offset==-1) return false;
IAccessibleHypertext* paccHypertext=NULL;
pacc2->QueryInterface(IID_IAccessibleHypertext,(void**)&paccHypertext);
if(paccHypertext) {
long hi=-1;
paccHypertext->get_hyperlinkIndex(offset,&hi);
IAccessibleHyperlink* paccHyperlink=NULL;
if(hi>=0) {
paccHypertext->get_hyperlink(hi,&paccHyperlink);
}
paccHypertext->Release();
if(paccHyperlink) {
IAccessible2* pacc2Child=NULL;
paccHyperlink->QueryInterface(IID_IAccessible2,(void**)&pacc2Child);
paccHyperlink->Release();
if(pacc2Child) {
foundDescendant=findContentDescendant(pacc2Child,what,descendantID,descendantOffset);
if(!foundDescendant&&what==FINDCONTENTDESCENDANT_CARET) {
foundDescendant=findContentDescendant(pacc2Child,FINDCONTENTDESCENDANT_FIRST,descendantID,descendantOffset);
}
pacc2Child->Release();
}
}
}
if(!foundDescendant) {
pacc2->get_uniqueID(descendantID);
*descendantOffset=offset;
foundDescendant=true;
}
} else {
long childCount=0;
pacc2->get_accChildCount(&childCount);
VARIANT varChild;
varChild.vt=VT_I4;
for(int i=1;i<=childCount;++i) {
varChild.lVal=(what==FINDCONTENTDESCENDANT_LAST||what==FINDCONTENTDESCENDANT_SELECTIONEND)?(childCount-(i-1)):i;
IDispatch* pdispatchChild=NULL;
pacc2->get_accChild(varChild,&pdispatchChild);
if(!pdispatchChild) continue;
IAccessible2* pacc2Child=NULL;
pdispatchChild->QueryInterface(IID_IAccessible2,(void**)&pacc2Child);
pdispatchChild->Release();
if(!pacc2Child) continue;
foundDescendant=findContentDescendant(pacc2Child,what,descendantID,descendantOffset);
pacc2Child->Release();
if(foundDescendant) break;
}
}
return foundDescendant;
}
error_status_t nvdaInProcUtils_IA2Text_findContentDescendant(handle_t bindingHandle, const unsigned long windowHandle, long parentID, long what, long* descendantID, long* descendantOffset) {
HWND hwnd=(HWND)UlongToHandle(windowHandle);
auto func=[&](void* data){
IAccessible* pacc=NULL;
VARIANT varChild;
AccessibleObjectFromEvent((HWND)hwnd,OBJID_CLIENT,parentID,&pacc,&varChild);
if(!pacc) return;
IAccessible2* pacc2=NULL;
IServiceProvider* pserv=NULL;
pacc->QueryInterface(IID_IServiceProvider,(void**)&pserv);
pacc->Release();
if(!pserv) return;
pserv->QueryService(IID_IAccessible,IID_IAccessible2,(void**)&pacc2);
pserv->Release();
if(!pacc2) return;
findContentDescendant(pacc2,what,descendantID,descendantOffset);
pacc2->Release();
};
execInWindow((HWND)hwnd,func,NULL);
return 0;
}