1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
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
;
79 const OUString
& BootstrapException::getMessage() const
84 Reference
< XComponentContext
> SAL_CALL
bootstrap()
86 Reference
< XComponentContext
> xRemoteContext
;
90 auto* p1
= cppuhelper_detail_findSofficePath();
92 throw BootstrapException(
93 "no soffice installation found!");
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
));
108 throw BootstrapException(
109 "bad characters in soffice installation 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("/")) {
124 if (!Bootstrap::get("URE_BOOTSTRAP", uri
)) {
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() );
153 OUString("--nologo"),
154 OUString("--nodefault"),
155 OUString("--norestore"),
156 OUString("--nolockcheck"),
157 OUString("--accept=pipe,name=" + sPipeName
+ ";urp;")
159 rtl_uString
* ar_args
[] = {
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
,
174 nullptr, // => current working dir
175 nullptr, 0, // => no env vars
179 case osl_Process_E_None
:
180 osl_freeProcessHandle( hProcess
);
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
:
192 throw BootstrapException( "unmapped error!" );
195 // create a URL resolver
196 Reference
< bridge::XUnoUrlResolver
> xUrlResolver(
197 bridge::UnoUrlResolver::create( xLocalContext
) );
200 OUString
sConnectString( "uno:pipe,name=" + sPipeName
+ ";urp;StarOffice.ComponentContext" );
202 // wait until office is started
207 // try to connect to office
209 xUrlResolver
->resolve( sConnectString
), UNO_QUERY_THROW
);
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
) {
230 return uri
.startsWith("vnd.sun.star.expand:", &rest
)
231 ? cppuhelper::detail::expandMacros(
233 rest
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
))
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */