Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cppuhelper / source / shlib.cxx
blobc5979e64239316cbc6a65b87c27e19dc5dfcf6f8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <cstdlib>
25 #include <com/sun/star/loader/CannotActivateFactoryException.hpp>
26 #include <com/sun/star/registry/CannotRegisterImplementationException.hpp>
27 #include <com/sun/star/registry/XRegistryKey.hpp>
28 #include <cppuhelper/factory.hxx>
29 #include <cppuhelper/shlib.hxx>
30 #include <osl/module.hxx>
31 #include <sal/log.hxx>
32 #include <uno/environment.hxx>
33 #include <uno/mapping.hxx>
35 #include "loadsharedlibcomponentfactory.hxx"
37 #if defined DISABLE_DYNLOADING
38 #include <osl/detail/component-mapping.h>
39 #endif
41 css::uno::Environment cppuhelper::detail::getEnvironment(
42 OUString const & name, OUString const & implementation)
44 OUString n(name);
45 if (!implementation.isEmpty()) {
46 static char const * log = std::getenv("UNO_ENV_LOG");
47 if (log != nullptr && *log != 0) {
48 OString imps(log);
49 for (sal_Int32 i = 0; i != -1;) {
50 OString imp(imps.getToken(0, ';', i));
51 //TODO: this assumes UNO_ENV_LOG only contains ASCII characters:
52 if (implementation.equalsAsciiL(imp.getStr(), imp.getLength()))
54 n += ":log";
55 break;
60 return css::uno::Environment(n);
63 namespace {
65 #if !defined DISABLE_DYNLOADING
67 css::uno::Environment getEnvironmentFromModule(
68 osl::Module const & module, css::uno::Environment const & target,
69 OUString const & implementation, OUString const & prefix)
71 char const * name = nullptr;
72 css::uno::Environment env;
73 OUString fullPrefix(prefix);
74 if (!fullPrefix.isEmpty()) {
75 fullPrefix += "_";
77 component_getImplementationEnvironmentExtFunc fp1
78 = reinterpret_cast<component_getImplementationEnvironmentExtFunc>(
79 module.getFunctionSymbol(fullPrefix + COMPONENT_GETENVEXT));
80 if (fp1 != nullptr) {
81 (*fp1)(
82 &name, reinterpret_cast<uno_Environment **>(&env),
83 (OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US)
84 .getStr()),
85 target.get());
86 } else {
87 component_getImplementationEnvironmentFunc fp2
88 = reinterpret_cast<component_getImplementationEnvironmentFunc>(
89 module.getFunctionSymbol(fullPrefix + COMPONENT_GETENV));
90 if (fp2 != nullptr) {
91 (*fp2)(&name, reinterpret_cast<uno_Environment **>(&env));
92 } else {
93 name = CPPU_CURRENT_LANGUAGE_BINDING_NAME; //TODO: fail
96 if (!env.is() && name != nullptr) {
97 env = cppuhelper::detail::getEnvironment(
98 OUString::createFromAscii(name), implementation);
100 return env;
103 #endif
105 extern "C" void getFactory(va_list * args) {
106 component_getFactoryFunc fn = va_arg(*args, component_getFactoryFunc);
107 OString const * implementation = va_arg(*args, OString const *);
108 void * smgr = va_arg(*args, void *);
109 void ** factory = va_arg(*args, void **);
110 *factory = (*fn)(implementation->getStr(), smgr, nullptr);
113 css::uno::Reference<css::uno::XInterface> invokeComponentFactory(
114 css::uno::Environment const & source, css::uno::Environment const & target,
115 component_getFactoryFunc function, OUString const & uri,
116 OUString const & implementation,
117 css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager)
119 if (!(source.is() && target.is())) {
120 throw css::loader::CannotActivateFactoryException(
121 "cannot get environments",
122 css::uno::Reference<css::uno::XInterface>());
124 OString impl(
125 OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US));
126 if (source.get() == target.get()) {
127 return css::uno::Reference<css::uno::XInterface>(
128 static_cast<css::uno::XInterface *>(
129 (*function)(impl.getStr(), serviceManager.get(), nullptr)),
130 SAL_NO_ACQUIRE);
132 css::uno::Mapping mapTo(source, target);
133 css::uno::Mapping mapFrom(target, source);
134 if (!(mapTo.is() && mapFrom.is())) {
135 throw css::loader::CannotActivateFactoryException(
136 "cannot get mappings",
137 css::uno::Reference<css::uno::XInterface>());
139 void * smgr = mapTo.mapInterface(
140 serviceManager.get(),
141 cppu::UnoType<css::lang::XMultiServiceFactory>::get());
142 void * factory = nullptr;
143 target.invoke(getFactory, function, &impl, smgr, &factory);
144 if (smgr != nullptr) {
145 (*target.get()->pExtEnv->releaseInterface)(
146 target.get()->pExtEnv, smgr);
148 if (factory == nullptr) {
149 throw css::loader::CannotActivateFactoryException(
150 ("calling factory function for \"" + implementation + "\" in <"
151 + uri + "> returned null"),
152 css::uno::Reference<css::uno::XInterface>());
154 css::uno::Reference<css::uno::XInterface> res;
155 mapFrom.mapInterface(
156 reinterpret_cast<void **>(&res), factory,
157 cppu::UnoType<css::uno::XInterface>::get());
158 (*target.get()->pExtEnv->releaseInterface)(
159 target.get()->pExtEnv, factory);
160 return res;
163 #if !defined DISABLE_DYNLOADING
165 extern "C" void getInstance(va_list * args) {
166 cppuhelper::ImplementationConstructorFn * fn = va_arg(*args, cppuhelper::ImplementationConstructorFn *);
167 void * ctxt = va_arg(*args, void *);
168 assert(ctxt);
169 void * argseq = va_arg(*args, void *);
170 assert(argseq);
171 void ** instance = va_arg(*args, void **);
172 assert(instance);
173 assert(*instance == nullptr);
174 *instance = (*fn)(static_cast<css::uno::XComponentContext*>(ctxt),
175 *static_cast<css::uno::Sequence<css::uno::Any> const*>(argseq));
178 cppuhelper::WrapperConstructorFn mapConstructorFn(
179 css::uno::Environment const & source, css::uno::Environment const & target,
180 cppuhelper::ImplementationConstructorFn *const constructorFunction)
182 if (!(source.is() && target.is())) {
183 throw css::loader::CannotActivateFactoryException(
184 "cannot get environments",
185 css::uno::Reference<css::uno::XInterface>());
187 if (source.get() == target.get()) {
188 return cppuhelper::WrapperConstructorFn(constructorFunction);
190 // note: it should be valid to capture these mappings because they are
191 // ref-counted, and the returned closure will always be invoked in the
192 // "source" environment
193 css::uno::Mapping mapTo(source, target);
194 css::uno::Mapping mapFrom(target, source);
195 if (!(mapTo.is() && mapFrom.is())) {
196 throw css::loader::CannotActivateFactoryException(
197 "cannot get mappings",
198 css::uno::Reference<css::uno::XInterface>());
200 return [mapFrom, mapTo, target, constructorFunction]
201 (css::uno::XComponentContext *const context, css::uno::Sequence<css::uno::Any> const& args)
203 void *const ctxt = mapTo.mapInterface(
204 context,
205 cppu::UnoType<css::uno::XComponentContext>::get());
206 if (args.hasElements()) {
207 std::abort(); // TODO map args
209 void * instance = nullptr;
210 target.invoke(getInstance, constructorFunction, ctxt, &args, &instance);
211 if (ctxt != nullptr) {
212 (*target.get()->pExtEnv->releaseInterface)(
213 target.get()->pExtEnv, ctxt);
215 css::uno::XInterface * res = nullptr;
216 if (instance == nullptr) {
217 return res;
219 mapFrom.mapInterface(
220 reinterpret_cast<void **>(&res), instance,
221 cppu::UnoType<css::uno::XInterface>::get());
222 (*target.get()->pExtEnv->releaseInterface)(
223 target.get()->pExtEnv, instance);
224 return res;
228 #endif
232 void cppuhelper::detail::loadSharedLibComponentFactory(
233 OUString const & uri, OUString const & environment,
234 OUString const & prefix, OUString const & implementation,
235 OUString const & constructor,
236 css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager,
237 WrapperConstructorFn * constructorFunction,
238 css::uno::Reference<css::uno::XInterface> * factory)
240 assert(constructor.isEmpty() || !environment.isEmpty());
241 assert(
242 (constructorFunction == nullptr && constructor.isEmpty())
243 || !*constructorFunction);
244 assert(factory != nullptr && !factory->is());
245 #if defined DISABLE_DYNLOADING
246 assert(!environment.isEmpty());
247 if (constructor.isEmpty()) {
248 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
249 css::uno::Environment env(getEnvironment(environment, implementation));
250 if (!(curEnv.is() && env.is())) {
251 throw css::loader::CannotActivateFactoryException(
252 "cannot get environments",
253 css::uno::Reference<css::uno::XInterface>());
255 if (curEnv.get() != env.get()) {
256 std::abort();//TODO
258 OUString name(prefix == "direct" ? implementation : uri);
259 SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
260 lib_to_factory_mapping const * map = lo_get_factory_map();
261 component_getFactoryFunc fp = 0;
262 for (int i = 0; map[i].name != 0; ++i) {
263 if (name.equalsAscii(map[i].name)) {
264 fp = map[i].component_getFactory_function;
265 break;
268 if (fp == 0) {
269 SAL_WARN("cppuhelper", "unknown factory name \"" << name << "\"");
270 throw css::loader::CannotActivateFactoryException(
271 "unknown factory name \"" + name + "\"",
272 css::uno::Reference<css::uno::XInterface>());
274 *factory = invokeComponentFactory(
275 css::uno::Environment::getCurrent(),
276 getEnvironment(environment, implementation), fp, uri,
277 implementation, serviceManager);
278 } else {
279 SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
280 lib_to_constructor_mapping const * map = lo_get_constructor_map();
281 for (int i = 0; map[i].name != 0; ++i) {
282 if (constructor.equalsAscii(map[i].name)) {
283 *constructorFunction
284 = reinterpret_cast<ImplementationConstructorFn *>(
285 map[i].constructor_function);
286 return;
289 SAL_WARN("cppuhelper", "unknown constructor name \"" << constructor << "\"");
290 throw css::loader::CannotActivateFactoryException(
291 "unknown constructor name \"" + constructor + "\"",
292 css::uno::Reference<css::uno::XInterface>());
294 #else
295 osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
296 if (!mod.is()) {
297 throw css::loader::CannotActivateFactoryException(
298 "loading component library <" + uri + "> failed",
299 css::uno::Reference<css::uno::XInterface>());
301 if (constructor.isEmpty()) {
302 OUString sym;
303 SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
304 if (prefix == "direct") {
305 sym = implementation.replace('.', '_') + "_" COMPONENT_GETFACTORY;
306 } else if (!prefix.isEmpty()) {
307 sym = prefix + "_" COMPONENT_GETFACTORY;
308 } else {
309 sym = COMPONENT_GETFACTORY;
311 oslGenericFunction fp = mod.getFunctionSymbol(sym);
312 if (fp == nullptr) {
313 throw css::loader::CannotActivateFactoryException(
314 ("no factory symbol \"" + sym + "\" in component library <"
315 + uri + ">"),
316 css::uno::Reference<css::uno::XInterface>());
318 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
319 *factory = invokeComponentFactory(
320 curEnv,
321 (environment.isEmpty()
322 ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
323 : getEnvironment(environment, implementation)),
324 reinterpret_cast<component_getFactoryFunc>(fp), uri, implementation,
325 serviceManager);
326 } else {
327 SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
328 oslGenericFunction fp = mod.getFunctionSymbol(constructor);
329 if (fp == nullptr) {
330 throw css::loader::CannotActivateFactoryException(
331 ("no constructor symbol \"" + constructor
332 + "\" in component library <" + uri + ">"),
333 css::uno::Reference<css::uno::XInterface>());
335 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
336 *constructorFunction = mapConstructorFn(
337 curEnv,
338 (environment.isEmpty()
339 ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
340 : getEnvironment(environment, implementation)),
341 reinterpret_cast<ImplementationConstructorFn *>(fp));
343 mod.release();
344 #endif
347 css::uno::Reference<css::uno::XInterface> cppu::loadSharedLibComponentFactory(
348 OUString const & uri, OUString const & rPath,
349 OUString const & rImplName,
350 css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
351 css::uno::Reference<css::registry::XRegistryKey> const & xKey)
353 assert(rPath.isEmpty()); (void) rPath;
354 assert(!xKey.is()); (void) xKey;
355 css::uno::Reference<css::uno::XInterface> fac;
356 cppuhelper::detail::loadSharedLibComponentFactory(
357 uri, "", "", rImplName, "", xMgr, nullptr, &fac);
358 return fac;
361 #if !defined DISABLE_DYNLOADING
363 namespace {
365 extern "C" void writeInfo(va_list * args) {
366 component_writeInfoFunc fn = va_arg(*args, component_writeInfoFunc);
367 void * smgr = va_arg(*args, void *);
368 void * key = va_arg(*args, void *);
369 sal_Bool * ok = va_arg(*args, sal_Bool *);
370 *ok = (*fn)(smgr, key);
375 void cppu::writeSharedLibComponentInfo(
376 OUString const & uri, OUString const & rPath,
377 css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
378 css::uno::Reference<css::registry::XRegistryKey> const & xKey)
380 assert(rPath.isEmpty()); (void) rPath;
381 osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
382 if (!mod.is()) {
383 throw css::registry::CannotRegisterImplementationException(
384 "loading component library <" + uri + "> failed",
385 css::uno::Reference<css::uno::XInterface>());
387 oslGenericFunction fp = mod.getFunctionSymbol(COMPONENT_WRITEINFO);
388 if (fp == nullptr) {
389 throw css::registry::CannotRegisterImplementationException(
390 ("no symbol \"" COMPONENT_WRITEINFO "\" in component library <"
391 + uri + ">"),
392 css::uno::Reference<css::uno::XInterface>());
394 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
395 css::uno::Environment env(getEnvironmentFromModule(mod, curEnv, "", ""));
396 if (!(curEnv.is() && env.is())) {
397 throw css::registry::CannotRegisterImplementationException(
398 "cannot get environments",
399 css::uno::Reference<css::uno::XInterface>());
401 css::uno::Mapping map(curEnv, env);
402 if (!map.is()) {
403 throw css::registry::CannotRegisterImplementationException(
404 "cannot get mapping", css::uno::Reference<css::uno::XInterface>());
406 void * smgr = map.mapInterface(
407 xMgr.get(), cppu::UnoType<css::lang::XMultiServiceFactory>::get());
408 void * key = map.mapInterface(
409 xKey.get(), cppu::UnoType<css::registry::XRegistryKey>::get());
410 sal_Bool ok;
411 env.invoke(writeInfo, fp, smgr, key, &ok);
412 (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, key);
413 if (smgr != nullptr) {
414 (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, smgr);
416 if (!ok) {
417 throw css::registry::CannotRegisterImplementationException(
418 ("calling \"" COMPONENT_WRITEINFO "\" in component library <" + uri
419 + "> returned false"),
420 css::uno::Reference<css::uno::XInterface>());
424 #endif
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */