xref: /AOO41X/main/cppu/source/helper/purpenv/helper_purpenv_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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppu.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "Proxy.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "osl/interlck.h"
36*cdf0e10cSrcweir #include "uno/environment.hxx"
37*cdf0e10cSrcweir #include "uno/dispatcher.h"
38*cdf0e10cSrcweir #include "typelib/typedescription.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifdef debug
42*cdf0e10cSrcweir # define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_cppu_helper_purpenv_Mapping
46*cdf0e10cSrcweir #  include <iostream>
47*cdf0e10cSrcweir #  define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x) x
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #else
50*cdf0e10cSrcweir #  define LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(x)
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #endif
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir using namespace com::sun::star;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir class Mapping : public uno_Mapping
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     uno::Environment   m_from;
61*cdf0e10cSrcweir     uno::Environment   m_to;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	oslInterlockedCount m_nCount;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	cppu::helper::purpenv::ProbeFun * m_probeFun;
66*cdf0e10cSrcweir 	void                            * m_pContext;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir public:
69*cdf0e10cSrcweir     explicit  Mapping(uno_Environment                 * pFrom,
70*cdf0e10cSrcweir 					  uno_Environment                 * pTo,
71*cdf0e10cSrcweir 					  cppu::helper::purpenv::ProbeFun * probeFun,
72*cdf0e10cSrcweir 					  void                            * pProbeContext);
73*cdf0e10cSrcweir 	virtual  ~Mapping(void);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	void mapInterface(
76*cdf0e10cSrcweir 		uno_Interface                    ** ppOut,
77*cdf0e10cSrcweir 		uno_Interface                     * pUnoI,
78*cdf0e10cSrcweir 		typelib_InterfaceTypeDescription  * pTypeDescr);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	void acquire(void);
81*cdf0e10cSrcweir 	void release(void);
82*cdf0e10cSrcweir };
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir static void SAL_CALL s_mapInterface(
85*cdf0e10cSrcweir 	uno_Mapping                       * puno_Mapping,
86*cdf0e10cSrcweir 	uno_Interface                    ** ppOut,
87*cdf0e10cSrcweir 	uno_Interface                     * pUnoI,
88*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription  * pTypeDescr )
89*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
92*cdf0e10cSrcweir 	pMapping->mapInterface(ppOut, pUnoI, pTypeDescr);
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir extern "C" {
96*cdf0e10cSrcweir static void SAL_CALL s_acquire(uno_Mapping * puno_Mapping)
97*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
100*cdf0e10cSrcweir 	pMapping->acquire();
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir static void SAL_CALL s_release(uno_Mapping * puno_Mapping)
104*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir 	Mapping * pMapping = static_cast<Mapping * >(puno_Mapping);
107*cdf0e10cSrcweir 	pMapping->release();
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir static void s_getIdentifier_v(va_list * pParam)
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	uno_ExtEnvironment *  pEnv  = va_arg(*pParam, uno_ExtEnvironment *);
114*cdf0e10cSrcweir 	rtl_uString        ** ppOid = va_arg(*pParam, rtl_uString **);
115*cdf0e10cSrcweir 	uno_Interface      *  pUnoI = va_arg(*pParam, uno_Interface *);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI);
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir static void SAL_CALL s_free(uno_Mapping * puno_Mapping)
121*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir 	Mapping * pMapping = static_cast<Mapping *>(puno_Mapping);
124*cdf0e10cSrcweir 	delete pMapping;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir Mapping::Mapping(uno_Environment                 * pFrom,
129*cdf0e10cSrcweir 				 uno_Environment                 * pTo,
130*cdf0e10cSrcweir 				 cppu::helper::purpenv::ProbeFun * probeFun,
131*cdf0e10cSrcweir 				 void                            * pProbeContext
132*cdf0e10cSrcweir ) SAL_THROW( () )
133*cdf0e10cSrcweir 	: m_from    (pFrom),
134*cdf0e10cSrcweir 	  m_to      (pTo),
135*cdf0e10cSrcweir 	  m_nCount  (1),
136*cdf0e10cSrcweir 	  m_probeFun(probeFun),
137*cdf0e10cSrcweir 	  m_pContext(pProbeContext)
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo) SAL_THROW( () )", this));
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     uno_Mapping::acquire      = s_acquire;
142*cdf0e10cSrcweir     uno_Mapping::release      = s_release;
143*cdf0e10cSrcweir     uno_Mapping::mapInterface = (uno_MapInterfaceFunc)s_mapInterface;
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir Mapping::~Mapping(void)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	LOG_LIFECYCLE_cppu_helper_purpenv_Mapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Mapping::~Mapping(void)", this));
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void Mapping::mapInterface(
153*cdf0e10cSrcweir 	uno_Interface                    ** ppOut,
154*cdf0e10cSrcweir 	uno_Interface                     * pUnoI,
155*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription  * pTypeDescr)
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     OSL_ASSERT(ppOut && pTypeDescr);
158*cdf0e10cSrcweir     if (*ppOut)
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         (*ppOut)->release(*ppOut);
161*cdf0e10cSrcweir         *ppOut = 0;
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	if (!pUnoI)
165*cdf0e10cSrcweir 		return;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	// get object id of uno interface to be wrapped
168*cdf0e10cSrcweir 	// need to enter environment because of potential "queryInterface" call
169*cdf0e10cSrcweir 	rtl_uString * pOId = 0;
170*cdf0e10cSrcweir 	uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI);
171*cdf0e10cSrcweir 	OSL_ASSERT(pOId);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir  	// try to get any known interface from target environment
174*cdf0e10cSrcweir 	m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, (void **)ppOut, pOId, pTypeDescr);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	if (!*ppOut) // not yet there, register new proxy interface
177*cdf0e10cSrcweir 	{
178*cdf0e10cSrcweir 		// try to publish a new proxy (ref count initially 1)
179*cdf0e10cSrcweir 		uno_Interface * pProxy = new Proxy(this,
180*cdf0e10cSrcweir 										   m_from.get(),
181*cdf0e10cSrcweir 										   m_to.get(),
182*cdf0e10cSrcweir 										   pUnoI,
183*cdf0e10cSrcweir 										   pTypeDescr,
184*cdf0e10cSrcweir 										   pOId,
185*cdf0e10cSrcweir 										   m_probeFun,
186*cdf0e10cSrcweir 										   m_pContext);
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		// proxy may be exchanged during registration
189*cdf0e10cSrcweir 		m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv,
190*cdf0e10cSrcweir 													(void **)&pProxy,
191*cdf0e10cSrcweir 													Proxy_free,
192*cdf0e10cSrcweir 													pOId,
193*cdf0e10cSrcweir 													pTypeDescr);
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 		*ppOut = pProxy;
196*cdf0e10cSrcweir 	}
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	rtl_uString_release(pOId);
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir void Mapping::acquire() SAL_THROW(())
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	if (osl_incrementInterlockedCount(&m_nCount) == 1)
205*cdf0e10cSrcweir 	{
206*cdf0e10cSrcweir 		uno_Mapping * pMapping = this;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), NULL);
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir void Mapping::release() SAL_THROW(())
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir 	if (osl_decrementInterlockedCount(&m_nCount) == 0)
215*cdf0e10cSrcweir 		::uno_revokeMapping(this);
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir namespace cppu { namespace helper { namespace purpenv {
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir void createMapping(uno_Mapping     ** ppMapping,
222*cdf0e10cSrcweir 				   uno_Environment  * pFrom,
223*cdf0e10cSrcweir 				   uno_Environment  * pTo,
224*cdf0e10cSrcweir 				   ProbeFun         * probeFun,
225*cdf0e10cSrcweir 				   void             * pContext
226*cdf0e10cSrcweir  )
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir 	*ppMapping = new Mapping(pFrom, pTo, probeFun, pContext);
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	::uno_registerMapping(ppMapping, s_free, pFrom, pTo, NULL);
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir }}}
234