1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <toolkit/helper/accessibilityclient.hxx>
21 #include <toolkit/helper/accessiblefactory.hxx>
22 #include <osl/module.h>
23 #include <osl/diagnose.h>
24 #include <tools/solar.h>
26 // #define UNLOAD_ON_LAST_CLIENT_DYING
27 // this is not recommended currently. If enabled, the implementation will log
28 // the number of active clients, and unload the acc library when the last client
30 // Sounds like a good idea, unfortunately, there's no guarantee that all objects
31 // implemented in this library are already dead.
32 // Iow, just because an object implementing an XAccessible (implemented in this lib
33 // here) died, it's not said that everybody released all references to the
34 // XAccessibleContext used by this component, and implemented in the acc lib.
35 // So we cannot really unload the lib.
37 // Alternatively, if the lib would us own "usage counting", i.e. every component
38 // implemented therein would affect a static ref count, the acc lib could care
39 // for unloading itself.
41 //........................................................................
44 //........................................................................
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::accessibility
;
51 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
52 static oslInterlockedCount s_nAccessibilityClients
= 0;
53 #endif // UNLOAD_ON_LAST_CLIENT_DYING
54 #ifndef DISABLE_DYNLOADING
55 static oslModule s_hAccessibleImplementationModule
= NULL
;
57 static GetStandardAccComponentFactory s_pAccessibleFactoryFunc
= NULL
;
58 static ::rtl::Reference
< IAccessibleFactory
> s_pFactory
;
61 //====================================================================
62 //= AccessibleDummyFactory
63 //====================================================================
64 class AccessibleDummyFactory
: public IAccessibleFactory
67 AccessibleDummyFactory();
70 virtual ~AccessibleDummyFactory();
73 AccessibleDummyFactory( const AccessibleDummyFactory
& ); // never implemented
74 AccessibleDummyFactory
& operator=( const AccessibleDummyFactory
& ); // never implemented
76 oslInterlockedCount m_refCount
;
80 virtual oslInterlockedCount SAL_CALL
acquire();
81 virtual oslInterlockedCount SAL_CALL
release();
84 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
85 createAccessibleContext( VCLXButton
* /*_pXWindow*/ )
89 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
90 createAccessibleContext( VCLXCheckBox
* /*_pXWindow*/ )
94 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
95 createAccessibleContext( VCLXRadioButton
* /*_pXWindow*/ )
99 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
100 createAccessibleContext( VCLXListBox
* /*_pXWindow*/ )
104 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
105 createAccessibleContext( VCLXFixedHyperlink
* /*_pXWindow*/ )
109 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
110 createAccessibleContext( VCLXFixedText
* /*_pXWindow*/ )
114 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
115 createAccessibleContext( VCLXScrollBar
* /*_pXWindow*/ )
119 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
120 createAccessibleContext( VCLXEdit
* /*_pXWindow*/ )
124 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
125 createAccessibleContext( VCLXComboBox
* /*_pXWindow*/ )
129 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
130 createAccessibleContext( VCLXToolBox
* /*_pXWindow*/ )
134 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
135 createAccessibleContext( VCLXWindow
* /*_pXWindow*/ )
139 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
>
140 createAccessible( Menu
* /*_pMenu*/, sal_Bool
/*_bIsMenuBar*/ )
146 //--------------------------------------------------------------------
147 AccessibleDummyFactory::AccessibleDummyFactory()
151 //--------------------------------------------------------------------
152 AccessibleDummyFactory::~AccessibleDummyFactory()
156 //--------------------------------------------------------------------
157 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::acquire()
159 return osl_atomic_increment( &m_refCount
);
162 //--------------------------------------------------------------------
163 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::release()
165 if ( 0 == osl_atomic_decrement( &m_refCount
) )
173 //====================================================================
174 //= AccessibilityClient
175 //====================================================================
176 //--------------------------------------------------------------------
177 AccessibilityClient::AccessibilityClient()
178 :m_bInitialized( false )
182 //--------------------------------------------------------------------
183 #ifndef DISABLE_DYNLOADING
184 extern "C" { static void SAL_CALL
thisModule() {} }
186 extern "C" void *getStandardAccessibleFactory();
189 void AccessibilityClient::ensureInitialized()
191 if ( m_bInitialized
)
194 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
196 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
197 if ( 1 == osl_atomic_increment( &s_nAccessibilityClients
) )
198 { // the first client
199 #endif // UNLOAD_ON_LAST_CLIENT_DYING
200 // load the library implementing the factory
201 if ( !s_pFactory
.get() )
203 #ifndef DISABLE_DYNLOADING
204 const ::rtl::OUString
sModuleName( SVLIBRARY( "acc" ) );
205 s_hAccessibleImplementationModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
206 if ( s_hAccessibleImplementationModule
!= NULL
)
208 const ::rtl::OUString sFactoryCreationFunc
=
209 ::rtl::OUString("getStandardAccessibleFactory");
210 s_pAccessibleFactoryFunc
= (GetStandardAccComponentFactory
)
211 osl_getFunctionSymbol( s_hAccessibleImplementationModule
, sFactoryCreationFunc
.pData
);
214 OSL_ENSURE( s_pAccessibleFactoryFunc
, "AccessibilityClient::ensureInitialized: could not load the library, or not retrieve the needed symbol!" );
216 s_pAccessibleFactoryFunc
= getStandardAccessibleFactory
;
219 // get a factory instance
220 if ( s_pAccessibleFactoryFunc
)
222 IAccessibleFactory
* pFactory
= static_cast< IAccessibleFactory
* >( (*s_pAccessibleFactoryFunc
)() );
223 OSL_ENSURE( pFactory
, "AccessibilityClient::ensureInitialized: no factory provided by the A11Y lib!" );
226 s_pFactory
= pFactory
;
232 if ( !s_pFactory
.get() )
233 // the attempt to load the lib, or to create the factory, failed
234 // -> fall back to a dummy factory
235 s_pFactory
= new AccessibleDummyFactory
;
236 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
240 m_bInitialized
= true;
243 //--------------------------------------------------------------------
244 AccessibilityClient::~AccessibilityClient()
246 if ( m_bInitialized
)
248 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
250 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
251 if( 0 == osl_atomic_decrement( &s_nAccessibilityClients
) )
254 s_pAccessibleFactoryFunc
= NULL
;
255 if ( s_hAccessibleImplementationModule
)
257 osl_unloadModule( s_hAccessibleImplementationModule
);
258 s_hAccessibleImplementationModule
= NULL
;
261 #endif // UNLOAD_ON_LAST_CLIENT_DYING
265 //--------------------------------------------------------------------
266 IAccessibleFactory
& AccessibilityClient::getFactory()
269 OSL_ENSURE( s_pFactory
.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
273 //........................................................................
274 } // namespace toolkit
275 //........................................................................
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */