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 {
78  Reference<XMultiServiceFactory> sm;
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 namespace {
179 
180 class enable
181 {
182  private:
183  Sequence<Any> m_aSeq;
184  public:
185  enable() : m_aSeq(1) {
186  m_aSeq[0] <<= true;
187  }
188  void operator() (const AcceptorMap::value_type& val) {
189  if (val.second.is()) {
190  val.second->initialize(m_aSeq);
191  }
192  }
193 };
194 
195 }
196 
197 // enable acceptors
198 IMPL_STATIC_LINK_NOARG(Desktop, EnableAcceptors_Impl, void*, void)
199 {
200  if (!bAccept)
201  {
202  // from now on, all new acceptors are enabled
203  bAccept = true;
204  // enable existing acceptors by calling initialize(true)
205  // on all existing acceptors
206  AcceptorMap &rMap = acceptorMap::get();
207  std::for_each(rMap.begin(), rMap.end(), enable());
208  }
209 }
210 
211 void Desktop::destroyAcceptor(const OUString& aAcceptString)
212 {
213  // special case stop all acceptors
214  AcceptorMap &rMap = acceptorMap::get();
215  if (aAcceptString == "all") {
216  rMap.clear();
217 
218  } else {
219  // try to remove acceptor from map
220  AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
221  if (pIter != rMap.end() ) {
222  // remove reference from map
223  // this is the last reference and the acceptor will be destructed
224  rMap.erase(aAcceptString);
225  } else {
226  SAL_WARN( "desktop.app", "Found no acceptor to remove");
227  }
228  }
229 }
230 
231 
233 {
234  // stop all acceptors by clearing the map
235  acceptorMap::get().clear();
236 }
237 
239 {
240  OUString aTempBaseURL;
241  try
242  {
243  SvtPathOptions aOpt;
244  aTempBaseURL = aOpt.GetTempPath();
245  }
246  catch (RuntimeException& e)
247  {
248  // Catch runtime exception here: We have to add language dependent info
249  // to the exception message. Fallback solution uses hard coded string.
250  OUString aMsg = DpResId(STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE);
251  e.Message = aMsg + e.Message;
252  throw;
253  }
254 
255  // create new current temporary directory
256  OUString aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
257  if ( aTempPath.isEmpty()
258  && ::osl::File::getTempDirURL( aTempBaseURL ) == osl::FileBase::E_None )
259  {
260  aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempBaseURL );
261  }
262 
263  // set new current temporary directory
264  OUString aRet;
265  if (osl::FileBase::getFileURLFromSystemPath( aTempPath, aRet )
266  != osl::FileBase::E_None)
267  {
268  aRet.clear();
269  }
270  CurrentTempURL::get() = aRet;
271 }
272 
274 {
275  // remove current temporary directory
276  OUString &rCurrentTempURL = CurrentTempURL::get();
277  if ( !rCurrentTempURL.isEmpty() )
278  {
279  ::utl::UCBContentHelper::Kill( rCurrentTempURL );
280  }
281 }
282 
283 } // namespace desktop
284 
285 /* 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:211
Sequence< Any > m_aSeq
Definition: appinit.cxx:183
const OUString & GetTempPath() const
static void CreateTemporaryDirectory()
Definition: appinit.cxx:238
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:168
bool IsHeadless() const
Definition: cmdlineargs.hxx:69
static void InitApplicationServiceManager()
Definition: appinit.cxx:76
static void DeregisterServices()
Definition: appinit.cxx:232
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:273
static void EnableEventTestingMode()
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
static CommandLineArgs & GetCommandLineArgs()
Definition: app.cxx:395
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:2480
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:2087
static OUString SetTempNameBaseDirectory(const OUString &rBaseName)
bool m_bServicesRegistered
Definition: app.hxx:167
std::vector< OUString > const & GetAccept() const
Reference< XComponentContext > getProcessComponentContext()
Sequence< sal_Int8 > aSeq
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