Line data Source code
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 :
21 : #include "rtl/uuid.h"
22 : #include "osl/thread.h"
23 : #include "osl/mutex.hxx"
24 :
25 : #include "uno/environment.hxx"
26 : #include "uno/mapping.hxx"
27 : #include "typelib/typedescription.h"
28 :
29 : #include "current.hxx"
30 :
31 :
32 : using namespace ::osl;
33 : using namespace ::rtl;
34 : using namespace ::cppu;
35 : using namespace ::com::sun::star::uno;
36 :
37 : namespace cppu
38 : {
39 :
40 2066 : static typelib_InterfaceTypeDescription * get_type_XCurrentContext()
41 : {
42 : static typelib_InterfaceTypeDescription * s_type_XCurrentContext = 0;
43 2066 : if (0 == s_type_XCurrentContext)
44 : {
45 182 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
46 182 : if (0 == s_type_XCurrentContext)
47 : {
48 182 : OUString sTypeName("com.sun.star.uno.XCurrentContext");
49 182 : typelib_InterfaceTypeDescription * pTD = 0;
50 182 : typelib_TypeDescriptionReference * pMembers[1] = { 0 };
51 364 : OUString sMethodName0("com.sun.star.uno.XCurrentContext::getValueByName");
52 : typelib_typedescriptionreference_new(
53 : &pMembers[0],
54 : typelib_TypeClass_INTERFACE_METHOD,
55 182 : sMethodName0.pData );
56 : typelib_typedescription_newInterface(
57 : &pTD,
58 : sTypeName.pData, 0, 0, 0, 0, 0,
59 182 : * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE ),
60 : 1,
61 182 : pMembers );
62 :
63 182 : typelib_typedescription_register( (typelib_TypeDescription**)&pTD );
64 182 : typelib_typedescriptionreference_release( pMembers[0] );
65 :
66 182 : typelib_InterfaceMethodTypeDescription * pMethod = 0;
67 : typelib_Parameter_Init aParameters[1];
68 364 : OUString sParamName0("Name");
69 364 : OUString sParamType0("string");
70 182 : aParameters[0].pParamName = sParamName0.pData;
71 182 : aParameters[0].eTypeClass = typelib_TypeClass_STRING;
72 182 : aParameters[0].pTypeName = sParamType0.pData;
73 182 : aParameters[0].bIn = sal_True;
74 182 : aParameters[0].bOut = sal_False;
75 : rtl_uString * pExceptions[1];
76 364 : OUString sExceptionName0("com.sun.star.uno.RuntimeException");
77 182 : pExceptions[0] = sExceptionName0.pData;
78 364 : OUString sReturnType0("any");
79 : typelib_typedescription_newInterfaceMethod(
80 : &pMethod,
81 : 3, sal_False,
82 : sMethodName0.pData,
83 : typelib_TypeClass_ANY, sReturnType0.pData,
84 182 : 1, aParameters, 1, pExceptions );
85 182 : typelib_typedescription_register( (typelib_TypeDescription**)&pMethod );
86 182 : typelib_typedescription_release( (typelib_TypeDescription*)pMethod );
87 : // another static ref:
88 : ++reinterpret_cast< typelib_TypeDescription * >( pTD )->
89 182 : nStaticRefCount;
90 364 : s_type_XCurrentContext = pTD;
91 182 : }
92 : }
93 2066 : return s_type_XCurrentContext;
94 : }
95 :
96 :
97 :
98 :
99 : class ThreadKey
100 : {
101 : bool _bInit;
102 : oslThreadKey _hThreadKey;
103 : oslThreadKeyCallbackFunction _pCallback;
104 :
105 : public:
106 : inline oslThreadKey getThreadKey();
107 :
108 : inline ThreadKey( oslThreadKeyCallbackFunction pCallback );
109 : inline ~ThreadKey();
110 : };
111 :
112 379 : inline ThreadKey::ThreadKey( oslThreadKeyCallbackFunction pCallback )
113 : : _bInit( false )
114 : , _hThreadKey( 0 )
115 379 : , _pCallback( pCallback )
116 : {
117 379 : }
118 :
119 379 : inline ThreadKey::~ThreadKey()
120 : {
121 379 : if (_bInit)
122 : {
123 379 : ::osl_destroyThreadKey( _hThreadKey );
124 : }
125 379 : }
126 :
127 1950078 : inline oslThreadKey ThreadKey::getThreadKey()
128 : {
129 1950078 : if (! _bInit)
130 : {
131 379 : MutexGuard aGuard( Mutex::getGlobalMutex() );
132 379 : if (! _bInit)
133 : {
134 379 : _hThreadKey = ::osl_createThreadKey( _pCallback );
135 379 : _bInit = true;
136 379 : }
137 : }
138 1950078 : return _hThreadKey;
139 : }
140 :
141 :
142 1694 : extern "C" void SAL_CALL delete_IdContainer( void * p )
143 : {
144 1694 : if (p)
145 : {
146 1695 : IdContainer * pId = reinterpret_cast< IdContainer * >( p );
147 1695 : if (pId->pCurrentContext)
148 : {
149 : (*pId->pCurrentContextEnv->releaseInterface)(
150 0 : pId->pCurrentContextEnv, pId->pCurrentContext );
151 : (*((uno_Environment *)pId->pCurrentContextEnv)->release)(
152 0 : (uno_Environment *)pId->pCurrentContextEnv );
153 : }
154 1694 : if (pId->bInit)
155 : {
156 1694 : ::rtl_byte_sequence_release( pId->pLocalThreadId );
157 1692 : ::rtl_byte_sequence_release( pId->pCurrentId );
158 : }
159 1695 : delete pId;
160 : }
161 1694 : }
162 :
163 1950078 : IdContainer * getIdContainer()
164 : {
165 1950078 : static ThreadKey s_key( delete_IdContainer );
166 1950078 : oslThreadKey aKey = s_key.getThreadKey();
167 :
168 1950078 : IdContainer * pId = reinterpret_cast< IdContainer * >( ::osl_getThreadKeyData( aKey ) );
169 1950077 : if (! pId)
170 : {
171 2072 : pId = new IdContainer();
172 2072 : pId->pCurrentContext = 0;
173 2072 : pId->pCurrentContextEnv = 0;
174 2072 : pId->bInit = false;
175 2072 : ::osl_setThreadKeyData( aKey, pId );
176 : }
177 1950077 : return pId;
178 : }
179 :
180 : }
181 :
182 :
183 785583 : extern "C" CPPU_DLLPUBLIC sal_Bool SAL_CALL uno_setCurrentContext(
184 : void * pCurrentContext,
185 : rtl_uString * pEnvTypeName, void * pEnvContext )
186 : SAL_THROW_EXTERN_C()
187 : {
188 785583 : IdContainer * pId = getIdContainer();
189 : OSL_ASSERT( pId );
190 :
191 : // free old one
192 785583 : if (pId->pCurrentContext)
193 : {
194 : (*pId->pCurrentContextEnv->releaseInterface)(
195 1646 : pId->pCurrentContextEnv, pId->pCurrentContext );
196 : (*((uno_Environment *)pId->pCurrentContextEnv)->release)(
197 1646 : (uno_Environment *)pId->pCurrentContextEnv );
198 1646 : pId->pCurrentContextEnv = 0;
199 :
200 1646 : pId->pCurrentContext = 0;
201 : }
202 :
203 785583 : if (pCurrentContext)
204 : {
205 2019 : uno_Environment * pEnv = 0;
206 2019 : ::uno_getEnvironment( &pEnv, pEnvTypeName, pEnvContext );
207 : OSL_ASSERT( pEnv && pEnv->pExtEnv );
208 2019 : if (pEnv)
209 : {
210 2019 : if (pEnv->pExtEnv)
211 : {
212 2019 : pId->pCurrentContextEnv = pEnv->pExtEnv;
213 : (*pId->pCurrentContextEnv->acquireInterface)(
214 2019 : pId->pCurrentContextEnv, pCurrentContext );
215 2019 : pId->pCurrentContext = pCurrentContext;
216 : }
217 : else
218 : {
219 0 : (*pEnv->release)( pEnv );
220 0 : return sal_False;
221 : }
222 : }
223 : else
224 : {
225 0 : return sal_False;
226 : }
227 : }
228 785583 : return sal_True;
229 : }
230 :
231 417597 : extern "C" CPPU_DLLPUBLIC sal_Bool SAL_CALL uno_getCurrentContext(
232 : void ** ppCurrentContext, rtl_uString * pEnvTypeName, void * pEnvContext )
233 : SAL_THROW_EXTERN_C()
234 : {
235 417597 : IdContainer * pId = getIdContainer();
236 : OSL_ASSERT( pId );
237 :
238 417597 : Environment target_env;
239 :
240 : // release inout parameter
241 417597 : if (*ppCurrentContext)
242 : {
243 0 : target_env = Environment(rtl::OUString(pEnvTypeName), pEnvContext);
244 : OSL_ASSERT( target_env.is() );
245 0 : if (! target_env.is())
246 0 : return sal_False;
247 0 : uno_ExtEnvironment * pEnv = target_env.get()->pExtEnv;
248 : OSL_ASSERT( 0 != pEnv );
249 0 : if (0 == pEnv)
250 0 : return sal_False;
251 0 : (*pEnv->releaseInterface)( pEnv, *ppCurrentContext );
252 :
253 0 : *ppCurrentContext = 0;
254 : }
255 :
256 : // case: null-ref
257 417597 : if (0 == pId->pCurrentContext)
258 415531 : return sal_True;
259 :
260 2066 : if (! target_env.is())
261 : {
262 2066 : target_env = Environment(rtl::OUString(pEnvTypeName), pEnvContext);
263 : OSL_ASSERT( target_env.is() );
264 2066 : if (! target_env.is())
265 0 : return sal_False;
266 : }
267 :
268 4132 : Mapping mapping((uno_Environment *) pId->pCurrentContextEnv, target_env.get());
269 : OSL_ASSERT( mapping.is() );
270 2066 : if (! mapping.is())
271 0 : return sal_False;
272 :
273 2066 : mapping.mapInterface(ppCurrentContext, pId->pCurrentContext, ::cppu::get_type_XCurrentContext() );
274 :
275 419663 : return sal_True;
276 : }
277 :
278 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|