Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <cassert>
23 : #include <cstdlib>
24 :
25 : #include <com/sun/star/loader/CannotActivateFactoryException.hpp>
26 : #include <cppuhelper/factory.hxx>
27 : #include <cppuhelper/shlib.hxx>
28 : #include <osl/module.hxx>
29 : #include <uno/environment.hxx>
30 : #include <uno/mapping.hxx>
31 :
32 : #include "loadsharedlibcomponentfactory.hxx"
33 :
34 : #if defined DISABLE_DYNLOADING
35 : #include <osl/detail/component-mapping.h>
36 : #endif
37 :
38 23338 : css::uno::Environment cppuhelper::detail::getEnvironment(
39 : rtl::OUString const & name, rtl::OUString const & implementation)
40 : {
41 : assert(!implementation.isEmpty());
42 23338 : rtl::OUString n(name);
43 23338 : static char const * log = std::getenv("UNO_ENV_LOG");
44 23338 : if (log != 0 && *log != 0) {
45 0 : rtl::OString imps(log);
46 0 : for (sal_Int32 i = 0; i != -1;) {
47 0 : rtl::OString imp(imps.getToken(0, ';', i));
48 : //TODO: this assumes UNO_ENV_LOG only contains ASCII characters:
49 0 : if (implementation.equalsAsciiL(imp.getStr(), imp.getLength())) {
50 0 : n += ":log";
51 0 : break;
52 : }
53 0 : }
54 : }
55 23338 : return css::uno::Environment(n);
56 : }
57 :
58 : namespace {
59 :
60 : #if !defined DISABLE_DYNLOADING
61 :
62 0 : css::uno::Environment getEnvironmentFromModule(
63 : osl::Module const & module, css::uno::Environment const & target,
64 : rtl::OUString const & implementation, rtl::OUString const & prefix)
65 : {
66 0 : char const * name = 0;
67 0 : css::uno::Environment env;
68 0 : rtl::OUString fullPrefix(prefix);
69 0 : if (!fullPrefix.isEmpty()) {
70 0 : fullPrefix += "_";
71 : }
72 : component_getImplementationEnvironmentExtFunc fp1
73 : = reinterpret_cast<component_getImplementationEnvironmentExtFunc>(
74 0 : module.getFunctionSymbol(fullPrefix + COMPONENT_GETENVEXT));
75 0 : if (fp1 != 0) {
76 : (*fp1)(
77 : &name, reinterpret_cast<uno_Environment **>(&env),
78 : (rtl::OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US)
79 : .getStr()),
80 0 : target.get());
81 : } else {
82 : component_getImplementationEnvironmentFunc fp2
83 : = reinterpret_cast<component_getImplementationEnvironmentFunc>(
84 0 : module.getFunctionSymbol(fullPrefix + COMPONENT_GETENV));
85 0 : if (fp2 != 0) {
86 0 : (*fp2)(&name, reinterpret_cast<uno_Environment **>(&env));
87 : } else {
88 0 : name = CPPU_CURRENT_LANGUAGE_BINDING_NAME; //TODO: fail
89 : }
90 : }
91 0 : if (!env.is() && name != 0) {
92 0 : env = cppuhelper::detail::getEnvironment(
93 0 : rtl::OUString::createFromAscii(name), implementation);
94 : }
95 0 : return env;
96 : }
97 :
98 : #endif
99 :
100 4 : extern "C" void getFactory(va_list * args) {
101 4 : component_getFactoryFunc fn = va_arg(*args, component_getFactoryFunc);
102 4 : rtl::OString const * implementation = va_arg(*args, rtl::OString const *);
103 4 : void * smgr = va_arg(*args, void *);
104 4 : void * key = va_arg(*args, void *);
105 4 : void ** factory = va_arg(*args, void **);
106 4 : *factory = (*fn)(implementation->getStr(), smgr, key);
107 4 : }
108 :
109 13660 : css::uno::Reference<css::uno::XInterface> invokeComponentFactory(
110 : css::uno::Environment const & source, css::uno::Environment const & target,
111 : component_getFactoryFunc function, rtl::OUString const & uri,
112 : rtl::OUString const & implementation,
113 : css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager)
114 : {
115 13660 : if (!(source.is() && target.is())) {
116 : throw css::loader::CannotActivateFactoryException(
117 : "cannot get environments",
118 0 : css::uno::Reference<css::uno::XInterface>());
119 : }
120 : rtl::OString impl(
121 13660 : rtl::OUStringToOString(implementation, RTL_TEXTENCODING_ASCII_US));
122 13660 : if (source.get() == target.get()) {
123 : return css::uno::Reference<css::uno::XInterface>(
124 : static_cast<css::uno::XInterface *>(
125 13656 : (*function)(impl.getStr(), serviceManager.get(), 0)),
126 13656 : SAL_NO_ACQUIRE);
127 : } else {
128 4 : css::uno::Mapping mapTo(source, target);
129 8 : css::uno::Mapping mapFrom(target, source);
130 4 : if (!(mapTo.is() && mapFrom.is())) {
131 : throw css::loader::CannotActivateFactoryException(
132 : "cannot get mappings",
133 0 : css::uno::Reference<css::uno::XInterface>());
134 : }
135 : void * smgr = mapTo.mapInterface(
136 4 : serviceManager.get(),
137 8 : cppu::UnoType<css::lang::XMultiServiceFactory>::get());
138 4 : void * factory = 0;
139 4 : target.invoke(getFactory, function, &impl, smgr, 0, &factory);
140 4 : if (smgr != 0) {
141 4 : (*target.get()->pExtEnv->releaseInterface)(
142 4 : target.get()->pExtEnv, smgr);
143 : }
144 4 : if (factory == 0) {
145 : throw css::loader::CannotActivateFactoryException(
146 0 : ("calling factory function for \"" + implementation + "\" in <"
147 0 : + uri + "> returned null"),
148 0 : css::uno::Reference<css::uno::XInterface>());
149 : }
150 8 : css::uno::Reference<css::uno::XInterface> res;
151 : mapFrom.mapInterface(
152 : reinterpret_cast<void **>(&res), factory,
153 4 : cppu::UnoType<css::uno::XInterface>::get());
154 4 : (*target.get()->pExtEnv->releaseInterface)(
155 4 : target.get()->pExtEnv, factory);
156 8 : return res;
157 13660 : }
158 : }
159 :
160 : }
161 :
162 23338 : void cppuhelper::detail::loadSharedLibComponentFactory(
163 : rtl::OUString const & uri, rtl::OUString const & environment,
164 : rtl::OUString const & prefix, rtl::OUString const & implementation,
165 : rtl::OUString const & constructor,
166 : css::uno::Reference<css::lang::XMultiServiceFactory> const & serviceManager,
167 : ImplementationConstructorFn ** constructorFunction,
168 : css::uno::Reference<css::uno::XInterface> * factory)
169 : {
170 : assert(constructor.isEmpty() || !environment.isEmpty());
171 : assert(
172 : (constructorFunction == 0 && constructor.isEmpty())
173 : || *constructorFunction == 0);
174 : assert(factory != 0 && !factory->is());
175 : #if defined DISABLE_DYNLOADING
176 : assert(!environment.isEmpty());
177 : if (constructor.isEmpty()) {
178 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
179 : css::uno::Environment env(getEnvironment(environment, implementation));
180 : if (!(curEnv.is() && env.is())) {
181 : throw css::loader::CannotActivateFactoryException(
182 : "cannot get environments",
183 : css::uno::Reference<css::uno::XInterface>());
184 : }
185 : if (curEnv.get() != env.get()) {
186 : std::abort();//TODO
187 : }
188 : rtl::OUString name(prefix == "direct" ? implementation : uri);
189 : SAL_INFO("cppuhelper.shlib", "prefix=" << prefix << " implementation=" << implementation << " uri=" << uri);
190 : lib_to_factory_mapping const * map = lo_get_factory_map();
191 : component_getFactoryFunc fp = 0;
192 : for (int i = 0; map[i].name != 0; ++i) {
193 : if (name.equalsAscii(map[i].name)) {
194 : fp = map[i].component_getFactory_function;
195 : break;
196 : }
197 : }
198 : if (fp == 0) {
199 : SAL_WARN("cppuhelper", "unknown factory name \"" << name << "\"");
200 : #if defined IOS && !defined SAL_LOG_WARN
201 : // If the above SAL_WARN expanded to nothing, print to stderr...
202 : fprintf(stderr, "Unknown factory name %s\n", OUStringToOString(name, RTL_TEXTENCODING_UTF8).getStr());
203 : #endif
204 : throw css::loader::CannotActivateFactoryException(
205 : "unknown factory name \"" + name + "\"",
206 : css::uno::Reference<css::uno::XInterface>());
207 : }
208 : *factory = invokeComponentFactory(
209 : css::uno::Environment::getCurrent(),
210 : getEnvironment(environment, implementation), fp, uri,
211 : implementation, serviceManager);
212 : } else {
213 : SAL_INFO("cppuhelper.shlib", "constructor=" << constructor);
214 : lib_to_constructor_mapping const * map = lo_get_constructor_map();
215 : for (int i = 0; map[i].name != 0; ++i) {
216 : if (constructor.equalsAscii(map[i].name)) {
217 : *constructorFunction
218 : = reinterpret_cast<ImplementationConstructorFn *>(
219 : map[i].constructor_function);
220 : return;
221 : }
222 : }
223 : SAL_WARN("cppuhelper", "unknown constructor name \"" << constructor << "\"");
224 : #if defined IOS && !defined SAL_LOG_WARN
225 : // If the above SAL_WARN expanded to nothing, print to stderr...
226 : fprintf(stderr, "Unknown constructor name %s\n", OUStringToOString(constructor, RTL_TEXTENCODING_UTF8).getStr());
227 : #endif
228 : throw css::loader::CannotActivateFactoryException(
229 : "unknown constructor name \"" + constructor + "\"",
230 : css::uno::Reference<css::uno::XInterface>());
231 : }
232 : #else
233 23338 : osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
234 23338 : if (!mod.is()) {
235 : throw css::loader::CannotActivateFactoryException(
236 0 : "loading component library <" + uri + "> failed",
237 0 : css::uno::Reference<css::uno::XInterface>());
238 : }
239 23338 : if (constructor.isEmpty()) {
240 13660 : rtl::OUString sym;
241 13660 : if (prefix == "direct") {
242 0 : sym = implementation.replace('.', '_') + "_" COMPONENT_GETFACTORY;
243 13660 : } else if (!prefix.isEmpty()) {
244 13652 : sym = prefix + "_" COMPONENT_GETFACTORY;
245 : } else {
246 8 : sym = COMPONENT_GETFACTORY;
247 : }
248 13660 : oslGenericFunction fp = mod.getFunctionSymbol(sym);
249 13660 : if (fp == 0) {
250 : throw css::loader::CannotActivateFactoryException(
251 0 : ("no factory symbol \"" + sym + "\" in component library <"
252 0 : + uri + ">"),
253 0 : css::uno::Reference<css::uno::XInterface>());
254 : }
255 27320 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
256 27320 : *factory = invokeComponentFactory(
257 : curEnv,
258 13660 : (environment.isEmpty()
259 : ? getEnvironmentFromModule(mod, curEnv, implementation, prefix)
260 : : getEnvironment(environment, implementation)),
261 : reinterpret_cast<component_getFactoryFunc>(fp), uri, implementation,
262 27320 : serviceManager);
263 : } else {
264 9678 : oslGenericFunction fp = mod.getFunctionSymbol(constructor);
265 9678 : if (fp == 0) {
266 : throw css::loader::CannotActivateFactoryException(
267 0 : ("no constructor symbol \"" + constructor
268 0 : + "\" in component library <" + uri + ">"),
269 0 : css::uno::Reference<css::uno::XInterface>());
270 : }
271 : *constructorFunction = reinterpret_cast<ImplementationConstructorFn *>(
272 9678 : fp);
273 : }
274 23338 : mod.release();
275 : #endif
276 23338 : }
277 :
278 0 : css::uno::Reference<css::uno::XInterface> cppu::loadSharedLibComponentFactory(
279 : rtl::OUString const & uri, rtl::OUString const & rPath,
280 : rtl::OUString const & rImplName,
281 : css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
282 : css::uno::Reference<css::registry::XRegistryKey> const & xKey)
283 : {
284 : assert(rPath.isEmpty()); (void) rPath;
285 : assert(!xKey.is()); (void) xKey;
286 0 : css::uno::Reference<css::uno::XInterface> fac;
287 : cppuhelper::detail::loadSharedLibComponentFactory(
288 0 : uri, "", "", rImplName, "", xMgr, 0, &fac);
289 0 : return fac;
290 : }
291 :
292 : #if !defined DISABLE_DYNLOADING
293 :
294 : namespace {
295 :
296 0 : extern "C" void writeInfo(va_list * args) {
297 0 : component_writeInfoFunc fn = va_arg(*args, component_writeInfoFunc);
298 0 : void * smgr = va_arg(*args, void *);
299 0 : void * key = va_arg(*args, void *);
300 0 : sal_Bool * ok = va_arg(*args, sal_Bool *);
301 0 : *ok = (*fn)(smgr, key);
302 0 : }
303 :
304 : }
305 :
306 0 : void cppu::writeSharedLibComponentInfo(
307 : rtl::OUString const & uri, rtl::OUString const & rPath,
308 : css::uno::Reference<css::lang::XMultiServiceFactory> const & xMgr,
309 : css::uno::Reference<css::registry::XRegistryKey> const & xKey)
310 : {
311 : assert(rPath.isEmpty()); (void) rPath;
312 0 : osl::Module mod(uri, SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
313 0 : if (!mod.is()) {
314 : throw css::registry::CannotRegisterImplementationException(
315 0 : "loading component library <" + uri + "> failed",
316 0 : css::uno::Reference<css::uno::XInterface>());
317 : }
318 0 : oslGenericFunction fp = mod.getFunctionSymbol(COMPONENT_WRITEINFO);
319 0 : if (fp == 0) {
320 : throw css::registry::CannotRegisterImplementationException(
321 : ("no symbol \"" COMPONENT_WRITEINFO "\" in component library <"
322 0 : + uri + ">"),
323 0 : css::uno::Reference<css::uno::XInterface>());
324 : }
325 0 : css::uno::Environment curEnv(css::uno::Environment::getCurrent());
326 0 : css::uno::Environment env(getEnvironmentFromModule(mod, curEnv, "", ""));
327 0 : if (!(curEnv.is() && env.is())) {
328 : throw css::registry::CannotRegisterImplementationException(
329 : "cannot get environments",
330 0 : css::uno::Reference<css::uno::XInterface>());
331 : }
332 0 : css::uno::Mapping map(curEnv, env);
333 0 : if (!map.is()) {
334 : throw css::registry::CannotRegisterImplementationException(
335 0 : "cannot get mapping", css::uno::Reference<css::uno::XInterface>());
336 : }
337 : void * smgr = map.mapInterface(
338 0 : xMgr.get(), cppu::UnoType<css::lang::XMultiServiceFactory>::get());
339 : void * key = map.mapInterface(
340 0 : xKey.get(), cppu::UnoType<css::registry::XRegistryKey>::get());
341 : sal_Bool ok;
342 0 : env.invoke(writeInfo, fp, smgr, key, &ok);
343 0 : (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, key);
344 0 : if (smgr != 0) {
345 0 : (*env.get()->pExtEnv->releaseInterface)(env.get()->pExtEnv, smgr);
346 : }
347 0 : if (!ok) {
348 : throw css::registry::CannotRegisterImplementationException(
349 0 : ("calling \"" COMPONENT_WRITEINFO "\" in component library <" + uri
350 0 : + "> returned false"),
351 0 : css::uno::Reference<css::uno::XInterface>());
352 0 : }
353 0 : }
354 :
355 : #endif
356 :
357 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|