1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_toolkit.hxx"
30*cdf0e10cSrcweir #include <toolkit/helper/accessibilityclient.hxx>
31*cdf0e10cSrcweir #include <toolkit/helper/accessiblefactory.hxx>
32*cdf0e10cSrcweir #include <osl/module.h>
33*cdf0e10cSrcweir #include <osl/diagnose.h>
34*cdf0e10cSrcweir #include <tools/solar.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir // #define UNLOAD_ON_LAST_CLIENT_DYING
37*cdf0e10cSrcweir     // this is not recommended currently. If enabled, the implementation will log
38*cdf0e10cSrcweir     // the number of active clients, and unload the acc library when the last client
39*cdf0e10cSrcweir     // goes away.
40*cdf0e10cSrcweir     // Sounds like a good idea, unfortunately, there's no guarantee that all objects
41*cdf0e10cSrcweir     // implemented in this library are already dead.
42*cdf0e10cSrcweir     // Iow, just because an object implementing an XAccessible (implemented in this lib
43*cdf0e10cSrcweir     // here) died, it's not said that everybody released all references to the
44*cdf0e10cSrcweir     // XAccessibleContext used by this component, and implemented in the acc lib.
45*cdf0e10cSrcweir     // So we cannot really unload the lib.
46*cdf0e10cSrcweir     //
47*cdf0e10cSrcweir     // Alternatively, if the lib would us own "usage counting", i.e. every component
48*cdf0e10cSrcweir     // implemented therein would affect a static ref count, the acc lib could care
49*cdf0e10cSrcweir     // for unloading itself.
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //........................................................................
52*cdf0e10cSrcweir namespace toolkit
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir //........................................................................
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
57*cdf0e10cSrcweir     using namespace ::com::sun::star::accessibility;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     namespace
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
62*cdf0e10cSrcweir         static oslInterlockedCount                      s_nAccessibilityClients = 0;
63*cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
64*cdf0e10cSrcweir         static oslModule                                s_hAccessibleImplementationModule = NULL;
65*cdf0e10cSrcweir         static GetStandardAccComponentFactory           s_pAccessibleFactoryFunc = NULL;
66*cdf0e10cSrcweir         static ::rtl::Reference< IAccessibleFactory >   s_pFactory;
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     //====================================================================
70*cdf0e10cSrcweir 	//= AccessibleDummyFactory
71*cdf0e10cSrcweir 	//====================================================================
72*cdf0e10cSrcweir     class AccessibleDummyFactory : public IAccessibleFactory
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir     public:
75*cdf0e10cSrcweir         AccessibleDummyFactory();
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     protected:
78*cdf0e10cSrcweir         virtual ~AccessibleDummyFactory();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     private:
81*cdf0e10cSrcweir         AccessibleDummyFactory( const AccessibleDummyFactory& );            // never implemented
82*cdf0e10cSrcweir         AccessibleDummyFactory& operator=( const AccessibleDummyFactory& ); // never implemented
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         oslInterlockedCount m_refCount;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     public:
87*cdf0e10cSrcweir         // IReference
88*cdf0e10cSrcweir 	    virtual oslInterlockedCount SAL_CALL acquire();
89*cdf0e10cSrcweir 	    virtual oslInterlockedCount SAL_CALL release();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         // IAccessibleFactory
92*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
93*cdf0e10cSrcweir                 createAccessibleContext( VCLXButton* /*_pXWindow*/ )
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir             return NULL;
96*cdf0e10cSrcweir         }
97*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
98*cdf0e10cSrcweir                 createAccessibleContext( VCLXCheckBox* /*_pXWindow*/ )
99*cdf0e10cSrcweir         {
100*cdf0e10cSrcweir             return NULL;
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
103*cdf0e10cSrcweir                 createAccessibleContext( VCLXRadioButton* /*_pXWindow*/ )
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             return NULL;
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
108*cdf0e10cSrcweir                 createAccessibleContext( VCLXListBox* /*_pXWindow*/ )
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             return NULL;
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
113*cdf0e10cSrcweir                 createAccessibleContext( VCLXFixedHyperlink* /*_pXWindow*/ )
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             return NULL;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
118*cdf0e10cSrcweir                 createAccessibleContext( VCLXFixedText* /*_pXWindow*/ )
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             return NULL;
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
123*cdf0e10cSrcweir                 createAccessibleContext( VCLXScrollBar* /*_pXWindow*/ )
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             return NULL;
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
128*cdf0e10cSrcweir                 createAccessibleContext( VCLXEdit* /*_pXWindow*/ )
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir             return NULL;
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
133*cdf0e10cSrcweir                 createAccessibleContext( VCLXComboBox* /*_pXWindow*/ )
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             return NULL;
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
138*cdf0e10cSrcweir                 createAccessibleContext( VCLXToolBox* /*_pXWindow*/ )
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             return NULL;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
143*cdf0e10cSrcweir                 createAccessibleContext( VCLXWindow* /*_pXWindow*/ )
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             return NULL;
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible >
148*cdf0e10cSrcweir                 createAccessible( Menu* /*_pMenu*/, sal_Bool /*_bIsMenuBar*/ )
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir             return NULL;
151*cdf0e10cSrcweir         }
152*cdf0e10cSrcweir     };
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	//--------------------------------------------------------------------
155*cdf0e10cSrcweir     AccessibleDummyFactory::AccessibleDummyFactory()
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	//--------------------------------------------------------------------
160*cdf0e10cSrcweir     AccessibleDummyFactory::~AccessibleDummyFactory()
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	//--------------------------------------------------------------------
165*cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AccessibleDummyFactory::acquire()
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_refCount );
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	//--------------------------------------------------------------------
171*cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AccessibleDummyFactory::release()
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             delete this;
176*cdf0e10cSrcweir             return 0;
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         return m_refCount;
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     //====================================================================
182*cdf0e10cSrcweir 	//= AccessibilityClient
183*cdf0e10cSrcweir 	//====================================================================
184*cdf0e10cSrcweir 	//--------------------------------------------------------------------
185*cdf0e10cSrcweir     AccessibilityClient::AccessibilityClient()
186*cdf0e10cSrcweir         :m_bInitialized( false )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	//--------------------------------------------------------------------
191*cdf0e10cSrcweir     extern "C" { static void SAL_CALL thisModule() {} }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     void AccessibilityClient::ensureInitialized()
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         if ( m_bInitialized )
196*cdf0e10cSrcweir             return;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
201*cdf0e10cSrcweir         if ( 1 == osl_incrementInterlockedCount( &s_nAccessibilityClients ) )
202*cdf0e10cSrcweir         {   // the first client
203*cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
204*cdf0e10cSrcweir             // load the library implementing the factory
205*cdf0e10cSrcweir             if ( !s_pFactory.get() )
206*cdf0e10cSrcweir             {
207*cdf0e10cSrcweir 			    const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
208*cdf0e10cSrcweir 				    SVLIBRARY( "acc" )
209*cdf0e10cSrcweir 			    );
210*cdf0e10cSrcweir                 s_hAccessibleImplementationModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, 0 );
211*cdf0e10cSrcweir                 if ( s_hAccessibleImplementationModule != NULL )
212*cdf0e10cSrcweir                 {
213*cdf0e10cSrcweir 				    const ::rtl::OUString sFactoryCreationFunc =
214*cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii( "getStandardAccessibleFactory" );
215*cdf0e10cSrcweir 				    s_pAccessibleFactoryFunc = (GetStandardAccComponentFactory)
216*cdf0e10cSrcweir                         osl_getFunctionSymbol( s_hAccessibleImplementationModule, sFactoryCreationFunc.pData );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir                 }
219*cdf0e10cSrcweir                 OSL_ENSURE( s_pAccessibleFactoryFunc, "AccessibilityClient::ensureInitialized: could not load the library, or not retrieve the needed symbol!" );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir                 // get a factory instance
222*cdf0e10cSrcweir                 if ( s_pAccessibleFactoryFunc )
223*cdf0e10cSrcweir                 {
224*cdf0e10cSrcweir                     IAccessibleFactory* pFactory = static_cast< IAccessibleFactory* >( (*s_pAccessibleFactoryFunc)() );
225*cdf0e10cSrcweir                     OSL_ENSURE( pFactory, "AccessibilityClient::ensureInitialized: no factory provided by the A11Y lib!" );
226*cdf0e10cSrcweir                     if ( pFactory )
227*cdf0e10cSrcweir                     {
228*cdf0e10cSrcweir                         s_pFactory = pFactory;
229*cdf0e10cSrcweir                         pFactory->release();
230*cdf0e10cSrcweir                     }
231*cdf0e10cSrcweir                 }
232*cdf0e10cSrcweir             }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             if ( !s_pFactory.get() )
235*cdf0e10cSrcweir                 // the attempt to load the lib, or to create the factory, failed
236*cdf0e10cSrcweir                 // -> fall back to a dummy factory
237*cdf0e10cSrcweir                 s_pFactory = new AccessibleDummyFactory;
238*cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
239*cdf0e10cSrcweir         }
240*cdf0e10cSrcweir #endif
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         m_bInitialized = true;
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	//--------------------------------------------------------------------
246*cdf0e10cSrcweir     AccessibilityClient::~AccessibilityClient()
247*cdf0e10cSrcweir     {
248*cdf0e10cSrcweir         if ( m_bInitialized )
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
253*cdf0e10cSrcweir             if( 0 == osl_decrementInterlockedCount( &s_nAccessibilityClients ) )
254*cdf0e10cSrcweir             {
255*cdf0e10cSrcweir                 s_pFactory = NULL;
256*cdf0e10cSrcweir                 s_pAccessibleFactoryFunc = NULL;
257*cdf0e10cSrcweir                 if ( s_hAccessibleImplementationModule )
258*cdf0e10cSrcweir                 {
259*cdf0e10cSrcweir                     osl_unloadModule( s_hAccessibleImplementationModule );
260*cdf0e10cSrcweir                     s_hAccessibleImplementationModule = NULL;
261*cdf0e10cSrcweir                 }
262*cdf0e10cSrcweir             }
263*cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
264*cdf0e10cSrcweir         }
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     //--------------------------------------------------------------------
268*cdf0e10cSrcweir     IAccessibleFactory& AccessibilityClient::getFactory()
269*cdf0e10cSrcweir     {
270*cdf0e10cSrcweir         ensureInitialized();
271*cdf0e10cSrcweir         OSL_ENSURE( s_pFactory.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
272*cdf0e10cSrcweir         return *s_pFactory;
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir //........................................................................
276*cdf0e10cSrcweir }   // namespace toolkit
277*cdf0e10cSrcweir //........................................................................
278