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/.
10 #include <sal/config.h>
16 #include <string_view>
20 #include <com/sun/star/beans/NamedValue.hpp>
21 #include <com/sun/star/beans/PropertyAttribute.hpp>
22 #include <com/sun/star/container/ElementExistException.hpp>
23 #include <com/sun/star/container/XEnumeration.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <com/sun/star/loader/XImplementationLoader.hpp>
30 #include <com/sun/star/registry/InvalidRegistryException.hpp>
31 #include <com/sun/star/uno/DeploymentException.hpp>
32 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <comphelper/sequence.hxx>
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/component_context.hxx>
37 #include <cppuhelper/implbase.hxx>
38 #include <cppuhelper/supportsservice.hxx>
39 #include <cppuhelper/factory.hxx>
40 #include <o3tl/safeint.hxx>
41 #include <osl/file.hxx>
42 #include <osl/module.hxx>
43 #include <rtl/ref.hxx>
44 #include <rtl/uri.hxx>
45 #include <rtl/ustring.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <sal/log.hxx>
48 #include <uno/environment.hxx>
49 #include <uno/mapping.hxx>
50 #include <o3tl/string_view.hxx>
52 #include "loadsharedlibcomponentfactory.hxx"
54 #include <registry/registry.hxx>
55 #include <xmlreader/xmlreader.hxx>
58 #include "servicemanager.hxx"
62 void insertImplementationMap(
63 cppuhelper::ServiceManager::Data::ImplementationMap
* destination
,
64 cppuhelper::ServiceManager::Data::ImplementationMap
const & source
)
66 assert(destination
!= nullptr);
67 for (const auto& [rName
, rImpls
] : source
)
71 cppuhelper::ServiceManager::Data::Implementation
> > & impls
72 = (*destination
)[rName
];
73 impls
.insert(impls
.end(), rImpls
.begin(), rImpls
.end());
77 void removeFromImplementationMap(
78 cppuhelper::ServiceManager::Data::ImplementationMap
* map
,
79 std::vector
< OUString
> const & elements
,
80 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
>
81 const & implementation
)
83 // The underlying data structures make this function somewhat inefficient,
84 // but the assumption is that it is rarely called:
85 assert(map
!= nullptr);
86 for (const auto& rElement
: elements
)
88 cppuhelper::ServiceManager::Data::ImplementationMap::iterator
j(
90 assert(j
!= map
->end());
93 cppuhelper::ServiceManager::Data::Implementation
> >::iterator
94 k(std::find(j
->second
.begin(), j
->second
.end(), implementation
));
95 assert(k
!= j
->second
.end());
97 if (j
->second
.empty()) {
103 // For simplicity, this code keeps throwing
104 // css::registry::InvalidRegistryException for invalid XML rdbs (even though
105 // that does not fit the exception's name):
109 OUString
const & uri
,
110 css::uno::Reference
< css::uno::XComponentContext
> alienContext
,
111 cppuhelper::ServiceManager::Data
* data
);
113 Parser(const Parser
&) = delete;
114 const Parser
& operator=(const Parser
&) = delete;
117 void handleComponent();
119 void handleImplementation();
121 void handleService();
123 void handleSingleton();
125 OUString
getNameAttribute();
127 xmlreader::XmlReader reader_
;
128 css::uno::Reference
< css::uno::XComponentContext
> alienContext_
;
129 cppuhelper::ServiceManager::Data
* data_
;
130 OUString attrLoader_
;
132 OUString attrEnvironment_
;
133 OUString attrPrefix_
;
134 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
>
139 OUString
const & uri
,
140 css::uno::Reference
< css::uno::XComponentContext
> alienContext
,
141 cppuhelper::ServiceManager::Data
* data
):
142 reader_(uri
), alienContext_(std::move(alienContext
)), data_(data
)
144 assert(data
!= nullptr);
145 int ucNsId
= reader_
.registerNamespaceIri(
147 RTL_CONSTASCII_STRINGPARAM(
148 "http://openoffice.org/2010/uno-components")));
150 STATE_BEGIN
, STATE_END
, STATE_COMPONENTS
, STATE_COMPONENT_INITIAL
,
151 STATE_COMPONENT
, STATE_IMPLEMENTATION
, STATE_SERVICE
, STATE_SINGLETON
};
152 for (State state
= STATE_BEGIN
;;) {
153 xmlreader::Span name
;
155 xmlreader::XmlReader::Result res
= reader_
.nextItem(
156 xmlreader::XmlReader::Text::NONE
, &name
, &nsId
);
159 if (res
== xmlreader::XmlReader::Result::Begin
&& nsId
== ucNsId
160 && name
.equals(RTL_CONSTASCII_STRINGPARAM("components")))
162 state
= STATE_COMPONENTS
;
165 throw css::registry::InvalidRegistryException(
166 reader_
.getUrl() + ": unexpected item in outer level");
168 if (res
== xmlreader::XmlReader::Result::Done
) {
171 throw css::registry::InvalidRegistryException(
172 reader_
.getUrl() + ": unexpected item in outer level");
173 case STATE_COMPONENTS
:
174 if (res
== xmlreader::XmlReader::Result::End
) {
178 if (res
== xmlreader::XmlReader::Result::Begin
&& nsId
== ucNsId
179 && name
.equals(RTL_CONSTASCII_STRINGPARAM("component")))
182 state
= STATE_COMPONENT_INITIAL
;
185 throw css::registry::InvalidRegistryException(
186 reader_
.getUrl() + ": unexpected item in <components>");
187 case STATE_COMPONENT
:
188 if (res
== xmlreader::XmlReader::Result::End
) {
189 state
= STATE_COMPONENTS
;
193 case STATE_COMPONENT_INITIAL
:
194 if (res
== xmlreader::XmlReader::Result::Begin
&& nsId
== ucNsId
195 && name
.equals(RTL_CONSTASCII_STRINGPARAM("implementation")))
197 handleImplementation();
198 state
= STATE_IMPLEMENTATION
;
201 throw css::registry::InvalidRegistryException(
202 reader_
.getUrl() + ": unexpected item in <component>");
203 case STATE_IMPLEMENTATION
:
204 if (res
== xmlreader::XmlReader::Result::End
) {
205 state
= STATE_COMPONENT
;
208 if (res
== xmlreader::XmlReader::Result::Begin
&& nsId
== ucNsId
209 && name
.equals(RTL_CONSTASCII_STRINGPARAM("service")))
212 state
= STATE_SERVICE
;
215 if (res
== xmlreader::XmlReader::Result::Begin
&& nsId
== ucNsId
216 && name
.equals(RTL_CONSTASCII_STRINGPARAM("singleton")))
219 state
= STATE_SINGLETON
;
222 throw css::registry::InvalidRegistryException(
223 reader_
.getUrl() + ": unexpected item in <implementation>");
225 if (res
== xmlreader::XmlReader::Result::End
) {
226 state
= STATE_IMPLEMENTATION
;
229 throw css::registry::InvalidRegistryException(
230 reader_
.getUrl() + ": unexpected item in <service>");
231 case STATE_SINGLETON
:
232 if (res
== xmlreader::XmlReader::Result::End
) {
233 state
= STATE_IMPLEMENTATION
;
236 throw css::registry::InvalidRegistryException(
237 reader_
.getUrl() + ": unexpected item in <service>");
242 void Parser::handleComponent() {
243 attrLoader_
= OUString();
244 attrUri_
= OUString();
245 attrEnvironment_
= OUString();
246 attrPrefix_
= OUString();
247 xmlreader::Span name
;
249 while (reader_
.nextAttribute(&nsId
, &name
)) {
250 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
251 && name
.equals(RTL_CONSTASCII_STRINGPARAM("loader")))
253 if (!attrLoader_
.isEmpty()) {
254 throw css::registry::InvalidRegistryException(
256 + ": <component> has multiple \"loader\" attributes");
258 attrLoader_
= reader_
.getAttributeValue(false).convertFromUtf8();
259 if (attrLoader_
.isEmpty()) {
260 throw css::registry::InvalidRegistryException(
262 + ": <component> has empty \"loader\" attribute");
264 } else if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
265 && name
.equals(RTL_CONSTASCII_STRINGPARAM("uri")))
267 if (!attrUri_
.isEmpty()) {
268 throw css::registry::InvalidRegistryException(
270 + ": <component> has multiple \"uri\" attributes");
272 attrUri_
= reader_
.getAttributeValue(false).convertFromUtf8();
273 if (attrUri_
.isEmpty()) {
274 throw css::registry::InvalidRegistryException(
276 + ": <component> has empty \"uri\" attribute");
278 } else if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
279 && name
.equals(RTL_CONSTASCII_STRINGPARAM("environment")))
281 if (!attrEnvironment_
.isEmpty()) {
282 throw css::registry::InvalidRegistryException(
284 ": <component> has multiple \"environment\" attributes");
286 attrEnvironment_
= reader_
.getAttributeValue(false)
288 if (attrEnvironment_
.isEmpty()) {
289 throw css::registry::InvalidRegistryException(
291 ": <component> has empty \"environment\" attribute");
293 } else if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
294 && name
.equals(RTL_CONSTASCII_STRINGPARAM("prefix")))
296 if (!attrPrefix_
.isEmpty()) {
297 throw css::registry::InvalidRegistryException(
299 ": <component> has multiple \"prefix\" attributes");
301 attrPrefix_
= reader_
.getAttributeValue(false).convertFromUtf8();
302 if (attrPrefix_
.isEmpty()) {
303 throw css::registry::InvalidRegistryException(
305 ": <component> has empty \"prefix\" attribute");
308 throw css::registry::InvalidRegistryException(
309 reader_
.getUrl() + ": unexpected attribute \""
310 + name
.convertFromUtf8() + "\" in <component>");
313 if (attrLoader_
.isEmpty()) {
314 throw css::registry::InvalidRegistryException(
315 reader_
.getUrl() + ": <component> is missing \"loader\" attribute");
317 if (attrUri_
.isEmpty()) {
318 throw css::registry::InvalidRegistryException(
319 reader_
.getUrl() + ": <component> is missing \"uri\" attribute");
321 #ifndef DISABLE_DYNLOADING
323 attrUri_
= rtl::Uri::convertRelToAbs(reader_
.getUrl(), attrUri_
);
324 } catch (const rtl::MalformedUriException
& e
) {
325 throw css::registry::InvalidRegistryException(
326 reader_
.getUrl() + ": bad \"uri\" attribute: " + e
.getMessage());
331 void Parser::handleImplementation() {
333 OUString attrConstructor
;
334 bool attrSingleInstance
= false;
335 xmlreader::Span name
;
337 while (reader_
.nextAttribute(&nsId
, &name
)) {
338 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
339 && name
.equals(RTL_CONSTASCII_STRINGPARAM("name")))
341 if (!attrName
.isEmpty()) {
342 throw css::registry::InvalidRegistryException(
344 + ": <implementation> has multiple \"name\" attributes");
346 attrName
= reader_
.getAttributeValue(false).convertFromUtf8();
347 if (attrName
.isEmpty()) {
348 throw css::registry::InvalidRegistryException(
350 + ": <implementation> has empty \"name\" attribute");
352 } else if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
353 && name
.equals(RTL_CONSTASCII_STRINGPARAM("constructor")))
355 if (!attrConstructor
.isEmpty()) {
356 throw css::registry::InvalidRegistryException(
358 + ": <implementation> has multiple \"constructor\""
361 attrConstructor
= reader_
.getAttributeValue(false)
363 if (attrConstructor
.isEmpty()) {
364 throw css::registry::InvalidRegistryException(
366 + ": element has empty \"constructor\" attribute");
368 if (attrEnvironment_
.isEmpty()) {
369 throw css::registry::InvalidRegistryException(
371 + ": <implementation> has \"constructor\" attribute but"
372 " <component> has no \"environment\" attribute");
374 } else if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
375 && name
.equals(RTL_CONSTASCII_STRINGPARAM("single-instance")))
377 if (attrSingleInstance
) {
378 throw css::registry::InvalidRegistryException(
380 + ": <implementation> has multiple \"single-instance\" attributes");
382 if (!reader_
.getAttributeValue(false).equals(RTL_CONSTASCII_STRINGPARAM("true"))) {
383 throw css::registry::InvalidRegistryException(
384 reader_
.getUrl() + ": <implementation> has bad \"single-instance\" attribute");
386 attrSingleInstance
= true;
388 throw css::registry::InvalidRegistryException(
389 reader_
.getUrl() + ": unexpected element attribute \""
390 + name
.convertFromUtf8() + "\" in <implementation>");
393 if (attrName
.isEmpty()) {
394 throw css::registry::InvalidRegistryException(
396 + ": <implementation> is missing \"name\" attribute");
399 std::make_shared
<cppuhelper::ServiceManager::Data::Implementation
>(
400 attrName
, attrLoader_
, attrUri_
, attrEnvironment_
, attrConstructor
,
401 attrPrefix_
, attrSingleInstance
, alienContext_
, reader_
.getUrl());
402 if (!data_
->namedImplementations
.emplace(attrName
, implementation_
).
405 throw css::registry::InvalidRegistryException(
406 reader_
.getUrl() + ": duplicate <implementation name=\"" + attrName
411 void Parser::handleService() {
412 OUString
name(getNameAttribute());
413 implementation_
->services
.push_back(name
);
414 data_
->services
[name
].push_back(implementation_
);
417 void Parser::handleSingleton() {
418 OUString
name(getNameAttribute());
419 implementation_
->singletons
.push_back(name
);
420 data_
->singletons
[name
].push_back(implementation_
);
423 OUString
Parser::getNameAttribute() {
425 xmlreader::Span name
;
427 while (reader_
.nextAttribute(&nsId
, &name
)) {
428 if (nsId
!= xmlreader::XmlReader::NAMESPACE_NONE
429 || !name
.equals(RTL_CONSTASCII_STRINGPARAM("name")))
431 throw css::registry::InvalidRegistryException(
432 reader_
.getUrl() + ": expected element attribute \"name\"");
434 if (!attrName
.isEmpty()) {
435 throw css::registry::InvalidRegistryException(
437 + ": element has multiple \"name\" attributes");
439 attrName
= reader_
.getAttributeValue(false).convertFromUtf8();
440 if (attrName
.isEmpty()) {
441 throw css::registry::InvalidRegistryException(
442 reader_
.getUrl() + ": element has empty \"name\" attribute");
445 if (attrName
.isEmpty()) {
446 throw css::registry::InvalidRegistryException(
447 reader_
.getUrl() + ": element is missing \"name\" attribute");
452 class ContentEnumeration
:
453 public cppu::WeakImplHelper
< css::container::XEnumeration
>
456 explicit ContentEnumeration(std::vector
< css::uno::Any
>&& factories
):
457 factories_(std::move(factories
)), iterator_(factories_
.begin()) {}
459 ContentEnumeration(const ContentEnumeration
&) = delete;
460 const ContentEnumeration
& operator=(const ContentEnumeration
&) = delete;
463 virtual ~ContentEnumeration() override
{}
465 virtual sal_Bool SAL_CALL
hasMoreElements() override
;
467 virtual css::uno::Any SAL_CALL
nextElement() override
;
470 std::vector
< css::uno::Any
> factories_
;
471 std::vector
< css::uno::Any
>::const_iterator iterator_
;
474 sal_Bool
ContentEnumeration::hasMoreElements()
476 std::scoped_lock
g(mutex_
);
477 return iterator_
!= factories_
.end();
480 css::uno::Any
ContentEnumeration::nextElement()
482 std::scoped_lock
g(mutex_
);
483 if (iterator_
== factories_
.end()) {
484 throw css::container::NoSuchElementException(
485 "Bootstrap service manager service enumerator has no more elements",
486 static_cast< cppu::OWeakObject
* >(this));
491 css::beans::Property
getDefaultContextProperty() {
492 return css::beans::Property(
493 "DefaultContext", -1,
494 cppu::UnoType
< css::uno::XComponentContext
>::get(),
495 css::beans::PropertyAttribute::READONLY
);
498 class SingletonFactory
:
499 public cppu::WeakImplHelper
<css::lang::XSingleComponentFactory
>
503 rtl::Reference
< cppuhelper::ServiceManager
> const & manager
,
505 cppuhelper::ServiceManager::Data::Implementation
> const &
507 manager_(manager
), implementation_(implementation
)
508 { assert(manager
.is()); assert(implementation
); }
510 SingletonFactory(const SingletonFactory
&) = delete;
511 const SingletonFactory
& operator=(const SingletonFactory
&) = delete;
514 virtual ~SingletonFactory() override
{}
516 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
517 createInstanceWithContext(
518 css::uno::Reference
< css::uno::XComponentContext
> const & Context
) override
;
520 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
521 createInstanceWithArgumentsAndContext(
522 css::uno::Sequence
< css::uno::Any
> const & Arguments
,
523 css::uno::Reference
< css::uno::XComponentContext
> const & Context
) override
;
525 rtl::Reference
< cppuhelper::ServiceManager
> manager_
;
526 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
>
530 css::uno::Reference
< css::uno::XInterface
>
531 SingletonFactory::createInstanceWithContext(
532 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
534 manager_
->loadImplementation(Context
, implementation_
);
535 return implementation_
->createInstance(Context
, true);
538 css::uno::Reference
< css::uno::XInterface
>
539 SingletonFactory::createInstanceWithArgumentsAndContext(
540 css::uno::Sequence
< css::uno::Any
> const & Arguments
,
541 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
543 manager_
->loadImplementation(Context
, implementation_
);
544 return implementation_
->createInstanceWithArguments(
545 Context
, true, Arguments
);
548 class ImplementationWrapper
:
549 public cppu::WeakImplHelper
<
550 css::lang::XSingleComponentFactory
, css::lang::XSingleServiceFactory
,
551 css::lang::XServiceInfo
>
554 ImplementationWrapper(
555 rtl::Reference
< cppuhelper::ServiceManager
> const & manager
,
557 cppuhelper::ServiceManager::Data::Implementation
> const &
559 manager_(manager
), implementation_(implementation
)
560 { assert(manager
.is()); assert(implementation
); }
562 ImplementationWrapper(const ImplementationWrapper
&) = delete;
563 const ImplementationWrapper
& operator=(const ImplementationWrapper
&) = delete;
566 virtual ~ImplementationWrapper() override
{}
568 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
569 createInstanceWithContext(
570 css::uno::Reference
< css::uno::XComponentContext
> const & Context
) override
;
572 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
573 createInstanceWithArgumentsAndContext(
574 css::uno::Sequence
< css::uno::Any
> const & Arguments
,
575 css::uno::Reference
< css::uno::XComponentContext
> const & Context
) override
;
577 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
578 createInstance() override
;
580 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
581 createInstanceWithArguments(
582 css::uno::Sequence
< css::uno::Any
> const & Arguments
) override
;
584 virtual OUString SAL_CALL
getImplementationName() override
;
586 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
;
588 virtual css::uno::Sequence
< OUString
> SAL_CALL
589 getSupportedServiceNames() override
;
591 rtl::Reference
< cppuhelper::ServiceManager
> manager_
;
592 std::weak_ptr
< cppuhelper::ServiceManager::Data::Implementation
>
596 css::uno::Reference
< css::uno::XInterface
>
597 ImplementationWrapper::createInstanceWithContext(
598 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
600 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
> impl
= implementation_
.lock();
602 manager_
->loadImplementation(Context
, impl
);
603 return impl
->createInstance(Context
, false);
606 css::uno::Reference
< css::uno::XInterface
>
607 ImplementationWrapper::createInstanceWithArgumentsAndContext(
608 css::uno::Sequence
< css::uno::Any
> const & Arguments
,
609 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
611 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
> impl
= implementation_
.lock();
613 manager_
->loadImplementation(Context
, impl
);
614 return impl
->createInstanceWithArguments(
615 Context
, false, Arguments
);
618 css::uno::Reference
< css::uno::XInterface
>
619 ImplementationWrapper::createInstance()
621 return createInstanceWithContext(manager_
->getContext());
624 css::uno::Reference
< css::uno::XInterface
>
625 ImplementationWrapper::createInstanceWithArguments(
626 css::uno::Sequence
< css::uno::Any
> const & Arguments
)
628 return createInstanceWithArgumentsAndContext(
629 Arguments
, manager_
->getContext());
632 OUString
ImplementationWrapper::getImplementationName()
634 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
> impl
= implementation_
.lock();
639 sal_Bool
ImplementationWrapper::supportsService(OUString
const & ServiceName
)
641 return cppu::supportsService(this, ServiceName
);
644 css::uno::Sequence
< OUString
>
645 ImplementationWrapper::getSupportedServiceNames()
647 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
> impl
= implementation_
.lock();
649 if (impl
->services
.size()
650 > o3tl::make_unsigned(SAL_MAX_INT32
))
652 throw css::uno::RuntimeException(
653 ("Implementation " + impl
->name
654 + " supports too many services"),
655 static_cast< cppu::OWeakObject
* >(this));
657 return comphelper::containerToSequence(impl
->services
);
662 css::uno::Reference
<css::uno::XInterface
>
663 cppuhelper::ServiceManager::Data::Implementation::createInstance(
664 css::uno::Reference
<css::uno::XComponentContext
> const & context
,
665 bool singletonRequest
)
667 css::uno::Reference
<css::uno::XInterface
> inst
;
668 if (isSingleInstance
) {
669 std::unique_lock
g(mutex
);
670 if (!singleInstance
.is()) {
671 singleInstance
= doCreateInstance(context
);
673 inst
= singleInstance
;
675 inst
= doCreateInstance(context
);
677 updateDisposeInstance(singletonRequest
, inst
);
681 css::uno::Reference
<css::uno::XInterface
>
682 cppuhelper::ServiceManager::Data::Implementation::createInstanceWithArguments(
683 css::uno::Reference
<css::uno::XComponentContext
> const & context
,
684 bool singletonRequest
, css::uno::Sequence
<css::uno::Any
> const & arguments
)
686 css::uno::Reference
<css::uno::XInterface
> inst
;
687 if (isSingleInstance
) {
688 std::unique_lock
g(mutex
);
689 if (!singleInstance
.is()) {
690 singleInstance
= doCreateInstanceWithArguments(context
, arguments
);
692 inst
= singleInstance
;
694 inst
= doCreateInstanceWithArguments(context
, arguments
);
696 updateDisposeInstance(singletonRequest
, inst
);
700 css::uno::Reference
<css::uno::XInterface
>
701 cppuhelper::ServiceManager::Data::Implementation::doCreateInstance(
702 css::uno::Reference
<css::uno::XComponentContext
> const & context
)
705 return css::uno::Reference
<css::uno::XInterface
>(
706 constructorFn(context
.get(), css::uno::Sequence
<css::uno::Any
>()),
708 } else if (factory1
.is()) {
709 return factory1
->createInstanceWithContext(context
);
711 assert(factory2
.is());
712 return factory2
->createInstance();
716 css::uno::Reference
<css::uno::XInterface
>
717 cppuhelper::ServiceManager::Data::Implementation::doCreateInstanceWithArguments(
718 css::uno::Reference
<css::uno::XComponentContext
> const & context
,
719 css::uno::Sequence
<css::uno::Any
> const & arguments
)
722 css::uno::Reference
<css::uno::XInterface
> inst(
723 constructorFn(context
.get(), arguments
), SAL_NO_ACQUIRE
);
724 //HACK: The constructor will either observe arguments and return inst
725 // that does not implement XInitialization (or null), or ignore
726 // arguments and return inst that implements XInitialization; this
727 // should be removed again once XInitialization-based implementations
729 css::uno::Reference
<css::lang::XInitialization
> init(
730 inst
, css::uno::UNO_QUERY
);
732 init
->initialize(arguments
);
735 } else if (factory1
.is()) {
736 return factory1
->createInstanceWithArgumentsAndContext(
739 assert(factory2
.is());
740 return factory2
->createInstanceWithArguments(arguments
);
744 void cppuhelper::ServiceManager::Data::Implementation::updateDisposeInstance(
745 bool singletonRequest
,
746 css::uno::Reference
<css::uno::XInterface
> const & instance
)
748 // This is an optimization, to only call dispose once (from the component
749 // context) on a singleton that is obtained both via the component context
750 // and via the service manager; however, there is a harmless race here that
751 // may cause two calls to dispose nevertheless (also, this calls dispose on
752 // at most one of the instances obtained via the service manager, in case
753 // the implementation hands out different instances):
754 if (singletonRequest
) {
755 std::unique_lock
g(mutex
);
756 disposeInstance
.clear();
758 } else if (shallDispose()) {
759 css::uno::Reference
<css::lang::XComponent
> comp(
760 instance
, css::uno::UNO_QUERY
);
762 std::unique_lock
g(mutex
);
764 disposeInstance
= comp
;
770 void cppuhelper::ServiceManager::addSingletonContextEntries(
771 std::vector
< cppu::ContextEntry_Init
> * entries
)
773 assert(entries
!= nullptr);
774 for (const auto& [rName
, rImpls
] : data_
.singletons
)
776 assert(!rImpls
.empty());
779 rImpls
.size() > 1, "cppuhelper",
780 "Arbitrarily choosing " << rImpls
[0]->name
781 << " among multiple implementations for " << rName
);
783 cppu::ContextEntry_Init(
784 "/singletons/" + rName
,
786 css::uno::Reference
<css::lang::XSingleComponentFactory
>(
787 new SingletonFactory(this, rImpls
[0]))),
792 void cppuhelper::ServiceManager::loadImplementation(
793 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
794 std::shared_ptr
< Data::Implementation
> const & implementation
)
796 assert(implementation
);
798 std::unique_lock
g(m_aMutex
);
799 if (implementation
->status
== Data::Implementation::STATUS_LOADED
) {
805 uri
= cppu::bootstrap_expandUri(implementation
->uri
);
806 } catch (css::lang::IllegalArgumentException
& e
) {
807 throw css::uno::DeploymentException(
808 "Cannot expand URI" + implementation
->uri
+ ": " + e
.Message
,
809 static_cast< cppu::OWeakObject
* >(this));
811 cppuhelper::WrapperConstructorFn ctor
;
812 css::uno::Reference
< css::uno::XInterface
> f0
;
813 // Special handling of SharedLibrary loader, with support for environment,
814 // constructor, and prefix arguments:
815 if (!implementation
->alienContext
.is()
816 && implementation
->loader
== "com.sun.star.loader.SharedLibrary")
818 cppuhelper::detail::loadSharedLibComponentFactory(
819 uri
, implementation
->environment
,
820 implementation
->prefix
, implementation
->name
,
821 implementation
->constructorName
, this, &ctor
, &f0
);
823 assert(!implementation
->environment
.isEmpty());
827 !implementation
->environment
.isEmpty(), "cppuhelper",
828 "Loader " << implementation
->loader
829 << " and non-empty environment "
830 << implementation
->environment
);
832 !implementation
->prefix
.isEmpty(), "cppuhelper",
833 "Loader " << implementation
->loader
834 << " and non-empty constructor "
835 << implementation
->constructorName
);
837 !implementation
->prefix
.isEmpty(), "cppuhelper",
838 "Loader " << implementation
->loader
839 << " and non-empty prefix " << implementation
->prefix
);
840 css::uno::Reference
< css::uno::XComponentContext
> ctxt
;
841 css::uno::Reference
< css::lang::XMultiComponentFactory
> smgr
;
842 if (implementation
->alienContext
.is()) {
843 ctxt
= implementation
->alienContext
;
844 smgr
.set(ctxt
->getServiceManager(), css::uno::UNO_SET_THROW
);
846 assert(context
.is());
850 css::uno::Reference
< css::loader::XImplementationLoader
> loader(
851 smgr
->createInstanceWithContext(implementation
->loader
, ctxt
),
852 css::uno::UNO_QUERY_THROW
);
853 f0
= loader
->activate(
854 implementation
->name
, OUString(), uri
,
855 css::uno::Reference
< css::registry::XRegistryKey
>());
857 css::uno::Reference
<css::lang::XSingleComponentFactory
> f1
;
858 css::uno::Reference
<css::lang::XSingleServiceFactory
> f2
;
860 f1
.set(f0
, css::uno::UNO_QUERY
);
862 f2
.set(f0
, css::uno::UNO_QUERY
);
864 throw css::uno::DeploymentException(
865 ("Implementation " + implementation
->name
866 + " does not provide a constructor or factory"),
867 static_cast< cppu::OWeakObject
* >(this));
871 //TODO: There is a race here, as the relevant service factory can be removed
872 // while the mutex is unlocked and loading can thus fail, as the entity from
873 // which to load can disappear once the service factory is removed.
874 std::unique_lock
g(m_aMutex
);
876 || implementation
->status
== Data::Implementation::STATUS_LOADED
))
878 implementation
->status
= Data::Implementation::STATUS_LOADED
;
879 implementation
->constructorFn
= ctor
;
880 implementation
->factory1
= f1
;
881 implementation
->factory2
= f2
;
885 void cppuhelper::ServiceManager::disposing(std::unique_lock
<std::mutex
>& rGuard
) {
886 std::vector
< css::uno::Reference
<css::lang::XComponent
> > sngls
;
887 std::vector
< css::uno::Reference
< css::lang::XComponent
> > comps
;
890 for (const auto& rEntry
: data_
.namedImplementations
)
892 assert(rEntry
.second
);
893 if (rEntry
.second
->shallDispose()) {
894 std::unique_lock
g2(rEntry
.second
->mutex
);
895 if (rEntry
.second
->disposeInstance
.is()) {
896 sngls
.push_back(rEntry
.second
->disposeInstance
);
900 for (const auto& rEntry
: data_
.dynamicImplementations
)
902 assert(rEntry
.second
);
903 if (rEntry
.second
->shallDispose()) {
904 std::unique_lock
g2(rEntry
.second
->mutex
);
905 if (rEntry
.second
->disposeInstance
.is()) {
906 sngls
.push_back(rEntry
.second
->disposeInstance
);
909 if (rEntry
.second
->component
.is()) {
910 comps
.push_back(rEntry
.second
->component
);
913 data_
.namedImplementations
.swap(clear
.namedImplementations
);
914 data_
.dynamicImplementations
.swap(clear
.dynamicImplementations
);
915 data_
.services
.swap(clear
.services
);
916 data_
.singletons
.swap(clear
.singletons
);
919 for (const auto& rxSngl
: sngls
)
923 } catch (css::uno::RuntimeException
& e
) {
924 SAL_WARN("cppuhelper", "Ignoring " << e
<< " while disposing singleton");
927 for (const auto& rxComp
: comps
)
929 removeEventListenerFromComponent(rxComp
);
934 void cppuhelper::ServiceManager::initialize(
935 css::uno::Sequence
<css::uno::Any
> const & aArguments
)
938 if (aArguments
.getLength() != 1 || !(aArguments
[0] >>= arg
)
941 throw css::lang::IllegalArgumentException(
942 "invalid ServiceManager::initialize argument",
943 css::uno::Reference
<css::uno::XInterface
>(), 0);
945 preloadImplementations();
948 OUString
cppuhelper::ServiceManager::getImplementationName()
951 "com.sun.star.comp.cppuhelper.bootstrap.ServiceManager";
954 sal_Bool
cppuhelper::ServiceManager::supportsService(
955 OUString
const & ServiceName
)
957 return cppu::supportsService(this, ServiceName
);
960 css::uno::Sequence
< OUString
>
961 cppuhelper::ServiceManager::getSupportedServiceNames()
963 return { "com.sun.star.lang.MultiServiceFactory", "com.sun.star.lang.ServiceManager" };
966 css::uno::Reference
< css::uno::XInterface
>
967 cppuhelper::ServiceManager::createInstance(
968 OUString
const & aServiceSpecifier
)
970 assert(context_
.is());
971 return createInstanceWithContext(aServiceSpecifier
, context_
);
974 css::uno::Reference
< css::uno::XInterface
>
975 cppuhelper::ServiceManager::createInstanceWithArguments(
976 OUString
const & ServiceSpecifier
,
977 css::uno::Sequence
< css::uno::Any
> const & Arguments
)
979 assert(context_
.is());
980 return createInstanceWithArgumentsAndContext(
981 ServiceSpecifier
, Arguments
, context_
);
984 css::uno::Sequence
< OUString
>
985 cppuhelper::ServiceManager::getAvailableServiceNames()
987 std::unique_lock
g(m_aMutex
);
989 return css::uno::Sequence
< OUString
>();
991 if (data_
.services
.size() > o3tl::make_unsigned(SAL_MAX_INT32
)) {
992 throw css::uno::RuntimeException(
993 "getAvailableServiceNames: too many services",
994 static_cast< cppu::OWeakObject
* >(this));
996 return comphelper::mapKeysToSequence(data_
.services
);
999 css::uno::Reference
< css::uno::XInterface
>
1000 cppuhelper::ServiceManager::createInstanceWithContext(
1001 OUString
const & aServiceSpecifier
,
1002 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
1004 std::shared_ptr
< Data::Implementation
> impl(
1005 findServiceImplementation(Context
, aServiceSpecifier
));
1006 return impl
== nullptr ? css::uno::Reference
<css::uno::XInterface
>()
1007 : impl
->createInstance(Context
, false);
1010 css::uno::Reference
< css::uno::XInterface
>
1011 cppuhelper::ServiceManager::createInstanceWithArgumentsAndContext(
1012 OUString
const & ServiceSpecifier
,
1013 css::uno::Sequence
< css::uno::Any
> const & Arguments
,
1014 css::uno::Reference
< css::uno::XComponentContext
> const & Context
)
1016 std::shared_ptr
< Data::Implementation
> impl(
1017 findServiceImplementation(Context
, ServiceSpecifier
));
1018 return impl
== nullptr ? css::uno::Reference
<css::uno::XInterface
>()
1019 : impl
->createInstanceWithArguments(Context
, false, Arguments
);
1022 css::uno::Type
cppuhelper::ServiceManager::getElementType()
1024 return css::uno::Type();
1027 sal_Bool
cppuhelper::ServiceManager::hasElements()
1029 std::unique_lock
g(m_aMutex
);
1031 !(data_
.namedImplementations
.empty()
1032 && data_
.dynamicImplementations
.empty());
1035 css::uno::Reference
< css::container::XEnumeration
>
1036 cppuhelper::ServiceManager::createEnumeration()
1038 throw css::uno::RuntimeException(
1039 "ServiceManager createEnumeration: method not supported",
1040 static_cast< cppu::OWeakObject
* >(this));
1043 sal_Bool
cppuhelper::ServiceManager::has(css::uno::Any
const &)
1045 throw css::uno::RuntimeException(
1046 "ServiceManager has: method not supported",
1047 static_cast< cppu::OWeakObject
* >(this));
1050 void cppuhelper::ServiceManager::insert(css::uno::Any
const & aElement
)
1052 css::uno::Sequence
< css::beans::NamedValue
> args
;
1053 if (aElement
>>= args
) {
1054 std::vector
< OUString
> uris
;
1055 css::uno::Reference
< css::uno::XComponentContext
> alienContext
;
1056 for (const auto & arg
: std::as_const(args
)) {
1057 if (arg
.Name
== "uri") {
1059 if (!(arg
.Value
>>= uri
)) {
1060 throw css::lang::IllegalArgumentException(
1062 static_cast< cppu::OWeakObject
* >(this), 0);
1064 uris
.push_back(uri
);
1065 } else if (arg
.Name
== "component-context") {
1066 if (alienContext
.is()) {
1067 throw css::lang::IllegalArgumentException(
1068 "Multiple component-context arguments",
1069 static_cast< cppu::OWeakObject
* >(this), 0);
1071 if (!(arg
.Value
>>= alienContext
) || !alienContext
.is()) {
1072 throw css::lang::IllegalArgumentException(
1073 "Bad component-context argument",
1074 static_cast< cppu::OWeakObject
* >(this), 0);
1077 throw css::lang::IllegalArgumentException(
1078 "Bad argument " + arg
.Name
,
1079 static_cast< cppu::OWeakObject
* >(this), 0);
1082 insertRdbFiles(uris
, alienContext
);
1085 css::uno::Reference
< css::lang::XServiceInfo
> info
;
1086 if ((aElement
>>= info
) && info
.is()) {
1087 insertLegacyFactory(info
);
1091 throw css::lang::IllegalArgumentException(
1092 "Bad insert element", static_cast< cppu::OWeakObject
* >(this), 0);
1095 void cppuhelper::ServiceManager::remove(css::uno::Any
const & aElement
)
1097 css::uno::Sequence
< css::beans::NamedValue
> args
;
1098 if (aElement
>>= args
) {
1099 std::vector
< OUString
> uris
;
1100 for (const auto & i
: std::as_const(args
)) {
1101 if (i
.Name
!= "uri") {
1102 throw css::lang::IllegalArgumentException(
1103 "Bad argument " + i
.Name
,
1104 static_cast< cppu::OWeakObject
* >(this), 0);
1107 if (!(i
.Value
>>= uri
)) {
1108 throw css::lang::IllegalArgumentException(
1110 static_cast< cppu::OWeakObject
* >(this), 0);
1112 uris
.push_back(uri
);
1114 removeRdbFiles(uris
);
1117 css::uno::Reference
< css::lang::XServiceInfo
> info
;
1118 if ((aElement
>>= info
) && info
.is()) {
1119 if (!removeLegacyFactory(info
, true)) {
1120 throw css::container::NoSuchElementException(
1121 "Remove non-inserted factory object",
1122 static_cast< cppu::OWeakObject
* >(this));
1127 if (aElement
>>= impl
) {
1128 // For live-removal of extensions:
1129 removeImplementation(impl
);
1132 throw css::lang::IllegalArgumentException(
1133 "Bad remove element", static_cast< cppu::OWeakObject
* >(this), 0);
1136 css::uno::Reference
< css::container::XEnumeration
>
1137 cppuhelper::ServiceManager::createContentEnumeration(
1138 OUString
const & aServiceName
)
1140 std::vector
< std::shared_ptr
< Data::Implementation
> > impls
;
1142 std::unique_lock
g(m_aMutex
);
1143 Data::ImplementationMap::const_iterator
i(
1144 data_
.services
.find(aServiceName
));
1145 if (i
!= data_
.services
.end()) {
1149 std::vector
< css::uno::Any
> factories
;
1150 for (const auto& rxImpl
: impls
)
1152 Data::Implementation
* impl
= rxImpl
.get();
1153 assert(impl
!= nullptr);
1155 std::unique_lock
g(m_aMutex
);
1160 if (impl
->status
== Data::Implementation::STATUS_NEW
) {
1161 // Postpone actual implementation instantiation as long as
1162 // possible (so that e.g. opening LO's "Tools - Macros" menu
1163 // does not try to instantiate a JVM, which can lead to a
1164 // synchronous error dialog when no JVM is specified, and
1165 // showing the dialog while hovering over a menu can cause
1167 impl
->factory1
= new ImplementationWrapper(this, rxImpl
);
1168 impl
->status
= Data::Implementation::STATUS_WRAPPER
;
1170 if (impl
->constructorFn
!= nullptr && !impl
->factory1
.is()) {
1171 impl
->factory1
= new ImplementationWrapper(this, rxImpl
);
1174 if (impl
->factory1
.is()) {
1175 factories
.push_back(css::uno::Any(impl
->factory1
));
1177 assert(impl
->factory2
.is());
1178 factories
.push_back(css::uno::Any(impl
->factory2
));
1181 return new ContentEnumeration(std::move(factories
));
1184 css::uno::Reference
< css::beans::XPropertySetInfo
>
1185 cppuhelper::ServiceManager::getPropertySetInfo()
1190 void cppuhelper::ServiceManager::setPropertyValue(
1191 OUString
const & aPropertyName
, css::uno::Any
const &)
1193 if (aPropertyName
== "DefaultContext") {
1194 throw css::beans::PropertyVetoException(
1195 aPropertyName
, static_cast< cppu::OWeakObject
* >(this));
1197 throw css::beans::UnknownPropertyException(
1198 aPropertyName
, static_cast< cppu::OWeakObject
* >(this));
1202 css::uno::Any
cppuhelper::ServiceManager::getPropertyValue(
1203 OUString
const & PropertyName
)
1205 if (PropertyName
!= "DefaultContext") {
1206 throw css::beans::UnknownPropertyException(
1207 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
1209 assert(context_
.is());
1210 return css::uno::Any(context_
);
1213 void cppuhelper::ServiceManager::addPropertyChangeListener(
1214 OUString
const & aPropertyName
,
1215 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &
1218 if (!aPropertyName
.isEmpty() && aPropertyName
!= "DefaultContext") {
1219 throw css::beans::UnknownPropertyException(
1220 aPropertyName
, static_cast< cppu::OWeakObject
* >(this));
1222 // DefaultContext does not change, so just treat it as an event listener:
1223 return addEventListener(xListener
);
1226 void cppuhelper::ServiceManager::removePropertyChangeListener(
1227 OUString
const & aPropertyName
,
1228 css::uno::Reference
< css::beans::XPropertyChangeListener
> const &
1231 if (!aPropertyName
.isEmpty() && aPropertyName
!= "DefaultContext") {
1232 throw css::beans::UnknownPropertyException(
1233 aPropertyName
, static_cast< cppu::OWeakObject
* >(this));
1235 // DefaultContext does not change, so just treat it as an event listener:
1236 return removeEventListener(aListener
);
1239 void cppuhelper::ServiceManager::addVetoableChangeListener(
1240 OUString
const & PropertyName
,
1241 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &
1244 if (!PropertyName
.isEmpty() && PropertyName
!= "DefaultContext") {
1245 throw css::beans::UnknownPropertyException(
1246 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
1248 // DefaultContext does not change, so just treat it as an event listener:
1249 return addEventListener(aListener
);
1252 void cppuhelper::ServiceManager::removeVetoableChangeListener(
1253 OUString
const & PropertyName
,
1254 css::uno::Reference
< css::beans::XVetoableChangeListener
> const &
1257 if (!PropertyName
.isEmpty() && PropertyName
!= "DefaultContext") {
1258 throw css::beans::UnknownPropertyException(
1259 PropertyName
, static_cast< cppu::OWeakObject
* >(this));
1261 // DefaultContext does not change, so just treat it as an event listener:
1262 return removeEventListener(aListener
);
1265 css::uno::Sequence
< css::beans::Property
>
1266 cppuhelper::ServiceManager::getProperties() {
1267 return { getDefaultContextProperty() };
1270 css::beans::Property
cppuhelper::ServiceManager::getPropertyByName(
1271 OUString
const & aName
)
1273 if (aName
!= "DefaultContext") {
1274 throw css::beans::UnknownPropertyException(
1275 aName
, static_cast< cppu::OWeakObject
* >(this));
1277 return getDefaultContextProperty();
1280 sal_Bool
cppuhelper::ServiceManager::hasPropertyByName(
1281 OUString
const & Name
)
1283 return Name
== "DefaultContext";
1286 cppuhelper::ServiceManager::~ServiceManager() {}
1288 void cppuhelper::ServiceManager::disposing(
1289 css::lang::EventObject
const & Source
)
1291 removeLegacyFactory(
1292 css::uno::Reference
< css::lang::XServiceInfo
>(
1293 Source
.Source
, css::uno::UNO_QUERY_THROW
),
1297 void cppuhelper::ServiceManager::removeEventListenerFromComponent(
1298 css::uno::Reference
< css::lang::XComponent
> const & component
)
1300 assert(component
.is());
1302 component
->removeEventListener(this);
1303 } catch (css::uno::RuntimeException
& e
) {
1306 "Ignored removeEventListener RuntimeException " + e
.Message
);
1310 void cppuhelper::ServiceManager::init(std::u16string_view rdbUris
) {
1311 for (sal_Int32 i
= 0; i
!= -1;) {
1312 std::u16string_view
uri(o3tl::getToken(rdbUris
, 0, ' ', i
));
1318 cppu::decodeRdbUri(&uri
, &optional
, &directory
);
1320 readRdbDirectory(uri
, optional
);
1322 readRdbFile(OUString(uri
), optional
);
1327 void cppuhelper::ServiceManager::readRdbDirectory(
1328 std::u16string_view uri
, bool optional
)
1330 osl::Directory dir
= OUString(uri
);
1331 switch (dir
.open()) {
1332 case osl::FileBase::E_None
:
1334 case osl::FileBase::E_NOENT
:
1336 SAL_INFO("cppuhelper", "Ignored optional " << OUString(uri
));
1341 throw css::uno::DeploymentException(
1342 OUString::Concat("Cannot open directory ") + uri
,
1343 static_cast< cppu::OWeakObject
* >(this));
1347 if (!cppu::nextDirectoryItem(dir
, &url
)) {
1350 readRdbFile(url
, false);
1354 void cppuhelper::ServiceManager::readRdbFile(
1355 OUString
const & uri
, bool optional
)
1359 uri
, css::uno::Reference
< css::uno::XComponentContext
>(), &data_
);
1360 } catch (css::container::NoSuchElementException
&) {
1362 throw css::uno::DeploymentException(
1363 uri
+ ": no such file",
1364 static_cast< cppu::OWeakObject
* >(this));
1366 SAL_INFO("cppuhelper", "Ignored optional " << uri
);
1367 } catch (css::registry::InvalidRegistryException
& e
) {
1368 if (!readLegacyRdbFile(uri
)) {
1369 throw css::uno::DeploymentException(
1370 "InvalidRegistryException: " + e
.Message
,
1371 static_cast< cppu::OWeakObject
* >(this));
1373 } catch (css::uno::RuntimeException
&) {
1374 if (!readLegacyRdbFile(uri
)) {
1380 bool cppuhelper::ServiceManager::readLegacyRdbFile(OUString
const & uri
) {
1382 switch (reg
.open(uri
, RegAccessMode::READONLY
)) {
1383 case RegError::NO_ERROR
:
1385 case RegError::REGISTRY_NOT_EXISTS
:
1386 case RegError::INVALID_REGISTRY
:
1388 // Ignore empty rdb files (which are at least seen by subordinate
1389 // uno processes during extension registration; Registry::open can
1390 // fail on them if mmap(2) returns EINVAL for a zero length):
1391 osl::DirectoryItem item
;
1392 if (osl::DirectoryItem::get(uri
, item
) == osl::FileBase::E_None
) {
1393 osl::FileStatus
status(osl_FileStatus_Mask_FileSize
);
1394 if (item
.getFileStatus(status
) == osl::FileBase::E_None
1395 && status
.getFileSize() == 0)
1405 RegistryKey rootKey
;
1406 if (reg
.openRootKey(rootKey
) != RegError::NO_ERROR
) {
1407 throw css::uno::DeploymentException(
1408 "Failure reading legacy rdb file " + uri
,
1409 static_cast< cppu::OWeakObject
* >(this));
1411 RegistryKeyArray impls
;
1412 switch (rootKey
.openSubKeys("IMPLEMENTATIONS", impls
)) {
1413 case RegError::NO_ERROR
:
1415 case RegError::KEY_NOT_EXISTS
:
1418 throw css::uno::DeploymentException(
1419 "Failure reading legacy rdb file " + uri
,
1420 static_cast< cppu::OWeakObject
* >(this));
1422 for (sal_uInt32 i
= 0; i
!= impls
.getLength(); ++i
) {
1423 RegistryKey
implKey(impls
.getElement(i
));
1424 assert(implKey
.getName().match("/IMPLEMENTATIONS/"));
1426 implKey
.getName().copy(RTL_CONSTASCII_LENGTH("/IMPLEMENTATIONS/")));
1427 std::shared_ptr
< Data::Implementation
> impl
=
1428 std::make_shared
<Data::Implementation
>(
1429 name
, readLegacyRdbString(uri
, implKey
, "UNO/ACTIVATOR"),
1430 readLegacyRdbString(uri
, implKey
, "UNO/LOCATION"), "", "", "", false,
1431 css::uno::Reference
< css::uno::XComponentContext
>(), uri
);
1432 if (!data_
.namedImplementations
.emplace(name
, impl
).second
)
1434 throw css::registry::InvalidRegistryException(
1435 uri
+ ": duplicate <implementation name=\"" + name
+ "\">");
1437 readLegacyRdbStrings(
1438 uri
, implKey
, "UNO/SERVICES", &impl
->services
);
1439 for (const auto& rService
: impl
->services
)
1441 data_
.services
[rService
].push_back(impl
);
1443 readLegacyRdbStrings(
1444 uri
, implKey
, "UNO/SINGLETONS", &impl
->singletons
);
1445 for (const auto& rSingleton
: impl
->singletons
)
1447 data_
.singletons
[rSingleton
].push_back(impl
);
1453 OUString
cppuhelper::ServiceManager::readLegacyRdbString(
1454 std::u16string_view uri
, RegistryKey
& key
, OUString
const & path
)
1459 if (key
.openKey(path
, subkey
) != RegError::NO_ERROR
1460 || subkey
.getValueInfo(OUString(), &t
, &s
) != RegError::NO_ERROR
1461 || t
!= RegValueType::STRING
1462 || s
== 0 || s
> o3tl::make_unsigned(SAL_MAX_INT32
))
1464 throw css::uno::DeploymentException(
1465 OUString::Concat("Failure reading legacy rdb file ") + uri
,
1466 static_cast< cppu::OWeakObject
* >(this));
1469 std::vector
< char > v(s
); // assuming sal_uInt32 fits into vector::size_type
1470 if (subkey
.getValue(OUString(), v
.data()) != RegError::NO_ERROR
1472 || !rtl_convertStringToUString(
1473 &val
.pData
, v
.data(), static_cast< sal_Int32
>(s
- 1),
1474 RTL_TEXTENCODING_UTF8
,
1475 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
1476 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
1477 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR
)))
1479 throw css::uno::DeploymentException(
1480 OUString::Concat("Failure reading legacy rdb file ") + uri
,
1481 static_cast< cppu::OWeakObject
* >(this));
1486 void cppuhelper::ServiceManager::readLegacyRdbStrings(
1487 std::u16string_view uri
, RegistryKey
& key
, OUString
const & path
,
1488 std::vector
< OUString
> * strings
)
1490 assert(strings
!= nullptr);
1492 switch (key
.openKey(path
, subkey
)) {
1493 case RegError::NO_ERROR
:
1495 case RegError::KEY_NOT_EXISTS
:
1498 throw css::uno::DeploymentException(
1499 OUString::Concat("Failure reading legacy rdb file ") + uri
,
1500 static_cast< cppu::OWeakObject
* >(this));
1502 OUString
prefix(subkey
.getName() + "/");
1503 RegistryKeyNames names
;
1504 if (subkey
.getKeyNames(OUString(), names
) != RegError::NO_ERROR
) {
1505 throw css::uno::DeploymentException(
1506 OUString::Concat("Failure reading legacy rdb file ") + uri
,
1507 static_cast< cppu::OWeakObject
* >(this));
1509 for (sal_uInt32 i
= 0; i
!= names
.getLength(); ++i
) {
1510 assert(names
.getElement(i
).match(prefix
));
1511 strings
->push_back(names
.getElement(i
).copy(prefix
.getLength()));
1515 void cppuhelper::ServiceManager::insertRdbFiles(
1516 std::vector
< OUString
> const & uris
,
1517 css::uno::Reference
< css::uno::XComponentContext
> const & alienContext
)
1520 for (const auto& rUri
: uris
)
1523 Parser(rUri
, alienContext
, &extra
);
1524 } catch (css::container::NoSuchElementException
&) {
1525 throw css::lang::IllegalArgumentException(
1526 rUri
+ ": no such file", static_cast< cppu::OWeakObject
* >(this),
1528 } catch (css::registry::InvalidRegistryException
& e
) {
1529 throw css::lang::IllegalArgumentException(
1530 "InvalidRegistryException: " + e
.Message
,
1531 static_cast< cppu::OWeakObject
* >(this), 0);
1534 insertExtraData(extra
);
1537 void cppuhelper::ServiceManager::insertLegacyFactory(
1538 css::uno::Reference
< css::lang::XServiceInfo
> const & factoryInfo
)
1540 assert(factoryInfo
.is());
1541 OUString
name(factoryInfo
->getImplementationName());
1542 css::uno::Reference
< css::lang::XSingleComponentFactory
> f1(
1543 factoryInfo
, css::uno::UNO_QUERY
);
1544 css::uno::Reference
< css::lang::XSingleServiceFactory
> f2
;
1546 f2
.set(factoryInfo
, css::uno::UNO_QUERY
);
1548 throw css::lang::IllegalArgumentException(
1549 ("Bad XServiceInfo argument implements neither"
1550 " XSingleComponentFactory nor XSingleServiceFactory"),
1551 static_cast< cppu::OWeakObject
* >(this), 0);
1554 css::uno::Reference
< css::lang::XComponent
> comp(
1555 factoryInfo
, css::uno::UNO_QUERY
);
1556 std::shared_ptr
< Data::Implementation
> impl
=
1557 std::make_shared
<Data::Implementation
>(name
, f1
, f2
, comp
);
1559 if (!name
.isEmpty()) {
1560 extra
.namedImplementations
.emplace(name
, impl
);
1562 extra
.dynamicImplementations
.emplace(factoryInfo
, impl
);
1563 const css::uno::Sequence
< OUString
> services(
1564 factoryInfo
->getSupportedServiceNames());
1565 for (const auto & i
: services
) {
1566 impl
->services
.push_back(i
);
1567 extra
.services
[i
].push_back(impl
);
1569 if (insertExtraData(extra
) && comp
.is()) {
1570 comp
->addEventListener(this);
1574 bool cppuhelper::ServiceManager::insertExtraData(Data
const & extra
) {
1576 std::unique_lock
g(m_aMutex
);
1580 auto i
= std::find_if(extra
.namedImplementations
.begin(), extra
.namedImplementations
.end(),
1581 [this](const Data::NamedImplementations::value_type
& rEntry
) {
1582 return data_
.namedImplementations
.find(rEntry
.first
) != data_
.namedImplementations
.end(); });
1583 if (i
!= extra
.namedImplementations
.end())
1585 throw css::lang::IllegalArgumentException(
1586 "Insert duplicate implementation name " + i
->first
,
1587 static_cast< cppu::OWeakObject
* >(this), 0);
1589 bool bDuplicate
= std::any_of(extra
.dynamicImplementations
.begin(), extra
.dynamicImplementations
.end(),
1590 [this](const Data::DynamicImplementations::value_type
& rEntry
) {
1591 return data_
.dynamicImplementations
.find(rEntry
.first
) != data_
.dynamicImplementations
.end(); });
1594 throw css::lang::IllegalArgumentException(
1595 "Insert duplicate factory object",
1596 static_cast< cppu::OWeakObject
* >(this), 0);
1598 //TODO: The below leaves data_ in an inconsistent state upon exceptions:
1599 data_
.namedImplementations
.insert(
1600 extra
.namedImplementations
.begin(),
1601 extra
.namedImplementations
.end());
1602 data_
.dynamicImplementations
.insert(
1603 extra
.dynamicImplementations
.begin(),
1604 extra
.dynamicImplementations
.end());
1605 insertImplementationMap(&data_
.services
, extra
.services
);
1606 insertImplementationMap(&data_
.singletons
, extra
.singletons
);
1608 //TODO: Updating the component context singleton data should be part of the
1609 // atomic service manager update:
1610 if (extra
.singletons
.empty())
1613 assert(context_
.is());
1614 css::uno::Reference
< css::container::XNameContainer
> cont(
1615 context_
, css::uno::UNO_QUERY_THROW
);
1616 for (const auto& [rName
, rImpls
] : extra
.singletons
)
1618 OUString
name("/singletons/" + rName
);
1619 //TODO: Update should be atomic:
1621 cont
->removeByName(name
+ "/arguments");
1622 } catch (const css::container::NoSuchElementException
&) {}
1623 assert(!rImpls
.empty());
1626 rImpls
.size() > 1, "cppuhelper",
1627 "Arbitrarily choosing " << rImpls
[0]->name
1628 << " among multiple implementations for singleton "
1632 name
+ "/service", css::uno::Any(rImpls
[0]->name
));
1633 } catch (css::container::ElementExistException
&) {
1634 cont
->replaceByName(
1635 name
+ "/service", css::uno::Any(rImpls
[0]->name
));
1638 cont
->insertByName(name
, css::uno::Any());
1639 } catch (css::container::ElementExistException
&) {
1640 SAL_INFO("cppuhelper", "Overwriting singleton " << rName
);
1641 cont
->replaceByName(name
, css::uno::Any());
1647 void cppuhelper::ServiceManager::removeRdbFiles(
1648 std::vector
< OUString
> const & uris
)
1650 // The underlying data structures make this function somewhat inefficient,
1651 // but the assumption is that it is rarely called (and that if it is called,
1652 // it is called with a uris vector of size one):
1653 std::vector
< std::shared_ptr
< Data::Implementation
> > clear
;
1655 std::unique_lock
g(m_aMutex
);
1656 for (const auto& rUri
: uris
)
1658 for (Data::NamedImplementations::iterator
j(
1659 data_
.namedImplementations
.begin());
1660 j
!= data_
.namedImplementations
.end();)
1663 if (j
->second
->rdbFile
== rUri
) {
1664 clear
.push_back(j
->second
);
1665 //TODO: The below leaves data_ in an inconsistent state upon
1667 removeFromImplementationMap(
1668 &data_
.services
, j
->second
->services
, j
->second
);
1669 removeFromImplementationMap(
1670 &data_
.singletons
, j
->second
->singletons
,
1672 j
= data_
.namedImplementations
.erase(j
);
1679 //TODO: Update the component context singleton data
1682 bool cppuhelper::ServiceManager::removeLegacyFactory(
1683 css::uno::Reference
< css::lang::XServiceInfo
> const & factoryInfo
,
1684 bool removeListener
)
1686 assert(factoryInfo
.is());
1687 std::shared_ptr
< Data::Implementation
> clear
;
1688 css::uno::Reference
< css::lang::XComponent
> comp
;
1690 std::unique_lock
g(m_aMutex
);
1691 Data::DynamicImplementations::iterator
i(
1692 data_
.dynamicImplementations
.find(factoryInfo
));
1693 if (i
== data_
.dynamicImplementations
.end()) {
1698 if (removeListener
) {
1699 comp
= i
->second
->component
;
1701 //TODO: The below leaves data_ in an inconsistent state upon exceptions:
1702 removeFromImplementationMap(
1703 &data_
.services
, i
->second
->services
, i
->second
);
1704 removeFromImplementationMap(
1705 &data_
.singletons
, i
->second
->singletons
, i
->second
);
1706 if (!i
->second
->name
.isEmpty()) {
1707 data_
.namedImplementations
.erase(i
->second
->name
);
1709 data_
.dynamicImplementations
.erase(i
);
1712 removeEventListenerFromComponent(comp
);
1717 void cppuhelper::ServiceManager::removeImplementation(const OUString
& name
) {
1718 // The underlying data structures make this function somewhat inefficient,
1719 // but the assumption is that it is rarely called:
1720 std::shared_ptr
< Data::Implementation
> clear
;
1722 std::unique_lock
g(m_aMutex
);
1726 Data::NamedImplementations::iterator
i(
1727 data_
.namedImplementations
.find(name
));
1728 if (i
== data_
.namedImplementations
.end()) {
1729 throw css::container::NoSuchElementException(
1730 "Remove non-inserted implementation " + name
,
1731 static_cast< cppu::OWeakObject
* >(this));
1735 //TODO: The below leaves data_ in an inconsistent state upon exceptions:
1736 removeFromImplementationMap(
1737 &data_
.services
, i
->second
->services
, i
->second
);
1738 removeFromImplementationMap(
1739 &data_
.singletons
, i
->second
->singletons
, i
->second
);
1740 auto j
= std::find_if(data_
.dynamicImplementations
.begin(), data_
.dynamicImplementations
.end(),
1741 [&i
](const Data::DynamicImplementations::value_type
& rEntry
) { return rEntry
.second
== i
->second
; });
1742 if (j
!= data_
.dynamicImplementations
.end())
1743 data_
.dynamicImplementations
.erase(j
);
1744 data_
.namedImplementations
.erase(i
);
1748 std::shared_ptr
< cppuhelper::ServiceManager::Data::Implementation
>
1749 cppuhelper::ServiceManager::findServiceImplementation(
1750 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
1751 OUString
const & specifier
)
1753 std::shared_ptr
< Data::Implementation
> impl
;
1756 std::unique_lock
g(m_aMutex
);
1757 Data::ImplementationMap::const_iterator
i(
1758 data_
.services
.find(specifier
));
1759 if (i
== data_
.services
.end()) {
1760 Data::NamedImplementations::const_iterator
j(
1761 data_
.namedImplementations
.find(specifier
));
1762 if (j
== data_
.namedImplementations
.end()) {
1763 SAL_INFO("cppuhelper", "No implementation for " << specifier
);
1764 return std::shared_ptr
< Data::Implementation
>();
1768 assert(!i
->second
.empty());
1770 i
->second
.size() > 1, "cppuhelper",
1771 "Arbitrarily choosing " << i
->second
[0]->name
1772 << " among multiple implementations for " << i
->first
);
1773 impl
= i
->second
[0];
1776 loaded
= impl
->status
== Data::Implementation::STATUS_LOADED
;
1779 loadImplementation(context
, impl
);
1784 /// Make a simpler unique name for preload / progress reporting.
1785 #ifndef DISABLE_DYNLOADING
1786 static OUString
simplifyModule(std::u16string_view uri
)
1789 OUStringBuffer
edit(uri
);
1790 if ((nIdx
= edit
.lastIndexOf('/')) > 0)
1791 edit
.remove(0,nIdx
+1);
1792 if ((nIdx
= edit
.lastIndexOf(':')) > 0)
1793 edit
.remove(0,nIdx
+1);
1794 if ((nIdx
= edit
.lastIndexOf("lo.so")) > 0)
1795 edit
.truncate(nIdx
);
1796 if ((nIdx
= edit
.lastIndexOf(".3")) > 0)
1797 edit
.truncate(nIdx
);
1798 if ((nIdx
= edit
.lastIndexOf("gcc3.so")) > 0)
1799 edit
.truncate(nIdx
);
1800 if ((nIdx
= edit
.lastIndexOf(".so")) > 0)
1801 edit
.truncate(nIdx
);
1802 if ((nIdx
= edit
.lastIndexOf("_uno")) > 0)
1803 edit
.truncate(nIdx
);
1804 if ((nIdx
= edit
.lastIndexOf(".jar")) > 0)
1805 edit
.truncate(nIdx
);
1806 if (edit
.indexOf("lib") == 0)
1808 return edit
.makeStringAndClear();
1812 /// Used only by LibreOfficeKit when used by Online to pre-initialize
1813 void cppuhelper::ServiceManager::preloadImplementations() {
1814 #ifdef DISABLE_DYNLOADING
1818 std::unique_lock
g(m_aMutex
);
1819 css::uno::Environment
aSourceEnv(css::uno::Environment::getCurrent());
1821 std::cerr
<< "preload:";
1822 std::vector
<OUString
> aReported
;
1823 std::vector
<OUString
> aDisabled
;
1824 OUStringBuffer aDisabledMsg
;
1825 OUStringBuffer aMissingMsg
;
1827 /// Allow external callers & testers to disable certain components
1828 const char *pDisable
= getenv("UNODISABLELIBRARY");
1831 OUString
aDisable(pDisable
, strlen(pDisable
), RTL_TEXTENCODING_UTF8
);
1832 for (sal_Int32 i
= 0; i
>= 0; )
1834 OUString
tok( aDisable
.getToken(0, ' ', i
) );
1837 aDisabled
.push_back(tok
);
1841 // loop all implementations
1842 for (const auto& rEntry
: data_
.namedImplementations
)
1844 if (rEntry
.second
->loader
!= "com.sun.star.loader.SharedLibrary" ||
1845 rEntry
.second
->status
== Data::Implementation::STATUS_LOADED
)
1848 OUString simplified
;
1851 const OUString
&aLibrary
= rEntry
.second
->uri
;
1853 if (aLibrary
.isEmpty())
1856 simplified
= simplifyModule(aLibrary
);
1859 std::find(aDisabled
.begin(), aDisabled
.end(), simplified
) != aDisabled
.end();
1861 if (std::find(aReported
.begin(), aReported
.end(), aLibrary
) == aReported
.end())
1865 aDisabledMsg
.append(simplified
+ " ");
1869 std::cerr
<< " " << simplified
;
1872 aReported
.push_back(aLibrary
);
1878 // expand absolute URI implementation component library
1879 aUri
= cppu::bootstrap_expandUri(aLibrary
);
1881 catch (css::lang::IllegalArgumentException
& aError
)
1883 throw css::uno::DeploymentException(
1884 "Cannot expand URI" + rEntry
.second
->uri
+ ": " + aError
.Message
,
1885 static_cast< cppu::OWeakObject
* >(this));
1888 // load component library
1889 osl::Module
aModule(aUri
, SAL_LOADMODULE_NOW
| SAL_LOADMODULE_GLOBAL
);
1893 aMissingMsg
.append(simplified
+ " ");
1897 !rEntry
.second
->environment
.isEmpty())
1899 oslGenericFunction fpFactory
;
1900 css::uno::Environment aTargetEnv
;
1901 css::uno::Reference
<css::uno::XInterface
> xFactory
;
1903 if(rEntry
.second
->constructorName
.isEmpty())
1905 OUString aSymFactory
;
1906 // expand full name component factory symbol
1907 if (rEntry
.second
->prefix
== "direct")
1908 aSymFactory
= rEntry
.second
->name
.replace('.', '_') + "_" COMPONENT_GETFACTORY
;
1909 else if (!rEntry
.second
->prefix
.isEmpty())
1910 aSymFactory
= rEntry
.second
->prefix
+ "_" COMPONENT_GETFACTORY
;
1912 aSymFactory
= COMPONENT_GETFACTORY
;
1914 // get function symbol component factory
1915 fpFactory
= aModule
.getFunctionSymbol(aSymFactory
);
1916 if (fpFactory
== nullptr)
1918 throw css::loader::CannotActivateFactoryException(
1919 ("no factory symbol \"" + aSymFactory
+ "\" in component library :" + aUri
),
1920 css::uno::Reference
<css::uno::XInterface
>());
1923 aTargetEnv
= cppuhelper::detail::getEnvironment(rEntry
.second
->environment
, rEntry
.second
->name
);
1924 component_getFactoryFunc fpComponentFactory
= reinterpret_cast<component_getFactoryFunc
>(fpFactory
);
1926 if (aSourceEnv
.get() == aTargetEnv
.get())
1928 // invoke function component factory
1929 OString
aImpl(OUStringToOString(rEntry
.second
->name
, RTL_TEXTENCODING_ASCII_US
));
1930 xFactory
.set(css::uno::Reference
<css::uno::XInterface
>(static_cast<css::uno::XInterface
*>(
1931 (*fpComponentFactory
)(aImpl
.getStr(), this, nullptr)), SAL_NO_ACQUIRE
));
1936 // get function symbol component factory
1937 aTargetEnv
= cppuhelper::detail::getEnvironment(rEntry
.second
->environment
, rEntry
.second
->name
);
1938 fpFactory
= (aSourceEnv
.get() == aTargetEnv
.get()) ?
1939 aModule
.getFunctionSymbol(rEntry
.second
->constructorName
) : nullptr;
1942 css::uno::Reference
<css::lang::XSingleComponentFactory
> xSCFactory
;
1943 css::uno::Reference
<css::lang::XSingleServiceFactory
> xSSFactory
;
1945 // query interface XSingleComponentFactory or XSingleServiceFactory
1948 xSCFactory
.set(xFactory
, css::uno::UNO_QUERY
);
1949 if (!xSCFactory
.is())
1951 xSSFactory
.set(xFactory
, css::uno::UNO_QUERY
);
1952 if (!xSSFactory
.is())
1953 throw css::uno::DeploymentException(
1954 ("Implementation " + rEntry
.second
->name
1955 + " does not provide a constructor or factory"),
1956 static_cast< cppu::OWeakObject
* >(this));
1960 if (!rEntry
.second
->constructorName
.isEmpty() && fpFactory
)
1961 rEntry
.second
->constructorFn
= WrapperConstructorFn(reinterpret_cast<ImplementationConstructorFn
*>(fpFactory
));
1963 rEntry
.second
->factory1
= xSCFactory
;
1964 rEntry
.second
->factory2
= xSSFactory
;
1965 rEntry
.second
->status
= Data::Implementation::STATUS_LOADED
;
1969 // Some libraries use other (non-UNO) libraries requiring preinit
1970 oslGenericFunction fpPreload
= aModule
.getFunctionSymbol( "lok_preload_hook" );
1973 static std::vector
<oslGenericFunction
> aPreloaded
;
1974 if (std::find(aPreloaded
.begin(), aPreloaded
.end(), fpPreload
) == aPreloaded
.end())
1976 aPreloaded
.push_back(fpPreload
);
1984 std::cerr
<< std::endl
;
1986 if (aMissingMsg
.getLength() > 0)
1988 OUString aMsg
= aMissingMsg
.makeStringAndClear();
1989 std::cerr
<< "Absent (often optional): " << aMsg
<< "\n";
1991 if (aDisabledMsg
.getLength() > 0)
1993 OUString aMsg
= aDisabledMsg
.makeStringAndClear();
1994 std::cerr
<< "Disabled: " << aMsg
<< "\n";
1998 // Various rather important uno mappings.
2002 const char *mpPurpose
;
2003 } const aMappingLoad
[] = {
2004 { "gcc3", "uno", "" },
2005 { "uno", "gcc3", "" },
2008 static std::vector
<css::uno::Mapping
> maMaps
;
2009 for (auto &it
: aMappingLoad
)
2011 maMaps
.push_back(css::uno::Mapping(
2012 OUString::createFromAscii(it
.mpFrom
),
2013 OUString::createFromAscii(it
.mpTo
),
2014 OUString::createFromAscii(it
.mpPurpose
)));
2019 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */