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

Generated by: LCOV version 1.10