nss: makefile bitrot: no need for android spcial case anymore
[LibreOffice.git] / cppuhelper / source / bootstrap.cxx
blob34ae38823becc76e1641f599098b24f141919dce
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <chrono>
23 #include <cstring>
25 #include <rtl/bootstrap.hxx>
26 #include <rtl/random.h>
27 #include <rtl/ustrbuf.hxx>
28 #include <rtl/uri.hxx>
29 #include <osl/file.hxx>
30 #include <osl/security.hxx>
31 #include <osl/thread.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
33 #include <osl/process.h>
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/findsofficepath.h>
38 #include <com/sun/star/bridge/UnoUrlResolver.hpp>
39 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
41 #include "macro_expander.hxx"
43 namespace com :: sun :: star :: uno { class XComponentContext; }
45 using namespace ::osl;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
49 using rtl::Bootstrap;
51 namespace cppu
54 BootstrapException::BootstrapException()
58 BootstrapException::BootstrapException( const OUString & rMessage )
59 :m_aMessage( rMessage )
63 BootstrapException::BootstrapException( const BootstrapException & e )
65 m_aMessage = e.m_aMessage;
68 BootstrapException::~BootstrapException()
72 BootstrapException & BootstrapException::operator=( const BootstrapException & e )
74 m_aMessage = e.m_aMessage;
75 return *this;
78 const OUString & BootstrapException::getMessage() const
80 return m_aMessage;
83 Reference< XComponentContext > SAL_CALL bootstrap()
85 Reference< XComponentContext > xRemoteContext;
87 try
89 auto* p1 = cppuhelper_detail_findSofficePath();
90 if (p1 == nullptr) {
91 throw BootstrapException(
92 "no soffice installation found!");
94 OUString p2;
95 #if defined(_WIN32)
96 p2 = o3tl::toU(p1);
97 free(p1);
98 #else
99 bool bOk = rtl_convertStringToUString(
100 &p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
101 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
102 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
103 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR));
104 free(p1);
105 if (!bOk)
107 throw BootstrapException(
108 "bad characters in soffice installation path!");
110 #endif
111 OUString path;
112 if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
113 osl::FileBase::E_None)
115 throw BootstrapException(
116 "cannot convert soffice installation path to URL!");
118 if (!path.isEmpty() && !path.endsWith("/")) {
119 path += "/";
122 OUString uri;
123 if (!Bootstrap::get("URE_BOOTSTRAP", uri)) {
124 Bootstrap::set(
125 "URE_BOOTSTRAP",
126 Bootstrap::encode(
127 path +
128 #if defined MACOSX
129 "../Resources/"
130 #endif
131 SAL_CONFIGFILE("fundamental")));
134 // create default local component context
135 Reference< XComponentContext > xLocalContext(
136 defaultBootstrap_InitialComponentContext() );
137 if ( !xLocalContext.is() )
138 throw BootstrapException( "no local component context!" );
140 // create a random pipe name
141 rtlRandomPool hPool = rtl_random_createPool();
142 if ( hPool == nullptr )
143 throw BootstrapException( "cannot create random pool!" );
144 sal_uInt8 bytes[ 16 ];
145 if ( rtl_random_getBytes( hPool, bytes, SAL_N_ELEMENTS( bytes ) )
146 != rtl_Random_E_None )
147 throw BootstrapException( "random pool error!" );
148 rtl_random_destroyPool( hPool );
149 OUStringBuffer buf("uno");
150 for (unsigned char byte : bytes)
151 buf.append( static_cast< sal_Int32 >( byte ) );
152 OUString sPipeName( buf.makeStringAndClear() );
154 // arguments
155 OUString args [] = {
156 OUString("--nologo"),
157 OUString("--nodefault"),
158 OUString("--norestore"),
159 OUString("--nolockcheck"),
160 OUString("--accept=pipe,name=" + sPipeName + ";urp;")
162 rtl_uString * ar_args [] = {
163 args[ 0 ].pData,
164 args[ 1 ].pData,
165 args[ 2 ].pData,
166 args[ 3 ].pData,
167 args[ 4 ].pData
169 ::osl::Security sec;
171 // start office process
172 oslProcess hProcess = nullptr;
173 oslProcessError rc = osl_executeProcess(
174 OUString(path + "soffice").pData, ar_args, SAL_N_ELEMENTS( ar_args ),
175 osl_Process_DETACHED,
176 sec.getHandle(),
177 nullptr, // => current working dir
178 nullptr, 0, // => no env vars
179 &hProcess );
180 switch ( rc )
182 case osl_Process_E_None:
183 osl_freeProcessHandle( hProcess );
184 break;
185 case osl_Process_E_NotFound:
186 throw BootstrapException( "image not found!" );
187 case osl_Process_E_TimedOut:
188 throw BootstrapException( "timeout occurred!" );
189 case osl_Process_E_NoPermission:
190 throw BootstrapException( "permission denied!" );
191 case osl_Process_E_Unknown:
192 throw BootstrapException( "unknown error!" );
193 case osl_Process_E_InvalidError:
194 default:
195 throw BootstrapException( "unmapped error!" );
198 // create a URL resolver
199 Reference< bridge::XUnoUrlResolver > xUrlResolver(
200 bridge::UnoUrlResolver::create( xLocalContext ) );
202 // connection string
203 OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
205 // wait until office is started
206 for ( ; ; )
210 // try to connect to office
211 xRemoteContext.set(
212 xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
213 break;
215 catch ( connection::NoConnectException & )
217 // wait 500 ms, then try to connect again
218 ::osl::Thread::wait( std::chrono::milliseconds(500) );
222 catch ( Exception & e )
224 throw BootstrapException(
225 "unexpected UNO exception caught: " + e.Message );
228 return xRemoteContext;
231 OUString bootstrap_expandUri(OUString const & uri) {
232 OUString rest;
233 return uri.startsWith("vnd.sun.star.expand:", &rest)
234 ? cppuhelper::detail::expandMacros(
235 rtl::Uri::decode(
236 rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
237 : uri;
240 } // namespace cppu
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */