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 rtlRandomPool hPool
= rtl_random_createPool();
143 if ( hPool
== nullptr )
144 throw BootstrapException( "cannot create random pool!" );
145 sal_uInt8 bytes
[ 16 ];
146 if ( rtl_random_getBytes( hPool
, bytes
, std::size( bytes
) )
147 != rtl_Random_E_None
)
148 throw BootstrapException( "random pool error!" );
149 rtl_random_destroyPool( hPool
);
150 OUStringBuffer
buf("uno");
151 for (unsigned char byte
: bytes
)
152 buf
.append( static_cast< sal_Int32
>( byte
) );
153 OUString
sPipeName( buf
.makeStringAndClear() );
157 OUString("--nologo"),
158 OUString("--nodefault"),
159 OUString("--norestore"),
160 OUString("--nolockcheck"),
161 OUString("--accept=pipe,name=" + sPipeName
+ ";urp;")
163 rtl_uString
* ar_args
[] = {
172 // start office process
173 oslProcess hProcess
= nullptr;
174 oslProcessError rc
= osl_executeProcess(
175 OUString(path
+ "soffice").pData
, ar_args
, std::size( ar_args
),
176 osl_Process_DETACHED
,
178 nullptr, // => current working dir
179 nullptr, 0, // => no env vars
183 case osl_Process_E_None
:
184 osl_freeProcessHandle( hProcess
);
186 case osl_Process_E_NotFound
:
187 throw BootstrapException( "image not found!" );
188 case osl_Process_E_TimedOut
:
189 throw BootstrapException( "timeout occurred!" );
190 case osl_Process_E_NoPermission
:
191 throw BootstrapException( "permission denied!" );
192 case osl_Process_E_Unknown
:
193 throw BootstrapException( "unknown error!" );
194 case osl_Process_E_InvalidError
:
196 throw BootstrapException( "unmapped error!" );
199 // create a URL resolver
200 Reference
< bridge::XUnoUrlResolver
> xUrlResolver(
201 bridge::UnoUrlResolver::create( xLocalContext
) );
204 OUString
sConnectString( "uno:pipe,name=" + sPipeName
+ ";urp;StarOffice.ComponentContext" );
206 // wait until office is started
211 // try to connect to office
213 xUrlResolver
->resolve( sConnectString
), UNO_QUERY_THROW
);
216 catch ( connection::NoConnectException
& )
218 // wait 500 ms, then try to connect again
219 std::this_thread::sleep_for(std::chrono::milliseconds(500));
223 catch ( Exception
& e
)
225 throw BootstrapException(
226 "unexpected UNO exception caught: " + e
.Message
);
229 return xRemoteContext
;
232 OUString
bootstrap_expandUri(OUString
const & uri
) {
234 return uri
.startsWith("vnd.sun.star.expand:", &rest
)
235 ? cppuhelper::detail::expandMacros(
237 rest
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
))
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */