Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <osl/mutex.hxx>
22 : #include <osl/process.h>
23 : #include <cppuhelper/implementationentry.hxx>
24 : #include <cppuhelper/factory.hxx>
25 : #include <cppuhelper/implbase3.hxx>
26 : #include <cppuhelper/supportsservice.hxx>
27 :
28 : #include <com/sun/star/uno/XComponentContext.hpp>
29 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 : #include <com/sun/star/lang/XServiceInfo.hpp>
31 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
33 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
34 : #include <com/sun/star/container/XNameAccess.hpp>
35 : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
36 : #include <com/sun/star/beans/PropertyValue.hpp>
37 :
38 : #include <boost/bind.hpp>
39 : #include <vector>
40 : #include <utility>
41 : #include <o3tl/compat_functional.hxx>
42 : #include <algorithm>
43 :
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::uno;
47 :
48 :
49 : namespace
50 : {
51 :
52 0 : OUString SAL_CALL getImplName()
53 : {
54 0 : return OUString("com.sun.star.comp.rendering.CanvasFactory");
55 : }
56 :
57 0 : Sequence<OUString> SAL_CALL getSuppServices()
58 : {
59 0 : OUString name("com.sun.star.rendering.CanvasFactory");
60 0 : return Sequence<OUString>(&name, 1);
61 : }
62 :
63 : class CanvasFactory
64 : : public ::cppu::WeakImplHelper3< lang::XServiceInfo,
65 : lang::XMultiComponentFactory,
66 : lang::XMultiServiceFactory >
67 : {
68 : typedef std::pair<OUString,Sequence<OUString> > AvailPair;
69 : typedef std::pair<OUString,OUString> CachePair;
70 : typedef std::vector< AvailPair > AvailVector;
71 : typedef std::vector< CachePair > CacheVector;
72 :
73 :
74 : mutable ::osl::Mutex m_mutex;
75 : Reference<XComponentContext> m_xContext;
76 : Reference<container::XNameAccess> m_xCanvasConfigNameAccess;
77 : AvailVector m_aAvailableImplementations;
78 : AvailVector m_aAcceleratedImplementations;
79 : AvailVector m_aAAImplementations;
80 : mutable CacheVector m_aCachedImplementations;
81 : mutable bool m_bCacheHasForcedLastImpl;
82 : mutable bool m_bCacheHasUseAcceleratedEntry;
83 : mutable bool m_bCacheHasUseAAEntry;
84 :
85 : void checkConfigFlag( bool& r_bFlag,
86 : bool& r_CacheFlag,
87 : const OUString& nodeName ) const;
88 : Reference<XInterface> use(
89 : OUString const & serviceName,
90 : Sequence<Any> const & args,
91 : Reference<XComponentContext> const & xContext ) const;
92 : Reference<XInterface> lookupAndUse(
93 : OUString const & serviceName, Sequence<Any> const & args,
94 : Reference<XComponentContext> const & xContext ) const;
95 :
96 : public:
97 : virtual ~CanvasFactory();
98 : CanvasFactory( Reference<XComponentContext> const & xContext );
99 :
100 : // XServiceInfo
101 : virtual OUString SAL_CALL getImplementationName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
102 : virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
103 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
104 : virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
105 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
106 :
107 : // XMultiComponentFactory
108 : virtual Sequence<OUString> SAL_CALL getAvailableServiceNames()
109 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
110 : virtual Reference<XInterface> SAL_CALL createInstanceWithContext(
111 : OUString const & name,
112 : Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
113 : virtual Reference<XInterface> SAL_CALL
114 : createInstanceWithArgumentsAndContext(
115 : OUString const & name,
116 : Sequence<Any> const & args,
117 : Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
118 :
119 : // XMultiServiceFactory
120 : virtual Reference<XInterface> SAL_CALL createInstance(
121 : OUString const & name )
122 : throw (Exception, std::exception) SAL_OVERRIDE;
123 : virtual Reference<XInterface> SAL_CALL createInstanceWithArguments(
124 : OUString const & name, Sequence<Any> const & args )
125 : throw (Exception, std::exception) SAL_OVERRIDE;
126 : };
127 :
128 0 : CanvasFactory::CanvasFactory( Reference<XComponentContext> const & xContext ) :
129 : m_mutex(),
130 : m_xContext(xContext),
131 : m_xCanvasConfigNameAccess(),
132 : m_aAvailableImplementations(),
133 : m_aAcceleratedImplementations(),
134 : m_aAAImplementations(),
135 : m_aCachedImplementations(),
136 : m_bCacheHasForcedLastImpl(),
137 : m_bCacheHasUseAcceleratedEntry(),
138 0 : m_bCacheHasUseAAEntry()
139 : {
140 : try
141 : {
142 : // read out configuration for preferred services:
143 : Reference<lang::XMultiServiceFactory> xConfigProvider(
144 0 : configuration::theDefaultProvider::get( m_xContext ) );
145 :
146 : Any propValue(
147 : makeAny( beans::PropertyValue(
148 : OUString("nodepath"), -1,
149 : makeAny( OUString("/org.openoffice.Office.Canvas") ),
150 0 : beans::PropertyState_DIRECT_VALUE ) ) );
151 :
152 : m_xCanvasConfigNameAccess.set(
153 0 : xConfigProvider->createInstanceWithArguments(
154 : OUString("com.sun.star.configuration.ConfigurationAccess"),
155 0 : Sequence<Any>( &propValue, 1 ) ),
156 0 : UNO_QUERY_THROW );
157 :
158 0 : propValue = makeAny(
159 : beans::PropertyValue(
160 : OUString("nodepath"), -1,
161 : makeAny( OUString("/org.openoffice.Office.Canvas/CanvasServiceList") ),
162 0 : beans::PropertyState_DIRECT_VALUE ) );
163 :
164 : Reference<container::XNameAccess> xNameAccess(
165 0 : xConfigProvider->createInstanceWithArguments(
166 : OUString("com.sun.star.configuration.ConfigurationAccess"),
167 0 : Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
168 : Reference<container::XHierarchicalNameAccess> xHierarchicalNameAccess(
169 0 : xNameAccess, UNO_QUERY_THROW);
170 :
171 0 : Sequence<OUString> serviceNames = xNameAccess->getElementNames();
172 0 : const OUString* pCurr = serviceNames.getConstArray();
173 0 : const OUString* const pEnd = pCurr + serviceNames.getLength();
174 0 : while( pCurr != pEnd )
175 : {
176 : Reference<container::XNameAccess> xEntryNameAccess(
177 0 : xHierarchicalNameAccess->getByHierarchicalName(*pCurr),
178 0 : UNO_QUERY );
179 :
180 0 : if( xEntryNameAccess.is() )
181 : {
182 0 : Sequence<OUString> implementationList;
183 0 : if( (xEntryNameAccess->getByName("PreferredImplementations") >>= implementationList) )
184 0 : m_aAvailableImplementations.push_back( std::make_pair(*pCurr,implementationList) );
185 0 : if( (xEntryNameAccess->getByName("AcceleratedImplementations") >>= implementationList) )
186 0 : m_aAcceleratedImplementations.push_back( std::make_pair(*pCurr,implementationList) );
187 0 : if( (xEntryNameAccess->getByName("AntialiasingImplementations") >>= implementationList) )
188 0 : m_aAAImplementations.push_back( std::make_pair(*pCurr,implementationList) );
189 : }
190 :
191 0 : ++pCurr;
192 0 : }
193 : }
194 0 : catch (const RuntimeException &)
195 : {
196 0 : throw;
197 : }
198 0 : catch (const Exception&)
199 : {
200 : }
201 :
202 0 : if( m_aAvailableImplementations.empty() )
203 : {
204 : // Ugh. Looks like configuration is borked. Fake minimal
205 : // setup.
206 0 : Sequence<OUString> aServices(1);
207 0 : aServices[0] = "com.sun.star.comp.rendering.Canvas.VCL";
208 : m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.Canvas"),
209 0 : aServices) );
210 :
211 0 : aServices[0] = "com.sun.star.comp.rendering.SpriteCanvas.VCL";
212 : m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.SpriteCanvas"),
213 0 : aServices) );
214 : }
215 0 : }
216 :
217 0 : CanvasFactory::~CanvasFactory()
218 : {
219 0 : }
220 :
221 :
222 0 : Reference<XInterface> create( Reference<XComponentContext> const & xContext )
223 : {
224 : return static_cast< ::cppu::OWeakObject * >(
225 0 : new CanvasFactory( xContext ) );
226 : }
227 :
228 : // XServiceInfo
229 0 : OUString CanvasFactory::getImplementationName() throw (RuntimeException, std::exception)
230 : {
231 0 : return getImplName();
232 : }
233 :
234 0 : sal_Bool CanvasFactory::supportsService( OUString const & serviceName )
235 : throw (RuntimeException, std::exception)
236 : {
237 0 : return cppu::supportsService(this, serviceName);
238 : }
239 :
240 0 : Sequence<OUString> CanvasFactory::getSupportedServiceNames()
241 : throw (RuntimeException, std::exception)
242 : {
243 0 : return getSuppServices();
244 : }
245 :
246 : // XMultiComponentFactory
247 0 : Sequence<OUString> CanvasFactory::getAvailableServiceNames()
248 : throw (RuntimeException, std::exception)
249 : {
250 0 : Sequence<OUString> aServiceNames(m_aAvailableImplementations.size());
251 : std::transform(m_aAvailableImplementations.begin(),
252 : m_aAvailableImplementations.end(),
253 : aServiceNames.getArray(),
254 0 : o3tl::select1st<AvailPair>());
255 0 : return aServiceNames;
256 : }
257 :
258 0 : Reference<XInterface> CanvasFactory::createInstanceWithContext(
259 : OUString const & name, Reference<XComponentContext> const & xContext )
260 : throw (Exception, std::exception)
261 : {
262 : return createInstanceWithArgumentsAndContext(
263 0 : name, Sequence<Any>(), xContext );
264 : }
265 :
266 :
267 0 : Reference<XInterface> CanvasFactory::use(
268 : OUString const & serviceName,
269 : Sequence<Any> const & args,
270 : Reference<XComponentContext> const & xContext ) const
271 : {
272 : try {
273 0 : return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
274 0 : serviceName, args, xContext);
275 : }
276 0 : catch (css::lang::IllegalArgumentException &)
277 : {
278 0 : return Reference<XInterface>();
279 : }
280 0 : catch (const RuntimeException &)
281 : {
282 0 : throw;
283 : }
284 0 : catch (const Exception &)
285 : {
286 0 : return Reference<XInterface>();
287 : }
288 : }
289 :
290 :
291 0 : void CanvasFactory::checkConfigFlag( bool& r_bFlag,
292 : bool& r_CacheFlag,
293 : const OUString& nodeName ) const
294 : {
295 0 : if( m_xCanvasConfigNameAccess.is() )
296 : {
297 0 : m_xCanvasConfigNameAccess->getByName( nodeName ) >>= r_bFlag;
298 :
299 0 : if( r_CacheFlag != r_bFlag )
300 : {
301 : // cache is invalid, because of different order of
302 : // elements
303 0 : r_CacheFlag = r_bFlag;
304 0 : m_aCachedImplementations.clear();
305 : }
306 : }
307 0 : }
308 :
309 :
310 0 : Reference<XInterface> CanvasFactory::lookupAndUse(
311 : OUString const & serviceName, Sequence<Any> const & args,
312 : Reference<XComponentContext> const & xContext ) const
313 : {
314 0 : ::osl::MutexGuard guard(m_mutex);
315 :
316 : // forcing last entry from impl list, if config flag set
317 0 : bool bForceLastEntry(false);
318 : checkConfigFlag( bForceLastEntry,
319 : m_bCacheHasForcedLastImpl,
320 0 : OUString("ForceSafeServiceImpl") );
321 :
322 : // use anti-aliasing canvas, if config flag set (or not existing)
323 0 : bool bUseAAEntry(true);
324 : checkConfigFlag( bUseAAEntry,
325 : m_bCacheHasUseAAEntry,
326 0 : OUString("UseAntialiasingCanvas") );
327 :
328 : // use accelerated canvas, if config flag set (or not existing)
329 0 : bool bUseAcceleratedEntry(true);
330 : checkConfigFlag( bUseAcceleratedEntry,
331 : m_bCacheHasUseAcceleratedEntry,
332 0 : OUString("UseAcceleratedCanvas") );
333 :
334 : // try to reuse last working implementation for given service name
335 0 : const CacheVector::iterator aEnd(m_aCachedImplementations.end());
336 0 : CacheVector::iterator aMatch;
337 0 : if( (aMatch=std::find_if(m_aCachedImplementations.begin(),
338 : aEnd,
339 : boost::bind(&OUString::equals,
340 : boost::cref(serviceName),
341 : boost::bind(
342 : o3tl::select1st<CachePair>(),
343 0 : _1)))) != aEnd )
344 : {
345 0 : Reference<XInterface> xCanvas( use( aMatch->second, args, xContext ) );
346 0 : if(xCanvas.is())
347 0 : return xCanvas;
348 : }
349 :
350 : // lookup in available service list
351 0 : const AvailVector::const_iterator aAvailEnd(m_aAvailableImplementations.end());
352 0 : AvailVector::const_iterator aAvailImplsMatch;
353 0 : if( (aAvailImplsMatch=std::find_if(m_aAvailableImplementations.begin(),
354 : aAvailEnd,
355 : boost::bind(&OUString::equals,
356 : boost::cref(serviceName),
357 : boost::bind(
358 : o3tl::select1st<AvailPair>(),
359 0 : _1)))) == aAvailEnd )
360 : {
361 0 : return Reference<XInterface>();
362 : }
363 :
364 0 : const AvailVector::const_iterator aAAEnd(m_aAAImplementations.end());
365 0 : AvailVector::const_iterator aAAImplsMatch;
366 0 : if( (aAAImplsMatch=std::find_if(m_aAAImplementations.begin(),
367 : aAAEnd,
368 : boost::bind(&OUString::equals,
369 : boost::cref(serviceName),
370 : boost::bind(
371 : o3tl::select1st<AvailPair>(),
372 0 : _1)))) == aAAEnd )
373 : {
374 0 : return Reference<XInterface>();
375 : }
376 :
377 0 : const AvailVector::const_iterator aAccelEnd(m_aAcceleratedImplementations.end());
378 0 : AvailVector::const_iterator aAccelImplsMatch;
379 0 : if( (aAccelImplsMatch=std::find_if(m_aAcceleratedImplementations.begin(),
380 : aAccelEnd,
381 : boost::bind(&OUString::equals,
382 : boost::cref(serviceName),
383 : boost::bind(
384 : o3tl::select1st<AvailPair>(),
385 0 : _1)))) == aAccelEnd )
386 : {
387 0 : return Reference<XInterface>();
388 : }
389 :
390 0 : const Sequence<OUString> aPreferredImpls( aAvailImplsMatch->second );
391 0 : const OUString* pCurrImpl = aPreferredImpls.getConstArray();
392 0 : const OUString* const pEndImpl = pCurrImpl + aPreferredImpls.getLength();
393 :
394 0 : const Sequence<OUString> aAAImpls( aAAImplsMatch->second );
395 0 : const OUString* const pFirstAAImpl = aAAImpls.getConstArray();
396 0 : const OUString* const pEndAAImpl = pFirstAAImpl + aAAImpls.getLength();
397 :
398 0 : const Sequence<OUString> aAccelImpls( aAccelImplsMatch->second );
399 0 : const OUString* const pFirstAccelImpl = aAccelImpls.getConstArray();
400 0 : const OUString* const pEndAccelImpl = pFirstAccelImpl + aAccelImpls.getLength();
401 :
402 : // force last entry from impl list, if config flag set
403 0 : if( bForceLastEntry )
404 0 : pCurrImpl = pEndImpl-1;
405 :
406 0 : while( pCurrImpl != pEndImpl )
407 : {
408 0 : const OUString aCurrName(pCurrImpl->trim());
409 :
410 : // check whether given canvas service is listed in the
411 : // sequence of "accelerated canvas implementations"
412 : const bool bIsAcceleratedImpl(
413 : std::find_if(pFirstAccelImpl,
414 : pEndAccelImpl,
415 : boost::bind(&OUString::equals,
416 : boost::cref(aCurrName),
417 : boost::bind(
418 : &OUString::trim,
419 0 : _1))) != pEndAccelImpl );
420 :
421 : // check whether given canvas service is listed in the
422 : // sequence of "antialiasing canvas implementations"
423 : const bool bIsAAImpl(
424 : std::find_if(pFirstAAImpl,
425 : pEndAAImpl,
426 : boost::bind(&OUString::equals,
427 : boost::cref(aCurrName),
428 : boost::bind(
429 : &OUString::trim,
430 0 : _1))) != pEndAAImpl );
431 :
432 : // try to instantiate canvas *only* if either accel and AA
433 : // property match preference, *or*, if there's a mismatch, only
434 : // go for a less capable canvas (that effectively let those
435 : // pour canvas impls still work as fallbacks, should an
436 : // accelerated/AA one fail). Property implies configuration:
437 : // http://en.wikipedia.org/wiki/Truth_table#Logical_implication
438 0 : if( (!bIsAAImpl || bUseAAEntry) && (!bIsAcceleratedImpl || bUseAcceleratedEntry) )
439 : {
440 : Reference<XInterface> xCanvas(
441 0 : use( pCurrImpl->trim(), args, xContext ) );
442 :
443 0 : if(xCanvas.is())
444 : {
445 0 : if( aMatch != aEnd )
446 : {
447 : // cache entry exists, replace dysfunctional
448 : // implementation name
449 0 : aMatch->second = pCurrImpl->trim();
450 : }
451 : else
452 : {
453 : // new service name, add new cache entry
454 : m_aCachedImplementations.push_back(std::make_pair(serviceName,
455 0 : pCurrImpl->trim()));
456 : }
457 :
458 0 : return xCanvas;
459 0 : }
460 : }
461 :
462 0 : ++pCurrImpl;
463 0 : }
464 :
465 0 : return Reference<XInterface>();
466 : }
467 :
468 :
469 0 : Reference<XInterface> CanvasFactory::createInstanceWithArgumentsAndContext(
470 : OUString const & preferredOne, Sequence<Any> const & args,
471 : Reference<XComponentContext> const & xContext ) throw (Exception, std::exception)
472 : {
473 : Reference<XInterface> xCanvas(
474 0 : lookupAndUse( preferredOne, args, xContext ) );
475 0 : if(xCanvas.is())
476 0 : return xCanvas;
477 :
478 : // last resort: try service name directly
479 0 : return use( preferredOne, args, xContext );
480 : }
481 :
482 : // XMultiServiceFactory
483 :
484 0 : Reference<XInterface> CanvasFactory::createInstance( OUString const & name )
485 : throw (Exception, std::exception)
486 : {
487 : return createInstanceWithArgumentsAndContext(
488 0 : name, Sequence<Any>(), m_xContext );
489 : }
490 :
491 :
492 0 : Reference<XInterface> CanvasFactory::createInstanceWithArguments(
493 : OUString const & name, Sequence<Any> const & args ) throw (Exception, std::exception)
494 : {
495 : return createInstanceWithArgumentsAndContext(
496 0 : name, args, m_xContext );
497 : }
498 :
499 : const ::cppu::ImplementationEntry s_entries [] = {
500 : {
501 : create,
502 : getImplName,
503 : getSuppServices,
504 : ::cppu::createSingleComponentFactory,
505 : 0, 0
506 : },
507 : { 0, 0, 0, 0, 0, 0 }
508 : };
509 :
510 : } // anon namespace
511 :
512 : extern "C" {
513 :
514 0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL canvasfactory_component_getFactory(
515 : sal_Char const * pImplName,
516 : lang::XMultiServiceFactory * pServiceManager,
517 : registry::XRegistryKey * pRegistryKey )
518 : {
519 : return ::cppu::component_getFactoryHelper(
520 0 : pImplName, pServiceManager, pRegistryKey, s_entries );
521 : }
522 :
523 0 : }
524 :
525 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|