LibreOffice Module bridges (master)  1
component.cxx
Go to the documentation of this file.
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 <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/mutex.hxx>
26 #include <osl/time.h>
27 #include <rtl/process.h>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/ustring.h>
30 #include <rtl/ustring.hxx>
31 #include <sal/log.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 
38 
39 namespace {
40 
41 const OUString & cppu_cppenv_getStaticOIdPart()
42 {
43  static OUString s_aStaticOidPart = []() {
44  OUStringBuffer aRet(64);
45  aRet.append("];");
46  // good guid
47  sal_uInt8 ar[16];
48  ::rtl_getGlobalProcessId(ar);
49  for (unsigned char i : ar)
50  {
51  aRet.append(static_cast<sal_Int32>(i), 16);
52  }
53  return aRet.makeStringAndClear();
54  }();
55  return s_aStaticOidPart;
56 }
57 
58 }
59 
60 extern "C" {
61 
62 static void s_stub_computeObjectIdentifier(va_list * pParam)
63 {
64  uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
65  rtl_uString ** ppOId = va_arg(*pParam, rtl_uString **);
66  void * pInterface = va_arg(*pParam, void *);
67 
68 
69  assert(pEnv && ppOId && pInterface);
70  if (!(pEnv && ppOId && pInterface))
71  return;
72 
73  if (*ppOId)
74  {
75  rtl_uString_release( *ppOId );
76  *ppOId = nullptr;
77  }
78 
79  try
80  {
82  ::com::sun::star::uno::XInterface > xHome(
83  static_cast< ::com::sun::star::uno::XInterface * >(
84  pInterface ),
85  ::com::sun::star::uno::UNO_QUERY );
86  assert(xHome.is() && "### query to XInterface failed!");
87  if (xHome.is())
88  {
89  // interface
90  OUString aRet =
91  OUString::number( reinterpret_cast< sal_Int64 >(xHome.get()), 16 ) +
92  ";" +
93  // ;environment[context]
94  OUString::unacquired(&pEnv->aBase.pTypeName) +
95  "[" +
96  OUString::number( reinterpret_cast< sal_Int64 >(pEnv->aBase.pContext), 16 ) +
97  // ];good guid
98  cppu_cppenv_getStaticOIdPart();
99  *ppOId = aRet.pData;
100  ::rtl_uString_acquire( *ppOId );
101  }
102  }
103  catch (const ::com::sun::star::uno::RuntimeException & e)
104  {
105  SAL_WARN("bridges",
106  "### RuntimeException occurred during queryInterface(): "
107  << e.Message);
108  }
109 }
110 
112  uno_ExtEnvironment * pExtEnv, rtl_uString ** ppOId, void * pInterface )
113 {
114  uno_Environment_invoke(&pExtEnv->aBase, s_stub_computeObjectIdentifier, pExtEnv, ppOId, pInterface);
115 }
116 
117 static void s_stub_acquireInterface(va_list * pParam)
118 {
119  /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
120  void * pCppI = va_arg(*pParam, void *);
121 
122  static_cast< ::com::sun::star::uno::XInterface * >( pCppI )->acquire();
123 }
124 
125 static void acquireInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
126 {
127  uno_Environment_invoke(&pExtEnv->aBase, s_stub_acquireInterface, pExtEnv, pCppI);
128 }
129 
130 static void s_stub_releaseInterface(va_list * pParam)
131 {
132  /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
133  void * pCppI = va_arg(*pParam, void *);
134 
135  static_cast< ::com::sun::star::uno::XInterface * >( pCppI )->release();
136 }
137 
138 static void releaseInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
139 {
140  uno_Environment_invoke(&pExtEnv->aBase, s_stub_releaseInterface, pExtEnv, pCppI);
141 }
142 
144  SAL_UNUSED_PARAMETER uno_Environment * )
145 {
146 }
147 
148 #ifdef DISABLE_DYNLOADING
149 #define uno_initEnvironment CPPU_ENV_uno_initEnvironment
150 #endif
151 
152 SAL_DLLPUBLIC_EXPORT void uno_initEnvironment(uno_Environment * pCppEnv)
154 {
155  assert(pCppEnv->pExtEnv);
156  assert(
157  ::rtl_ustr_ascii_compare_WithLength(
158  pCppEnv->pTypeName->buffer, rtl_str_getLength(CPPU_CURRENT_LANGUAGE_BINDING_NAME), CPPU_CURRENT_LANGUAGE_BINDING_NAME )
159  == 0
160  && "### wrong environment type!");
161  reinterpret_cast<uno_ExtEnvironment *>(pCppEnv)->computeObjectIdentifier
163  reinterpret_cast<uno_ExtEnvironment *>(pCppEnv)->acquireInterface = acquireInterface;
164  reinterpret_cast<uno_ExtEnvironment *>(pCppEnv)->releaseInterface = releaseInterface;
165  pCppEnv->environmentDisposing = environmentDisposing;
166 }
167 
168 #ifdef DISABLE_DYNLOADING
169 #define uno_ext_getMapping CPPU_ENV_uno_ext_getMapping
170 #endif
171 
172 SAL_DLLPUBLIC_EXPORT void uno_ext_getMapping(
173  uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo)
175 {
176  assert(ppMapping && pFrom && pTo);
177  if (!(ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv))
178  return;
179 
180  uno_Mapping * pMapping = nullptr;
181 
182  OUString from_envTypeName(cppu::EnvDcp::getTypeName(pFrom->pTypeName));
183  OUString to_envTypeName(cppu::EnvDcp::getTypeName(pTo->pTypeName));
184 
185  if (rtl_ustr_ascii_compare(
186  from_envTypeName.pData->buffer,
187  CPPU_CURRENT_LANGUAGE_BINDING_NAME ) == 0 &&
188  rtl_ustr_ascii_compare(
189  to_envTypeName.pData->buffer, UNO_LB_UNO ) == 0)
190  {
191  // ref count initially 1
193  pFrom->pExtEnv, pTo->pExtEnv, true );
194  ::uno_registerMapping(
196  &pFrom->pExtEnv->aBase,
197  &pTo->pExtEnv->aBase, nullptr );
198  }
199  else if (rtl_ustr_ascii_compare(
200  to_envTypeName.pData->buffer,
201  CPPU_CURRENT_LANGUAGE_BINDING_NAME ) == 0 &&
202  rtl_ustr_ascii_compare(
203  from_envTypeName.pData->buffer, UNO_LB_UNO ) == 0)
204  {
205  // ref count initially 1
207  pTo->pExtEnv, pFrom->pExtEnv, false );
208  ::uno_registerMapping(
210  &pFrom->pExtEnv->aBase,
211  &pTo->pExtEnv->aBase, nullptr );
212  }
213 
214  if (*ppMapping)
215  {
216  (*(*ppMapping)->release)( *ppMapping );
217  }
218  if (pMapping)
219  *ppMapping = pMapping;
220 }
221 
222 }
223 
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _uno_Environment uno_Environment
SAL_DLLPUBLIC_EXPORT void uno_ext_getMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo) SAL_THROW_EXTERN_C()
Definition: component.cxx:172
struct _uno_Mapping uno_Mapping
Definition: msvc/except.hxx:32
static void acquireInterface(uno_ExtEnvironment *pExtEnv, void *pCppI)
Definition: component.cxx:125
rtl::OUString getTypeName(rtl::OUString const &rEnvDcp)
uno_Mapping * pMapping
#define SAL_THROW_EXTERN_C()
SAL_DLLPUBLIC_EXPORT void uno_initEnvironment(uno_Environment *pCppEnv) SAL_THROW_EXTERN_C()
Definition: component.cxx:152
static uno_Mapping * createMapping(uno_ExtEnvironment *pCppEnv, uno_ExtEnvironment *pUnoEnv, bool bExportCpp2Uno)
Definition: bridge.cxx:142
static void releaseInterface(uno_ExtEnvironment *pExtEnv, void *pCppI)
Definition: component.cxx:138
unsigned char sal_uInt8
static void s_stub_computeObjectIdentifier(va_list *pParam)
Definition: component.cxx:62
static void s_stub_releaseInterface(va_list *pParam)
Definition: component.cxx:130
static void s_stub_acquireInterface(va_list *pParam)
Definition: component.cxx:117
void freeMapping(uno_Mapping *pMapping)
Definition: bridge.cxx:36
#define SAL_WARN(area, stream)
static void computeObjectIdentifier(uno_ExtEnvironment *pExtEnv, rtl_uString **ppOId, void *pInterface)
Definition: component.cxx:111
void SAL_CALL uno_Environment_invoke(uno_Environment *pEnv, uno_EnvCallee *pCallee,...) SAL_THROW_EXTERN_C()
static void environmentDisposing(SAL_UNUSED_PARAMETER uno_Environment *)
Definition: component.cxx:143