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