xref: /AOO41X/main/cppu/source/uno/cascade_mapping.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <iostream>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "osl/interlck.h"
31*cdf0e10cSrcweir #include "rtl/ustring.hxx"
32*cdf0e10cSrcweir #include "uno/environment.hxx"
33*cdf0e10cSrcweir #include "uno/mapping.hxx"
34*cdf0e10cSrcweir #include "uno/dispatcher.h"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //#include "cascade_mappping.hxx"
37*cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //#define LOG_CALLING_named_purpose_getMapping
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //#define LOG_LIFECYLE_MediatorMapping
43*cdf0e10cSrcweir #ifdef LOG_LIFECYLE_MediatorMapping
44*cdf0e10cSrcweir #  define LOG_LIFECYLE_MediatorMapping_emit(x) x
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #else
47*cdf0e10cSrcweir #  define LOG_LIFECYLE_MediatorMapping_emit(x)
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir using namespace com::sun::star;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir class MediatorMapping : public uno_Mapping
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	oslInterlockedCount m_refCount;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	uno::Mapping        m_from2uno;
59*cdf0e10cSrcweir 	uno::Mapping        m_uno2to;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	uno::Environment    m_from;
62*cdf0e10cSrcweir 	uno::Environment    m_interm;
63*cdf0e10cSrcweir 	uno::Environment    m_to;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir public:
66*cdf0e10cSrcweir 	void acquire(void);
67*cdf0e10cSrcweir 	void release(void);
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	void mapInterface(void                            ** ppOut,
70*cdf0e10cSrcweir 					  void                             * pInterface,
71*cdf0e10cSrcweir 					  typelib_InterfaceTypeDescription * pInterfaceTypeDescr);
72*cdf0e10cSrcweir 	MediatorMapping(uno_Environment * pFrom,
73*cdf0e10cSrcweir 					uno_Environment * pInterm,
74*cdf0e10cSrcweir 					uno_Environment * pTo);
75*cdf0e10cSrcweir 	~MediatorMapping();
76*cdf0e10cSrcweir };
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir extern "C" {
79*cdf0e10cSrcweir static void SAL_CALL s_acquire(uno_Mapping * mapping)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
82*cdf0e10cSrcweir 	pMediatorMapping->acquire();
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir static void SAL_CALL s_release(uno_Mapping * mapping)
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir 	MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
88*cdf0e10cSrcweir 	pMediatorMapping->release();
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir static void SAL_CALL s_mapInterface(
92*cdf0e10cSrcweir 	uno_Mapping                      * mapping,
93*cdf0e10cSrcweir 	void                            ** ppOut,
94*cdf0e10cSrcweir 	void                             * pInterface,
95*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	MediatorMapping   * pMediatorMapping  = static_cast<MediatorMapping *>(mapping);
98*cdf0e10cSrcweir 	pMediatorMapping->mapInterface(ppOut, pInterface, pInterfaceTypeDescr);
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir MediatorMapping::MediatorMapping(uno_Environment * pFrom,
103*cdf0e10cSrcweir 								 uno_Environment * pInterm,
104*cdf0e10cSrcweir 								 uno_Environment * pTo)
105*cdf0e10cSrcweir     : m_refCount(0),
106*cdf0e10cSrcweir 	  m_from2uno(pFrom, pInterm),
107*cdf0e10cSrcweir 	  m_uno2to  (pInterm, pTo),
108*cdf0e10cSrcweir 	  m_from    (pFrom),
109*cdf0e10cSrcweir 	  m_interm  (pInterm),
110*cdf0e10cSrcweir 	  m_to      (pTo)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	if (!m_from2uno.get() || !m_uno2to.get())
115*cdf0e10cSrcweir 		abort();
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	uno_Mapping::acquire      = s_acquire;
118*cdf0e10cSrcweir 	uno_Mapping::release      = s_release;
119*cdf0e10cSrcweir 	uno_Mapping::mapInterface = s_mapInterface;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir MediatorMapping::~MediatorMapping()
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir 	LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void MediatorMapping::acquire(void)
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir 	LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	osl_incrementInterlockedCount(&m_refCount);
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir void MediatorMapping::release(void)
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir 	LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	if (osl_decrementInterlockedCount(&m_refCount) == 0)
139*cdf0e10cSrcweir 	{
140*cdf0e10cSrcweir         ::uno_revokeMapping(this);
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir extern "C" { static void s_mapInterface_v(va_list * pParam)
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	void                            ** ppOut               = va_arg(*pParam, void **);
147*cdf0e10cSrcweir 	void                             * pInterface          = va_arg(*pParam, void *);
148*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pInterfaceTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
149*cdf0e10cSrcweir 	uno_Mapping                      * pMapping            = va_arg(*pParam, uno_Mapping *);
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	pMapping->mapInterface(pMapping, ppOut, pInterface, pInterfaceTypeDescr);
152*cdf0e10cSrcweir }}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir void MediatorMapping::mapInterface(
155*cdf0e10cSrcweir 	void                            ** ppOut,
156*cdf0e10cSrcweir 	void                             * pInterface,
157*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	if (*ppOut != 0)
160*cdf0e10cSrcweir 	{
161*cdf0e10cSrcweir 		uno_ExtEnvironment * env = m_to.get()->pExtEnv;
162*cdf0e10cSrcweir 		OSL_ASSERT( env != 0 );
163*cdf0e10cSrcweir 		env->releaseInterface( env, *ppOut );
164*cdf0e10cSrcweir 		*ppOut = NULL;
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	void * ret = 0;
168*cdf0e10cSrcweir 	uno_Interface * pUnoI = 0;
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	m_from.invoke(s_mapInterface_v, &pUnoI, pInterface, pInterfaceTypeDescr, m_from2uno.get());
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	m_uno2to.mapInterface(&ret, pUnoI, pInterfaceTypeDescr);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	if (pUnoI)
175*cdf0e10cSrcweir 		m_interm.get()->pExtEnv->releaseInterface(m_interm.get()->pExtEnv, pUnoI);
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	*ppOut = ret;
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir extern "C" { static void SAL_CALL s_MediatorMapping_free(uno_Mapping * pMapping)
181*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     delete static_cast<MediatorMapping *>(pMapping);
184*cdf0e10cSrcweir }}
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir static rtl::OUString getPrefix(rtl::OUString const & str1, rtl::OUString const & str2)
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 	sal_Int32 nIndex1 = 0;
191*cdf0e10cSrcweir 	sal_Int32 nIndex2 = 0;
192*cdf0e10cSrcweir 	sal_Int32 sim = 0;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	rtl::OUString token1;
195*cdf0e10cSrcweir 	rtl::OUString token2;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	do
198*cdf0e10cSrcweir 	{
199*cdf0e10cSrcweir 		token1 = str1.getToken(0, ':', nIndex1);
200*cdf0e10cSrcweir 		token2 = str2.getToken(0, ':', nIndex2);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 		if (token1.equals(token2))
203*cdf0e10cSrcweir 			sim += token1.getLength() + 1;
204*cdf0e10cSrcweir 	}
205*cdf0e10cSrcweir 	while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1.equals(token2));
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	rtl::OUString result;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	if (sim)
210*cdf0e10cSrcweir 		result = str1.copy(0, sim - 1);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	return result;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir // 	rtl::OUString str1(RTL_CONSTASCII_USTRINGPARAM("abc:def:ghi"));
216*cdf0e10cSrcweir // 	rtl::OUString str2(RTL_CONSTASCII_USTRINGPARAM("abc:def"));
217*cdf0e10cSrcweir // 	rtl::OUString str3(RTL_CONSTASCII_USTRINGPARAM("abc"));
218*cdf0e10cSrcweir // 	rtl::OUString str4(RTL_CONSTASCII_USTRINGPARAM(""));
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir // 	rtl::OUString pref;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir // 	pref = getPrefix(str1, str1);
223*cdf0e10cSrcweir // 	pref = getPrefix(str1, str2);
224*cdf0e10cSrcweir // 	pref = getPrefix(str1, str3);
225*cdf0e10cSrcweir // 	pref = getPrefix(str1, str4);
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir // 	pref = getPrefix(str2, str1);
228*cdf0e10cSrcweir // 	pref = getPrefix(str3, str1);
229*cdf0e10cSrcweir // 	pref = getPrefix(str4, str1);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir void getCascadeMapping(uno_Mapping     ** ppMapping,
233*cdf0e10cSrcweir 					   uno_Environment  * pFrom,
234*cdf0e10cSrcweir 					   uno_Environment  * pTo,
235*cdf0e10cSrcweir 					   rtl_uString      * pAddPurpose)
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir 	if (pAddPurpose && pAddPurpose->length)
238*cdf0e10cSrcweir 		return;
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	rtl::OUString uno_envType(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO));
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	rtl::OUString from_envType    = cppu::EnvDcp::getTypeName(pFrom->pTypeName);
243*cdf0e10cSrcweir 	rtl::OUString to_envType      = cppu::EnvDcp::getTypeName(pTo->pTypeName);
244*cdf0e10cSrcweir 	rtl::OUString from_envPurpose = cppu::EnvDcp::getPurpose(pFrom->pTypeName);
245*cdf0e10cSrcweir 	rtl::OUString to_envPurpose   = cppu::EnvDcp::getPurpose(pTo->pTypeName);
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir #ifdef LOG_CALLING_named_purpose_getMapping
248*cdf0e10cSrcweir 	rtl::OString s_from_name = rtl::OUStringToOString(pFrom->pTypeName, RTL_TEXTENCODING_ASCII_US);
249*cdf0e10cSrcweir 	rtl::OString s_to_name   = rtl::OUStringToOString(pTo->pTypeName,   RTL_TEXTENCODING_ASCII_US);
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 	std::cerr << __FUNCTION__ << " - creating mediation ";
252*cdf0e10cSrcweir 	std::cerr << "pFrom: " << s_from_name.getStr();
253*cdf0e10cSrcweir 	std::cerr <<" pTo: "   << s_to_name.getStr() << std::endl;
254*cdf0e10cSrcweir #endif
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	if (from_envPurpose == to_envPurpose) // gcc:bla => uno:bla
257*cdf0e10cSrcweir 		return;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	// reaching this point means, we need a mediated mapping!!!
260*cdf0e10cSrcweir 	// we generall mediate via uno[:free]
261*cdf0e10cSrcweir 	uno_Environment * pInterm = NULL;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 	// chained uno -> uno
264*cdf0e10cSrcweir 	if (from_envType == uno_envType && to_envType == uno_envType)
265*cdf0e10cSrcweir 	{
266*cdf0e10cSrcweir 		rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 		rtl::OUString uno_envDcp = uno_envType;
269*cdf0e10cSrcweir 		uno_envDcp += purpose;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 		// direct mapping possible?
272*cdf0e10cSrcweir 		// uno:bla-->uno:bla:blubb
273*cdf0e10cSrcweir 		if (from_envPurpose.equals(purpose))
274*cdf0e10cSrcweir 		{
275*cdf0e10cSrcweir 			rtl::OUString rest = to_envPurpose.copy(purpose.getLength());
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir 			sal_Int32 index = rest.indexOf(':', 1);
278*cdf0e10cSrcweir 			if (index == -1)
279*cdf0e10cSrcweir 			{
280*cdf0e10cSrcweir 				uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
281*cdf0e10cSrcweir 				return;
282*cdf0e10cSrcweir 			}
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir 			uno_envDcp += rest.copy(0, index);
285*cdf0e10cSrcweir 		}
286*cdf0e10cSrcweir 		else if (to_envPurpose.equals(purpose))
287*cdf0e10cSrcweir 		{
288*cdf0e10cSrcweir 			rtl::OUString rest = from_envPurpose.copy(purpose.getLength());
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 			sal_Int32 index = rest.indexOf(':', 1);
291*cdf0e10cSrcweir 			if (index == -1)
292*cdf0e10cSrcweir 			{
293*cdf0e10cSrcweir 				uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
294*cdf0e10cSrcweir 				return;
295*cdf0e10cSrcweir 			}
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 			uno_envDcp += rest.copy(0, index);
298*cdf0e10cSrcweir 		}
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir 		uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
301*cdf0e10cSrcweir 	}
302*cdf0e10cSrcweir 	else if (from_envType != uno_envType && to_envType == uno_envType) // <ANY> -> UNO ?
303*cdf0e10cSrcweir 		// mediate via uno:purpose(fromEnv)
304*cdf0e10cSrcweir 	{
305*cdf0e10cSrcweir 		rtl::OUString     envDcp = uno_envType;
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir 		envDcp += from_envPurpose;
308*cdf0e10cSrcweir  		uno_getEnvironment(&pInterm, envDcp.pData, NULL);
309*cdf0e10cSrcweir 	}
310*cdf0e10cSrcweir 	else if (from_envType == uno_envType && to_envType != uno_envType) // UNO -> <ANY>?
311*cdf0e10cSrcweir 		// mediate via uno(context)
312*cdf0e10cSrcweir 	{
313*cdf0e10cSrcweir 		rtl::OUString     envDcp = uno_envType;
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir  		envDcp += to_envPurpose;
316*cdf0e10cSrcweir 		uno_getEnvironment(&pInterm, envDcp.pData, NULL);
317*cdf0e10cSrcweir 	}
318*cdf0e10cSrcweir 	else // everything else
319*cdf0e10cSrcweir 		// mediate via uno:purpose
320*cdf0e10cSrcweir 	{
321*cdf0e10cSrcweir 		rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 		rtl::OUString uno_envDcp = uno_envType;
324*cdf0e10cSrcweir 		uno_envDcp += purpose;
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 		uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
327*cdf0e10cSrcweir 	}
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 	uno_Mapping * pMapping = new MediatorMapping(pFrom, pInterm, pTo);
330*cdf0e10cSrcweir 	pInterm->release(pInterm);
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 	pMapping->acquire(pMapping);
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	::uno_registerMapping(&pMapping, s_MediatorMapping_free, pFrom, pTo, pAddPurpose);
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir 	if (*ppMapping)
338*cdf0e10cSrcweir 		(*ppMapping)->release(*ppMapping);
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 	*ppMapping = pMapping;
341*cdf0e10cSrcweir }
342