LibreOffice Module cppuhelper (master) 1
bootstrap.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <chrono>
23#include <cstring>
24#include <thread>
25
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>
34#include <osl/process.h>
35
38
39#include <com/sun/star/bridge/UnoUrlResolver.hpp>
40#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
41
42#include "macro_expander.hxx"
43
44namespace com :: sun :: star :: uno { class XComponentContext; }
45
46using namespace ::osl;
47using namespace ::com::sun::star;
48using namespace ::com::sun::star::uno;
49
50using rtl::Bootstrap;
51
52namespace cppu
53{
54
56{
57}
58
59BootstrapException::BootstrapException( const OUString & rMessage )
60 :m_aMessage( rMessage )
61{
62}
63
65{
67}
68
70{
71}
72
74{
76 return *this;
77}
78
79const OUString & BootstrapException::getMessage() const
80{
81 return m_aMessage;
82}
83
84Reference< XComponentContext > SAL_CALL bootstrap()
85{
86 Reference< XComponentContext > xRemoteContext;
87
88 try
89 {
91 if (p1 == nullptr) {
93 "no soffice installation found!");
94 }
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)
107 {
108 throw BootstrapException(
109 "bad characters in soffice installation path!");
110 }
111#endif
112 OUString path;
113 if (osl::FileBase::getFileURLFromSystemPath(p2, path) !=
114 osl::FileBase::E_None)
115 {
116 throw BootstrapException(
117 "cannot convert soffice installation path to URL!");
118 }
119 if (!path.isEmpty() && !path.endsWith("/")) {
120 path += "/";
121 }
122
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")));
133 }
134
135 // create default local component context
136 Reference< XComponentContext > xLocalContext(
138 if ( !xLocalContext.is() )
139 throw BootstrapException( "no local component context!" );
140
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() );
154
155 // arguments
156 OUString args [] = {
157 OUString("--nologo"),
158 OUString("--nodefault"),
159 OUString("--norestore"),
160 OUString("--nolockcheck"),
161 OUString("--accept=pipe,name=" + sPipeName + ";urp;")
162 };
163 rtl_uString * ar_args [] = {
164 args[ 0 ].pData,
165 args[ 1 ].pData,
166 args[ 2 ].pData,
167 args[ 3 ].pData,
168 args[ 4 ].pData
169 };
170 ::osl::Security sec;
171
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,
177 sec.getHandle(),
178 nullptr, // => current working dir
179 nullptr, 0, // => no env vars
180 &hProcess );
181 switch ( rc )
182 {
183 case osl_Process_E_None:
184 osl_freeProcessHandle( hProcess );
185 break;
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:
195 default:
196 throw BootstrapException( "unmapped error!" );
197 }
198
199 // create a URL resolver
200 Reference< bridge::XUnoUrlResolver > xUrlResolver(
201 bridge::UnoUrlResolver::create( xLocalContext ) );
202
203 // connection string
204 OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
205
206 // wait until office is started
207 for ( ; ; )
208 {
209 try
210 {
211 // try to connect to office
212 xRemoteContext.set(
213 xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
214 break;
215 }
216 catch ( connection::NoConnectException & )
217 {
218 // wait 500 ms, then try to connect again
219 std::this_thread::sleep_for(std::chrono::milliseconds(500));
220 }
221 }
222 }
223 catch ( Exception & e )
224 {
225 throw BootstrapException(
226 "unexpected UNO exception caught: " + e.Message );
227 }
228
229 return xRemoteContext;
230}
231
232OUString bootstrap_expandUri(OUString const & uri) {
233 OUString rest;
234 return uri.startsWith("vnd.sun.star.expand:", &rest)
236 rtl::Uri::decode(
237 rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
238 : uri;
239}
240
241} // namespace cppu
242
243/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * rtlRandomPool
An exception indicating a bootstrap error.
Definition: bootstrap.hxx:88
BootstrapException & operator=(const BootstrapException &e)
Assigns a BootstrapException.
Definition: bootstrap.cxx:73
BootstrapException()
Constructs a BootstrapException.
Definition: bootstrap.cxx:55
::rtl::OUString m_aMessage
Definition: bootstrap.hxx:127
virtual ~BootstrapException()
Destructs a BootstrapException.
Definition: bootstrap.cxx:69
const ::rtl::OUString & getMessage() const
Gets the message.
Definition: bootstrap.cxx:79
#define SAL_CONFIGFILE(name)
char * cppuhelper_detail_findSofficePath(void)
std::unique_ptr< sal_Int32[]> pData
@ Exception
OUString bootstrap_expandUri(OUString const &uri)
Definition: bootstrap.cxx:232
Reference< XComponentContext > SAL_CALL bootstrap()
Bootstraps the component context from a UNO installation.
Definition: bootstrap.cxx:84
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
Bootstraps an initial component context with service manager upon information from bootstrap variable...
OUString expandMacros(OUString const &text)
Helper function to expand macros based on the unorc/uno.ini.
args
std::vector< sal_uInt8 > bytes
unsigned char sal_uInt8