LibreOffice Module cppuhelper (master) 1
servicemanager.hxx
Go to the documentation of this file.
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
10#pragma once
11
12#include <sal/config.h>
13
14#include <cassert>
15#include <functional>
16#include <memory>
17#include <mutex>
18#include <string_view>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/beans/XPropertySetInfo.hpp>
25#include <com/sun/star/container/XContentEnumerationAccess.hpp>
26#include <com/sun/star/container/XSet.hpp>
27#include <com/sun/star/lang/XEventListener.hpp>
28#include <com/sun/star/lang/XInitialization.hpp>
29#include <com/sun/star/lang/XMultiComponentFactory.hpp>
30#include <com/sun/star/lang/XMultiServiceFactory.hpp>
31#include <com/sun/star/lang/XServiceInfo.hpp>
32#include <com/sun/star/uno/Reference.hxx>
33#include <compbase2.hxx>
34#include <rtl/ustring.hxx>
35
36namespace com::sun::star::lang {
37 class XSingleComponentFactory;
38}
39namespace cppu { struct ContextEntry_Init; }
40namespace com :: sun :: star :: lang { class XSingleServiceFactory; }
41namespace com :: sun :: star :: uno { class XComponentContext; }
42
43class RegistryKey;
44
45namespace cppuhelper {
46
47extern "C" {
48
49typedef css::uno::XInterface * ImplementationConstructorFn(
50 css::uno::XComponentContext *, css::uno::Sequence<css::uno::Any> const &);
51
52}
53
54typedef std::function<css::uno::XInterface * (css::uno::XComponentContext *, css::uno::Sequence<css::uno::Any> const&)> WrapperConstructorFn;
55
57 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
58 css::lang::XMultiComponentFactory, css::container::XSet,
59 css::container::XContentEnumerationAccess, css::beans::XPropertySet,
60 css::beans::XPropertySetInfo, css::lang::XEventListener,
61 css::lang::XInitialization>
63
65{
66public:
67 struct Data {
68 Data() = default;
69 Data(const Data&) = delete;
70 const Data& operator=(const Data&) = delete;
71
74 OUString theName, OUString theLoader,
75 OUString theUri, OUString theEnvironment,
76 OUString theConstructorName,
77 OUString thePrefix,
78 bool theIsSingleInstance,
79 css::uno::Reference< css::uno::XComponentContext > theAlienContext,
80 OUString theRdbFile):
81 name(std::move(theName)), loader(std::move(theLoader)), uri(std::move(theUri)), environment(std::move(theEnvironment)),
82 constructorName(std::move(theConstructorName)), prefix(std::move(thePrefix)),
83 isSingleInstance(theIsSingleInstance),
84 alienContext(std::move(theAlienContext)), rdbFile(std::move(theRdbFile)),
85 constructorFn(nullptr), status(STATUS_NEW), dispose(true)
86 {}
87
89 OUString theName,
90 css::uno::Reference< css::lang::XSingleComponentFactory >
91 const & theFactory1,
92 css::uno::Reference< css::lang::XSingleServiceFactory > const &
93 theFactory2,
94 css::uno::Reference< css::lang::XComponent > theComponent):
95 name(std::move(theName)), isSingleInstance(false), constructorFn(nullptr),
96 factory1(theFactory1), factory2(theFactory2),
97 component(std::move(theComponent)), status(STATUS_LOADED), dispose(true)
98 { assert(theFactory1.is() || theFactory2.is()); }
99
101 const Implementation& operator=(const Implementation&) = delete;
102
103 css::uno::Reference<css::uno::XInterface> createInstance(
104 css::uno::Reference<css::uno::XComponentContext> const &
105 context,
106 bool singletonRequest);
107
108 css::uno::Reference<css::uno::XInterface>
110 css::uno::Reference<css::uno::XComponentContext> const &
111 context,
112 bool singletonRequest,
113 css::uno::Sequence<css::uno::Any> const & arguments);
114
115 bool shallDispose() const { return isSingleInstance || !singletons.empty(); }
116
118
119 // Logically, exactly one of constructorFn, factory1, factory2 should
120 // be set. However, there are two exceptions: For one, when
121 // constructorFn is set, ServiceManager::createContentEnumeration will
122 // store the necessary ImplementationWrapper in factory1 (so that
123 // multiple calls to createContentEnumeration will return the same
124 // wrapper). For another, when factory1 should be set but status is
125 // STATUS_NEW, factory1 is not yet set (and when status is
126 // STATUS_WRAPPER, factory1 is merely set to an
127 // ImplementationWrapper---also due to a
128 // ServiceManager::createContentEnumeration call---and will be
129 // loaded later).
130 OUString name;
131 OUString loader;
132 OUString uri;
133 OUString environment;
135 OUString prefix;
137 css::uno::Reference< css::uno::XComponentContext > alienContext;
138 OUString rdbFile;
139 std::vector< OUString > services;
140 std::vector< OUString > singletons;
142 css::uno::Reference< css::lang::XSingleComponentFactory > factory1;
143 css::uno::Reference< css::lang::XSingleServiceFactory > factory2;
144 css::uno::Reference< css::lang::XComponent > component;
146
147 std::mutex mutex;
148 css::uno::Reference<css::uno::XInterface> singleInstance;
149 css::uno::Reference< css::lang::XComponent > disposeInstance;
151
152 private:
153 css::uno::Reference<css::uno::XInterface> doCreateInstance(
154 css::uno::Reference<css::uno::XComponentContext> const & context);
155
156 css::uno::Reference<css::uno::XInterface> doCreateInstanceWithArguments(
157 css::uno::Reference<css::uno::XComponentContext> const & context,
158 css::uno::Sequence<css::uno::Any> const & arguments);
159
161 bool singletonRequest,
162 css::uno::Reference<css::uno::XInterface> const & instance);
163 };
164
165 typedef std::unordered_map< OUString, std::shared_ptr< Implementation > >
167
168 typedef
169 std::unordered_map<
170 css::uno::Reference< css::lang::XServiceInfo >,
171 std::shared_ptr< Implementation > >
173
174 typedef
175 std::unordered_map<
176 OUString,
177 std::vector< std::shared_ptr< Implementation > > >
179
184 };
185
187
189 const ServiceManager& operator=(const ServiceManager&) = delete;
190
193
194 void init(std::u16string_view rdbUris);
195
197 css::uno::Reference< css::uno::XComponentContext > const & context)
198 {
199 assert(context.is());
200 assert(!context_.is());
201 context_ = context;
202 }
203
205 std::vector< cppu::ContextEntry_Init > * entries);
206
207 css::uno::Reference< css::uno::XComponentContext > const & getContext()
208 const
209 {
210 assert(context_.is());
211 return context_;
212 }
213
215 css::uno::Reference< css::uno::XComponentContext > const & context,
216 std::shared_ptr< Data::Implementation > const & implementation);
217
218private:
219 virtual ~ServiceManager() override;
220
221 virtual void disposing(std::unique_lock<std::mutex>&) override;
222
223 virtual OUString SAL_CALL getImplementationName() override;
224
225 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override;
226
227 virtual css::uno::Sequence< OUString > SAL_CALL
228 getSupportedServiceNames() override;
229
230 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
231 OUString const & aServiceSpecifier) override;
232
233 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
235 OUString const & ServiceSpecifier,
236 css::uno::Sequence< css::uno::Any > const & Arguments) override;
237
238 virtual css::uno::Sequence< OUString > SAL_CALL
239 getAvailableServiceNames() override;
240
241 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
243 OUString const & aServiceSpecifier,
244 css::uno::Reference< css::uno::XComponentContext > const & Context) override;
245
246 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
248 OUString const & ServiceSpecifier,
249 css::uno::Sequence< css::uno::Any > const & Arguments,
250 css::uno::Reference< css::uno::XComponentContext > const & Context) override;
251
252 virtual css::uno::Type SAL_CALL getElementType() override;
253
254 virtual sal_Bool SAL_CALL hasElements() override;
255
256 virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL
257 createEnumeration() override;
258
259 virtual sal_Bool SAL_CALL has(css::uno::Any const & aElement) override;
260
261 virtual void SAL_CALL insert(css::uno::Any const & aElement) override;
262
263 virtual void SAL_CALL remove(css::uno::Any const & aElement) override;
264
265 virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL
266 createContentEnumeration(OUString const & aServiceName) override;
267
268 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
269 getPropertySetInfo() override;
270
271 virtual void SAL_CALL setPropertyValue(
272 OUString const & aPropertyName, css::uno::Any const & aValue) override;
273
274 virtual css::uno::Any SAL_CALL getPropertyValue(
275 OUString const & PropertyName) override;
276
277 virtual void SAL_CALL addPropertyChangeListener(
278 OUString const & aPropertyName,
279 css::uno::Reference< css::beans::XPropertyChangeListener > const &
280 xListener) override;
281
282 virtual void SAL_CALL removePropertyChangeListener(
283 OUString const & aPropertyName,
284 css::uno::Reference< css::beans::XPropertyChangeListener > const &
285 aListener) override;
286
287 virtual void SAL_CALL addVetoableChangeListener(
288 OUString const & PropertyName,
289 css::uno::Reference< css::beans::XVetoableChangeListener > const &
290 aListener) override;
291
292 virtual void SAL_CALL removeVetoableChangeListener(
293 OUString const & PropertyName,
294 css::uno::Reference< css::beans::XVetoableChangeListener > const &
295 aListener) override;
296
297 virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties() override;
298
299 virtual css::beans::Property SAL_CALL getPropertyByName(
300 OUString const & aName) override;
301
302 virtual sal_Bool SAL_CALL hasPropertyByName(OUString const & Name) override;
303
304 virtual void SAL_CALL disposing(css::lang::EventObject const & Source) override;
305
306 virtual void SAL_CALL initialize(
307 css::uno::Sequence<css::uno::Any> const & aArguments)
308 override;
309
311 css::uno::Reference< css::lang::XComponent > const & component);
312
313 void readRdbDirectory(std::u16string_view uri, bool optional);
314
315 void readRdbFile(OUString const & uri, bool optional);
316
317 bool readLegacyRdbFile(OUString const & uri);
318
319 OUString readLegacyRdbString(
320 std::u16string_view uri, RegistryKey & key,
321 OUString const & path);
322
324 std::u16string_view uri, RegistryKey & key,
325 OUString const & path, std::vector< OUString > * strings);
326
327 void insertRdbFiles(
328 std::vector< OUString > const & uris,
329 css::uno::Reference< css::uno::XComponentContext > const &
330 alientContext);
331
333 css::uno::Reference< css::lang::XServiceInfo > const & factoryInfo);
334
335 bool insertExtraData(Data const & extra);
336
337 void removeRdbFiles(std::vector< OUString > const & uris);
338
340 css::uno::Reference< css::lang::XServiceInfo > const & factoryInfo,
341 bool removeListener);
342
343 void removeImplementation(const OUString & name);
344
345 std::shared_ptr< Data::Implementation > findServiceImplementation(
346 css::uno::Reference< css::uno::XComponentContext > const & context,
347 OUString const & specifier);
348
350
351 css::uno::Reference< css::uno::XComponentContext > context_;
353};
354
355}
356
357/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties() override
bool readLegacyRdbFile(OUString const &uri)
virtual css::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames() override
const ServiceManager & operator=(const ServiceManager &)=delete
virtual void SAL_CALL removeVetoableChangeListener(OUString const &PropertyName, css::uno::Reference< css::beans::XVetoableChangeListener > const &aListener) override
void loadImplementation(css::uno::Reference< css::uno::XComponentContext > const &context, std::shared_ptr< Data::Implementation > const &implementation)
bool insertExtraData(Data const &extra)
void addSingletonContextEntries(std::vector< cppu::ContextEntry_Init > *entries)
void removeEventListenerFromComponent(css::uno::Reference< css::lang::XComponent > const &component)
virtual void SAL_CALL setPropertyValue(OUString const &aPropertyName, css::uno::Any const &aValue) override
virtual sal_Bool SAL_CALL hasPropertyByName(OUString const &Name) override
virtual void SAL_CALL insert(css::uno::Any const &aElement) override
void insertLegacyFactory(css::uno::Reference< css::lang::XServiceInfo > const &factoryInfo)
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual void SAL_CALL removePropertyChangeListener(OUString const &aPropertyName, css::uno::Reference< css::beans::XPropertyChangeListener > const &aListener) override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual ~ServiceManager() override
void readRdbDirectory(std::u16string_view uri, bool optional)
virtual css::beans::Property SAL_CALL getPropertyByName(OUString const &aName) override
virtual void SAL_CALL addVetoableChangeListener(OUString const &PropertyName, css::uno::Reference< css::beans::XVetoableChangeListener > const &aListener) override
void insertRdbFiles(std::vector< OUString > const &uris, css::uno::Reference< css::uno::XComponentContext > const &alientContext)
void readLegacyRdbStrings(std::u16string_view uri, RegistryKey &key, OUString const &path, std::vector< OUString > *strings)
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(OUString const &ServiceSpecifier, css::uno::Sequence< css::uno::Any > const &Arguments, css::uno::Reference< css::uno::XComponentContext > const &Context) override
std::shared_ptr< Data::Implementation > findServiceImplementation(css::uno::Reference< css::uno::XComponentContext > const &context, OUString const &specifier)
void removeImplementation(const OUString &name)
virtual void disposing(std::unique_lock< std::mutex > &) override
Called by dispose for subclasses to do dispose() work.
bool removeLegacyFactory(css::uno::Reference< css::lang::XServiceInfo > const &factoryInfo, bool removeListener)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL has(css::uno::Any const &aElement) override
css::uno::Reference< css::uno::XComponentContext > const & getContext() const
virtual void SAL_CALL remove(css::uno::Any const &aElement) override
void removeRdbFiles(std::vector< OUString > const &uris)
void setContext(css::uno::Reference< css::uno::XComponentContext > const &context)
virtual css::uno::Any SAL_CALL getPropertyValue(OUString const &PropertyName) override
void preloadImplementations()
Used only by LibreOfficeKit when used by Online to pre-initialize.
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(OUString const &aServiceSpecifier) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext(OUString const &aServiceSpecifier, css::uno::Reference< css::uno::XComponentContext > const &Context) override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(OUString const &ServiceSpecifier, css::uno::Sequence< css::uno::Any > const &Arguments) override
ServiceManager(const ServiceManager &)=delete
virtual void SAL_CALL initialize(css::uno::Sequence< css::uno::Any > const &aArguments) override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createContentEnumeration(OUString const &aServiceName) override
virtual sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
void readRdbFile(OUString const &uri, bool optional)
css::uno::Reference< css::uno::XComponentContext > context_
OUString readLegacyRdbString(std::u16string_view uri, RegistryKey &key, OUString const &path)
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL addPropertyChangeListener(OUString const &aPropertyName, css::uno::Reference< css::beans::XPropertyChangeListener > const &xListener) override
void init(std::u16string_view rdbUris)
virtual void SAL_CALL release() noexcept override
decreasing m_refCount
virtual void SAL_CALL acquire() noexcept override
increasing m_refCount
This is a straight copy of the include/comphelper/compbase.hxx file, copied here because it is nigh i...
Definition: compbase.cxx:15
WeakComponentImplHelper2< css::lang::XServiceInfo, css::lang::XMultiServiceFactory, css::lang::XMultiComponentFactory, css::container::XSet, css::container::XContentEnumerationAccess, css::beans::XPropertySet, css::beans::XPropertySetInfo, css::lang::XEventListener, css::lang::XInitialization > ServiceManagerBase
css::uno::XInterface * ImplementationConstructorFn(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
std::function< css::uno::XInterface *(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)> WrapperConstructorFn
css::uno::Reference< css::lang::XComponent > disposeInstance
css::uno::Reference< css::uno::XInterface > createInstanceWithArguments(css::uno::Reference< css::uno::XComponentContext > const &context, bool singletonRequest, css::uno::Sequence< css::uno::Any > const &arguments)
const Implementation & operator=(const Implementation &)=delete
css::uno::Reference< css::uno::XInterface > doCreateInstanceWithArguments(css::uno::Reference< css::uno::XComponentContext > const &context, css::uno::Sequence< css::uno::Any > const &arguments)
css::uno::Reference< css::uno::XInterface > createInstance(css::uno::Reference< css::uno::XComponentContext > const &context, bool singletonRequest)
Implementation(OUString theName, OUString theLoader, OUString theUri, OUString theEnvironment, OUString theConstructorName, OUString thePrefix, bool theIsSingleInstance, css::uno::Reference< css::uno::XComponentContext > theAlienContext, OUString theRdbFile)
css::uno::Reference< css::uno::XInterface > doCreateInstance(css::uno::Reference< css::uno::XComponentContext > const &context)
css::uno::Reference< css::lang::XSingleServiceFactory > factory2
css::uno::Reference< css::lang::XSingleComponentFactory > factory1
Implementation(OUString theName, css::uno::Reference< css::lang::XSingleComponentFactory > const &theFactory1, css::uno::Reference< css::lang::XSingleServiceFactory > const &theFactory2, css::uno::Reference< css::lang::XComponent > theComponent)
Implementation(const Implementation &)=delete
css::uno::Reference< css::uno::XInterface > singleInstance
css::uno::Reference< css::lang::XComponent > component
void updateDisposeInstance(bool singletonRequest, css::uno::Reference< css::uno::XInterface > const &instance)
css::uno::Reference< css::uno::XComponentContext > alienContext
std::unordered_map< OUString, std::shared_ptr< Implementation > > NamedImplementations
DynamicImplementations dynamicImplementations
std::unordered_map< css::uno::Reference< css::lang::XServiceInfo >, std::shared_ptr< Implementation > > DynamicImplementations
const Data & operator=(const Data &)=delete
NamedImplementations namedImplementations
std::unordered_map< OUString, std::vector< std::shared_ptr< Implementation > > > ImplementationMap
unsigned char sal_Bool