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>
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
;
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
;
78 const OUString
& BootstrapException::getMessage() const
83 Reference
< XComponentContext
> SAL_CALL
bootstrap()
85 Reference
< XComponentContext
> xRemoteContext
;
89 auto* p1
= cppuhelper_detail_findSofficePath();
91 throw BootstrapException(
92 "no soffice installation found!");
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
));
107 throw BootstrapException(
108 "bad characters in soffice installation 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("/")) {
123 if (!Bootstrap::get("URE_BOOTSTRAP", uri
)) {
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() );
156 OUString("--nologo"),
157 OUString("--nodefault"),
158 OUString("--norestore"),
159 OUString("--nolockcheck"),
160 OUString("--accept=pipe,name=" + sPipeName
+ ";urp;")
162 rtl_uString
* ar_args
[] = {
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
,
177 nullptr, // => current working dir
178 nullptr, 0, // => no env vars
182 case osl_Process_E_None
:
183 osl_freeProcessHandle( hProcess
);
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
:
195 throw BootstrapException( "unmapped error!" );
198 // create a URL resolver
199 Reference
< bridge::XUnoUrlResolver
> xUrlResolver(
200 bridge::UnoUrlResolver::create( xLocalContext
) );
203 OUString
sConnectString( "uno:pipe,name=" + sPipeName
+ ";urp;StarOffice.ComponentContext" );
205 // wait until office is started
210 // try to connect to office
212 xUrlResolver
->resolve( sConnectString
), UNO_QUERY_THROW
);
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
) {
233 return uri
.startsWith("vnd.sun.star.expand:", &rest
)
234 ? cppuhelper::detail::expandMacros(
236 rest
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
))
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */