1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_bridges.hxx"
27 
28 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
29 
30 #include "guardedarray.hxx"
31 
32 #include "bridges/cpp_uno/shared/bridge.hxx"
33 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
34 
35 #include "com/sun/star/uno/XInterface.hpp"
36 #include "osl/diagnose.h"
37 #include "osl/getglobalmutex.hxx"
38 #include "osl/interlck.h"
39 #include "osl/mutex.hxx"
40 #include "rtl/instance.hxx"
41 #include "typelib/typedescription.h"
42 
43 #include <cstddef>
44 #include <new>
45 
46 
47 static bridges::cpp_uno::shared::VtableFactory * pInstance;
48 
49 #if defined(__GNUG__) && !defined(__MINGW32__)
50 void dso_init(void) __attribute__((constructor));
51 void dso_exit(void) __attribute__((destructor));
52 #endif
53 
dso_init(void)54 void dso_init(void) {
55     if (!pInstance)
56         pInstance = new bridges::cpp_uno::shared::VtableFactory();
57 }
58 
dso_exit(void)59 void dso_exit(void) {
60     if (pInstance)
61     {
62         delete pInstance;
63         pInstance = NULL;
64     }
65 }
66 
67 #ifdef __SUNPRO_CC
68 # pragma init(dso_init)
69 # pragma fini(dso_exit)
70 #endif
71 
72 
73 
74 namespace {
75 
76 struct InitVtableFactory {
operator ()__anon7526fb1d0111::InitVtableFactory77     bridges::cpp_uno::shared::VtableFactory * operator()() {
78         return pInstance;
79     }
80 };
81 
getVtableFactory()82 bridges::cpp_uno::shared::VtableFactory * getVtableFactory() {
83     return rtl_Instance<
84         bridges::cpp_uno::shared::VtableFactory, InitVtableFactory,
85         osl::MutexGuard, osl::GetGlobalMutex >::create(
86             InitVtableFactory(), osl::GetGlobalMutex());
87 }
88 
89 }
90 
91 namespace bridges { namespace cpp_uno { namespace shared {
92 
freeCppInterfaceProxy(uno_ExtEnvironment * pEnv,void * pInterface)93 void freeCppInterfaceProxy(uno_ExtEnvironment * pEnv, void * pInterface)
94 {
95     CppInterfaceProxy * pThis = CppInterfaceProxy::castInterfaceToProxy(
96         pInterface);
97     if (pEnv != pThis->pBridge->getCppEnv()) {
98         OSL_ASSERT(false);
99     }
100 
101     (*pThis->pBridge->getUnoEnv()->revokeInterface)(
102         pThis->pBridge->getUnoEnv(), pThis->pUnoI );
103     (*pThis->pUnoI->release)( pThis->pUnoI );
104     ::typelib_typedescription_release(
105         (typelib_TypeDescription *)pThis->pTypeDescr );
106     pThis->pBridge->release();
107 
108 #if OSL_DEBUG_LEVEL > 1
109     *(int *)pInterface = 0xdeadbabe;
110 #endif
111     pThis->~CppInterfaceProxy();
112     delete[] reinterpret_cast< char * >(pThis);
113 }
114 
create(bridges::cpp_uno::shared::Bridge * pBridge,uno_Interface * pUnoI,typelib_InterfaceTypeDescription * pTypeDescr,rtl::OUString const & rOId)115 com::sun::star::uno::XInterface * CppInterfaceProxy::create(
116     bridges::cpp_uno::shared::Bridge * pBridge, uno_Interface * pUnoI,
117     typelib_InterfaceTypeDescription * pTypeDescr, rtl::OUString const & rOId)
118     SAL_THROW(())
119 {
120     typelib_typedescription_complete(
121         reinterpret_cast< typelib_TypeDescription ** >(&pTypeDescr));
122     bridges::cpp_uno::shared::VtableFactory::Vtables aVtables(
123         getVtableFactory()->getVtables(pTypeDescr));
124     bridges::cpp_uno::shared::GuardedArray< char > pMemory(
125         new char[
126             sizeof (CppInterfaceProxy)
127             + (aVtables.count - 1) * sizeof (void **)]);
128     new(pMemory.get()) CppInterfaceProxy(pBridge, pUnoI, pTypeDescr, rOId);
129     CppInterfaceProxy * pProxy = reinterpret_cast< CppInterfaceProxy * >(
130         pMemory.release());
131     for (sal_Int32 i = 0; i < aVtables.count; ++i) {
132         pProxy->vtables[i] = VtableFactory::mapBlockToVtable(
133             aVtables.blocks[i].start);
134     }
135     return castProxyToInterface(pProxy);
136 }
137 
acquireProxy()138 void CppInterfaceProxy::acquireProxy() SAL_THROW(())
139 {
140     if (1 == osl_incrementInterlockedCount( &nRef ))
141     {
142         // rebirth of proxy zombie
143         // register at cpp env
144         void * pThis = castProxyToInterface( this );
145         (*pBridge->getCppEnv()->registerProxyInterface)(
146             pBridge->getCppEnv(), &pThis, freeCppInterfaceProxy, oid.pData,
147             pTypeDescr );
148         OSL_ASSERT( pThis == castProxyToInterface( this ) );
149     }
150 }
151 
releaseProxy()152 void CppInterfaceProxy::releaseProxy() SAL_THROW(())
153 {
154     if (! osl_decrementInterlockedCount( &nRef )) // last release
155     {
156         // revoke from cpp env
157         (*pBridge->getCppEnv()->revokeInterface)(
158             pBridge->getCppEnv(), castProxyToInterface( this ) );
159     }
160 }
161 
CppInterfaceProxy(bridges::cpp_uno::shared::Bridge * pBridge_,uno_Interface * pUnoI_,typelib_InterfaceTypeDescription * pTypeDescr_,rtl::OUString const & rOId_)162 CppInterfaceProxy::CppInterfaceProxy(
163     bridges::cpp_uno::shared::Bridge * pBridge_, uno_Interface * pUnoI_,
164     typelib_InterfaceTypeDescription * pTypeDescr_, rtl::OUString const & rOId_)
165     SAL_THROW(())
166     : nRef( 1 )
167     , pBridge( pBridge_ )
168     , pUnoI( pUnoI_ )
169     , pTypeDescr( pTypeDescr_ )
170     , oid( rOId_ )
171 {
172     pBridge->acquire();
173     ::typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
174     (*pUnoI->acquire)( pUnoI );
175     (*pBridge->getUnoEnv()->registerInterface)(
176         pBridge->getUnoEnv(), reinterpret_cast< void ** >( &pUnoI ), oid.pData,
177         pTypeDescr );
178 }
179 
~CppInterfaceProxy()180 CppInterfaceProxy::~CppInterfaceProxy()
181 {}
182 
castProxyToInterface(CppInterfaceProxy * pProxy)183 com::sun::star::uno::XInterface * CppInterfaceProxy::castProxyToInterface(
184     CppInterfaceProxy * pProxy)
185 {
186     return reinterpret_cast< com::sun::star::uno::XInterface * >(
187         &pProxy->vtables);
188 }
189 
castInterfaceToProxy(void * pInterface)190 CppInterfaceProxy * CppInterfaceProxy::castInterfaceToProxy(void * pInterface)
191 {
192     // pInterface == &pProxy->vtables (this emulated offsetof is not truly
193     // portable):
194     char const * const base = reinterpret_cast< char const * >(16);
195     std::ptrdiff_t const offset = reinterpret_cast< char const * >(
196         &reinterpret_cast< CppInterfaceProxy const * >(base)->vtables) - base;
197     return reinterpret_cast< CppInterfaceProxy * >(
198         static_cast< char * >(pInterface) - offset);
199 }
200 
201 } } }
202