lok: jsbuilder: avoid sending early id to client side
[LibreOffice.git] / binaryurp / source / proxy.cxx
blob0e5a92b68f9e98bed78ea8d64d5fac7c3c3bfaf4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <exception>
24 #include <vector>
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <o3tl/runtimetooustring.hxx>
28 #include <rtl/ref.hxx>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
31 #include <typelib/typedescription.h>
32 #include <typelib/typedescription.hxx>
33 #include <uno/any2.h>
34 #include <uno/dispatcher.h>
35 #include <uno/dispatcher.hxx>
37 #include "binaryany.hxx"
38 #include "bridge.hxx"
39 #include "proxy.hxx"
41 namespace binaryurp {
43 namespace {
45 extern "C" void proxy_acquireInterface(uno_Interface * pInterface) {
46 assert(pInterface != nullptr);
47 static_cast< Proxy * >(pInterface)->do_acquire();
50 extern "C" void proxy_releaseInterface(uno_Interface * pInterface) {
51 assert(pInterface != nullptr);
52 static_cast< Proxy * >(pInterface)->do_release();
55 extern "C" void proxy_dispatchInterface(
56 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
57 void * pReturn, void ** pArgs, uno_Any ** ppException)
59 assert(pUnoI != nullptr);
60 static_cast< Proxy * >(pUnoI)->do_dispatch(
61 pMemberType, pReturn, pArgs, ppException);
66 Proxy::Proxy(
67 rtl::Reference< Bridge > const & bridge, OUString const & oid,
68 css::uno::TypeDescription const & type):
69 bridge_(bridge), oid_(oid), type_(type), references_(1)
71 assert(bridge.is());
72 acquire = &proxy_acquireInterface;
73 release = &proxy_releaseInterface;
74 pDispatcher = &proxy_dispatchInterface;
78 void Proxy::do_acquire() {
79 if (++references_ == 1) {
80 bridge_->resurrectProxy(*this);
84 void Proxy::do_release() {
85 if (--references_ == 0) {
86 bridge_->revokeProxy(*this);
90 void Proxy::do_free() {
91 bridge_->freeProxy(*this);
92 delete this;
95 void Proxy::do_dispatch(
96 typelib_TypeDescription const * member, void * returnValue,
97 void ** arguments, uno_Any ** exception) const
99 try {
100 try {
101 do_dispatch_throw(member, returnValue, arguments, exception);
102 } catch (const std::exception & e) {
103 throw css::uno::RuntimeException(
104 "caught C++ exception: " + o3tl::runtimeToOUString(e.what()));
106 } catch (const css::uno::RuntimeException &) {
107 css::uno::Any exc(cppu::getCaughtException());
108 uno_copyAndConvertData(
109 *exception, &exc,
110 (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
111 get()),
112 bridge_->getCppToBinaryMapping().get());
116 bool Proxy::isProxy(
117 rtl::Reference< Bridge > const & bridge,
118 css::uno::UnoInterfaceReference const & object, OUString * oid)
120 assert(object.is());
121 return object.m_pUnoI->acquire == &proxy_acquireInterface &&
122 static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
125 Proxy::~Proxy() {}
127 void Proxy::do_dispatch_throw(
128 typelib_TypeDescription const * member, void * returnValue,
129 void ** arguments, uno_Any ** exception) const
131 //TODO: Optimize queryInterface:
132 assert(member != nullptr);
133 bool bSetter = false;
134 std::vector< BinaryAny > inArgs;
135 switch (member->eTypeClass) {
136 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
137 bSetter = returnValue == nullptr;
138 if (bSetter) {
139 inArgs.emplace_back(
140 css::uno::TypeDescription(
141 reinterpret_cast<
142 typelib_InterfaceAttributeTypeDescription const * >(
143 member)->
144 pAttributeTypeRef),
145 arguments[0]);
147 break;
148 case typelib_TypeClass_INTERFACE_METHOD:
150 typelib_InterfaceMethodTypeDescription const * mtd =
151 reinterpret_cast<
152 typelib_InterfaceMethodTypeDescription const * >(member);
153 for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
154 if (mtd->pParams[i].bIn) {
155 inArgs.emplace_back(
156 css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
157 arguments[i]);
160 break;
162 default:
163 assert(false); // this cannot happen
164 break;
166 BinaryAny ret;
167 std::vector< BinaryAny > outArgs;
168 if (bridge_->makeCall(
169 oid_,
170 css::uno::TypeDescription(
171 const_cast< typelib_TypeDescription * >(member)),
172 bSetter, inArgs, &ret, &outArgs))
174 assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
175 uno_any_construct(
176 *exception, ret.getValue(ret.getType()), ret.getType().get(), nullptr);
177 } else {
178 switch (member->eTypeClass) {
179 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
180 if (!bSetter) {
181 css::uno::TypeDescription t(
182 reinterpret_cast<
183 typelib_InterfaceAttributeTypeDescription const * >(
184 member)->
185 pAttributeTypeRef);
186 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
188 break;
189 case typelib_TypeClass_INTERFACE_METHOD:
191 typelib_InterfaceMethodTypeDescription const * mtd =
192 reinterpret_cast<
193 typelib_InterfaceMethodTypeDescription const * >(
194 member);
195 css::uno::TypeDescription t(mtd->pReturnTypeRef);
196 if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
197 uno_copyData(returnValue, ret.getValue(t), t.get(), nullptr);
199 std::vector< BinaryAny >::iterator i(outArgs.begin());
200 for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
201 if (mtd->pParams[j].bOut) {
202 css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
203 if (mtd->pParams[j].bIn) {
204 (void) uno_assignData(
205 arguments[j], pt.get(), i++->getValue(pt),
206 pt.get(), nullptr, nullptr, nullptr);
207 } else {
208 uno_copyData(
209 arguments[j], i++->getValue(pt), pt.get(), nullptr);
213 assert(i == outArgs.end());
214 break;
216 default:
217 assert(false); // this cannot happen
218 break;
220 *exception = nullptr;
224 bool Proxy::isProxy(
225 rtl::Reference< Bridge > const & bridge, OUString * oid) const
227 assert(oid != nullptr);
228 if (bridge == bridge_) {
229 *oid = oid_;
230 return true;
231 } else {
232 return false;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */