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 "bridges/cpp_uno/shared/bridge.hxx"
21
22#include "com/sun/star/uno/Reference.hxx"
23#include "com/sun/star/uno/RuntimeException.hpp"
24#include "com/sun/star/uno/XInterface.hpp"
25#include "osl/diagnose.h"
26#include "osl/mutex.hxx"
27#include "osl/time.h"
28#include "rtl/process.h"
29#include "rtl/ustrbuf.hxx"
30#include "rtl/ustring.h"
31#include "rtl/ustring.hxx"
32#include "sal/types.h"
33#include "uno/environment.h"
34#include "uno/lbnames.h"
35#include "uno/mapping.h"
36#include "cppu/EnvDcp.hxx"
37
38namespace bridges { namespace cpp_uno { namespace shared {
39
40} } }
41
42namespace {
43
44#if (defined(__GNUC__) && defined(__APPLE__))
45static OUString * s_pStaticOidPart = 0;
46#endif
47
48const OUString & SAL_CALL cppu_cppenv_getStaticOIdPart() SAL_THROW(())
49{
50#if ! (defined(__GNUC__) && defined(__APPLE__))
51 static OUString * s_pStaticOidPart = 0;
52#endif
53 if (! s_pStaticOidPart)
54 {
55 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
56 if (! s_pStaticOidPart)
57 {
58 OUStringBuffer aRet( 64 );
59 aRet.append( "];" );
60 // good guid
61 sal_uInt8 ar[16];
62 ::rtl_getGlobalProcessId( ar );
63 for ( sal_Int32 i = 0; i < 16; ++i )
64 {
65 aRet.append( (sal_Int32)ar[i], 16 );
66 }
67#if (defined(__GNUC__) && defined(__APPLE__))
68 s_pStaticOidPart = new OUString( aRet.makeStringAndClear() );
69#else
70 static OUString s_aStaticOidPart(
71 aRet.makeStringAndClear() );
72 s_pStaticOidPart = &s_aStaticOidPart;
73#endif
74 }
75 }
76 return *s_pStaticOidPart;
77}
78
79}
80
81extern "C" {
82
83static void s_stub_computeObjectIdentifier(va_list * pParam)
84 SAL_THROW(())
85{
86 uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
87 rtl_uString ** ppOId = va_arg(*pParam, rtl_uString **);
88 void * pInterface = va_arg(*pParam, void *);
89
90
91 OSL_ENSURE( pEnv && ppOId && pInterface, "### null ptr!" );
92 if (pEnv && ppOId && pInterface)
93 {
94 if (*ppOId)
95 {
96 rtl_uString_release( *ppOId );
97 *ppOId = 0;
98 }
99
100 try
101 {
102 ::com::sun::star::uno::Reference<
103 ::com::sun::star::uno::XInterface > xHome(
104 reinterpret_cast< ::com::sun::star::uno::XInterface * >(
105 pInterface ),
106 ::com::sun::star::uno::UNO_QUERY );
107 OSL_ENSURE( xHome.is(), "### query to XInterface failed!" );
108 if (xHome.is())
109 {
110 // interface
111 OUStringBuffer oid( 64 );
112 oid.append( reinterpret_cast< sal_Int64 >(xHome.get()), 16 );
113 oid.append( ';' );
114 // ;environment[context]
115 oid.append(
116 *reinterpret_cast< OUString const * >(
117 &((uno_Environment *) pEnv)->pTypeName ) );
118 oid.append( '[' );
119 oid.append(
120 reinterpret_cast< sal_Int64 >(
121 ((uno_Environment *)pEnv)->pContext),
122 16 );
123 // ];good guid
124 oid.append( cppu_cppenv_getStaticOIdPart() );
125 OUString aRet( oid.makeStringAndClear() );
126 ::rtl_uString_acquire( *ppOId = aRet.pData );
127 }
128 }
129 catch (const ::com::sun::star::uno::RuntimeException &)
130 {
131 OSL_FAIL(
132 "### RuntimeException occurred during queryInterface()!" );
133 }
134 }
135}
136
137static void SAL_CALL computeObjectIdentifier(
138 uno_ExtEnvironment * pExtEnv, rtl_uString ** ppOId, void * pInterface )
139 SAL_THROW(())
140{
141 uno_Environment_invoke(&pExtEnv->aBase, s_stub_computeObjectIdentifier, pExtEnv, ppOId, pInterface);
142}
143
144static void s_stub_acquireInterface(va_list * pParam)
145 SAL_THROW(())
146{
147 /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
148 void * pCppI = va_arg(*pParam, void *);
149
150 reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->acquire();
151}
152
153static void SAL_CALL acquireInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
154 SAL_THROW(())
155{
156 uno_Environment_invoke(&pExtEnv->aBase, s_stub_acquireInterface, pExtEnv, pCppI);
157}
158
159static void s_stub_releaseInterface(va_list * pParam)
160 SAL_THROW(())
161{
162 /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
163 void * pCppI = va_arg(*pParam, void *);
164
165 reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->release();
166}
167
168static void SAL_CALL releaseInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
169 SAL_THROW(())
170{
171 uno_Environment_invoke(&pExtEnv->aBase, s_stub_releaseInterface, pExtEnv, pCppI);
172}
173
174static void SAL_CALL environmentDisposing(
175 SAL_UNUSED_PARAMETER uno_Environment * ) SAL_THROW(())
176{
177}
178
179#ifdef DISABLE_DYNLOADING
180#define uno_initEnvironment CPPU_ENV_uno_initEnvironment
181#endif
182
183SAL_DLLPUBLIC_EXPORT void SAL_CALL uno_initEnvironment(uno_Environment * pCppEnv)
184 SAL_THROW_EXTERN_C()
185{
186 OSL_ENSURE( pCppEnv->pExtEnv, "### expected extended environment!" );
187 OSL_ENSURE(
188 ::rtl_ustr_ascii_compare_WithLength(
189 pCppEnv->pTypeName->buffer, rtl_str_getLength(CPPU_CURRENT_LANGUAGE_BINDING_NAME), CPPU_CURRENT_LANGUAGE_BINDING_NAME )
190 == 0,
191 "### wrong environment type!" );
192 ((uno_ExtEnvironment *)pCppEnv)->computeObjectIdentifier
193 = computeObjectIdentifier;
194 ((uno_ExtEnvironment *)pCppEnv)->acquireInterface = acquireInterface;
195 ((uno_ExtEnvironment *)pCppEnv)->releaseInterface = releaseInterface;
196 pCppEnv->environmentDisposing = environmentDisposing;
197}
198
199#ifdef DISABLE_DYNLOADING
200#define uno_ext_getMapping CPPU_ENV_uno_ext_getMapping
201#endif
202
203SAL_DLLPUBLIC_EXPORT void SAL_CALL uno_ext_getMapping(
204 uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo)
205 SAL_THROW_EXTERN_C()
206{
207 OSL_ASSERT( ppMapping && pFrom && pTo );
208 if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
209 {
210 uno_Mapping * pMapping = 0;
211
212 OUString from_envTypeName(cppu::EnvDcp::getTypeName(pFrom->pTypeName));
213 OUString to_envTypeName(cppu::EnvDcp::getTypeName(pTo->pTypeName));
214
215 if (0 == rtl_ustr_ascii_compare(
216 from_envTypeName.pData->buffer,
217 CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
218 0 == rtl_ustr_ascii_compare(
219 to_envTypeName.pData->buffer, UNO_LB_UNO ))
220 {
221 // ref count initially 1
222 pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
223 pFrom->pExtEnv, pTo->pExtEnv, true );
224 ::uno_registerMapping(
225 &pMapping, bridges::cpp_uno::shared::freeMapping,
226 (uno_Environment *)pFrom->pExtEnv,
227 (uno_Environment *)pTo->pExtEnv, 0 );
228 }
229 else if (0 == rtl_ustr_ascii_compare(
230 to_envTypeName.pData->buffer,
231 CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
232 0 == rtl_ustr_ascii_compare(
233 from_envTypeName.pData->buffer, UNO_LB_UNO ))
234 {
235 // ref count initially 1
236 pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
237 pTo->pExtEnv, pFrom->pExtEnv, false );
238 ::uno_registerMapping(
239 &pMapping, bridges::cpp_uno::shared::freeMapping,
240 (uno_Environment *)pFrom->pExtEnv,
241 (uno_Environment *)pTo->pExtEnv, 0 );
242 }
243
244 if (*ppMapping)
245 {
246 (*(*ppMapping)->release)( *ppMapping );
247 }
248 if (pMapping)
249 *ppMapping = pMapping;
250 }
251}
252
253}
254
255/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
256