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 51712 : extern "C" void SAL_CALL proxy_acquireInterface(uno_Interface * pInterface) {
45 : assert(pInterface != 0);
46 51712 : static_cast< Proxy * >(pInterface)->do_acquire();
47 51712 : }
48 :
49 55686 : extern "C" void SAL_CALL proxy_releaseInterface(uno_Interface * pInterface) {
50 : assert(pInterface != 0);
51 55686 : static_cast< Proxy * >(pInterface)->do_release();
52 55686 : }
53 :
54 14142 : 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 14142 : pMemberType, pReturn, pArgs, ppException);
61 14142 : }
62 :
63 : }
64 :
65 3990 : Proxy::Proxy(
66 : rtl::Reference< Bridge > const & bridge, OUString const & oid,
67 : css::uno::TypeDescription const & type):
68 3990 : bridge_(bridge), oid_(oid), type_(type), references_(1)
69 : {
70 : assert(bridge.is());
71 3990 : acquire = &proxy_acquireInterface;
72 3990 : release = &proxy_releaseInterface;
73 3990 : pDispatcher = &proxy_dispatchInterface;
74 3990 : }
75 :
76 :
77 :
78 51712 : void Proxy::do_acquire() {
79 51712 : if (osl_atomic_increment(&references_) == 1) {
80 1 : bridge_->resurrectProxy(*this);
81 : }
82 51712 : }
83 :
84 55686 : void Proxy::do_release() {
85 55686 : if (osl_atomic_decrement(&references_) == 0) {
86 3975 : bridge_->revokeProxy(*this);
87 : }
88 55686 : }
89 :
90 3974 : void Proxy::do_free() {
91 3974 : bridge_->freeProxy(*this);
92 3974 : delete this;
93 3974 : }
94 :
95 14142 : void Proxy::do_dispatch(
96 : typelib_TypeDescription const * member, void * returnValue,
97 : void ** arguments, uno_Any ** exception) const
98 : {
99 : try {
100 : try {
101 14142 : do_dispatch_throw(member, returnValue, arguments, exception);
102 0 : } catch (const std::exception & e) {
103 : throw css::uno::RuntimeException(
104 0 : "caught C++ exception: " +
105 : OStringToOUString(
106 0 : OString(e.what()), RTL_TEXTENCODING_ASCII_US));
107 : // best-effort string conversion
108 : }
109 4 : } catch (const css::uno::RuntimeException &) {
110 4 : css::uno::Any exc(cppu::getCaughtException());
111 : uno_copyAndConvertData(
112 : *exception, &exc,
113 4 : (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
114 : get()),
115 8 : bridge_->getCppToBinaryMapping().get());
116 : }
117 14142 : }
118 :
119 152406 : bool Proxy::isProxy(
120 : rtl::Reference< Bridge > const & bridge,
121 : css::uno::UnoInterfaceReference const & object, OUString * oid)
122 : {
123 : assert(object.is());
124 152668 : return object.m_pUnoI->acquire == &proxy_acquireInterface &&
125 152668 : static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
126 : }
127 :
128 3974 : Proxy::~Proxy() {}
129 :
130 14142 : void Proxy::do_dispatch_throw(
131 : typelib_TypeDescription const * member, void * returnValue,
132 : void ** arguments, uno_Any ** exception) const
133 : {
134 : //TODO: Optimize queryInterface:
135 : assert(member != 0);
136 14142 : bool setter = false;
137 14142 : std::vector< BinaryAny > inArgs;
138 14142 : switch (member->eTypeClass) {
139 : case typelib_TypeClass_INTERFACE_ATTRIBUTE:
140 1799 : setter = returnValue == 0;
141 1799 : if (setter) {
142 : inArgs.push_back(
143 : BinaryAny(
144 : css::uno::TypeDescription(
145 : reinterpret_cast<
146 : typelib_InterfaceAttributeTypeDescription const * >(
147 : member)->
148 : pAttributeTypeRef),
149 0 : arguments[0]));
150 : }
151 1799 : break;
152 : case typelib_TypeClass_INTERFACE_METHOD:
153 : {
154 : typelib_InterfaceMethodTypeDescription const * mtd =
155 : reinterpret_cast<
156 12343 : typelib_InterfaceMethodTypeDescription const * >(member);
157 24060 : for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
158 11717 : if (mtd->pParams[i].bIn) {
159 : inArgs.push_back(
160 : BinaryAny(
161 11703 : css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
162 23406 : arguments[i]));
163 : }
164 : }
165 12343 : break;
166 : }
167 : default:
168 : assert(false); // this cannot happen
169 0 : break;
170 : }
171 14142 : BinaryAny ret;
172 28284 : std::vector< BinaryAny > outArgs;
173 28280 : if (bridge_->makeCall(
174 : oid_,
175 : css::uno::TypeDescription(
176 : const_cast< typelib_TypeDescription * >(member)),
177 28284 : setter, inArgs, &ret, &outArgs))
178 : {
179 : assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
180 : uno_any_construct(
181 32 : *exception, ret.getValue(ret.getType()), ret.getType().get(), 0);
182 : } else {
183 14106 : switch (member->eTypeClass) {
184 : case typelib_TypeClass_INTERFACE_ATTRIBUTE:
185 1799 : if (!setter) {
186 : css::uno::TypeDescription t(
187 : reinterpret_cast<
188 : typelib_InterfaceAttributeTypeDescription const * >(
189 : member)->
190 1799 : pAttributeTypeRef);
191 1799 : uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
192 : }
193 1799 : break;
194 : case typelib_TypeClass_INTERFACE_METHOD:
195 : {
196 : typelib_InterfaceMethodTypeDescription const * mtd =
197 : reinterpret_cast<
198 : typelib_InterfaceMethodTypeDescription const * >(
199 12307 : member);
200 12307 : css::uno::TypeDescription t(mtd->pReturnTypeRef);
201 12307 : if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
202 2080 : uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
203 : }
204 12307 : std::vector< BinaryAny >::iterator i(outArgs.begin());
205 24020 : for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
206 11713 : if (mtd->pParams[j].bOut) {
207 14 : css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
208 14 : if (mtd->pParams[j].bIn) {
209 : (void) uno_assignData(
210 0 : arguments[j], pt.get(), i++->getValue(pt),
211 0 : pt.get(), 0, 0, 0);
212 : } else {
213 : uno_copyData(
214 14 : arguments[j], i++->getValue(pt), pt.get(), 0);
215 14 : }
216 : }
217 : }
218 : assert(i == outArgs.end());
219 12307 : break;
220 : }
221 : default:
222 : assert(false); // this cannot happen
223 0 : break;
224 : }
225 14106 : *exception = 0;
226 14142 : }
227 14138 : }
228 :
229 262 : bool Proxy::isProxy(
230 : rtl::Reference< Bridge > const & bridge, OUString * oid) const
231 : {
232 : assert(oid != 0);
233 262 : if (bridge == bridge_) {
234 262 : *oid = oid_;
235 262 : return true;
236 : } else {
237 0 : return false;
238 : }
239 : }
240 :
241 : }
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|