1129fa3d1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3129fa3d1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4129fa3d1SAndrew Rist * or more contributor license agreements. See the NOTICE file 5129fa3d1SAndrew Rist * distributed with this work for additional information 6129fa3d1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7129fa3d1SAndrew Rist * to you under the Apache License, Version 2.0 (the 8129fa3d1SAndrew Rist * "License"); you may not use this file except in compliance 9129fa3d1SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11129fa3d1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13129fa3d1SAndrew Rist * Unless required by applicable law or agreed to in writing, 14129fa3d1SAndrew Rist * software distributed under the License is distributed on an 15129fa3d1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16129fa3d1SAndrew Rist * KIND, either express or implied. See the License for the 17129fa3d1SAndrew Rist * specific language governing permissions and limitations 18129fa3d1SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20129fa3d1SAndrew Rist *************************************************************/ 21129fa3d1SAndrew Rist 22129fa3d1SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "uno/environment.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include "cppu/EnvDcp.hxx" 27cdf0e10cSrcweir #include "cppu/Enterable.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include "osl/thread.h" 30cdf0e10cSrcweir #include "osl/mutex.hxx" 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <hash_map> 33cdf0e10cSrcweir 34cdf0e10cSrcweir 35cdf0e10cSrcweir using namespace com::sun::star; 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir struct SAL_DLLPRIVATE oslThreadIdentifier_equal 39cdf0e10cSrcweir { 40cdf0e10cSrcweir bool operator()(oslThreadIdentifier s1, oslThreadIdentifier s2) const; 41cdf0e10cSrcweir }; 42cdf0e10cSrcweir 43cdf0e10cSrcweir bool oslThreadIdentifier_equal::operator()(oslThreadIdentifier s1, oslThreadIdentifier s2) const 44cdf0e10cSrcweir { 45cdf0e10cSrcweir bool result = s1 == s2; 46cdf0e10cSrcweir 47cdf0e10cSrcweir return result; 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir 51cdf0e10cSrcweir struct SAL_DLLPRIVATE oslThreadIdentifier_hash 52cdf0e10cSrcweir { 53cdf0e10cSrcweir size_t operator()(oslThreadIdentifier s1) const; 54cdf0e10cSrcweir }; 55cdf0e10cSrcweir 56cdf0e10cSrcweir size_t oslThreadIdentifier_hash::operator()(oslThreadIdentifier s1) const 57cdf0e10cSrcweir { 58cdf0e10cSrcweir return s1; 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir typedef ::std::hash_map<oslThreadIdentifier, 62cdf0e10cSrcweir uno_Environment *, 63cdf0e10cSrcweir oslThreadIdentifier_hash, 64cdf0e10cSrcweir oslThreadIdentifier_equal> ThreadMap; 65cdf0e10cSrcweir 66cdf0e10cSrcweir static osl::Mutex s_threadMap_mutex; 67cdf0e10cSrcweir static ThreadMap s_threadMap; 68cdf0e10cSrcweir 69cdf0e10cSrcweir 70cdf0e10cSrcweir static rtl::OUString s_uno_envDcp(RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO)); 71cdf0e10cSrcweir 72cdf0e10cSrcweir static void s_setCurrent(uno_Environment * pEnv) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir oslThreadIdentifier threadId = osl_getThreadIdentifier(NULL); 75cdf0e10cSrcweir 76cdf0e10cSrcweir osl::MutexGuard guard(s_threadMap_mutex); 77cdf0e10cSrcweir if (pEnv) 78cdf0e10cSrcweir s_threadMap[threadId] = pEnv; 79cdf0e10cSrcweir else 80cdf0e10cSrcweir { 81cdf0e10cSrcweir ThreadMap::iterator iEnv = s_threadMap.find(threadId); 82*9f66c43bSHerbert Dürr if( iEnv != s_threadMap.end()) 83cdf0e10cSrcweir s_threadMap.erase(iEnv); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir static uno_Environment * s_getCurrent(void) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir uno_Environment * pEnv = NULL; 90cdf0e10cSrcweir 91cdf0e10cSrcweir oslThreadIdentifier threadId = osl_getThreadIdentifier(NULL); 92cdf0e10cSrcweir 93cdf0e10cSrcweir osl::MutexGuard guard(s_threadMap_mutex); 94cdf0e10cSrcweir ThreadMap::iterator iEnv = s_threadMap.find(threadId); 95cdf0e10cSrcweir if(iEnv != s_threadMap.end()) 96cdf0e10cSrcweir pEnv = iEnv->second; 97cdf0e10cSrcweir 98cdf0e10cSrcweir return pEnv; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir 102cdf0e10cSrcweir extern "C" void SAL_CALL uno_getCurrentEnvironment(uno_Environment ** ppEnv, rtl_uString * pTypeName) 103cdf0e10cSrcweir SAL_THROW_EXTERN_C() 104cdf0e10cSrcweir { 105cdf0e10cSrcweir if (*ppEnv) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir (*ppEnv)->release(*ppEnv); 108cdf0e10cSrcweir *ppEnv = NULL; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir rtl::OUString currPurpose; 112cdf0e10cSrcweir 113cdf0e10cSrcweir uno_Environment * pCurrEnv = s_getCurrent(); 114cdf0e10cSrcweir if (pCurrEnv) // no environment means no purpose 115cdf0e10cSrcweir currPurpose = cppu::EnvDcp::getPurpose(pCurrEnv->pTypeName); 116cdf0e10cSrcweir 117cdf0e10cSrcweir if (pTypeName && rtl_uString_getLength(pTypeName)) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir rtl::OUString envDcp(pTypeName); 120cdf0e10cSrcweir envDcp += currPurpose; 121cdf0e10cSrcweir 122cdf0e10cSrcweir uno_getEnvironment(ppEnv, envDcp.pData, NULL); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir else 125cdf0e10cSrcweir { 126cdf0e10cSrcweir if (pCurrEnv) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir *ppEnv = pCurrEnv; 129cdf0e10cSrcweir (*ppEnv)->acquire(*ppEnv); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir else 132cdf0e10cSrcweir uno_getEnvironment(ppEnv, s_uno_envDcp.pData, NULL); 133cdf0e10cSrcweir 134cdf0e10cSrcweir } 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir static rtl::OUString s_getPrefix(rtl::OUString const & str1, rtl::OUString const & str2) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir sal_Int32 nIndex1 = 0; 140cdf0e10cSrcweir sal_Int32 nIndex2 = 0; 141cdf0e10cSrcweir sal_Int32 sim = 0; 142cdf0e10cSrcweir 143cdf0e10cSrcweir rtl::OUString token1; 144cdf0e10cSrcweir rtl::OUString token2; 145cdf0e10cSrcweir 146cdf0e10cSrcweir do 147cdf0e10cSrcweir { 148cdf0e10cSrcweir token1 = str1.getToken(0, ':', nIndex1); 149cdf0e10cSrcweir token2 = str2.getToken(0, ':', nIndex2); 150cdf0e10cSrcweir 151cdf0e10cSrcweir if (token1.equals(token2)) 152cdf0e10cSrcweir sim += token1.getLength() + 1; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1.equals(token2)); 155cdf0e10cSrcweir 156cdf0e10cSrcweir rtl::OUString result; 157cdf0e10cSrcweir 158cdf0e10cSrcweir if (sim) 159cdf0e10cSrcweir result = str1.copy(0, sim - 1); 160cdf0e10cSrcweir 161cdf0e10cSrcweir return result; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir static int s_getNextEnv(uno_Environment ** ppEnv, uno_Environment * pCurrEnv, uno_Environment * pTargetEnv) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir int res = 0; 167cdf0e10cSrcweir 168cdf0e10cSrcweir rtl::OUString nextPurpose; 169cdf0e10cSrcweir 170cdf0e10cSrcweir rtl::OUString currPurpose; 171cdf0e10cSrcweir if (pCurrEnv) 172cdf0e10cSrcweir currPurpose = cppu::EnvDcp::getPurpose(pCurrEnv->pTypeName); 173cdf0e10cSrcweir 174cdf0e10cSrcweir rtl::OUString targetPurpose; 175cdf0e10cSrcweir if (pTargetEnv) 176cdf0e10cSrcweir targetPurpose = cppu::EnvDcp::getPurpose(pTargetEnv->pTypeName); 177cdf0e10cSrcweir 178cdf0e10cSrcweir rtl::OUString intermPurpose(s_getPrefix(currPurpose, targetPurpose)); 179cdf0e10cSrcweir if (currPurpose.getLength() > intermPurpose.getLength()) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir sal_Int32 idx = currPurpose.lastIndexOf(':'); 182cdf0e10cSrcweir nextPurpose = currPurpose.copy(0, idx); 183cdf0e10cSrcweir 184cdf0e10cSrcweir res = -1; 185cdf0e10cSrcweir } 186cdf0e10cSrcweir else if (intermPurpose.getLength() < targetPurpose.getLength()) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir sal_Int32 idx = targetPurpose.indexOf(':', intermPurpose.getLength() + 1); 189cdf0e10cSrcweir if (idx == -1) 190cdf0e10cSrcweir nextPurpose = targetPurpose; 191cdf0e10cSrcweir 192cdf0e10cSrcweir else 193cdf0e10cSrcweir nextPurpose = targetPurpose.copy(0, idx); 194cdf0e10cSrcweir 195cdf0e10cSrcweir res = 1; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir if (nextPurpose.getLength()) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir rtl::OUString next_envDcp(s_uno_envDcp); 201cdf0e10cSrcweir next_envDcp += nextPurpose; 202cdf0e10cSrcweir 203cdf0e10cSrcweir uno_getEnvironment(ppEnv, next_envDcp.pData, NULL); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir else 206cdf0e10cSrcweir { 207cdf0e10cSrcweir if (*ppEnv) 208cdf0e10cSrcweir (*ppEnv)->release(*ppEnv); 209cdf0e10cSrcweir 210cdf0e10cSrcweir *ppEnv = NULL; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir return res; 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir extern "C" { static void s_pull(va_list * pParam) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir uno_EnvCallee * pCallee = va_arg(*pParam, uno_EnvCallee *); 219cdf0e10cSrcweir va_list * pXparam = va_arg(*pParam, va_list *); 220cdf0e10cSrcweir 221cdf0e10cSrcweir pCallee(pXparam); 222cdf0e10cSrcweir }} 223cdf0e10cSrcweir 224cdf0e10cSrcweir static void s_callInto_v(uno_Environment * pEnv, uno_EnvCallee * pCallee, va_list * pParam) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir cppu::Enterable * pEnterable = reinterpret_cast<cppu::Enterable *>(pEnv->pReserved); 227cdf0e10cSrcweir if (pEnterable) 228cdf0e10cSrcweir pEnterable->callInto(s_pull, pCallee, pParam); 229cdf0e10cSrcweir 230cdf0e10cSrcweir else 231cdf0e10cSrcweir pCallee(pParam); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir static void s_callInto(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir va_list param; 237cdf0e10cSrcweir 238cdf0e10cSrcweir va_start(param, pCallee); 239cdf0e10cSrcweir s_callInto_v(pEnv, pCallee, ¶m); 240cdf0e10cSrcweir va_end(param); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir static void s_callOut_v(uno_Environment * pEnv, uno_EnvCallee * pCallee, va_list * pParam) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir cppu::Enterable * pEnterable = reinterpret_cast<cppu::Enterable *>(pEnv->pReserved); 246cdf0e10cSrcweir if (pEnterable) 247cdf0e10cSrcweir pEnterable->callOut_v(pCallee, pParam); 248cdf0e10cSrcweir 249cdf0e10cSrcweir else 250cdf0e10cSrcweir pCallee(pParam); 251cdf0e10cSrcweir } 252cdf0e10cSrcweir 253cdf0e10cSrcweir static void s_callOut(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) 254cdf0e10cSrcweir { 255cdf0e10cSrcweir va_list param; 256cdf0e10cSrcweir 257cdf0e10cSrcweir va_start(param, pCallee); 258cdf0e10cSrcweir s_callOut_v(pEnv, pCallee, ¶m); 259cdf0e10cSrcweir va_end(param); 260cdf0e10cSrcweir } 261cdf0e10cSrcweir 262cdf0e10cSrcweir static void s_environment_invoke_v(uno_Environment *, uno_Environment *, uno_EnvCallee *, va_list *); 263cdf0e10cSrcweir 264cdf0e10cSrcweir extern "C" { static void s_environment_invoke_vv(va_list * pParam) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir uno_Environment * pCurrEnv = va_arg(*pParam, uno_Environment *); 267cdf0e10cSrcweir uno_Environment * pTargetEnv = va_arg(*pParam, uno_Environment *); 268cdf0e10cSrcweir uno_EnvCallee * pCallee = va_arg(*pParam, uno_EnvCallee *); 269cdf0e10cSrcweir va_list * pXparam = va_arg(*pParam, va_list *); 270cdf0e10cSrcweir 271cdf0e10cSrcweir s_environment_invoke_v(pCurrEnv, pTargetEnv, pCallee, pXparam); 272cdf0e10cSrcweir }} 273cdf0e10cSrcweir 274cdf0e10cSrcweir static void s_environment_invoke_v(uno_Environment * pCurrEnv, uno_Environment * pTargetEnv, uno_EnvCallee * pCallee, va_list * pParam) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir uno_Environment * pNextEnv = NULL; 277cdf0e10cSrcweir switch(s_getNextEnv(&pNextEnv, pCurrEnv, pTargetEnv)) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir case -1: 280cdf0e10cSrcweir s_setCurrent(pNextEnv); 281cdf0e10cSrcweir s_callOut(pCurrEnv, s_environment_invoke_vv, pNextEnv, pTargetEnv, pCallee, pParam); 282cdf0e10cSrcweir s_setCurrent(pCurrEnv); 283cdf0e10cSrcweir break; 284cdf0e10cSrcweir 285cdf0e10cSrcweir case 0: { 286cdf0e10cSrcweir uno_Environment * hld = s_getCurrent(); 287cdf0e10cSrcweir s_setCurrent(pCurrEnv); 288cdf0e10cSrcweir pCallee(pParam); 289cdf0e10cSrcweir s_setCurrent(hld); 290cdf0e10cSrcweir } 291cdf0e10cSrcweir break; 292cdf0e10cSrcweir 293cdf0e10cSrcweir case 1: 294cdf0e10cSrcweir s_setCurrent(pNextEnv); 295cdf0e10cSrcweir s_callInto(pNextEnv, s_environment_invoke_vv, pNextEnv, pTargetEnv, pCallee, pParam); 296cdf0e10cSrcweir s_setCurrent(pCurrEnv); 297cdf0e10cSrcweir break; 298cdf0e10cSrcweir } 299cdf0e10cSrcweir 300cdf0e10cSrcweir if (pNextEnv) 301cdf0e10cSrcweir pNextEnv->release(pNextEnv); 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir extern "C" void SAL_CALL uno_Environment_invoke_v(uno_Environment * pTargetEnv, uno_EnvCallee * pCallee, va_list * pParam) 305cdf0e10cSrcweir SAL_THROW_EXTERN_C() 306cdf0e10cSrcweir { 307cdf0e10cSrcweir s_environment_invoke_v(s_getCurrent(), pTargetEnv, pCallee, pParam); 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir extern "C" void SAL_CALL uno_Environment_invoke(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) 311cdf0e10cSrcweir SAL_THROW_EXTERN_C() 312cdf0e10cSrcweir { 313cdf0e10cSrcweir va_list param; 314cdf0e10cSrcweir 315cdf0e10cSrcweir va_start(param, pCallee); 316cdf0e10cSrcweir uno_Environment_invoke_v(pEnv, pCallee, ¶m); 317cdf0e10cSrcweir va_end(param); 318cdf0e10cSrcweir } 319cdf0e10cSrcweir 320cdf0e10cSrcweir extern "C" void SAL_CALL uno_Environment_enter(uno_Environment * pTargetEnv) 321cdf0e10cSrcweir SAL_THROW_EXTERN_C() 322cdf0e10cSrcweir { 323cdf0e10cSrcweir uno_Environment * pNextEnv = NULL; 324cdf0e10cSrcweir uno_Environment * pCurrEnv = s_getCurrent(); 325cdf0e10cSrcweir 326cdf0e10cSrcweir int res; 327cdf0e10cSrcweir while ( (res = s_getNextEnv(&pNextEnv, pCurrEnv, pTargetEnv)) != 0) 328cdf0e10cSrcweir { 329cdf0e10cSrcweir cppu::Enterable * pEnterable; 330cdf0e10cSrcweir 331cdf0e10cSrcweir switch(res) 332cdf0e10cSrcweir { 333cdf0e10cSrcweir case -1: 334cdf0e10cSrcweir pEnterable = reinterpret_cast<cppu::Enterable *>(pCurrEnv->pReserved); 335cdf0e10cSrcweir if (pEnterable) 336cdf0e10cSrcweir pEnterable->leave(); 337cdf0e10cSrcweir pCurrEnv->release(pCurrEnv); 338cdf0e10cSrcweir break; 339cdf0e10cSrcweir 340cdf0e10cSrcweir case 1: 341cdf0e10cSrcweir pNextEnv->acquire(pNextEnv); 342cdf0e10cSrcweir pEnterable = reinterpret_cast<cppu::Enterable *>(pNextEnv->pReserved); 343cdf0e10cSrcweir if (pEnterable) 344cdf0e10cSrcweir pEnterable->enter(); 345cdf0e10cSrcweir break; 346cdf0e10cSrcweir } 347cdf0e10cSrcweir 348cdf0e10cSrcweir s_setCurrent(pNextEnv); 349cdf0e10cSrcweir pCurrEnv = pNextEnv; 350cdf0e10cSrcweir } 351cdf0e10cSrcweir } 352cdf0e10cSrcweir 353cdf0e10cSrcweir int SAL_CALL uno_Environment_isValid(uno_Environment * pEnv, rtl_uString ** pReason) 354cdf0e10cSrcweir SAL_THROW_EXTERN_C() 355cdf0e10cSrcweir { 356cdf0e10cSrcweir int result = 1; 357cdf0e10cSrcweir 358cdf0e10cSrcweir rtl::OUString typeName(cppu::EnvDcp::getTypeName(pEnv->pTypeName)); 359cdf0e10cSrcweir if (typeName.equals(s_uno_envDcp)) 360cdf0e10cSrcweir { 361cdf0e10cSrcweir cppu::Enterable * pEnterable = reinterpret_cast<cppu::Enterable *>(pEnv->pReserved); 362cdf0e10cSrcweir if (pEnterable) 363cdf0e10cSrcweir result = pEnterable->isValid((rtl::OUString *)pReason); 364cdf0e10cSrcweir } 365cdf0e10cSrcweir else 366cdf0e10cSrcweir { 367cdf0e10cSrcweir rtl::OUString envDcp(s_uno_envDcp); 368cdf0e10cSrcweir envDcp += cppu::EnvDcp::getPurpose(pEnv->pTypeName); 369cdf0e10cSrcweir 370cdf0e10cSrcweir uno::Environment env(envDcp); 371cdf0e10cSrcweir 372cdf0e10cSrcweir result = env.isValid((rtl::OUString *)pReason); 373cdf0e10cSrcweir } 374cdf0e10cSrcweir 375cdf0e10cSrcweir return result; 376cdf0e10cSrcweir } 377