LibreOffice Module binaryurp (master) 1
incomingrequest.cxx
Go to the documentation of this file.
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 <utility>
24#include <vector>
25
26#include <com/sun/star/bridge/XInstanceProvider.hpp>
27#include <com/sun/star/container/NoSuchElementException.hpp>
30#include <rtl/byteseq.hxx>
31#include <rtl/ref.hxx>
32#include <rtl/ustring.hxx>
33#include <sal/log.hxx>
34#include <sal/types.h>
35#include <typelib/typedescription.hxx>
36#include <uno/dispatcher.hxx>
37
38#include "binaryany.hxx"
39#include "bridge.hxx"
40#include "currentcontext.hxx"
41#include "incomingrequest.hxx"
43
44namespace binaryurp {
45
47 rtl::Reference< Bridge > const & bridge, rtl::ByteSequence tid,
48 OUString oid, css::uno::UnoInterfaceReference object,
49 css::uno::TypeDescription type, sal_uInt16 functionId,
50 bool synchronous, css::uno::TypeDescription const & member, bool setter,
51 std::vector< BinaryAny >&& inArguments, bool currentContextMode,
52 css::uno::UnoInterfaceReference currentContext):
53 bridge_(bridge), tid_(std::move(tid)), oid_(std::move(oid)), object_(std::move(object)), type_(std::move(type)),
54 member_(member), currentContext_(std::move(currentContext)),
55 inArguments_(std::move(inArguments)), functionId_(functionId),
56 synchronous_(synchronous), setter_(setter), currentContextMode_(currentContextMode)
57{
58 assert(bridge.is());
59 assert(member.is());
60 assert(member.get()->bComplete);
61}
62
63IncomingRequest::~IncomingRequest() {}
64
65void IncomingRequest::execute() const {
66 BinaryAny ret;
67 std::vector< BinaryAny > outArgs;
68 bool isExc;
69 try {
70 bool resetCc = false;
71 css::uno::UnoInterfaceReference oldCc;
72 if (currentContextMode_) {
73 oldCc = current_context::get();
74 current_context::set(currentContext_);
75 resetCc = true;
76 }
77 try {
78 try {
79 isExc = !execute_throw(&ret, &outArgs);
80 } catch (const std::exception & e) {
81 throw css::uno::RuntimeException(
82 "caught C++ exception: "
83 + o3tl::runtimeToOUString(e.what()));
84 }
85 } catch (const css::uno::RuntimeException &) {
86 css::uno::Any exc(cppu::getCaughtException());
87 ret = bridge_->mapCppToBinaryAny(exc);
88 isExc = true;
89 }
90 if (resetCc) {
92 }
93 } catch (const css::uno::RuntimeException &) {
94 css::uno::Any exc(cppu::getCaughtException());
95 ret = bridge_->mapCppToBinaryAny(exc);
96 isExc = true;
97 }
98 if (synchronous_) {
99 bridge_->decrementActiveCalls();
100 try {
101 bridge_->getWriter()->queueReply(
102 tid_, member_, setter_, isExc, ret, std::move(outArgs), false);
103 return;
104 } catch (const css::uno::RuntimeException & e) {
105 SAL_INFO("binaryurp", "caught " << e);
106 } catch (const std::exception & e) {
107 SAL_INFO("binaryurp", "caught C++ exception " << e.what());
108 }
109 bridge_->terminate(false);
110 } else {
111 if (isExc) {
112 SAL_INFO("binaryurp", "oneway method raised exception");
113 }
114 bridge_->decrementCalls();
115 }
116}
117
118static size_t size_t_round(size_t val)
119{
120 return (val + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
121}
122
123bool IncomingRequest::execute_throw(
124 BinaryAny * returnValue, std::vector< BinaryAny > * outArguments) const
125{
126 assert(returnValue != nullptr);
127 assert(
128 returnValue->getType().equals(
129 css::uno::TypeDescription(cppu::UnoType<void>::get())));
130 assert(outArguments != nullptr);
131 assert(outArguments->empty());
132 bool isExc = false;
133 switch (functionId_) {
135 assert(false); // this cannot happen
136 break;
138 bridge_->releaseStub(oid_, type_);
139 break;
141 if (!object_.is()) {
142 css::uno::Reference< css::uno::XInterface > ifc;
143 css::uno::Reference< css::bridge::XInstanceProvider > prov(
144 bridge_->getProvider());
145 if (prov.is()) {
146 try {
147 ifc = prov->getInstance(oid_);
148 } catch (const css::container::NoSuchElementException & e) {
149 SAL_INFO("binaryurp", "initial element " << oid_ << ": " << e);
150 }
151 }
152 if (ifc.is()) {
153 css::uno::UnoInterfaceReference unoIfc(
154 static_cast< uno_Interface * >(
155 bridge_->getCppToBinaryMapping().mapInterface(
156 ifc.get(),
157 (css::uno::TypeDescription(
159 css::uno::Reference<
160 css::uno::XInterface > >::get()).
161 get()))),
162 SAL_NO_ACQUIRE);
163 *returnValue = BinaryAny(
164 css::uno::TypeDescription(
166 css::uno::Reference<
167 css::uno::XInterface > >::get()),
168 &unoIfc.m_pUnoI);
169 }
170 break;
171 }
172 [[fallthrough]];
173 default:
174 {
175 assert(object_.is());
176 css::uno::TypeDescription retType;
177 std::vector< std::vector< char > > outBufs;
178 std::vector< void * > args;
179 switch (member_.get()->eTypeClass) {
180 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
181 {
182 css::uno::TypeDescription t(
183 reinterpret_cast<
184 typelib_InterfaceAttributeTypeDescription * >(
185 member_.get())->
186 pAttributeTypeRef);
187 if (setter_) {
188 assert(inArguments_.size() == 1);
189 args.push_back(inArguments_[0].getValue(t));
190 } else {
191 assert(inArguments_.empty());
192 retType = t;
193 }
194 break;
195 }
196 case typelib_TypeClass_INTERFACE_METHOD:
197 {
198 typelib_InterfaceMethodTypeDescription * mtd =
199 reinterpret_cast<
200 typelib_InterfaceMethodTypeDescription * >(
201 member_.get());
202 retType = css::uno::TypeDescription(mtd->pReturnTypeRef);
203 std::vector< BinaryAny >::const_iterator i(
204 inArguments_.begin());
205 for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
206 void * p;
207 if (mtd->pParams[j].bIn) {
208 p = i++->getValue(
209 css::uno::TypeDescription(
210 mtd->pParams[j].pTypeRef));
211 } else {
212 outBufs.emplace_back(size_t_round(
213 css::uno::TypeDescription(
214 mtd->pParams[j].pTypeRef).
215 get()->nSize));
216 p = outBufs.back().data();
217 }
218 args.push_back(p);
219 if (mtd->pParams[j].bOut) {
220 outArguments->push_back(BinaryAny());
221 }
222 }
223 assert(i == inArguments_.end());
224 break;
225 }
226 default:
227 assert(false); // this cannot happen
228 break;
229 }
230 size_t nSize = 0;
231 if (retType.is())
232 nSize = size_t_round(retType.get()->nSize);
233 std::vector< char > retBuf(nSize);
234 uno_Any exc;
235 uno_Any * pexc = &exc;
236 (*object_.get()->pDispatcher)(
237 object_.get(), member_.get(), retBuf.empty() ? nullptr : retBuf.data(),
238 args.empty() ? nullptr : args.data(), &pexc);
239 isExc = pexc != nullptr;
240 if (isExc) {
241 *returnValue = BinaryAny(
242 css::uno::TypeDescription(
244 &exc);
245 uno_any_destruct(&exc, nullptr);
246 } else {
247 if (!retBuf.empty()) {
248 *returnValue = BinaryAny(retType, retBuf.data());
249 uno_destructData(retBuf.data(), retType.get(), nullptr);
250 }
251 if (!outArguments->empty()) {
252 assert(
253 member_.get()->eTypeClass ==
254 typelib_TypeClass_INTERFACE_METHOD);
255 typelib_InterfaceMethodTypeDescription * mtd =
256 reinterpret_cast<
257 typelib_InterfaceMethodTypeDescription * >(
258 member_.get());
259 std::vector< BinaryAny >::iterator i(outArguments->begin());
260 std::vector< std::vector< char > >::iterator j(
261 outBufs.begin());
262 for (sal_Int32 k = 0; k != mtd->nParams; ++k) {
263 if (mtd->pParams[k].bOut) {
264 *i++ = BinaryAny(
265 css::uno::TypeDescription(
266 mtd->pParams[k].pTypeRef),
267 args[k]);
268 }
269 if (!mtd->pParams[k].bIn) {
271 (j++)->data(), mtd->pParams[k].pTypeRef, nullptr);
272 }
273 }
274 assert(i == outArguments->end());
275 assert(j == outBufs.end());
276 }
277 }
278 break;
279 }
280 }
281 return !isExc;
282}
283
284}
285
286/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
rtl::ByteSequence tid_
Definition: bridge.cxx:114
com::sun::star::uno::TypeDescription getType() const noexcept
Definition: binaryany.cxx:97
IncomingRequest(const IncomingRequest &)=delete
void SAL_CALL uno_type_destructData(void *pValue, typelib_TypeDescriptionReference *pType, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_destructData(void *pValue, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
T * object_
GVariantType * type_
void * p
#define SAL_INFO(area, stream)
struct _uno_Any uno_Any
void set(css::uno::UnoInterfaceReference const &value)
css::uno::UnoInterfaceReference get()
@ SPECIAL_FUNCTION_ID_QUERY_INTERFACE
static size_t size_t_round(size_t val)
Any SAL_CALL getCaughtException()
int i
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
OUString runtimeToOUString(char const *runtimeString)
args
ResultType type