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