1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 
31 //------------------------------------------------------------------------
32 // includes
33 //------------------------------------------------------------------------
34 
35 #include <WindowsAccessBridgeAdapter.h>
36 
37 #include <tools/prewin.h>
38 #include <wtypes.h>
39 #include <tools/postwin.h>
40 #include <rtl/process.h>
41 #include <tools/link.hxx>
42 
43 #ifndef _SVAPP_HXX
44 #include <vcl/svapp.hxx>
45 #endif
46 #include <vcl/window.hxx>
47 #include <vcl/sysdata.hxx>
48 #include <uno/current_context.hxx>
49 #include <uno/environment.h>
50 #include <uno/mapping.hxx>
51 #include <com/sun/star/accessibility/AccessibleRole.hpp>
52 #include <com/sun/star/accessibility/XAccessible.hpp>
53 
54 #ifndef _JVMACCESS_UNOVIRTUALMACHINE_HXX_
55 #include "jvmaccess/unovirtualmachine.hxx"
56 #endif
57 
58 #ifndef _JVMACCESS_VIRTUALMACHINE_HXX_
59 #include "jvmaccess/virtualmachine.hxx"
60 #endif
61 
62 #include <osl/diagnose.h>
63 
64 using ::rtl::OUString;
65 using ::com::sun::star::uno::Mapping;
66 using ::com::sun::star::uno::Reference;
67 using ::com::sun::star::uno::RuntimeException;
68 using namespace ::com::sun::star::accessibility;
69 
70 long VCLEventListenerLinkFunc(void * pInst, void * pData);
71 
72 //------------------------------------------------------------------------
73 // global vatiables
74 //------------------------------------------------------------------------
75 
76 Link g_aEventListenerLink(NULL, VCLEventListenerLinkFunc);
77 
78 rtl::Reference< jvmaccess::UnoVirtualMachine > g_xUnoVirtualMachine;
79 typelib_InterfaceTypeDescription * g_pTypeDescription = NULL;
80 Mapping g_unoMapping;
81 
82 jclass g_jcWindowsAccessBridgeAdapter = NULL;
83 jmethodID g_jmRegisterTopWindow = 0;
84 jmethodID g_jmRevokeTopWindow = 0;
85 
86 //------------------------------------------------------------------------
87 // functions
88 //------------------------------------------------------------------------
89 
90 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *, void *)
91 {
92     return JNI_VERSION_1_2;
93 }
94 
95 JNIEXPORT jbyteArray JNICALL
96 Java_org_openoffice_accessibility_WindowsAccessBridgeAdapter_getProcessID(JNIEnv *pJNIEnv, jclass clazz)
97 {
98     // Initialize global class and method references
99     g_jcWindowsAccessBridgeAdapter =
100         static_cast< jclass > (pJNIEnv->NewGlobalRef(clazz));
101     if (NULL == g_jcWindowsAccessBridgeAdapter) {
102         return 0; /* jni error occured */
103     }
104     g_jmRegisterTopWindow =
105         pJNIEnv->GetStaticMethodID(clazz, "registerTopWindow", "(ILcom/sun/star/accessibility/XAccessible;)V");
106     if (0 == g_jmRegisterTopWindow) {
107         return 0; /* jni error occured */
108     }
109     g_jmRevokeTopWindow =
110         pJNIEnv->GetStaticMethodID(clazz, "revokeTopWindow", "(ILcom/sun/star/accessibility/XAccessible;)V");
111     if (0 == g_jmRevokeTopWindow) {
112         return 0; /* jni error occured */
113     }
114 
115     // Use the special protocol of XJavaVM.getJavaVM:  If the passed in
116     // process ID has an extra 17th byte of value one, the returned any
117     // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of
118     // the underlying JavaVM pointer:
119     jbyte processID[17];
120     rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *> (processID));
121     // #i51265# we need a jvmaccess::UnoVirtualMachine pointer for the
122     // uno_getEnvironment() call later.
123     processID[16] = 1;
124 
125     // Copy the result into a java byte[] and return.
126     jbyteArray jbaProcessID = pJNIEnv->NewByteArray(17);
127     pJNIEnv->SetByteArrayRegion(jbaProcessID, 0, 17, processID);
128     return jbaProcessID;
129 }
130 
131 JNIEXPORT jboolean JNICALL
132 Java_org_openoffice_accessibility_WindowsAccessBridgeAdapter_createMapping(JNIEnv *, jclass, jlong pointer)
133 {
134 	uno_Environment * pJava_environment = NULL;
135 	uno_Environment * pUno_environment = NULL;
136 
137     try {
138         // We get a non-refcounted pointer to a jvmaccess::VirtualMachine
139         // from the XJavaVM service (the pointer is guaranteed to be valid
140         // as long as our reference to the XJavaVM service lasts), and
141         // convert the non-refcounted pointer into a refcounted one
142         // immediately:
143         g_xUnoVirtualMachine = reinterpret_cast< jvmaccess::UnoVirtualMachine * >(pointer);
144 
145         if ( g_xUnoVirtualMachine.is() )
146         {
147             OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java"));
148             uno_getEnvironment(&pJava_environment, sJava.pData, g_xUnoVirtualMachine.get());
149 
150             OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
151             uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL);
152 
153             if ( pJava_environment && pUno_environment )
154             {
155                 g_unoMapping = Mapping(pUno_environment, pJava_environment);
156                 getCppuType((::com::sun::star::uno::Reference< XAccessible > *) 0).getDescription((typelib_TypeDescription **) & g_pTypeDescription);
157             }
158 
159             if ( pJava_environment )
160             {
161                 // release java environment
162                 pJava_environment->release(pJava_environment);
163                 pJava_environment = NULL;
164             }
165 
166             if ( pUno_environment )
167             {
168                 // release uno environment
169                 pUno_environment->release(pUno_environment);
170                 pUno_environment = NULL;
171             }
172         }
173     }
174 
175     catch ( RuntimeException e)
176     {
177         OSL_TRACE("RuntimeException caught while initializing the mapping");
178     }
179 
180     if ( (0 != g_jmRegisterTopWindow) && (0 != g_jmRevokeTopWindow) )
181     {
182         ::Application::AddEventListener(g_aEventListenerLink);
183     }
184     return JNI_TRUE;
185 }
186 
187 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *)
188 {
189     ::Application::RemoveEventListener(g_aEventListenerLink);
190 
191     if ( NULL != g_jcWindowsAccessBridgeAdapter )
192     {
193         JNIEnv * pJNIEnv;
194         if ( ! jvm->GetEnv((void **) &pJNIEnv, JNI_VERSION_1_2) )
195         {
196             pJNIEnv->DeleteGlobalRef(g_jcWindowsAccessBridgeAdapter);
197             g_jcWindowsAccessBridgeAdapter = NULL;
198         }
199     }
200 
201     if ( NULL != g_pTypeDescription )
202     {
203         typelib_typedescription_release( reinterpret_cast< typelib_TypeDescription * > (g_pTypeDescription) );
204         g_pTypeDescription = NULL;
205     }
206 
207     g_unoMapping.clear();
208     g_xUnoVirtualMachine.clear();
209 }
210 
211 HWND GetHWND(Window * pWindow)
212 {
213     const SystemEnvData * pEnvData = pWindow->GetSystemData();
214     if (pEnvData != NULL)
215     {
216         return pEnvData->hWnd;
217     }
218     return (HWND) -1;
219 }
220 
221 void handleWindowEvent(Window * pWindow, bool bShow)
222 {
223     if ( pWindow && pWindow->IsTopWindow() )
224     {
225         ::com::sun::star::uno::Reference< XAccessible > xAccessible;
226 
227         // Test for combo box - drop down floating windows first
228         Window * pParentWindow = pWindow->GetParent();
229 
230         if ( pParentWindow )
231         {
232             try
233             {
234                 // The parent window of a combo box floating window should have the role COMBO_BOX
235                 ::com::sun::star::uno::Reference< XAccessible > xParentAccessible(pParentWindow->GetAccessible());
236                 if ( xParentAccessible.is() )
237                 {
238                     ::com::sun::star::uno::Reference< XAccessibleContext > xParentAC(xParentAccessible->getAccessibleContext());
239                     if ( xParentAC.is() && (AccessibleRole::COMBO_BOX == xParentAC->getAccessibleRole()) )
240                     {
241                         // O.k. - this is a combo box floating window corresponding to the child of role LIST of the parent.
242                         // Let's not rely on a specific child order, just search for the child with the role LIST
243                         sal_Int32 nCount = xParentAC->getAccessibleChildCount();
244                         for ( sal_Int32 n = 0; (n < nCount) && !xAccessible.is(); n++)
245                         {
246                             ::com::sun::star::uno::Reference< XAccessible > xChild = xParentAC->getAccessibleChild(n);
247                             if ( xChild.is() )
248                             {
249                                 ::com::sun::star::uno::Reference< XAccessibleContext > xChildAC = xChild->getAccessibleContext();
250                                 if ( xChildAC.is() && (AccessibleRole::LIST == xChildAC->getAccessibleRole()) )
251                                 {
252                                     xAccessible = xChild;
253                                 }
254                             }
255                         }
256                     }
257                 }
258             }
259             catch (::com::sun::star::uno::RuntimeException e)
260             {
261                 // Ignore show events that throw DisposedExceptions in getAccessibleContext(),
262                 // but keep revoking these windows in hide(s).
263                 if (bShow)
264                     return;
265             }
266         }
267 
268         // We have to rely on the fact that Window::GetAccessible()->getAccessibleContext() returns a valid XAccessibleContext
269         // also for other menus than menubar or toplevel popup window. Otherwise we had to traverse the hierarchy to find the
270         // context object to this menu floater. This makes the call to Window->IsMenuFloatingWindow() obsolete.
271         if ( ! xAccessible.is() )
272             xAccessible = pWindow->GetAccessible();
273 
274         if ( xAccessible.is() && g_unoMapping.is() )
275         {
276             jobject * joXAccessible = reinterpret_cast < jobject * > (g_unoMapping.mapInterface(
277                 xAccessible.get(), g_pTypeDescription));
278 
279             if ( NULL != joXAccessible )
280             {
281                 jvmaccess::VirtualMachine::AttachGuard aGuard(g_xUnoVirtualMachine->getVirtualMachine());
282                 JNIEnv * pJNIEnv = aGuard.getEnvironment();
283 
284                 if ( NULL != pJNIEnv )
285                 {
286                     // g_jmRegisterTopWindow and g_jmRevokeTopWindow are ensured to be != 0 - otherwise
287                     // the event listener would not have been attached.
288                     pJNIEnv->CallStaticVoidMethod(g_jcWindowsAccessBridgeAdapter,
289                         (bShow) ? g_jmRegisterTopWindow : g_jmRevokeTopWindow,
290                         (jint) GetHWND(pWindow), joXAccessible );
291 
292                     // Clear any exception that might have been occured.
293                     if (pJNIEnv->ExceptionCheck()) {
294                         pJNIEnv->ExceptionClear();
295                     }
296                 }
297             }
298         }
299     }
300 }
301 
302 long VCLEventListenerLinkFunc(void *, void * pData)
303 {
304     ::VclSimpleEvent const * pEvent = (::VclSimpleEvent const *) pData;
305 
306     switch (pEvent->GetId())
307     {
308     case VCLEVENT_WINDOW_SHOW:
309         handleWindowEvent(((::VclWindowEvent const *) pEvent)->GetWindow(), true);
310 	    break;
311     case VCLEVENT_WINDOW_HIDE:
312         handleWindowEvent(((::VclWindowEvent const *) pEvent)->GetWindow(), false);
313         break;
314     }
315 
316     return 0;
317 }
318