xref: /AOO41X/main/binaryurp/source/incomingrequest.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Copyright 2000, 2011 Oracle and/or its affiliates.
6*cdf0e10cSrcweir *
7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir *
9*cdf0e10cSrcweir * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir *
15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir *
26*cdf0e10cSrcweir ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "sal/config.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <list>
31*cdf0e10cSrcweir #include <vector>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "boost/noncopyable.hpp"
34*cdf0e10cSrcweir #include "com/sun/star/bridge/XInstanceProvider.hpp"
35*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
36*cdf0e10cSrcweir #include "rtl/byteseq.hxx"
37*cdf0e10cSrcweir #include "rtl/ref.hxx"
38*cdf0e10cSrcweir #include "rtl/ustring.hxx"
39*cdf0e10cSrcweir #include "sal/types.h"
40*cdf0e10cSrcweir #include "typelib/typedescription.hxx"
41*cdf0e10cSrcweir #include "uno/dispatcher.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "binaryany.hxx"
44*cdf0e10cSrcweir #include "bridge.hxx"
45*cdf0e10cSrcweir #include "currentcontext.hxx"
46*cdf0e10cSrcweir #include "incomingrequest.hxx"
47*cdf0e10cSrcweir #include "specialfunctionids.hxx"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace binaryurp {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir namespace {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir namespace css = com::sun::star;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir IncomingRequest::IncomingRequest(
58*cdf0e10cSrcweir     rtl::Reference< Bridge > const & bridge, rtl::ByteSequence const & tid,
59*cdf0e10cSrcweir     rtl::OUString const & oid, css::uno::UnoInterfaceReference const & object,
60*cdf0e10cSrcweir     css::uno::TypeDescription const & type, sal_uInt16 functionId,
61*cdf0e10cSrcweir     bool synchronous, css::uno::TypeDescription const & member, bool setter,
62*cdf0e10cSrcweir     std::vector< BinaryAny > const & inArguments, bool currentContextMode,
63*cdf0e10cSrcweir     css::uno::UnoInterfaceReference const & currentContext):
64*cdf0e10cSrcweir     bridge_(bridge), tid_(tid), oid_(oid), object_(object), type_(type),
65*cdf0e10cSrcweir     functionId_(functionId), synchronous_(synchronous), member_(member),
66*cdf0e10cSrcweir     setter_(setter), inArguments_(inArguments),
67*cdf0e10cSrcweir     currentContextMode_(currentContextMode), currentContext_(currentContext)
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     OSL_ASSERT(bridge.is() && member.is() && member.get()->bComplete);
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir IncomingRequest::~IncomingRequest() {}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir void IncomingRequest::execute() const {
75*cdf0e10cSrcweir     BinaryAny ret;
76*cdf0e10cSrcweir     std::vector< BinaryAny > outArgs;
77*cdf0e10cSrcweir     bool isExc;
78*cdf0e10cSrcweir     try {
79*cdf0e10cSrcweir         bool resetCc = false;
80*cdf0e10cSrcweir         css::uno::UnoInterfaceReference oldCc;
81*cdf0e10cSrcweir         if (currentContextMode_) {
82*cdf0e10cSrcweir             oldCc = current_context::get();
83*cdf0e10cSrcweir             current_context::set(currentContext_);
84*cdf0e10cSrcweir             resetCc = true;
85*cdf0e10cSrcweir         }
86*cdf0e10cSrcweir         try {
87*cdf0e10cSrcweir             try {
88*cdf0e10cSrcweir                 isExc = !execute_throw(&ret, &outArgs);
89*cdf0e10cSrcweir             } catch (std::exception & e) {
90*cdf0e10cSrcweir                 throw css::uno::RuntimeException(
91*cdf0e10cSrcweir                     (rtl::OUString(
92*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM("caught C++ exception: ")) +
93*cdf0e10cSrcweir                      rtl::OStringToOUString(
94*cdf0e10cSrcweir                          rtl::OString(e.what()), RTL_TEXTENCODING_ASCII_US)),
95*cdf0e10cSrcweir                     css::uno::Reference< css::uno::XInterface >());
96*cdf0e10cSrcweir                     // best-effort string conversion
97*cdf0e10cSrcweir             }
98*cdf0e10cSrcweir         } catch (css::uno::RuntimeException &) {
99*cdf0e10cSrcweir             css::uno::Any exc(cppu::getCaughtException());
100*cdf0e10cSrcweir             ret = bridge_->mapCppToBinaryAny(exc);
101*cdf0e10cSrcweir             isExc = true;
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir         if (resetCc) {
104*cdf0e10cSrcweir             current_context::set(oldCc);
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir     } catch (css::uno::RuntimeException &) {
107*cdf0e10cSrcweir         css::uno::Any exc(cppu::getCaughtException());
108*cdf0e10cSrcweir         ret = bridge_->mapCppToBinaryAny(exc);
109*cdf0e10cSrcweir         isExc = true;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir     if (synchronous_) {
112*cdf0e10cSrcweir         bridge_->decrementActiveCalls();
113*cdf0e10cSrcweir         try {
114*cdf0e10cSrcweir             bridge_->getWriter()->queueReply(
115*cdf0e10cSrcweir                 tid_, member_, setter_, isExc, ret, outArgs, false);
116*cdf0e10cSrcweir             return;
117*cdf0e10cSrcweir         } catch (css::uno::RuntimeException & e) {
118*cdf0e10cSrcweir             OSL_TRACE(
119*cdf0e10cSrcweir                 OSL_LOG_PREFIX "caught UNO runtime exception '%s'",
120*cdf0e10cSrcweir                 (rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).
121*cdf0e10cSrcweir                  getStr()));
122*cdf0e10cSrcweir         } catch (std::exception & e) {
123*cdf0e10cSrcweir             OSL_TRACE(OSL_LOG_PREFIX "caught C++ exception '%s'", e.what());
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         bridge_->terminate();
126*cdf0e10cSrcweir     } else {
127*cdf0e10cSrcweir         if (isExc) {
128*cdf0e10cSrcweir             OSL_TRACE(OSL_LOG_PREFIX "oneway method raised exception");
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir         bridge_->decrementCalls();
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir bool IncomingRequest::execute_throw(
135*cdf0e10cSrcweir     BinaryAny * returnValue, std::vector< BinaryAny > * outArguments) const
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     OSL_ASSERT(
138*cdf0e10cSrcweir         returnValue != 0 &&
139*cdf0e10cSrcweir         returnValue->getType().equals(
140*cdf0e10cSrcweir             css::uno::TypeDescription(
141*cdf0e10cSrcweir                 cppu::UnoType< cppu::UnoVoidType >::get())) &&
142*cdf0e10cSrcweir         outArguments != 0 && outArguments->empty());
143*cdf0e10cSrcweir     bool isExc = false;
144*cdf0e10cSrcweir     switch (functionId_) {
145*cdf0e10cSrcweir     case SPECIAL_FUNCTION_ID_RESERVED:
146*cdf0e10cSrcweir         OSL_ASSERT(false); // this cannot happen
147*cdf0e10cSrcweir         break;
148*cdf0e10cSrcweir     case SPECIAL_FUNCTION_ID_RELEASE:
149*cdf0e10cSrcweir         bridge_->releaseStub(oid_, type_);
150*cdf0e10cSrcweir         break;
151*cdf0e10cSrcweir     case SPECIAL_FUNCTION_ID_QUERY_INTERFACE:
152*cdf0e10cSrcweir         if (!object_.is()) {
153*cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface > ifc;
154*cdf0e10cSrcweir             css::uno::Reference< css::bridge::XInstanceProvider > prov(
155*cdf0e10cSrcweir                 bridge_->getProvider());
156*cdf0e10cSrcweir             if (prov.is()) {
157*cdf0e10cSrcweir                 try {
158*cdf0e10cSrcweir                     ifc = prov->getInstance(oid_);
159*cdf0e10cSrcweir                 } catch (css::container::NoSuchElementException & e) {
160*cdf0e10cSrcweir                     OSL_TRACE(
161*cdf0e10cSrcweir                         (OSL_LOG_PREFIX "initial element '%s':"
162*cdf0e10cSrcweir                          " NoSuchElementException '%s'"),
163*cdf0e10cSrcweir                         (rtl::OUStringToOString(oid_, RTL_TEXTENCODING_UTF8).
164*cdf0e10cSrcweir                          getStr()),
165*cdf0e10cSrcweir                         (rtl::OUStringToOString(
166*cdf0e10cSrcweir                             e.Message, RTL_TEXTENCODING_UTF8).
167*cdf0e10cSrcweir                          getStr()));
168*cdf0e10cSrcweir                 }
169*cdf0e10cSrcweir             }
170*cdf0e10cSrcweir             if (ifc.is()) {
171*cdf0e10cSrcweir                 css::uno::UnoInterfaceReference unoIfc(
172*cdf0e10cSrcweir                     static_cast< uno_Interface * >(
173*cdf0e10cSrcweir                         bridge_->getCppToBinaryMapping().mapInterface(
174*cdf0e10cSrcweir                             ifc.get(),
175*cdf0e10cSrcweir                             (css::uno::TypeDescription(
176*cdf0e10cSrcweir                                 cppu::UnoType<
177*cdf0e10cSrcweir                                     css::uno::Reference<
178*cdf0e10cSrcweir                                         css::uno::XInterface > >::get()).
179*cdf0e10cSrcweir                              get()))),
180*cdf0e10cSrcweir                     SAL_NO_ACQUIRE);
181*cdf0e10cSrcweir                 *returnValue = BinaryAny(
182*cdf0e10cSrcweir                     css::uno::TypeDescription(
183*cdf0e10cSrcweir                         cppu::UnoType<
184*cdf0e10cSrcweir                             css::uno::Reference<
185*cdf0e10cSrcweir                                 css::uno::XInterface > >::get()),
186*cdf0e10cSrcweir                     &unoIfc.m_pUnoI);
187*cdf0e10cSrcweir             }
188*cdf0e10cSrcweir             break;
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir         // fall through
191*cdf0e10cSrcweir     default:
192*cdf0e10cSrcweir         {
193*cdf0e10cSrcweir             OSL_ASSERT(object_.is());
194*cdf0e10cSrcweir             css::uno::TypeDescription retType;
195*cdf0e10cSrcweir             std::list< std::vector< char > > outBufs;
196*cdf0e10cSrcweir             std::vector< void * > args;
197*cdf0e10cSrcweir             switch (member_.get()->eTypeClass) {
198*cdf0e10cSrcweir             case typelib_TypeClass_INTERFACE_ATTRIBUTE:
199*cdf0e10cSrcweir                 {
200*cdf0e10cSrcweir                     css::uno::TypeDescription t(
201*cdf0e10cSrcweir                         reinterpret_cast<
202*cdf0e10cSrcweir                             typelib_InterfaceAttributeTypeDescription * >(
203*cdf0e10cSrcweir                                 member_.get())->
204*cdf0e10cSrcweir                         pAttributeTypeRef);
205*cdf0e10cSrcweir                     if (setter_) {
206*cdf0e10cSrcweir                         OSL_ASSERT(inArguments_.size() == 1);
207*cdf0e10cSrcweir                         args.push_back(inArguments_[0].getValue(t));
208*cdf0e10cSrcweir                     } else {
209*cdf0e10cSrcweir                         OSL_ASSERT(inArguments_.empty());
210*cdf0e10cSrcweir                         retType = t;
211*cdf0e10cSrcweir                     }
212*cdf0e10cSrcweir                     break;
213*cdf0e10cSrcweir                 }
214*cdf0e10cSrcweir             case typelib_TypeClass_INTERFACE_METHOD:
215*cdf0e10cSrcweir                 {
216*cdf0e10cSrcweir                     typelib_InterfaceMethodTypeDescription * mtd =
217*cdf0e10cSrcweir                         reinterpret_cast<
218*cdf0e10cSrcweir                             typelib_InterfaceMethodTypeDescription * >(
219*cdf0e10cSrcweir                                 member_.get());
220*cdf0e10cSrcweir                     retType = css::uno::TypeDescription(mtd->pReturnTypeRef);
221*cdf0e10cSrcweir                     std::vector< BinaryAny >::const_iterator i(
222*cdf0e10cSrcweir                         inArguments_.begin());
223*cdf0e10cSrcweir                     for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
224*cdf0e10cSrcweir                         void * p;
225*cdf0e10cSrcweir                         if (mtd->pParams[j].bIn) {
226*cdf0e10cSrcweir                             p = i++->getValue(
227*cdf0e10cSrcweir                                 css::uno::TypeDescription(
228*cdf0e10cSrcweir                                     mtd->pParams[j].pTypeRef));
229*cdf0e10cSrcweir                         } else {
230*cdf0e10cSrcweir                             outBufs.push_back(
231*cdf0e10cSrcweir                                 std::vector< char >(
232*cdf0e10cSrcweir                                     css::uno::TypeDescription(
233*cdf0e10cSrcweir                                         mtd->pParams[j].pTypeRef).
234*cdf0e10cSrcweir                                     get()->nSize));
235*cdf0e10cSrcweir                             p = &outBufs.back()[0];
236*cdf0e10cSrcweir                         }
237*cdf0e10cSrcweir                         args.push_back(p);
238*cdf0e10cSrcweir                         if (mtd->pParams[j].bOut) {
239*cdf0e10cSrcweir                             outArguments->push_back(BinaryAny());
240*cdf0e10cSrcweir                         }
241*cdf0e10cSrcweir                     }
242*cdf0e10cSrcweir                     OSL_ASSERT(i == inArguments_.end());
243*cdf0e10cSrcweir                     break;
244*cdf0e10cSrcweir                 }
245*cdf0e10cSrcweir             default:
246*cdf0e10cSrcweir                 OSL_ASSERT(false); // this cannot happen
247*cdf0e10cSrcweir                 break;
248*cdf0e10cSrcweir             }
249*cdf0e10cSrcweir             std::vector< char > retBuf(retType.is() ? retType.get()->nSize : 0);
250*cdf0e10cSrcweir             uno_Any exc;
251*cdf0e10cSrcweir             uno_Any * pexc = &exc;
252*cdf0e10cSrcweir             (*object_.get()->pDispatcher)(
253*cdf0e10cSrcweir                 object_.get(), member_.get(), retBuf.empty() ? 0 : &retBuf[0],
254*cdf0e10cSrcweir                 args.empty() ? 0 : &args[0], &pexc);
255*cdf0e10cSrcweir             isExc = pexc != 0;
256*cdf0e10cSrcweir             if (isExc) {
257*cdf0e10cSrcweir                 *returnValue = BinaryAny(
258*cdf0e10cSrcweir                     css::uno::TypeDescription(
259*cdf0e10cSrcweir                         cppu::UnoType< css::uno::Any >::get()),
260*cdf0e10cSrcweir                     &exc);
261*cdf0e10cSrcweir                 uno_any_destruct(&exc, 0);
262*cdf0e10cSrcweir             } else {
263*cdf0e10cSrcweir                 if (!retBuf.empty()) {
264*cdf0e10cSrcweir                     *returnValue = BinaryAny(retType, &retBuf[0]);
265*cdf0e10cSrcweir                     uno_destructData(&retBuf[0], retType.get(), 0);
266*cdf0e10cSrcweir                 }
267*cdf0e10cSrcweir                 if (!outArguments->empty()) {
268*cdf0e10cSrcweir                     OSL_ASSERT(
269*cdf0e10cSrcweir                         member_.get()->eTypeClass ==
270*cdf0e10cSrcweir                         typelib_TypeClass_INTERFACE_METHOD);
271*cdf0e10cSrcweir                     typelib_InterfaceMethodTypeDescription * mtd =
272*cdf0e10cSrcweir                         reinterpret_cast<
273*cdf0e10cSrcweir                             typelib_InterfaceMethodTypeDescription * >(
274*cdf0e10cSrcweir                                 member_.get());
275*cdf0e10cSrcweir                     std::vector< BinaryAny >::iterator i(outArguments->begin());
276*cdf0e10cSrcweir                     std::list< std::vector< char > >::iterator j(
277*cdf0e10cSrcweir                         outBufs.begin());
278*cdf0e10cSrcweir                     for (sal_Int32 k = 0; k != mtd->nParams; ++k) {
279*cdf0e10cSrcweir                         if (mtd->pParams[k].bOut) {
280*cdf0e10cSrcweir                             *i++ = BinaryAny(
281*cdf0e10cSrcweir                                 css::uno::TypeDescription(
282*cdf0e10cSrcweir                                     mtd->pParams[k].pTypeRef),
283*cdf0e10cSrcweir                                 args[k]);
284*cdf0e10cSrcweir                         }
285*cdf0e10cSrcweir                         if (!mtd->pParams[k].bIn) {
286*cdf0e10cSrcweir                             uno_type_destructData(
287*cdf0e10cSrcweir                                 &(*j++)[0], mtd->pParams[k].pTypeRef, 0);
288*cdf0e10cSrcweir                         }
289*cdf0e10cSrcweir                     }
290*cdf0e10cSrcweir                     OSL_ASSERT(i == outArguments->end());
291*cdf0e10cSrcweir                     OSL_ASSERT(j == outBufs.end());
292*cdf0e10cSrcweir                 }
293*cdf0e10cSrcweir             }
294*cdf0e10cSrcweir             break;
295*cdf0e10cSrcweir         }
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir     return !isExc;
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir }
301