Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cppu / source / uno / cascade_mapping.cxx
blobf03d88e529d613f2d1a478c34075e8e6537cfd52
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 <osl/interlck.h>
21 #include <rtl/ustring.hxx>
22 #include <uno/environment.hxx>
23 #include <uno/lbnames.h>
24 #include <uno/mapping.hxx>
25 #include <uno/dispatcher.h>
26 #include <o3tl/string_view.hxx>
28 #include <cppu/EnvDcp.hxx>
30 #include "cascade_mapping.hxx"
32 using namespace com::sun::star;
34 namespace {
36 class MediatorMapping : public uno_Mapping
38 oslInterlockedCount m_refCount;
40 uno::Mapping m_from2uno;
41 uno::Mapping m_uno2to;
43 uno::Environment m_from;
44 uno::Environment m_interm;
45 uno::Environment m_to;
47 public:
48 void acquire();
49 void release();
51 void mapInterface(void ** ppOut,
52 void * pInterface,
53 typelib_InterfaceTypeDescription * pInterfaceTypeDescr);
54 MediatorMapping(uno_Environment * pFrom,
55 uno_Environment * pInterm,
56 uno_Environment * pTo);
61 extern "C" {
62 static void s_acquire(uno_Mapping * mapping)
64 MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
65 pMediatorMapping->acquire();
68 static void s_release(uno_Mapping * mapping)
70 MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
71 pMediatorMapping->release();
74 static void s_mapInterface(
75 uno_Mapping * mapping,
76 void ** ppOut,
77 void * pInterface,
78 typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
80 MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
81 pMediatorMapping->mapInterface(ppOut, pInterface, pInterfaceTypeDescr);
85 MediatorMapping::MediatorMapping(uno_Environment * pFrom,
86 uno_Environment * pInterm,
87 uno_Environment * pTo)
88 : m_refCount(0),
89 m_from2uno(pFrom, pInterm),
90 m_uno2to (pInterm, pTo),
91 m_from (pFrom),
92 m_interm (pInterm),
93 m_to (pTo)
95 if (!m_from2uno.get() || !m_uno2to.get())
96 abort();
98 uno_Mapping::acquire = s_acquire;
99 uno_Mapping::release = s_release;
100 uno_Mapping::mapInterface = s_mapInterface;
103 void MediatorMapping::acquire()
105 osl_atomic_increment(&m_refCount);
108 void MediatorMapping::release()
110 if (osl_atomic_decrement(&m_refCount) == 0)
112 ::uno_revokeMapping(this);
116 extern "C" { static void s_mapInterface_v(va_list * pParam)
118 void ** ppOut = va_arg(*pParam, void **);
119 void * pInterface = va_arg(*pParam, void *);
120 typelib_InterfaceTypeDescription * pInterfaceTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
121 uno_Mapping * pMapping = va_arg(*pParam, uno_Mapping *);
123 pMapping->mapInterface(pMapping, ppOut, pInterface, pInterfaceTypeDescr);
126 void MediatorMapping::mapInterface(
127 void ** ppOut,
128 void * pInterface,
129 typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
131 if (*ppOut != nullptr)
133 uno_ExtEnvironment * env = m_to.get()->pExtEnv;
134 OSL_ASSERT( env != nullptr );
135 env->releaseInterface( env, *ppOut );
136 *ppOut = nullptr;
139 void * ret = nullptr;
140 uno_Interface * pUnoI = nullptr;
142 m_from.invoke(s_mapInterface_v, &pUnoI, pInterface, pInterfaceTypeDescr, m_from2uno.get());
144 m_uno2to.mapInterface(&ret, pUnoI, pInterfaceTypeDescr);
146 if (pUnoI)
147 m_interm.get()->pExtEnv->releaseInterface(m_interm.get()->pExtEnv, pUnoI);
149 *ppOut = ret;
152 extern "C" { static void s_MediatorMapping_free(uno_Mapping * pMapping)
153 SAL_THROW_EXTERN_C()
155 delete static_cast<MediatorMapping *>(pMapping);
159 static OUString getPrefix(std::u16string_view str1, std::u16string_view str2)
161 sal_Int32 nIndex1 = 0;
162 sal_Int32 nIndex2 = 0;
163 sal_Int32 sim = 0;
165 std::u16string_view token1;
166 std::u16string_view token2;
170 token1 = o3tl::getToken(str1, 0, ':', nIndex1);
171 token2 = o3tl::getToken(str2, 0, ':', nIndex2);
173 if (token1 == token2)
174 sim += token1.size() + 1;
176 while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1 == token2);
178 OUString result;
180 if (sim)
181 result = str1.substr(0, sim - 1);
183 return result;
186 // OUString str1("abc:def:ghi");
187 // OUString str2("abc:def");
188 // OUString str3("abc");
189 // OUString str4("");
191 // OUString pref;
193 // pref = getPrefix(str1, str1);
194 // pref = getPrefix(str1, str2);
195 // pref = getPrefix(str1, str3);
196 // pref = getPrefix(str1, str4);
198 // pref = getPrefix(str2, str1);
199 // pref = getPrefix(str3, str1);
200 // pref = getPrefix(str4, str1);
203 void getCascadeMapping(uno_Mapping ** ppMapping,
204 uno_Environment * pFrom,
205 uno_Environment * pTo,
206 rtl_uString * pAddPurpose)
208 if (pAddPurpose && pAddPurpose->length)
209 return;
211 OUString uno_envType(UNO_LB_UNO);
213 OUString from_envType = cppu::EnvDcp::getTypeName(pFrom->pTypeName);
214 OUString to_envType = cppu::EnvDcp::getTypeName(pTo->pTypeName);
215 OUString from_envPurpose = cppu::EnvDcp::getPurpose(pFrom->pTypeName);
216 OUString to_envPurpose = cppu::EnvDcp::getPurpose(pTo->pTypeName);
218 #ifdef LOG_CALLING_named_purpose_getMapping
219 OString s_from_name = OUStringToOString(pFrom->pTypeName, RTL_TEXTENCODING_ASCII_US);
220 OString s_to_name = OUStringToOString(pTo->pTypeName, RTL_TEXTENCODING_ASCII_US);
222 std::cerr << __FUNCTION__ << " - creating mediation ";
223 std::cerr << "pFrom: " << s_from_name.getStr();
224 std::cerr <<" pTo: " << s_to_name.getStr() << std::endl;
225 #endif
227 if (from_envPurpose == to_envPurpose) // gcc:bla => uno:bla
228 return;
230 // reaching this point means, we need a mediated mapping!!!
231 // we generally mediate via uno[:free]
232 uno_Environment * pInterm = nullptr;
234 // chained uno -> uno
235 if (from_envType == uno_envType && to_envType == uno_envType)
237 OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
239 OUString uno_envDcp = uno_envType + purpose;
241 // direct mapping possible?
242 // uno:bla-->uno:bla:blubb
243 if (from_envPurpose == purpose)
245 OUString rest = to_envPurpose.copy(purpose.getLength());
247 sal_Int32 index = rest.indexOf(':', 1);
248 if (index == -1)
250 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
251 return;
254 uno_envDcp += rest.subView(0, index);
256 else if (to_envPurpose == purpose)
258 OUString rest = from_envPurpose.copy(purpose.getLength());
260 sal_Int32 index = rest.indexOf(':', 1);
261 if (index == -1)
263 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
264 return;
267 uno_envDcp += rest.subView(0, index);
270 uno_getEnvironment(&pInterm, uno_envDcp.pData, nullptr);
272 else if (from_envType != uno_envType && to_envType == uno_envType) // <ANY> -> UNO ?
273 // mediate via uno:purpose(fromEnv)
275 OUString envDcp = uno_envType + from_envPurpose;
276 uno_getEnvironment(&pInterm, envDcp.pData, nullptr);
278 else if (from_envType == uno_envType && to_envType != uno_envType) // UNO -> <ANY>?
279 // mediate via uno(context)
281 OUString envDcp = uno_envType + to_envPurpose;
282 uno_getEnvironment(&pInterm, envDcp.pData, nullptr);
284 else // everything else
285 // mediate via uno:purpose
287 OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
289 OUString uno_envDcp = uno_envType + purpose;
291 uno_getEnvironment(&pInterm, uno_envDcp.pData, nullptr);
294 uno_Mapping * pMapping = new MediatorMapping(pFrom, pInterm, pTo);
295 pInterm->release(pInterm);
298 pMapping->acquire(pMapping);
300 ::uno_registerMapping(&pMapping, s_MediatorMapping_free, pFrom, pTo, pAddPurpose);
302 if (*ppMapping)
303 (*ppMapping)->release(*ppMapping);
305 *ppMapping = pMapping;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */