| 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 <cppu/helper/purpenv/Mapping.hxx> |
| 22 |
|
| 23 |
#include "Proxy.hxx" |
| 24 |
|
| 25 |
#include <osl/interlck.h> |
| 26 |
#include <sal/log.hxx> |
| 27 |
#include <uno/environment.hxx> |
| 28 |
#include <uno/dispatcher.h> |
| 29 |
|
| 30 |
using namespace com::sun::star; |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
class Mapping : public uno_Mapping |
| 35 |
{ |
| 36 |
uno::Environment m_from; |
| 37 |
uno::Environment m_to; |
| 38 |
|
| 39 |
oslInterlockedCount m_nCount; |
| 40 |
|
| 41 |
cppu::helper::purpenv::ProbeFun * m_probeFun; |
| 42 |
void * m_pContext; |
| 43 |
|
| 44 |
public: |
| 45 |
explicit Mapping(uno_Environment * pFrom, |
| 46 |
uno_Environment * pTo, |
| 47 |
cppu::helper::purpenv::ProbeFun * probeFun, |
| 48 |
void * pProbeContext); |
| 49 |
virtual ~Mapping(); |
| 50 |
|
| 51 |
void mapInterface( |
| 52 |
uno_Interface ** ppOut, |
| 53 |
uno_Interface * pUnoI, |
| 54 |
typelib_InterfaceTypeDescription * pTypeDescr); |
| 55 |
|
| 56 |
void acquire(); |
| 57 |
void release(); |
| 58 |
}; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
static void s_mapInterface( |
| 63 |
uno_Mapping * puno_Mapping, |
| 64 |
void ** ppOut, |
| 65 |
void * pUnoI, |
| 66 |
typelib_InterfaceTypeDescription * pTypeDescr ) noexcept |
| 67 |
{ |
| 68 |
Mapping * pMapping = static_cast<Mapping *>(puno_Mapping); |
| 69 |
pMapping->mapInterface( |
| 70 |
reinterpret_cast<uno_Interface **>(ppOut), |
| 71 |
static_cast<uno_Interface *>(pUnoI), pTypeDescr); |
| 72 |
} |
| 73 |
|
| 74 |
extern "C" { |
| 75 |
static void s_acquire(uno_Mapping * puno_Mapping) noexcept |
| 76 |
{ |
| 77 |
Mapping * pMapping = static_cast<Mapping *>(puno_Mapping); |
| 78 |
pMapping->acquire(); |
| 79 |
} |
| 80 |
|
| 81 |
static void s_release(uno_Mapping * puno_Mapping) noexcept |
| 82 |
{ |
| 83 |
Mapping * pMapping = static_cast<Mapping * >(puno_Mapping); |
| 84 |
pMapping->release(); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
static void s_getIdentifier_v(va_list * pParam) |
| 89 |
{ |
| 90 |
uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *); |
| 91 |
rtl_uString ** ppOid = va_arg(*pParam, rtl_uString **); |
| 92 |
uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *); |
| 93 |
|
| 94 |
pEnv->getObjectIdentifier(pEnv, ppOid, pUnoI); |
| 95 |
} |
| 96 |
|
| 97 |
static void s_free(uno_Mapping * puno_Mapping) noexcept |
| 98 |
{ |
| 99 |
Mapping * pMapping = static_cast<Mapping *>(puno_Mapping); |
| 100 |
delete pMapping; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
Mapping::Mapping(uno_Environment * pFrom, |
| 105 |
uno_Environment * pTo, |
| 106 |
cppu::helper::purpenv::ProbeFun * probeFun, |
| 107 |
void * pProbeContext |
| 108 |
) |
| 109 |
: m_from (pFrom), |
| 110 |
m_to (pTo), |
| 111 |
m_nCount (1), |
| 112 |
m_probeFun(probeFun), |
| 113 |
m_pContext(pProbeContext) |
| 114 |
{ |
| 115 |
SAL_INFO("cppu.purpenv", "LIFE: Mapping::Mapping(uno_Environment * pFrom, uno_Environment * pTo -> " << this); |
| 116 |
|
| 117 |
uno_Mapping::acquire = s_acquire; |
| 118 |
uno_Mapping::release = s_release; |
| 119 |
uno_Mapping::mapInterface = s_mapInterface; |
| 120 |
} |
| 121 |
|
| 122 |
Mapping::~Mapping() |
| 123 |
{ |
| 124 |
SAL_INFO("cppu.purpenv", "LIFE: Mapping:~Mapping() -> " << this); |
| 125 |
} |
| 126 |
|
| 127 |
void Mapping::mapInterface( |
| 128 |
uno_Interface ** ppOut, |
| 129 |
uno_Interface * pUnoI, |
| 130 |
typelib_InterfaceTypeDescription * pTypeDescr) |
| 131 |
{ |
| 132 |
assert(ppOut && pTypeDescr); |
| 133 |
if (*ppOut) |
| 134 |
{ |
| 135 |
(*ppOut)->release(*ppOut); |
| 136 |
*ppOut = nullptr; |
| 137 |
} |
| 138 |
|
| 139 |
if (!pUnoI) |
| 140 |
return; |
| 141 |
|
| 142 |
// get object id of uno interface to be wrapped |
| 143 |
// need to enter environment because of potential "queryInterface" call |
| 144 |
rtl_uString * pOId = nullptr; |
| 145 |
uno_Environment_invoke(m_from.get(), s_getIdentifier_v, m_from.get(), &pOId, pUnoI); |
| 146 |
OSL_ASSERT(pOId); |
| 147 |
|
| 148 |
// try to get any known interface from target environment |
| 149 |
m_to.get()->pExtEnv->getRegisteredInterface(m_to.get()->pExtEnv, reinterpret_cast<void **>(ppOut), pOId, pTypeDescr); |
| 150 |
|
| 151 |
if (!*ppOut) // not yet there, register new proxy interface |
| 152 |
{ |
| 153 |
// try to publish a new proxy (ref count initially 1) |
| 154 |
uno_Interface * pProxy = new Proxy(this, |
| 155 |
m_from.get(), |
| 156 |
m_to.get(), |
| 157 |
pUnoI, |
| 158 |
pTypeDescr, |
| 159 |
pOId, |
| 160 |
m_probeFun, |
| 161 |
m_pContext); |
| 162 |
|
| 163 |
// proxy may be exchanged during registration |
| 164 |
m_to.get()->pExtEnv->registerProxyInterface(m_to.get()->pExtEnv, |
| 165 |
reinterpret_cast<void **>(&pProxy), |
| 166 |
Proxy_free, |
| 167 |
pOId, |
| 168 |
pTypeDescr); |
| 169 |
|
| 170 |
*ppOut = pProxy; |
| 171 |
} |
| 172 |
|
| 173 |
rtl_uString_release(pOId); |
| 174 |
} |
| 175 |
|
| 176 |
void Mapping::acquire() |
| 177 |
{ |
| 178 |
if (osl_atomic_increment(&m_nCount) == 1) |
| 179 |
{ |
| 180 |
uno_Mapping * pMapping = this; |
| 181 |
|
| 182 |
::uno_registerMapping(&pMapping, s_free, m_from.get(), m_to.get(), nullptr); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void Mapping::release() |
| 187 |
{ |
| 188 |
if (osl_atomic_decrement(&m_nCount) == 0) |
| 189 |
::uno_revokeMapping(this); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
namespace cppu::helper::purpenv { |
| 194 |
|
| 195 |
void createMapping(uno_Mapping ** ppMapping, |
| 196 |
uno_Environment * pFrom, |
| 197 |
uno_Environment * pTo, |
| 198 |
ProbeFun * probeFun, |
| 199 |
void * pContext |
| 200 |
) |
| 201 |
{ |
| 202 |
*ppMapping = new Mapping(pFrom, pTo, probeFun, pContext); |
| 203 |
|
| 204 |
::uno_registerMapping(ppMapping, s_free, pFrom, pTo, nullptr); |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |