LCOV - code coverage report
Current view: top level - binaryurp/source - incomingrequest.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 100 115 87.0 %
Date: 2014-11-03 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          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      509676 : IncomingRequest::IncomingRequest(
      44             :     rtl::Reference< Bridge > const & bridge, rtl::ByteSequence const & tid,
      45             :     OUString const & oid, css::uno::UnoInterfaceReference const & object,
      46             :     css::uno::TypeDescription const & type, sal_uInt16 functionId,
      47             :     bool synchronous, css::uno::TypeDescription const & member, bool setter,
      48             :     std::vector< BinaryAny > const & inArguments, bool currentContextMode,
      49             :     css::uno::UnoInterfaceReference const & currentContext):
      50             :     bridge_(bridge), tid_(tid), oid_(oid), object_(object), type_(type),
      51             :     functionId_(functionId), synchronous_(synchronous), member_(member),
      52             :     setter_(setter), inArguments_(inArguments),
      53      509676 :     currentContextMode_(currentContextMode), currentContext_(currentContext)
      54             : {
      55             :     OSL_ASSERT(bridge.is() && member.is() && member.get()->bComplete);
      56      509676 : }
      57             : 
      58      509660 : IncomingRequest::~IncomingRequest() {}
      59             : 
      60      509641 : void IncomingRequest::execute() const {
      61      509641 :     BinaryAny ret;
      62      627401 :     std::vector< BinaryAny > outArgs;
      63             :     bool isExc;
      64             :     try {
      65      509602 :         bool resetCc = false;
      66      509602 :         css::uno::UnoInterfaceReference oldCc;
      67      509597 :         if (currentContextMode_) {
      68      391927 :             oldCc = current_context::get();
      69      391927 :             current_context::set(currentContext_);
      70      391927 :             resetCc = true;
      71             :         }
      72             :         try {
      73             :             try {
      74      509597 :                 isExc = !execute_throw(&ret, &outArgs);
      75           0 :             } catch (const std::exception & e) {
      76             :                 throw css::uno::RuntimeException(
      77           0 :                     "caught C++ exception: " +
      78             :                     OStringToOUString(
      79           0 :                          OString(e.what()), RTL_TEXTENCODING_ASCII_US));
      80             :                     // best-effort string conversion
      81             :             }
      82           0 :         } catch (const css::uno::RuntimeException &) {
      83           0 :             css::uno::Any exc(cppu::getCaughtException());
      84           0 :             ret = bridge_->mapCppToBinaryAny(exc);
      85           0 :             isExc = true;
      86             :         }
      87      509672 :         if (resetCc) {
      88      391927 :             current_context::set(oldCc);
      89      509672 :         }
      90           0 :     } catch (const css::uno::RuntimeException &) {
      91           0 :         css::uno::Any exc(cppu::getCaughtException());
      92           0 :         ret = bridge_->mapCppToBinaryAny(exc);
      93           0 :         isExc = true;
      94             :     }
      95      509676 :     if (synchronous_) {
      96      391927 :         bridge_->decrementActiveCalls();
      97             :         try {
      98             :             bridge_->getWriter()->queueReply(
      99      391927 :                 tid_, member_, setter_, isExc, ret, outArgs, false);
     100      901602 :             return;
     101           1 :         } catch (const css::uno::RuntimeException & e) {
     102             :             OSL_TRACE(
     103             :                 "caught UNO runtime exception '%s'",
     104             :                 (OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).
     105             :                  getStr()));
     106           0 :         } catch (const std::exception & e) {
     107             :             OSL_TRACE("caught C++ exception '%s'", e.what());
     108             :         }
     109           1 :         bridge_->terminate(false);
     110             :     } else {
     111             :         if (isExc) {
     112             :             OSL_TRACE("oneway method raised exception");
     113             :         }
     114      117749 :         bridge_->decrementCalls();
     115      117750 :     }
     116             : }
     117             : 
     118      391479 : static size_t size_t_round(size_t val)
     119             : {
     120      391479 :     return (val + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
     121             : }
     122             : 
     123      509655 : bool IncomingRequest::execute_throw(
     124             :     BinaryAny * returnValue, std::vector< BinaryAny > * outArguments) const
     125             : {
     126             :     OSL_ASSERT(
     127             :         returnValue != 0 &&
     128             :         returnValue->getType().equals(
     129             :             css::uno::TypeDescription(
     130             :                 cppu::UnoType< cppu::UnoVoidType >::get())) &&
     131             :         outArguments != 0 && outArguments->empty());
     132      509655 :     bool isExc = false;
     133      509655 :     switch (functionId_) {
     134             :     case SPECIAL_FUNCTION_ID_RESERVED:
     135             :         OSL_ASSERT(false); // this cannot happen
     136           0 :         break;
     137             :     case SPECIAL_FUNCTION_ID_RELEASE:
     138      117748 :         bridge_->releaseStub(oid_, type_);
     139      117749 :         break;
     140             :     case SPECIAL_FUNCTION_ID_QUERY_INTERFACE:
     141       43611 :         if (!object_.is()) {
     142         134 :             css::uno::Reference< css::uno::XInterface > ifc;
     143             :             css::uno::Reference< css::bridge::XInstanceProvider > prov(
     144         268 :                 bridge_->getProvider());
     145         134 :             if (prov.is()) {
     146             :                 try {
     147         134 :                     ifc = prov->getInstance(oid_);
     148           0 :                 } catch (const css::container::NoSuchElementException & e) {
     149             :                     OSL_TRACE(
     150             :                         "initial element '%s': NoSuchElementException '%s'",
     151             :                         OUStringToOString(oid_, RTL_TEXTENCODING_UTF8).getStr(),
     152             :                         (OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).
     153             :                          getStr()));
     154             :                 }
     155             :             }
     156         134 :             if (ifc.is()) {
     157             :                 css::uno::UnoInterfaceReference unoIfc(
     158             :                     static_cast< uno_Interface * >(
     159         134 :                         bridge_->getCppToBinaryMapping().mapInterface(
     160         134 :                             ifc.get(),
     161             :                             (css::uno::TypeDescription(
     162             :                                 cppu::UnoType<
     163             :                                     css::uno::Reference<
     164         134 :                                         css::uno::XInterface > >::get()).
     165         402 :                              get()))),
     166         134 :                     SAL_NO_ACQUIRE);
     167         268 :                 *returnValue = BinaryAny(
     168             :                     css::uno::TypeDescription(
     169             :                         cppu::UnoType<
     170             :                             css::uno::Reference<
     171         134 :                                 css::uno::XInterface > >::get()),
     172         268 :                     &unoIfc.m_pUnoI);
     173             :             }
     174         268 :             break;
     175             :         }
     176             :         // fall through
     177             :     default:
     178             :         {
     179             :             OSL_ASSERT(object_.is());
     180      391776 :             css::uno::TypeDescription retType;
     181      783586 :             std::list< std::vector< char > > outBufs;
     182      783586 :             std::vector< void * > args;
     183      391793 :             switch (member_.get()->eTypeClass) {
     184             :             case typelib_TypeClass_INTERFACE_ATTRIBUTE:
     185             :                 {
     186             :                     css::uno::TypeDescription t(
     187             :                         reinterpret_cast<
     188             :                             typelib_InterfaceAttributeTypeDescription * >(
     189        8002 :                                 member_.get())->
     190        8002 :                         pAttributeTypeRef);
     191        8002 :                     if (setter_) {
     192             :                         OSL_ASSERT(inArguments_.size() == 1);
     193         422 :                         args.push_back(inArguments_[0].getValue(t));
     194             :                     } else {
     195             :                         OSL_ASSERT(inArguments_.empty());
     196        7580 :                         retType = t;
     197             :                     }
     198        8002 :                     break;
     199             :                 }
     200             :             case typelib_TypeClass_INTERFACE_METHOD:
     201             :                 {
     202             :                     typelib_InterfaceMethodTypeDescription * mtd =
     203             :                         reinterpret_cast<
     204             :                             typelib_InterfaceMethodTypeDescription * >(
     205      383791 :                                 member_.get());
     206      383791 :                     retType = css::uno::TypeDescription(mtd->pReturnTypeRef);
     207             :                     std::vector< BinaryAny >::const_iterator i(
     208      383791 :                         inArguments_.begin());
     209      703038 :                     for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
     210             :                         void * p;
     211      319247 :                         if (mtd->pParams[j].bIn) {
     212             :                             p = i++->getValue(
     213             :                                 css::uno::TypeDescription(
     214      319139 :                                     mtd->pParams[j].pTypeRef));
     215             :                         } else {
     216             :                             outBufs.push_back(
     217             :                                 std::vector< char >(size_t_round(
     218             :                                     css::uno::TypeDescription(
     219         108 :                                         mtd->pParams[j].pTypeRef).
     220         216 :                                     get()->nSize)));
     221         108 :                             p = &outBufs.back()[0];
     222             :                         }
     223      319247 :                         args.push_back(p);
     224      319247 :                         if (mtd->pParams[j].bOut) {
     225         202 :                             outArguments->push_back(BinaryAny());
     226             :                         }
     227             :                     }
     228             :                     OSL_ASSERT(i == inArguments_.end());
     229      383791 :                     break;
     230             :                 }
     231             :             default:
     232             :                 OSL_ASSERT(false); // this cannot happen
     233           0 :                 break;
     234             :             }
     235      391793 :             size_t nSize = 0;
     236      391793 :             if (retType.is())
     237      391371 :                 nSize = size_t_round(retType.get()->nSize);
     238      391793 :             std::vector< char > retBuf(nSize);
     239             :             uno_Any exc;
     240      391793 :             uno_Any * pexc = &exc;
     241      391793 :             (*object_.get()->pDispatcher)(
     242      783586 :                 object_.get(), member_.get(), retBuf.empty() ? 0 : &retBuf[0],
     243     1175379 :                 args.empty() ? 0 : &args[0], &pexc);
     244      391793 :             isExc = pexc != 0;
     245      391793 :             if (isExc) {
     246        3932 :                 *returnValue = BinaryAny(
     247             :                     css::uno::TypeDescription(
     248        1966 :                         cppu::UnoType< css::uno::Any >::get()),
     249        1966 :                     &exc);
     250        1966 :                 uno_any_destruct(&exc, 0);
     251             :             } else {
     252      389827 :                 if (!retBuf.empty()) {
     253      325483 :                     *returnValue = BinaryAny(retType, &retBuf[0]);
     254      325483 :                     uno_destructData(&retBuf[0], retType.get(), 0);
     255             :                 }
     256      389827 :                 if (!outArguments->empty()) {
     257             :                     OSL_ASSERT(
     258             :                         member_.get()->eTypeClass ==
     259             :                         typelib_TypeClass_INTERFACE_METHOD);
     260             :                     typelib_InterfaceMethodTypeDescription * mtd =
     261             :                         reinterpret_cast<
     262             :                             typelib_InterfaceMethodTypeDescription * >(
     263         164 :                                 member_.get());
     264         164 :                     std::vector< BinaryAny >::iterator i(outArguments->begin());
     265             :                     std::list< std::vector< char > >::iterator j(
     266         164 :                         outBufs.begin());
     267         398 :                     for (sal_Int32 k = 0; k != mtd->nParams; ++k) {
     268         234 :                         if (mtd->pParams[k].bOut) {
     269         606 :                             *i++ = BinaryAny(
     270             :                                 css::uno::TypeDescription(
     271         202 :                                     mtd->pParams[k].pTypeRef),
     272         404 :                                 args[k]);
     273             :                         }
     274         234 :                         if (!mtd->pParams[k].bIn) {
     275             :                             uno_type_destructData(
     276         108 :                                 &(*j++)[0], mtd->pParams[k].pTypeRef, 0);
     277             :                         }
     278             :                     }
     279             :                     OSL_ASSERT(i == outArguments->end());
     280             :                     OSL_ASSERT(j == outBufs.end());
     281             :                 }
     282             :             }
     283      783586 :             break;
     284             :         }
     285             :     }
     286      509673 :     return !isExc;
     287             : }
     288             : 
     289             : }
     290             : 
     291             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10