1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <cassert>
#include <vector>
#include <com/sun/star/bridge/XInstanceProvider.hpp>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <cppuhelper/exc_hlp.hxx>
#include <o3tl/runtimetooustring.hxx>
#include <rtl/byteseq.hxx>
#include <rtl/ref.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <sal/types.h>
#include <typelib/typedescription.hxx>
#include <uno/dispatcher.hxx>
#include "binaryany.hxx"
#include "bridge.hxx"
#include "currentcontext.hxx"
#include "incomingrequest.hxx"
#include "specialfunctionids.hxx"
namespace binaryurp {
IncomingRequest::IncomingRequest(
rtl::Reference< Bridge > const & bridge, rtl::ByteSequence const & tid,
OUString const & oid, css::uno::UnoInterfaceReference const & object,
css::uno::TypeDescription const & type, sal_uInt16 functionId,
bool synchronous, css::uno::TypeDescription const & member, bool setter,
std::vector< BinaryAny >&& inArguments, bool currentContextMode,
css::uno::UnoInterfaceReference const & currentContext):
bridge_(bridge), tid_(tid), oid_(oid), object_(object), type_(type),
member_(member), currentContext_(currentContext),
inArguments_(std::move(inArguments)), functionId_(functionId),
synchronous_(synchronous), setter_(setter), currentContextMode_(currentContextMode)
{
assert(bridge.is());
assert(member.is());
assert(member.get()->bComplete);
}
IncomingRequest::~IncomingRequest() {}
void IncomingRequest::execute() const {
BinaryAny ret;
std::vector< BinaryAny > outArgs;
bool isExc;
try {
bool resetCc = false;
css::uno::UnoInterfaceReference oldCc;
if (currentContextMode_) {
oldCc = current_context::get();
current_context::set(currentContext_);
resetCc = true;
}
try {
try {
isExc = !execute_throw(&ret, &outArgs);
} catch (const std::exception & e) {
throw css::uno::RuntimeException(
"caught C++ exception: "
+ o3tl::runtimeToOUString(e.what()));
}
} catch (const css::uno::RuntimeException &) {
css::uno::Any exc(cppu::getCaughtException());
ret = bridge_->mapCppToBinaryAny(exc);
isExc = true;
}
if (resetCc) {
current_context::set(oldCc);
}
} catch (const css::uno::RuntimeException &) {
css::uno::Any exc(cppu::getCaughtException());
ret = bridge_->mapCppToBinaryAny(exc);
isExc = true;
}
if (synchronous_) {
bridge_->decrementActiveCalls();
try {
bridge_->getWriter()->queueReply(
tid_, member_, setter_, isExc, ret, std::move(outArgs), false);
return;
} catch (const css::uno::RuntimeException & e) {
SAL_INFO("binaryurp", "caught " << e);
} catch (const std::exception & e) {
SAL_INFO("binaryurp", "caught C++ exception " << e.what());
}
bridge_->terminate(false);
} else {
if (isExc) {
SAL_INFO("binaryurp", "oneway method raised exception");
}
bridge_->decrementCalls();
}
}
static size_t size_t_round(size_t val)
{
return (val + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
}
bool IncomingRequest::execute_throw(
BinaryAny * returnValue, std::vector< BinaryAny > * outArguments) const
{
assert(returnValue != nullptr);
assert(
returnValue->getType().equals(
css::uno::TypeDescription(cppu::UnoType<void>::get())));
assert(outArguments != nullptr);
assert(outArguments->empty());
bool isExc = false;
switch (functionId_) {
case SPECIAL_FUNCTION_ID_RESERVED:
assert(false); // this cannot happen
break;
case SPECIAL_FUNCTION_ID_RELEASE:
bridge_->releaseStub(oid_, type_);
break;
case SPECIAL_FUNCTION_ID_QUERY_INTERFACE:
if (!object_.is()) {
css::uno::Reference< css::uno::XInterface > ifc;
css::uno::Reference< css::bridge::XInstanceProvider > prov(
bridge_->getProvider());
if (prov.is()) {
try {
ifc = prov->getInstance(oid_);
} catch (const css::container::NoSuchElementException & e) {
SAL_INFO("binaryurp", "initial element " << oid_ << ": " << e);
}
}
if (ifc.is()) {
css::uno::UnoInterfaceReference unoIfc(
static_cast< uno_Interface * >(
bridge_->getCppToBinaryMapping().mapInterface(
ifc.get(),
(css::uno::TypeDescription(
cppu::UnoType<
css::uno::Reference<
css::uno::XInterface > >::get()).
get()))),
SAL_NO_ACQUIRE);
*returnValue = BinaryAny(
css::uno::TypeDescription(
cppu::UnoType<
css::uno::Reference<
css::uno::XInterface > >::get()),
&unoIfc.m_pUnoI);
}
break;
}
[[fallthrough]];
default:
{
assert(object_.is());
css::uno::TypeDescription retType;
std::vector< std::vector< char > > outBufs;
std::vector< void * > args;
switch (member_.get()->eTypeClass) {
case typelib_TypeClass_INTERFACE_ATTRIBUTE:
{
css::uno::TypeDescription t(
reinterpret_cast<
typelib_InterfaceAttributeTypeDescription * >(
member_.get())->
pAttributeTypeRef);
if (setter_) {
assert(inArguments_.size() == 1);
args.push_back(inArguments_[0].getValue(t));
} else {
assert(inArguments_.empty());
retType = t;
}
break;
}
case typelib_TypeClass_INTERFACE_METHOD:
{
typelib_InterfaceMethodTypeDescription * mtd =
reinterpret_cast<
typelib_InterfaceMethodTypeDescription * >(
member_.get());
retType = css::uno::TypeDescription(mtd->pReturnTypeRef);
std::vector< BinaryAny >::const_iterator i(
inArguments_.begin());
for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
void * p;
if (mtd->pParams[j].bIn) {
p = i++->getValue(
css::uno::TypeDescription(
mtd->pParams[j].pTypeRef));
} else {
outBufs.emplace_back(size_t_round(
css::uno::TypeDescription(
mtd->pParams[j].pTypeRef).
get()->nSize));
p = outBufs.back().data();
}
args.push_back(p);
if (mtd->pParams[j].bOut) {
outArguments->push_back(BinaryAny());
}
}
assert(i == inArguments_.end());
break;
}
default:
assert(false); // this cannot happen
break;
}
size_t nSize = 0;
if (retType.is())
nSize = size_t_round(retType.get()->nSize);
std::vector< char > retBuf(nSize);
uno_Any exc;
uno_Any * pexc = &exc;
(*object_.get()->pDispatcher)(
object_.get(), member_.get(), retBuf.empty() ? nullptr : retBuf.data(),
args.empty() ? nullptr : args.data(), &pexc);
isExc = pexc != nullptr;
if (isExc) {
*returnValue = BinaryAny(
css::uno::TypeDescription(
cppu::UnoType< css::uno::Any >::get()),
&exc);
uno_any_destruct(&exc, nullptr);
} else {
if (!retBuf.empty()) {
*returnValue = BinaryAny(retType, retBuf.data());
uno_destructData(retBuf.data(), retType.get(), nullptr);
}
if (!outArguments->empty()) {
assert(
member_.get()->eTypeClass ==
typelib_TypeClass_INTERFACE_METHOD);
typelib_InterfaceMethodTypeDescription * mtd =
reinterpret_cast<
typelib_InterfaceMethodTypeDescription * >(
member_.get());
std::vector< BinaryAny >::iterator i(outArguments->begin());
std::vector< std::vector< char > >::iterator j(
outBufs.begin());
for (sal_Int32 k = 0; k != mtd->nParams; ++k) {
if (mtd->pParams[k].bOut) {
*i++ = BinaryAny(
css::uno::TypeDescription(
mtd->pParams[k].pTypeRef),
args[k]);
}
if (!mtd->pParams[k].bIn) {
uno_type_destructData(
(j++)->data(), mtd->pParams[k].pTypeRef, nullptr);
}
}
assert(i == outArguments->end());
assert(j == outBufs.end());
}
}
break;
}
}
return !isExc;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|