LibreOffice Module desktop (master)  1
appinit.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 
21 #include <algorithm>
22 
23 #include <config_vclplug.h>
24 
25 #include <app.hxx>
26 #include <dp_shared.hxx>
27 #include "cmdlineargs.hxx"
28 #include <strings.hrc>
29 #include <com/sun/star/registry/XSimpleRegistry.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XInitialization.hpp>
32 #include <com/sun/star/uno/Exception.hpp>
33 #include <com/sun/star/uno/XCurrentContext.hpp>
34 #include <com/sun/star/packages/zip/ZipIOException.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
37 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
38 #include <uno/current_context.hxx>
39 #include <cppuhelper/bootstrap.hxx>
40 #include <officecfg/Setup.hxx>
41 #include <osl/file.hxx>
42 #include <osl/module.h>
43 #include <rtl/uri.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <rtl/bootstrap.hxx>
46 #include <sal/log.hxx>
47 #include <tools/diagnose_ex.h>
48 
49 #include <rtl/instance.hxx>
51 #include <unotools/ucbhelper.hxx>
52 #include <unotools/tempfile.hxx>
53 #include <vcl/svapp.hxx>
54 #include <unotools/pathoptions.hxx>
55 #include <sfx2/safemode.hxx>
56 #include <map>
57 
58 using namespace desktop;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::registry;
63 using namespace ::com::sun::star::ucb;
64 
65 namespace desktop
66 {
67 
68 
69 static void configureUcb()
70 {
71  // For backwards compatibility, in case some code still uses plain
72  // createInstance w/o args directly to obtain an instance:
74 }
75 
77 {
79 #ifdef ANDROID
80  OUString aUnoRc( "file:///assets/program/unorc" );
81  sm.set(
82  cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
83  UNO_QUERY_THROW);
84 #elif defined(IOS)
85  OUString uri( "$APP_DATA_DIR" );
86  rtl_bootstrap_expandMacros( &uri.pData );
87  OUString aUnoRc("file://" + uri + "/unorc");
88  sm.set(
89  cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
90  UNO_QUERY_THROW);
91 #else
92  sm.set(
94  UNO_QUERY_THROW);
95 #endif
97 }
98 
100 {
101  if( !m_bServicesRegistered )
102  {
103  // interpret command line arguments
104  CommandLineArgs& rCmdLine = GetCommandLineArgs();
105 
106  // Headless mode for FAT Office, auto cancels any dialogs that popup
107  if (rCmdLine.IsEventTesting())
109  else if (rCmdLine.IsHeadless())
111 
112  // read accept string from configuration
113  OUString conDcpCfg(
114  officecfg::Setup::Office::ooSetupConnectionURL::get(context));
115  if (!conDcpCfg.isEmpty()) {
116  createAcceptor(conDcpCfg);
117  }
118 
119  std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
120  for (auto const& elem : conDcp)
121  {
122  createAcceptor(elem);
123  }
124 
125  configureUcb();
126 
128  m_bServicesRegistered = true;
129  }
130 }
131 
132 typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
133 
134 namespace
135 {
136  struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
137  struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
138 }
139 
140 static bool bAccept = false;
141 
142 void Desktop::createAcceptor(const OUString& aAcceptString)
143 {
144  // check whether the requested acceptor already exists
145  AcceptorMap &rMap = acceptorMap::get();
146  AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
147  if (pIter == rMap.end() )
148  {
149  Sequence< Any > aSeq( 2 );
150  aSeq[0] <<= aAcceptString;
151  aSeq[1] <<= bAccept;
152  Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
153  Reference<XInitialization> rAcceptor(
154  xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
155  UNO_QUERY );
156  if ( rAcceptor.is() )
157  {
158  try
159  {
160  rAcceptor->initialize( aSeq );
161  rMap.emplace(aAcceptString, rAcceptor);
162  }
163  catch (const css::uno::Exception&)
164  {
165  // no error handling needed...
166  // acceptor just won't come up
167  TOOLS_WARN_EXCEPTION( "desktop.app", "Acceptor could not be created");
168  }
169  }
170  else
171  {
172  // there is already an acceptor with this description
173  SAL_WARN( "desktop.app", "Acceptor already exists.");
174  }
175  }
176 }
177 
178 class enable
179 {
180  private:
181  Sequence<Any> m_aSeq;
182  public:
183  enable() : m_aSeq(1) {
184  m_aSeq[0] <<= true;
185  }
186  void operator() (const AcceptorMap::value_type& val) {
187  if (val.second.is()) {
188  val.second->initialize(m_aSeq);
189  }
190  }
191 };
192 
193 // enable acceptors
194 IMPL_STATIC_LINK_NOARG(Desktop, EnableAcceptors_Impl, void*, void)
195 {
196  if (!bAccept)
197  {
198  // from now on, all new acceptors are enabled
199  bAccept = true;
200  // enable existing acceptors by calling initialize(true)
201  // on all existing acceptors
202  AcceptorMap &rMap = acceptorMap::get();
203  std::for_each(rMap.begin(), rMap.end(), enable());
204  }
205 }
206 
207 void Desktop::destroyAcceptor(const OUString& aAcceptString)
208 {
209  // special case stop all acceptors
210  AcceptorMap &rMap = acceptorMap::get();
211  if (aAcceptString == "all") {
212  rMap.clear();
213 
214  } else {
215  // try to remove acceptor from map
216  AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
217  if (pIter != rMap.end() ) {
218  // remove reference from map
219  // this is the last reference and the acceptor will be destructed
220  rMap.erase(aAcceptString);
221  } else {
222  SAL_WARN( "desktop.app", "Found no acceptor to remove");
223  }
224  }
225 }
226 
227 
229 {
230  // stop all acceptors by clearing the map
231  acceptorMap::get().clear();
232 }
233 
235 {
236  OUString aTempBaseURL;
237  try
238  {
239  SvtPathOptions aOpt;
240  aTempBaseURL = aOpt.GetTempPath();
241  }
242  catch (RuntimeException& e)
243  {
244  // Catch runtime exception here: We have to add language dependent info
245  // to the exception message. Fallback solution uses hard coded string.
246  OUString aMsg = DpResId(STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE);
247  e.Message = aMsg + e.Message;
248  throw;
249  }
250 
251  // create new current temporary directory
252  OUString aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
253  if ( aTempPath.isEmpty()
254  && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
255  {
256  aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
257  }
258 
259  // set new current temporary directory
260  OUString aRet;
261  if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
262  != osl::FileBase::E_None)
263  {
264  aRet.clear();
265  }
266  CurrentTempURL::get() = aRet;
267 }
268 
270 {
271  // remove current temporary directory
272  OUString &rCurrentTempURL = CurrentTempURL::get();
273  if ( !rCurrentTempURL.isEmpty() )
274  {
275  ::utl::UCBContentHelper::Kill( rCurrentTempURL );
276  }
277 }
278 
279 } // namespace desktop
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::map< OUString, css::uno::Reference< css::lang::XInitialization > > AcceptorMap
Definition: appinit.cxx:132
static void destroyAcceptor(const OUString &aDescription)
Definition: appinit.cxx:207
const OUString & GetTempPath() const
static void CreateTemporaryDirectory()
Definition: appinit.cxx:234
void operator()(const AcceptorMap::value_type &val)
Definition: appinit.cxx:186
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
Definition: app.cxx:166
Sequence< Any > m_aSeq
Definition: appinit.cxx:181
bool IsHeadless() const
Definition: cmdlineargs.hxx:69
static void InitApplicationServiceManager()
Definition: appinit.cxx:76
static void DeregisterServices()
Definition: appinit.cxx:228
void RegisterServices(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: appinit.cxx:99
#define TOOLS_WARN_EXCEPTION(area, stream)
static void RemoveTemporaryDirectory()
Definition: appinit.cxx:269
static void EnableEventTestingMode()
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
static CommandLineArgs & GetCommandLineArgs()
Definition: app.cxx:384
bool IsEventTesting() const
Definition: cmdlineargs.hxx:70
static void createAcceptor(const OUString &aDescription)
Definition: appinit.cxx:142
IMPL_STATIC_LINK_NOARG(Desktop, AsyncInitFirstRun, Timer *, void)
Definition: app.cxx:2467
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:1858
static OUString SetTempNameBaseDirectory(const OUString &rBaseName)
bool m_bServicesRegistered
Definition: app.hxx:167
std::vector< OUString > const & GetAccept() const
Reference< XComponentContext > getProcessComponentContext()
static void EnableHeadlessMode(bool dialogsAreFatal)
OUString DpResId(const char *pId)
Definition: dp_shared.hxx:38
#define SAL_WARN(area, stream)
static void configureUcb()
Definition: appinit.cxx:69
static bool bAccept
Definition: appinit.cxx:140