LCOV - code coverage report
Current view: top level - libreoffice/cppuhelper/source - bootstrap.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 3 81 3.7 %
Date: 2012-12-27 Functions: 1 9 11.1 %
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 <cstring>
      23             : 
      24             : #include "rtl/process.h"
      25             : #include "rtl/bootstrap.hxx"
      26             : #include "rtl/random.h"
      27             : #include "rtl/string.hxx"
      28             : #include "rtl/ustrbuf.hxx"
      29             : #include "rtl/uri.hxx"
      30             : #include "osl/diagnose.h"
      31             : #include "osl/file.hxx"
      32             : #include "osl/security.hxx"
      33             : #include "osl/thread.hxx"
      34             : 
      35             : #include "cppuhelper/bootstrap.hxx"
      36             : #include "cppuhelper/findsofficepath.h"
      37             : 
      38             : #include "com/sun/star/uno/XComponentContext.hpp"
      39             : 
      40             : #include "com/sun/star/bridge/UnoUrlResolver.hpp"
      41             : #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
      42             : 
      43             : #include "macro_expander.hxx"
      44             : 
      45             : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
      46             : #define ARLEN(x) sizeof (x) / sizeof *(x)
      47             : 
      48             : 
      49             : using namespace ::rtl;
      50             : using namespace ::osl;
      51             : using namespace ::com::sun::star;
      52             : using namespace ::com::sun::star::uno;
      53             : 
      54             : namespace cppu
      55             : {
      56             : 
      57           0 : BootstrapException::BootstrapException()
      58             : {
      59           0 : }
      60             : 
      61           0 : BootstrapException::BootstrapException( const ::rtl::OUString & rMessage )
      62           0 :     :m_aMessage( rMessage )
      63             : {
      64           0 : }
      65             : 
      66           0 : BootstrapException::BootstrapException( const BootstrapException & e )
      67             : {
      68           0 :     m_aMessage = e.m_aMessage;
      69           0 : }
      70             : 
      71           0 : BootstrapException::~BootstrapException()
      72             : {
      73           0 : }
      74             : 
      75           0 : BootstrapException & BootstrapException::operator=( const BootstrapException & e )
      76             : {
      77           0 :     m_aMessage = e.m_aMessage;
      78           0 :     return *this;
      79             : }
      80             : 
      81           0 : const ::rtl::OUString & BootstrapException::getMessage() const
      82             : {
      83           0 :     return m_aMessage;
      84             : }
      85             : 
      86           0 : Reference< XComponentContext > SAL_CALL bootstrap()
      87             : {
      88           0 :     Reference< XComponentContext > xRemoteContext;
      89             : 
      90             :     try
      91             :     {
      92           0 :         char const * p1 = cppuhelper_detail_findSofficePath();
      93           0 :         if (p1 == NULL) {
      94             :             throw BootstrapException(
      95           0 :                 OUSTR("no soffice installation found!"));
      96             :         }
      97           0 :         rtl::OUString p2;
      98           0 :         if (!rtl_convertStringToUString(
      99           0 :                 &p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
     100             :                 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
     101             :                  RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
     102           0 :                  RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
     103             :         {
     104             :             throw BootstrapException(
     105           0 :                 OUSTR("bad characters in soffice installation path!"));
     106             :         }
     107           0 :         OUString path;
     108           0 :         if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
     109             :             osl::FileBase::E_None)
     110             :         {
     111             :             throw BootstrapException(
     112           0 :                 OUSTR("cannot convert soffice installation path to URL!"));
     113             :         }
     114           0 :         if (!path.isEmpty() && path[path.getLength() - 1] != '/') {
     115           0 :             path += OUSTR("/");
     116             :         }
     117             : 
     118           0 :         OUString uri;
     119           0 :         if (!Bootstrap::get(OUSTR("URE_BOOTSTRAP"), uri)) {
     120             :             Bootstrap::set(
     121             :                 OUSTR("URE_BOOTSTRAP"),
     122           0 :                 Bootstrap::encode(path + OUSTR(SAL_CONFIGFILE("fundamental"))));
     123             :         }
     124             : 
     125             :         // create default local component context
     126             :         Reference< XComponentContext > xLocalContext(
     127           0 :             defaultBootstrap_InitialComponentContext() );
     128           0 :         if ( !xLocalContext.is() )
     129           0 :             throw BootstrapException( OUSTR( "no local component context!" ) );
     130             : 
     131             :         // create a random pipe name
     132           0 :         rtlRandomPool hPool = rtl_random_createPool();
     133           0 :         if ( hPool == 0 )
     134           0 :             throw BootstrapException( OUSTR( "cannot create random pool!" ) );
     135             :         sal_uInt8 bytes[ 16 ];
     136           0 :         if ( rtl_random_getBytes( hPool, bytes, ARLEN( bytes ) )
     137             :             != rtl_Random_E_None )
     138           0 :             throw BootstrapException( OUSTR( "random pool error!" ) );
     139           0 :         rtl_random_destroyPool( hPool );
     140           0 :         ::rtl::OUStringBuffer buf;
     141           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno" ) );
     142           0 :         for ( sal_uInt32 i = 0; i < ARLEN( bytes ); ++i )
     143           0 :             buf.append( static_cast< sal_Int32 >( bytes[ i ] ) );
     144           0 :         OUString sPipeName( buf.makeStringAndClear() );
     145             : 
     146             :         // accept string
     147             :         OSL_ASSERT( buf.getLength() == 0 );
     148           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "--accept=pipe,name=" ) );
     149           0 :         buf.append( sPipeName );
     150           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( ";urp;" ) );
     151             : 
     152             :         // arguments
     153             :         OUString args [] = {
     154             :             OUSTR( "--nologo" ),
     155             :             OUSTR( "--nodefault" ),
     156             :             OUSTR( "--norestore" ),
     157             :             OUSTR( "--nocrashreport" ),
     158             :             OUSTR( "--nolockcheck" ),
     159             :             buf.makeStringAndClear()
     160           0 :         };
     161             :         rtl_uString * ar_args [] = {
     162             :             args[ 0 ].pData,
     163             :             args[ 1 ].pData,
     164             :             args[ 2 ].pData,
     165             :             args[ 3 ].pData,
     166             :             args[ 4 ].pData,
     167             :             args[ 5 ].pData
     168           0 :         };
     169           0 :         ::osl::Security sec;
     170             : 
     171             :         // start office process
     172           0 :         oslProcess hProcess = 0;
     173             :         oslProcessError rc = osl_executeProcess(
     174             :             OUString(path + "soffice").pData, ar_args, ARLEN( ar_args ),
     175             :             osl_Process_DETACHED,
     176             :             sec.getHandle(),
     177             :             0, // => current working dir
     178             :             0, 0, // => no env vars
     179           0 :             &hProcess );
     180           0 :         switch ( rc )
     181             :         {
     182             :             case osl_Process_E_None:
     183           0 :                 osl_freeProcessHandle( hProcess );
     184           0 :                 break;
     185             :             case osl_Process_E_NotFound:
     186           0 :                 throw BootstrapException( OUSTR( "image not found!" ) );
     187             :             case osl_Process_E_TimedOut:
     188           0 :                 throw BootstrapException( OUSTR( "timout occurred!" ) );
     189             :             case osl_Process_E_NoPermission:
     190           0 :                 throw BootstrapException( OUSTR( "permission denied!" ) );
     191             :             case osl_Process_E_Unknown:
     192           0 :                 throw BootstrapException( OUSTR( "unknown error!" ) );
     193             :             case osl_Process_E_InvalidError:
     194             :             default:
     195           0 :                 throw BootstrapException( OUSTR( "unmapped error!" ) );
     196             :         }
     197             : 
     198             :         // create a URL resolver
     199             :         Reference< bridge::XUnoUrlResolver > xUrlResolver(
     200           0 :             bridge::UnoUrlResolver::create( xLocalContext ) );
     201             : 
     202             :         // connection string
     203             :         OSL_ASSERT( buf.getLength() == 0 );
     204           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "uno:pipe,name=" ) );
     205           0 :         buf.append( sPipeName );
     206             :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
     207           0 :             ";urp;StarOffice.ComponentContext" ) );
     208           0 :         OUString sConnectString( buf.makeStringAndClear() );
     209             : 
     210             :         // wait until office is started
     211           0 :         for ( ; ; )
     212             :         {
     213             :             try
     214             :             {
     215             :                 // try to connect to office
     216             :                 xRemoteContext.set(
     217           0 :                     xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
     218           0 :                 break;
     219             :             }
     220           0 :             catch ( connection::NoConnectException & )
     221             :             {
     222             :                 // wait 500 ms, then try to connect again
     223           0 :                 TimeValue tv = { 0 /* secs */, 500000000 /* nanosecs */ };
     224           0 :                 ::osl::Thread::wait( tv );
     225             :             }
     226           0 :         }
     227             :     }
     228           0 :     catch ( Exception & e )
     229             :     {
     230             :         throw BootstrapException(
     231           0 :             OUSTR( "unexpected UNO exception caught: " ) + e.Message );
     232             :     }
     233             : 
     234           0 :     return xRemoteContext;
     235             : }
     236             : 
     237        1854 : OUString bootstrap_expandUri(OUString const & uri) {
     238             :     static char const PREFIX[] = "vnd.sun.star.expand:";
     239        1854 :     return uri.matchAsciiL(RTL_CONSTASCII_STRINGPARAM(PREFIX))
     240             :         ? cppuhelper::detail::expandMacros(
     241             :             rtl::Uri::decode(
     242             :                 uri.copy(RTL_CONSTASCII_LENGTH(PREFIX)),
     243             :                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
     244        1854 :         : uri;
     245             : }
     246             : 
     247             : } // namespace cppu
     248             : 
     249             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10