android: Update app icon to new startcenter icon
[LibreOffice.git] / cppuhelper / source / bootstrap.cxx
blobbd975460f06cd83122153a9c1005083290e83abf
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 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() );
155 // arguments
156 OUString args [] = {
157 OUString("--nologo"),
158 OUString("--nodefault"),
159 OUString("--norestore"),
160 OUString("--nolockcheck"),
161 OUString("--accept=pipe,name=" + sPipeName + ";urp;")
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
170 ::osl::Security sec;
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 )
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!" );
199 // create a URL resolver
200 Reference< bridge::XUnoUrlResolver > xUrlResolver(
201 bridge::UnoUrlResolver::create( xLocalContext ) );
203 // connection string
204 OUString sConnectString( "uno:pipe,name=" + sPipeName + ";urp;StarOffice.ComponentContext" );
206 // wait until office is started
207 for ( ; ; )
211 // try to connect to office
212 xRemoteContext.set(
213 xUrlResolver->resolve( sConnectString ), UNO_QUERY_THROW );
214 break;
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) {
233 OUString rest;
234 return uri.startsWith("vnd.sun.star.expand:", &rest)
235 ? cppuhelper::detail::expandMacros(
236 rtl::Uri::decode(
237 rest, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8))
238 : uri;
241 } // namespace cppu
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */