nss: upgrade to release 3.73
[LibreOffice.git] / cppuhelper / source / shlib.cxx
blob89ce8b8554db8c6403884204de129a073fd3f091
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 SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
259 lib_to_factory_mapping const * map = lo_get_factory_map();
260 component_getFactoryFunc fp = 0;
261 for (int i = 0; map[i].name != 0; ++i) {
262 if (uri.equalsAscii(map[i].name)) {
263 fp = map[i].component_getFactory_function;
264 break;
267 if (fp == 0) {
268 SAL_WARN("cppuhelper", "unknown factory name \"" << uri << "\"");
269 throw css::loader::CannotActivateFactoryException(
270 "unknown factory name \"" + uri + "\"",
271 css::uno::Reference<css::uno::XInterface>());
273 *factory = invokeComponentFactory(
274 css::uno::Environment::getCurrent(),
275 getEnvironment(environment, implementation), fp, uri,
276 implementation, serviceManager);
277 } else {
278 SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
279 lib_to_constructor_mapping const * map = lo_get_constructor_map();
280 for (int i = 0; map[i].name != 0; ++i) {
281 if (constructor.equalsAscii(map[i].name)) {
282 *constructorFunction
283 = reinterpret_cast<ImplementationConstructorFn *>(
284 map[i].constructor_function);
285 return;
288 SAL_WARN("cppuhelper", "unknown constructor name \"" << constructor << "\"");
289 throw css::loader::CannotActivateFactoryException(
290 "unknown constructor name \"" + constructor + "\"",
291 css::uno::Reference<css::uno::XInterface>());
293 #else
294 osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
295 if (!mod.is()) {
296 throw css::loader::CannotActivateFactoryException(
297 "loading component library <" + uri + "> failed",
298 css::uno::Reference<css::uno::XInterface>());
300 if (constructor.isEmpty()) {
301 OUString sym;
302 SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
303 if (!prefix.isEmpty()) {
304 sym = prefix + "_" COMPONENT_GETFACTORY;
305 } else {
306 sym = COMPONENT_GETFACTORY;
308 oslGenericFunction fp = mod.getFunctionSymbol(sym);
309 if (fp == nullptr) {
310 throw css::loader::CannotActivateFactoryException(
311 ("no factory symbol \"" + sym + "\" in component library <"
312 + uri + ">"),
313 css::uno::Reference<css::uno::XInterface>());
315 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
316 *factory = invokeComponentFactory(
317 curEnv,
318 (environment.isEmpty()
319 ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
320 : getEnvironment(environment, implementation)),
321 reinterpret_cast<component_getFactoryFunc>(fp), uri, implementation,
322 serviceManager);
323 } else {
324 SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
325 oslGenericFunction fp = mod.getFunctionSymbol(constructor);
326 if (fp == nullptr) {
327 throw css::loader::CannotActivateFactoryException(
328 ("no constructor symbol \"" + constructor
329 + "\" in component library <" + uri + ">"),
330 css::uno::Reference<css::uno::XInterface>());
332 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
333 *constructorFunction = mapConstructorFn(
334 curEnv,
335 (environment.isEmpty()
336 ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
337 : getEnvironment(environment, implementation)),
338 reinterpret_cast<ImplementationConstructorFn *>(fp));
340 mod.release();
341 #endif
344 css::uno::Reference<css::uno::XInterface> cppu::loadSharedLibComponentFactory(
345 OUString const & uri, OUString const & rPath,
346 OUString const & rImplName,
347 css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
348 css::uno::Reference<css::registry::XRegistryKey> const & xKey)
350 assert(rPath.isEmpty()); (void) rPath;
351 assert(!xKey.is()); (void) xKey;
352 css::uno::Reference<css::uno::XInterface> fac;
353 cppuhelper::detail::loadSharedLibComponentFactory(
354 uri, "", "", rImplName, "", xMgr, nullptr, &fac);
355 return fac;
358 #if !defined DISABLE_DYNLOADING
360 namespace {
362 extern "C" void writeInfo(va_list * args) {
363 component_writeInfoFunc fn = va_arg(*args, component_writeInfoFunc);
364 void * smgr = va_arg(*args, void *);
365 void * key = va_arg(*args, void *);
366 sal_Bool * ok = va_arg(*args, sal_Bool *);
367 *ok = (*fn)(smgr, key);
372 void cppu::writeSharedLibComponentInfo(
373 OUString const & uri, OUString const & rPath,
374 css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
375 css::uno::Reference<css::registry::XRegistryKey> const & xKey)
377 assert(rPath.isEmpty()); (void) rPath;
378 osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
379 if (!mod.is()) {
380 throw css::registry::CannotRegisterImplementationException(
381 "loading component library <" + uri + "> failed",
382 css::uno::Reference<css::uno::XInterface>());
384 oslGenericFunction fp = mod.getFunctionSymbol(COMPONENT_WRITEINFO);
385 if (fp == nullptr) {
386 throw css::registry::CannotRegisterImplementationException(
387 ("no symbol \"" COMPONENT_WRITEINFO "\" in component library <"
388 + uri + ">"),
389 css::uno::Reference<css::uno::XInterface>());
391 css::uno::Environment curEnv(css::uno::Environment::getCurrent());
392 css::uno::Environment env(getEnvironmentFromModule(mod, curEnv, "", ""));
393 if (!(curEnv.is() && env.is())) {
394 throw css::registry::CannotRegisterImplementationException(
395 "cannot get environments",
396 css::uno::Reference<css::uno::XInterface>());
398 css::uno::Mapping map(curEnv, env);
399 if (!map.is()) {
400 throw css::registry::CannotRegisterImplementationException(
401 "cannot get mapping", css::uno::Reference<css::uno::XInterface>());
403 void * smgr = map.mapInterface(
404 xMgr.get(), cppu::UnoType<css::lang::XMultiServiceFactory>::get());
405 void * key = map.mapInterface(
406 xKey.get(), cppu::UnoType<css::registry::XRegistryKey>::get());
407 sal_Bool ok;
408 env.invoke(writeInfo, fp, smgr, key, &ok);
409 (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, key);
410 if (smgr != nullptr) {
411 (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, smgr);
413 if (!ok) {
414 throw css::registry::CannotRegisterImplementationException(
415 ("calling \"" COMPONENT_WRITEINFO "\" in component library <" + uri
416 + "> returned false"),
417 css::uno::Reference<css::uno::XInterface>());
421 #endif
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */