Branch data 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 <list>
23 : : #include <vector>
24 : :
25 : : #include "boost/noncopyable.hpp"
26 : : #include "com/sun/star/bridge/XInstanceProvider.hpp"
27 : : #include "cppuhelper/exc_hlp.hxx"
28 : : #include "rtl/byteseq.hxx"
29 : : #include "rtl/ref.hxx"
30 : : #include "rtl/ustring.hxx"
31 : : #include "sal/types.h"
32 : : #include "typelib/typedescription.hxx"
33 : : #include "uno/dispatcher.hxx"
34 : :
35 : : #include "binaryany.hxx"
36 : : #include "bridge.hxx"
37 : : #include "currentcontext.hxx"
38 : : #include "incomingrequest.hxx"
39 : : #include "specialfunctionids.hxx"
40 : :
41 : : namespace binaryurp {
42 : :
43 : : namespace {
44 : :
45 : : namespace css = com::sun::star;
46 : :
47 : : }
48 : :
49 : 463157 : IncomingRequest::IncomingRequest(
50 : : rtl::Reference< Bridge > const & bridge, rtl::ByteSequence const & tid,
51 : : OUString const & oid, css::uno::UnoInterfaceReference const & object,
52 : : css::uno::TypeDescription const & type, sal_uInt16 functionId,
53 : : bool synchronous, css::uno::TypeDescription const & member, bool setter,
54 : : std::vector< BinaryAny > const & inArguments, bool currentContextMode,
55 : : css::uno::UnoInterfaceReference const & currentContext):
56 : : bridge_(bridge), tid_(tid), oid_(oid), object_(object), type_(type),
57 : : functionId_(functionId), synchronous_(synchronous), member_(member),
58 : : setter_(setter), inArguments_(inArguments),
59 [ + - ][ + - ]: 463157 : currentContextMode_(currentContextMode), currentContext_(currentContext)
[ + - ]
60 : : {
61 : : OSL_ASSERT(bridge.is() && member.is() && member.get()->bComplete);
62 : 463157 : }
63 : :
64 [ + - ][ + - ]: 463154 : IncomingRequest::~IncomingRequest() {}
65 : :
66 : 463148 : void IncomingRequest::execute() const {
67 : 463148 : BinaryAny ret;
68 [ + - ]: 463156 : std::vector< BinaryAny > outArgs;
69 : : bool isExc;
70 : : try {
71 : 463147 : bool resetCc = false;
72 : 463147 : css::uno::UnoInterfaceReference oldCc;
73 [ + + ]: 463157 : if (currentContextMode_) {
74 [ + - ][ + - ]: 366723 : oldCc = current_context::get();
[ + - ]
75 [ + - ]: 366723 : current_context::set(currentContext_);
76 : 366723 : resetCc = true;
77 : : }
78 : : try {
79 : : try {
80 [ + + ]: 463157 : isExc = !execute_throw(&ret, &outArgs);
81 [ + - ]: 110 : } catch (const std::exception & e) {
82 : : throw css::uno::RuntimeException(
83 : : (OUString(
84 : : RTL_CONSTASCII_USTRINGPARAM("caught C++ exception: ")) +
85 : : rtl::OStringToOUString(
86 : 0 : rtl::OString(e.what()), RTL_TEXTENCODING_ASCII_US)),
87 [ # # # # : 0 : css::uno::Reference< css::uno::XInterface >());
# # ]
88 : : // best-effort string conversion
89 : : }
90 [ - + ][ + - ]: 220 : } catch (const css::uno::RuntimeException &) {
91 [ - + ]: 110 : css::uno::Any exc(cppu::getCaughtException());
92 [ - + ]: 110 : ret = bridge_->mapCppToBinaryAny(exc);
93 : 110 : isExc = true;
94 : : }
95 [ + + ]: 463157 : if (resetCc) {
96 [ + - ]: 366723 : current_context::set(oldCc);
97 [ + - ]: 463157 : }
98 [ # # # # ]: 0 : } catch (const css::uno::RuntimeException &) {
99 [ # # ]: 0 : css::uno::Any exc(cppu::getCaughtException());
100 [ # # ]: 0 : ret = bridge_->mapCppToBinaryAny(exc);
101 : 0 : isExc = true;
102 : : }
103 [ + + ]: 463157 : if (synchronous_) {
104 : 366723 : bridge_->decrementActiveCalls();
105 : : try {
106 : : bridge_->getWriter()->queueReply(
107 [ + - ][ + - ]: 366723 : tid_, member_, setter_, isExc, ret, outArgs, false);
[ - + - ]
[ + + ]
108 : 463157 : return;
109 [ + - ]: 6 : } catch (const css::uno::RuntimeException & e) {
110 : : OSL_TRACE(
111 : : OSL_LOG_PREFIX "caught UNO runtime exception '%s'",
112 : : (OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).
113 : : getStr()));
114 : 0 : } catch (const std::exception & e) {
115 : : OSL_TRACE(OSL_LOG_PREFIX "caught C++ exception '%s'", e.what());
116 : : }
117 [ + - ]: 6 : bridge_->terminate(false);
118 : : } else {
119 : : if (isExc) {
120 : : OSL_TRACE(OSL_LOG_PREFIX "oneway method raised exception");
121 : : }
122 [ + - ]: 96440 : bridge_->decrementCalls();
123 [ + + ][ + + ]: 463157 : }
124 : : }
125 : :
126 : 366217 : static size_t size_t_round(size_t val)
127 : : {
128 : 366217 : return (val + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
129 : : }
130 : :
131 : 463150 : bool IncomingRequest::execute_throw(
132 : : BinaryAny * returnValue, std::vector< BinaryAny > * outArguments) const
133 : : {
134 : : OSL_ASSERT(
135 : : returnValue != 0 &&
136 : : returnValue->getType().equals(
137 : : css::uno::TypeDescription(
138 : : cppu::UnoType< cppu::UnoVoidType >::get())) &&
139 : : outArguments != 0 && outArguments->empty());
140 : 463150 : bool isExc = false;
141 [ + + + ]: 463150 : switch (functionId_) {
142 : : case SPECIAL_FUNCTION_ID_RESERVED:
143 : : OSL_ASSERT(false); // this cannot happen
144 : 0 : break;
145 : : case SPECIAL_FUNCTION_ID_RELEASE:
146 : 96434 : bridge_->releaseStub(oid_, type_);
147 : 96324 : break;
148 : : case SPECIAL_FUNCTION_ID_QUERY_INTERFACE:
149 [ + + ]: 41697 : if (!object_.is()) {
150 : 132 : css::uno::Reference< css::uno::XInterface > ifc;
151 : : css::uno::Reference< css::bridge::XInstanceProvider > prov(
152 [ + - ]: 132 : bridge_->getProvider());
153 [ + - ]: 132 : if (prov.is()) {
154 : : try {
155 [ + - ][ + - ]: 132 : ifc = prov->getInstance(oid_);
[ + - ][ # # ]
156 [ # # ]: 0 : } catch (const css::container::NoSuchElementException & e) {
157 : : OSL_TRACE(
158 : : (OSL_LOG_PREFIX "initial element '%s':"
159 : : " NoSuchElementException '%s'"),
160 : : (OUStringToOString(oid_, RTL_TEXTENCODING_UTF8).
161 : : getStr()),
162 : : (OUStringToOString(
163 : : e.Message, RTL_TEXTENCODING_UTF8).
164 : : getStr()));
165 : : }
166 : : }
167 [ + - ]: 132 : if (ifc.is()) {
168 : : css::uno::UnoInterfaceReference unoIfc(
169 : : static_cast< uno_Interface * >(
170 [ + - ]: 132 : bridge_->getCppToBinaryMapping().mapInterface(
171 [ + - ]: 132 : ifc.get(),
172 : : (css::uno::TypeDescription(
173 : : cppu::UnoType<
174 : : css::uno::Reference<
175 [ + - ]: 132 : css::uno::XInterface > >::get()).
176 [ + - ]: 396 : get()))),
177 : 132 : SAL_NO_ACQUIRE);
178 : : *returnValue = BinaryAny(
179 : : css::uno::TypeDescription(
180 : : cppu::UnoType<
181 : : css::uno::Reference<
182 [ + - ]: 132 : css::uno::XInterface > >::get()),
183 [ + - ]: 132 : &unoIfc.m_pUnoI);
184 : : }
185 : 132 : break;
186 : : }
187 : : // fall through
188 : : default:
189 : : {
190 : : OSL_ASSERT(object_.is());
191 : 366585 : css::uno::TypeDescription retType;
192 [ + - ]: 366591 : std::list< std::vector< char > > outBufs;
193 [ + - ]: 366591 : std::vector< void * > args;
194 [ + + - ]: 366591 : switch (member_.get()->eTypeClass) {
195 : : case typelib_TypeClass_INTERFACE_ATTRIBUTE:
196 : : {
197 : : css::uno::TypeDescription t(
198 : : reinterpret_cast<
199 : : typelib_InterfaceAttributeTypeDescription * >(
200 : 7514 : member_.get())->
201 : 7514 : pAttributeTypeRef);
202 [ + + ]: 7514 : if (setter_) {
203 : : OSL_ASSERT(inArguments_.size() == 1);
204 [ + - ]: 482 : args.push_back(inArguments_[0].getValue(t));
205 : : } else {
206 : : OSL_ASSERT(inArguments_.empty());
207 [ + - ]: 7032 : retType = t;
208 : : }
209 : 7514 : break;
210 : : }
211 : : case typelib_TypeClass_INTERFACE_METHOD:
212 : : {
213 : : typelib_InterfaceMethodTypeDescription * mtd =
214 : : reinterpret_cast<
215 : : typelib_InterfaceMethodTypeDescription * >(
216 : 359077 : member_.get());
217 [ + - ]: 359077 : retType = css::uno::TypeDescription(mtd->pReturnTypeRef);
218 : : std::vector< BinaryAny >::const_iterator i(
219 : 359077 : inArguments_.begin());
220 [ + + ]: 659432 : for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
221 : : void * p;
222 [ + + ]: 300355 : if (mtd->pParams[j].bIn) {
223 : : p = i++->getValue(
224 : : css::uno::TypeDescription(
225 [ + - ]: 300247 : mtd->pParams[j].pTypeRef));
226 : : } else {
227 : : outBufs.push_back(
228 : : std::vector< char >(size_t_round(
229 : : css::uno::TypeDescription(
230 : 108 : mtd->pParams[j].pTypeRef).
231 [ + - ][ + - ]: 108 : get()->nSize)));
232 [ + - ][ + - ]: 108 : p = &outBufs.back()[0];
233 : : }
234 [ + - ]: 300355 : args.push_back(p);
235 [ + + ]: 300355 : if (mtd->pParams[j].bOut) {
236 [ + - ]: 202 : outArguments->push_back(BinaryAny());
237 : : }
238 : : }
239 : : OSL_ASSERT(i == inArguments_.end());
240 : : break;
241 : : }
242 : : default:
243 : : OSL_ASSERT(false); // this cannot happen
244 : 0 : break;
245 : : }
246 : 366591 : size_t nSize = 0;
247 [ + + ]: 366591 : if (retType.is())
248 : 366109 : nSize = size_t_round(retType.get()->nSize);
249 [ + - ]: 366591 : std::vector< char > retBuf(nSize);
250 : : uno_Any exc;
251 : 366591 : uno_Any * pexc = &exc;
252 : 366591 : (*object_.get()->pDispatcher)(
253 : 733182 : object_.get(), member_.get(), retBuf.empty() ? 0 : &retBuf[0],
254 [ + - ][ + + ]: 1099773 : args.empty() ? 0 : &args[0], &pexc);
[ + - ][ + - ]
[ + + ]
255 : 366591 : isExc = pexc != 0;
256 [ + + ]: 366591 : if (isExc) {
257 : : *returnValue = BinaryAny(
258 : : css::uno::TypeDescription(
259 : 1784 : cppu::UnoType< css::uno::Any >::get()),
260 : 1784 : &exc);
261 : 1784 : uno_any_destruct(&exc, 0);
262 : : } else {
263 [ + + ]: 364807 : if (!retBuf.empty()) {
264 [ + - ]: 305193 : *returnValue = BinaryAny(retType, &retBuf[0]);
265 [ + - ]: 305193 : uno_destructData(&retBuf[0], retType.get(), 0);
266 : : }
267 [ + + ]: 364807 : if (!outArguments->empty()) {
268 : : OSL_ASSERT(
269 : : member_.get()->eTypeClass ==
270 : : typelib_TypeClass_INTERFACE_METHOD);
271 : : typelib_InterfaceMethodTypeDescription * mtd =
272 : : reinterpret_cast<
273 : : typelib_InterfaceMethodTypeDescription * >(
274 : 162 : member_.get());
275 : 162 : std::vector< BinaryAny >::iterator i(outArguments->begin());
276 : : std::list< std::vector< char > >::iterator j(
277 : 162 : outBufs.begin());
278 [ + + ]: 392 : for (sal_Int32 k = 0; k != mtd->nParams; ++k) {
279 [ + + ]: 230 : if (mtd->pParams[k].bOut) {
280 [ + - ]: 202 : *i++ = BinaryAny(
281 : : css::uno::TypeDescription(
282 : 202 : mtd->pParams[k].pTypeRef),
283 [ + - ]: 404 : args[k]);
284 : : }
285 [ + + ]: 230 : if (!mtd->pParams[k].bIn) {
286 : : uno_type_destructData(
287 [ + - ][ + - ]: 108 : &(*j++)[0], mtd->pParams[k].pTypeRef, 0);
[ + - ]
288 : : }
289 : : }
290 : : OSL_ASSERT(i == outArguments->end());
291 : : OSL_ASSERT(j == outBufs.end());
292 : : }
293 : : }
294 : 366591 : break;
295 : : }
296 : : }
297 : 463046 : return !isExc;
298 : : }
299 : :
300 : : }
301 : :
302 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|