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