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 : #include "sal/config.h"
21 :
22 : #include <cassert>
23 : #include <exception>
24 : #include <vector>
25 :
26 : #include "cppuhelper/exc_hlp.hxx"
27 : #include "rtl/ref.hxx"
28 : #include "rtl/ustring.hxx"
29 : #include "sal/types.h"
30 : #include "typelib/typedescription.h"
31 : #include "typelib/typedescription.hxx"
32 : #include "uno/any2.h"
33 : #include "uno/dispatcher.h"
34 : #include "uno/dispatcher.hxx"
35 :
36 : #include "binaryany.hxx"
37 : #include "bridge.hxx"
38 : #include "proxy.hxx"
39 :
40 : namespace binaryurp {
41 :
42 : namespace {
43 :
44 25778 : extern "C" void SAL_CALL proxy_acquireInterface(uno_Interface * pInterface) {
45 : assert(pInterface != 0);
46 25778 : static_cast< Proxy * >(pInterface)->do_acquire();
47 25778 : }
48 :
49 27745 : extern "C" void SAL_CALL proxy_releaseInterface(uno_Interface * pInterface) {
50 : assert(pInterface != 0);
51 27745 : static_cast< Proxy * >(pInterface)->do_release();
52 27745 : }
53 :
54 7028 : extern "C" void SAL_CALL proxy_dispatchInterface(
55 : uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
56 : void * pReturn, void ** pArgs, uno_Any ** ppException)
57 : {
58 : assert(pUnoI != 0);
59 : static_cast< Proxy * >(pUnoI)->do_dispatch(
60 7028 : pMemberType, pReturn, pArgs, ppException);
61 7028 : }
62 :
63 : }
64 :
65 1974 : Proxy::Proxy(
66 : rtl::Reference< Bridge > const & bridge, OUString const & oid,
67 : css::uno::TypeDescription const & type):
68 1974 : bridge_(bridge), oid_(oid), type_(type), references_(1)
69 : {
70 : assert(bridge.is());
71 1974 : acquire = &proxy_acquireInterface;
72 1974 : release = &proxy_releaseInterface;
73 1974 : pDispatcher = &proxy_dispatchInterface;
74 1974 : }
75 :
76 1967 : OUString Proxy::getOid() const {
77 1967 : return oid_;
78 : }
79 :
80 1967 : css::uno::TypeDescription Proxy::getType() const {
81 1967 : return type_;
82 : }
83 :
84 25778 : void Proxy::do_acquire() {
85 25778 : if (osl_atomic_increment(&references_) == 1) {
86 0 : bridge_->resurrectProxy(*this);
87 : }
88 25778 : }
89 :
90 27745 : void Proxy::do_release() {
91 27745 : if (osl_atomic_decrement(&references_) == 0) {
92 1967 : bridge_->revokeProxy(*this);
93 : }
94 27745 : }
95 :
96 1967 : void Proxy::do_free() {
97 1967 : bridge_->freeProxy(*this);
98 1967 : delete this;
99 1967 : }
100 :
101 7028 : void Proxy::do_dispatch(
102 : typelib_TypeDescription const * member, void * returnValue,
103 : void ** arguments, uno_Any ** exception) const
104 : {
105 : try {
106 : try {
107 7028 : do_dispatch_throw(member, returnValue, arguments, exception);
108 0 : } catch (const std::exception & e) {
109 : throw css::uno::RuntimeException(
110 0 : ("caught C++ exception: " +
111 : OStringToOUString(
112 0 : OString(e.what()), RTL_TEXTENCODING_ASCII_US)),
113 0 : css::uno::Reference< css::uno::XInterface >());
114 : // best-effort string conversion
115 : }
116 2 : } catch (const css::uno::RuntimeException &) {
117 2 : css::uno::Any exc(cppu::getCaughtException());
118 : uno_copyAndConvertData(
119 : *exception, &exc,
120 2 : (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
121 : get()),
122 4 : bridge_->getCppToBinaryMapping().get());
123 : }
124 7028 : }
125 :
126 75343 : bool Proxy::isProxy(
127 : rtl::Reference< Bridge > const & bridge,
128 : css::uno::UnoInterfaceReference const & object, OUString * oid)
129 : {
130 : assert(object.is());
131 75475 : return object.m_pUnoI->acquire == &proxy_acquireInterface &&
132 75475 : static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
133 : }
134 :
135 1967 : Proxy::~Proxy() {}
136 :
137 7028 : void Proxy::do_dispatch_throw(
138 : typelib_TypeDescription const * member, void * returnValue,
139 : void ** arguments, uno_Any ** exception) const
140 : {
141 : //TODO: Optimize queryInterface:
142 : assert(member != 0);
143 7028 : bool setter = false;
144 7028 : std::vector< BinaryAny > inArgs;
145 7028 : switch (member->eTypeClass) {
146 : case typelib_TypeClass_INTERFACE_ATTRIBUTE:
147 902 : setter = returnValue == 0;
148 902 : if (setter) {
149 : inArgs.push_back(
150 : BinaryAny(
151 : css::uno::TypeDescription(
152 : reinterpret_cast<
153 : typelib_InterfaceAttributeTypeDescription const * >(
154 : member)->
155 : pAttributeTypeRef),
156 0 : arguments[0]));
157 : }
158 902 : break;
159 : case typelib_TypeClass_INTERFACE_METHOD:
160 : {
161 : typelib_InterfaceMethodTypeDescription const * mtd =
162 : reinterpret_cast<
163 6126 : typelib_InterfaceMethodTypeDescription const * >(member);
164 11937 : for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
165 5811 : if (mtd->pParams[i].bIn) {
166 : inArgs.push_back(
167 : BinaryAny(
168 5805 : css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
169 11610 : arguments[i]));
170 : }
171 : }
172 6126 : break;
173 : }
174 : default:
175 : assert(false); // this cannot happen
176 0 : break;
177 : }
178 7028 : BinaryAny ret;
179 14056 : std::vector< BinaryAny > outArgs;
180 14054 : if (bridge_->makeCall(
181 : oid_,
182 : css::uno::TypeDescription(
183 : const_cast< typelib_TypeDescription * >(member)),
184 14056 : setter, inArgs, &ret, &outArgs))
185 : {
186 : assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
187 : uno_any_construct(
188 16 : *exception, ret.getValue(ret.getType()), ret.getType().get(), 0);
189 : } else {
190 7010 : switch (member->eTypeClass) {
191 : case typelib_TypeClass_INTERFACE_ATTRIBUTE:
192 902 : if (!setter) {
193 : css::uno::TypeDescription t(
194 : reinterpret_cast<
195 : typelib_InterfaceAttributeTypeDescription const * >(
196 : member)->
197 902 : pAttributeTypeRef);
198 902 : uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
199 : }
200 902 : break;
201 : case typelib_TypeClass_INTERFACE_METHOD:
202 : {
203 : typelib_InterfaceMethodTypeDescription const * mtd =
204 : reinterpret_cast<
205 : typelib_InterfaceMethodTypeDescription const * >(
206 6108 : member);
207 6108 : css::uno::TypeDescription t(mtd->pReturnTypeRef);
208 6108 : if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
209 1039 : uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
210 : }
211 6108 : std::vector< BinaryAny >::iterator i(outArgs.begin());
212 11917 : for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
213 5809 : if (mtd->pParams[j].bOut) {
214 6 : css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
215 6 : if (mtd->pParams[j].bIn) {
216 : uno_assignData(
217 0 : arguments[j], pt.get(), i++->getValue(pt),
218 0 : pt.get(), 0, 0, 0);
219 : } else {
220 : uno_copyData(
221 6 : arguments[j], i++->getValue(pt), pt.get(), 0);
222 6 : }
223 : }
224 : }
225 : assert(i == outArgs.end());
226 6108 : break;
227 : }
228 : default:
229 : assert(false); // this cannot happen
230 0 : break;
231 : }
232 7010 : *exception = 0;
233 7028 : }
234 7026 : }
235 :
236 132 : bool Proxy::isProxy(
237 : rtl::Reference< Bridge > const & bridge, OUString * oid) const
238 : {
239 : assert(oid != 0);
240 132 : if (bridge == bridge_) {
241 132 : *oid = oid_;
242 132 : return true;
243 : } else {
244 0 : return false;
245 : }
246 : }
247 :
248 : }
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|