Bump version to 24.04.3.4
[LibreOffice.git] / cppuhelper / source / bootstrap.cxx
blobbb8997d1ef3dc6770c4c943a159c1093ed5b10e5
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>
24 #include <thread>
26 #include <rtl/bootstrap.hxx>
27 #include <rtl/random.h>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/uri.hxx>
30 #include <osl/file.hxx>
31 #include <osl/security.hxx>
32 #include <osl/thread.hxx>
33 #include <o3tl/char16_t2wchar_t.hxx>
34 #include <osl/process.h>
36 #include <cppuhelper/bootstrap.hxx>
37 #include <cppuhelper/findsofficepath.h>
39 #include <com/sun/star/bridge/UnoUrlResolver.hpp>
40 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
42 #include "macro_expander.hxx"
44 namespace com :: sun :: star :: uno { class XComponentContext; }
46 using namespace ::osl;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
50 using rtl::Bootstrap;
52 namespace cppu
55 BootstrapException::BootstrapException()
59 BootstrapException::BootstrapException( const OUString & rMessage )
60 :m_aMessage( rMessage )
64 BootstrapException::BootstrapException( const BootstrapException & e )
66 m_aMessage = e.m_aMessage;
69 BootstrapException::~BootstrapException()
73 BootstrapException & BootstrapException::operator=( const BootstrapException & e )
75 m_aMessage = e.m_aMessage;
76 return *this;
79 const OUString & BootstrapException::getMessage() const
81 return m_aMessage;
84 Reference< XComponentContext > SAL_CALL bootstrap()
86 Reference< XComponentContext > xRemoteContext;
88 try
90 auto* p1 = cppuhelper_detail_findSofficePath();
91 if (p1 == nullptr) {
92 throw BootstrapException(
93 "no soffice installation found!");
95 OUString p2;
96 #if defined(_WIN32)
97 p2 = o3tl::toU(p1);
98 free(p1);
99 #else
100 bool bOk = rtl_convertStringToUString(
101 &p2.pData, p1, std::strlen(p1), osl_getThreadTextEncoding(),
102 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
103 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
104 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR));
105 free(p1);
106 if (!bOk)
108 throw BootstrapException(
109 "bad characters in soffice installation path!");
111 #endif
112 OUString path;
113 if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
114 osl::FileBase::E_None)
116 throw BootstrapException(
117 "cannot convert soffice installation path to URL!");
119 if (!path.isEmpty() && !path.endsWith("/")) {
120 path += "/";
123 OUString uri;
124 if (!Bootstrap::get("URE_BOOTSTRAP", uri)) {
125 Bootstrap::set(
126 "URE_BOOTSTRAP",
127 Bootstrap::encode(
128 path +
129 #if defined MACOSX
130 "../Resources/"
131 #endif
132 SAL_CONFIGFILE("fundamental")));
135 // create default local component context
136 Reference< XComponentContext > xLocalContext(
137 defaultBootstrap_InitialComponentContext() );
138 if ( !xLocalContext.is() )
139 throw BootstrapException( "no local component context!" );
141 // create a random pipe name
142 sal_uInt8 bytes[ 16 ];
143 if ( rtl_random_getBytes( nullptr, bytes, std::size( bytes ) )
144 != rtl_Random_E_None )
145 throw BootstrapException( "random pool error!" );
146 OUStringBuffer buf("uno");
147 for (unsigned char byte : bytes)
148 buf.append( static_cast< sal_Int32 >( byte ) );
149 OUString sPipeName( buf.makeStringAndClear() );
151 // arguments
152 OUString args [] = {
153 OUString("--nologo"),
154 OUString("--nodefault"),
155 OUString("--norestore"),
156 OUString("--nolockcheck"),
157 OUString("--accept=pipe,name=" + sPipeName + ";urp;")
159 rtl_uString * ar_args [] = {
160 args[ 0 ].pData,
161 args[ 1 ].pData,
162 args[ 2 ].pData,
163 args[ 3 ].pData,
164 args[ 4 ].pData
166 ::osl::Security sec;
168 // start office process
169 oslProcess hProcess = nullptr;
170 oslProcessError rc = osl_executeProcess(
171 OUString(path + "soffice").pData, ar_args, std::size( ar_args ),
172 osl_Process_DETACHED,
173 sec.getHandle(),
174 nullptr, // => current working dir
175 nullptr, 0, // => no env vars
176 &hProcess );
177 switch ( rc )
179 case osl_Process_E_None:
180 osl_freeProcessHandle( hProcess );
181 break;
182 case osl_Process_E_NotFound:
183 throw BootstrapException( "image not found!" );
184 case osl_Process_E_TimedOut:
185 throw BootstrapException( "timeout occurred!" );
186 case osl_Process_E_NoPermission:
187 throw BootstrapException( "permission denied!" );
188 case osl_Process_E_Unknown:
189 throw BootstrapException( "unknown error!" );
190 case osl_Process_E_InvalidError:
191 default:
192 throw BootstrapException( "unmapped error!" );
195 // create a URL resolver
196 Reference< bridge::XUnoUrlResolver > xUrlResolver(
197 bridge::UnoUrlResolver::create( xLocalContext ) );
199 // connection string
200 OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
202 // wait until office is started
203 for ( ; ; )
207 // try to connect to office
208 xRemoteContext.set(
209 xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
210 break;
212 catch ( connection::NoConnectException & )
214 // wait 500 ms, then try to connect again
215 std::this_thread::sleep_for(std::chrono::milliseconds(500));
219 catch ( Exception & e )
221 throw BootstrapException(
222 "unexpected UNO exception caught: " + e.Message );
225 return xRemoteContext;
228 OUString bootstrap_expandUri(OUString const & uri) {
229 OUString rest;
230 return uri.startsWith("vnd.sun.star.expand:", &rest)
231 ? cppuhelper::detail::expandMacros(
232 rtl::Uri::decode(
233 rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
234 : uri;
237 } // namespace cppu
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */