LibreOffice Module desktop (master)  1
dp_executable.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 <memory>
22 #include <dp_misc.h>
23 #include <dp_backend.h>
24 #include <dp_ucb.h>
25 #include <dp_interact.h>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <osl/file.hxx>
28 #include <ucbhelper/content.hxx>
29 #include <svl/inettype.hxx>
32 
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::ucb;
36 using namespace dp_misc;
37 
39 namespace {
40 
41 class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
42 {
43  class ExecutablePackageImpl : public ::dp_registry::backend::Package
44  {
45  BackendImpl * getMyBackend() const;
46 
47  // Package
48  virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
49  ::osl::ResettableMutexGuard & guard,
50  ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
51  Reference<XCommandEnvironment> const & xCmdEnv ) override;
52  virtual void processPackage_(
53  ::osl::ResettableMutexGuard & guard,
54  bool registerPackage,
55  bool startup,
56  ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
57  Reference<XCommandEnvironment> const & xCmdEnv ) override;
58 
59  bool getFileAttributes(sal_uInt64& out_Attributes);
60  bool isUrlTargetInExtension() const;
61 
62  public:
63  ExecutablePackageImpl(
64  ::rtl::Reference<PackageRegistryBackend> const & myBackend,
65  OUString const & url, OUString const & name,
66  Reference<deployment::XPackageTypeInfo> const & xPackageType,
67  bool bRemoved, OUString const & identifier)
68  : Package( myBackend, url, name, name /* display-name */,
69  xPackageType, bRemoved, identifier)
70  {}
71  };
72  friend class ExecutablePackageImpl;
73 
74  // PackageRegistryBackend
75  virtual Reference<deployment::XPackage> bindPackage_(
76  OUString const & url, OUString const & mediaType, bool bRemoved,
77  OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv ) override;
78 
79  void addDataToDb(OUString const & url);
80  bool hasActiveEntry(OUString const & url);
81  void revokeEntryFromDb(OUString const & url);
82 
83  Reference<deployment::XPackageTypeInfo> m_xExecutableTypeInfo;
84  std::unique_ptr<ExecutableBackendDb> m_backendDb;
85 public:
86  BackendImpl( Sequence<Any> const & args,
87  Reference<XComponentContext> const & xComponentContext );
88 
89  // XServiceInfo
90  virtual OUString SAL_CALL getImplementationName() override;
91  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
92  virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
93 
94  // XPackageRegistry
95  virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
96  getSupportedPackageTypes() override;
97  virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType) override;
98 
99 };
100 
101 
102 BackendImpl::BackendImpl(
103  Sequence<Any> const & args,
104  Reference<XComponentContext> const & xComponentContext )
105  : PackageRegistryBackend( args, xComponentContext ),
106  m_xExecutableTypeInfo(new Package::TypeInfo(
107  "application/vnd.sun.star.executable",
108  "", "Executable" ) )
109 {
110  if (!transientMode())
111  {
112  OUString dbFile = makeURL(getCachePath(), "backenddb.xml");
113  m_backendDb.reset(
114  new ExecutableBackendDb(getComponentContext(), dbFile));
115  }
116 }
117 
118 // XServiceInfo
119 OUString BackendImpl::getImplementationName()
120 {
121  return "com.sun.star.comp.deployment.executable.PackageRegistryBackend";
122 }
123 
124 sal_Bool BackendImpl::supportsService( const OUString& ServiceName )
125 {
126  return cppu::supportsService(this, ServiceName);
127 }
128 
129 css::uno::Sequence< OUString > BackendImpl::getSupportedServiceNames()
130 {
131  return { BACKEND_SERVICE_NAME };
132 }
133 
134 void BackendImpl::addDataToDb(OUString const & url)
135 {
136  if (m_backendDb)
137  m_backendDb->addEntry(url);
138 }
139 
140 void BackendImpl::revokeEntryFromDb(OUString const & url)
141 {
142  if (m_backendDb)
143  m_backendDb->revokeEntry(url);
144 }
145 
146 bool BackendImpl::hasActiveEntry(OUString const & url)
147 {
148  if (m_backendDb)
149  return m_backendDb->hasActiveEntry(url);
150  return false;
151 }
152 
153 
154 // XPackageRegistry
155 Sequence< Reference<deployment::XPackageTypeInfo> >
156 BackendImpl::getSupportedPackageTypes()
157 {
158  return Sequence<Reference<deployment::XPackageTypeInfo> >(
159  & m_xExecutableTypeInfo, 1);
160 }
161 
162 void BackendImpl::packageRemoved(OUString const & url, OUString const & /*mediaType*/)
163 {
164  if (m_backendDb)
165  m_backendDb->removeEntry(url);
166 }
167 
168 // PackageRegistryBackend
169 Reference<deployment::XPackage> BackendImpl::bindPackage_(
170  OUString const & url, OUString const & mediaType, bool bRemoved,
171  OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
172 {
173  if (mediaType.isEmpty())
174  {
175  throw lang::IllegalArgumentException(
176  StrCannotDetectMediaType() + url,
177  static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
178  }
179 
180  OUString type, subType;
182  if (INetContentTypes::parse( mediaType, type, subType, &params ))
183  {
184  if (type.equalsIgnoreAsciiCase("application"))
185  {
186  OUString name;
187  if (!bRemoved)
188  {
189  ::ucbhelper::Content ucbContent(
190  url, xCmdEnv, getComponentContext() );
191  name = StrTitle::getTitle( ucbContent );
192  }
193  if (subType.equalsIgnoreAsciiCase("vnd.sun.star.executable"))
194  {
195  return new BackendImpl::ExecutablePackageImpl(
196  this, url, name, m_xExecutableTypeInfo, bRemoved,
197  identifier);
198  }
199  }
200  }
201  return Reference<deployment::XPackage>();
202 }
203 
204 
205 // Package
206 BackendImpl * BackendImpl::ExecutablePackageImpl::getMyBackend() const
207 {
208  BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
209  if (nullptr == pBackend)
210  {
211  //May throw a DisposedException
212  check();
213  //We should never get here...
214  throw RuntimeException( "Failed to get the BackendImpl",
215  static_cast<OWeakObject*>(const_cast<ExecutablePackageImpl *>(this)));
216  }
217  return pBackend;
218 }
219 
220 beans::Optional< beans::Ambiguous<sal_Bool> >
221 BackendImpl::ExecutablePackageImpl::isRegistered_(
222  ::osl::ResettableMutexGuard &,
225 {
226  bool registered = getMyBackend()->hasActiveEntry(getURL());
227  return beans::Optional< beans::Ambiguous<sal_Bool> >(
228  true /* IsPresent */,
229  beans::Ambiguous<sal_Bool>(
230  registered, false /* IsAmbiguous */ ) );
231 }
232 
233 void BackendImpl::ExecutablePackageImpl::processPackage_(
234  ::osl::ResettableMutexGuard &,
235  bool doRegisterPackage,
236  bool /*startup*/,
237  ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
238  Reference<XCommandEnvironment> const & /*xCmdEnv*/ )
239 {
240  checkAborted(abortChannel);
241  if (doRegisterPackage)
242  {
243  if (!isUrlTargetInExtension())
244  {
245  OSL_ASSERT(false);
246  return;
247  }
248  sal_uInt64 attributes = 0;
249  //Setting the executable attribute does not affect executables on Windows
250  if (getFileAttributes(attributes))
251  {
252  if(getMyBackend()->m_context == "user")
253  attributes |= osl_File_Attribute_OwnExe;
254  else if (getMyBackend()->m_context == "shared")
255  attributes |= (osl_File_Attribute_OwnExe | osl_File_Attribute_GrpExe
256  | osl_File_Attribute_OthExe);
257  else if (getMyBackend()->m_context != "bundled")
258  //Bundled extensions are required to be in the properly
259  //installed. That is an executable must have the right flags
260  OSL_ASSERT(false);
261 
262  //This won't have effect on Windows
263  osl::File::setAttributes(
264  dp_misc::expandUnoRcUrl(m_url), attributes);
265  }
266  getMyBackend()->addDataToDb(getURL());
267  }
268  else
269  {
270  getMyBackend()->revokeEntryFromDb(getURL());
271  }
272 }
273 
274 //We currently cannot check if this XPackage represents a content of a particular extension
275 //But we can check if we are within $UNO_USER_PACKAGES_CACHE etc.
276 //Done for security reasons. For example an extension manifest could contain a path to
277 //an executable outside the extension.
278 bool BackendImpl::ExecutablePackageImpl::isUrlTargetInExtension() const
279 {
280  bool bSuccess = false;
281  OUString sExtensionDir;
282  if(getMyBackend()->m_context == "user")
283  sExtensionDir = dp_misc::expandUnoRcTerm("$UNO_USER_PACKAGES_CACHE");
284  else if (getMyBackend()->m_context == "shared")
285  sExtensionDir = dp_misc::expandUnoRcTerm("$UNO_SHARED_PACKAGES_CACHE");
286  else if (getMyBackend()->m_context == "bundled")
287  sExtensionDir = dp_misc::expandUnoRcTerm("$BUNDLED_EXTENSIONS");
288  else
289  OSL_ASSERT(false);
290  //remove file ellipses
291  if (osl::File::E_None == osl::File::getAbsoluteFileURL(OUString(), sExtensionDir, sExtensionDir))
292  {
293  OUString sFile;
294  if (osl::File::E_None == osl::File::getAbsoluteFileURL(
295  OUString(), dp_misc::expandUnoRcUrl(m_url), sFile))
296  {
297  if (sFile.match(sExtensionDir))
298  bSuccess = true;
299  }
300  }
301  return bSuccess;
302 }
303 
304 bool BackendImpl::ExecutablePackageImpl::getFileAttributes(sal_uInt64& out_Attributes)
305 {
306  bool bSuccess = false;
307  const OUString url(dp_misc::expandUnoRcUrl(m_url));
308  osl::DirectoryItem item;
309  if (osl::FileBase::E_None == osl::DirectoryItem::get(url, item))
310  {
311  osl::FileStatus aStatus(osl_FileStatus_Mask_Attributes);
312  if( osl::FileBase::E_None == item.getFileStatus(aStatus))
313  {
314  out_Attributes = aStatus.getAttributes();
315  bSuccess = true;
316  }
317  }
318  return bSuccess;
319 }
320 
321 
322 } // anon namespace
323 
324 
325 } // namespace dp_registry::backend::executable
326 
327 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
329  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
330 {
331  return cppu::acquire(new dp_registry::backend::executable::BackendImpl(args, context));
332 }
333 
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString expandUnoRcTerm(OUString const &term_)
Definition: dp_misc.cxx:292
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
OUString makeURL(OUString const &baseURL, OUString const &relPath_)
appends a relative path to a url.
Definition: dp_misc.cxx:251
static bool parse(OUString const &rMediaType, OUString &rType, OUString &rSubType, INetContentTypeParameterList *pParameters=nullptr)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
def check(model)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_deployment_executable_PackageRegistryBackend_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
Reference< deployment::XPackageTypeInfo > m_xExecutableTypeInfo
unsigned char sal_Bool
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
OUString expandUnoRcUrl(OUString const &url)
Definition: dp_misc.cxx:315
::boost::spirit::classic::rule< ScannerT > identifier
#define BACKEND_SERVICE_NAME
Definition: dp_backend.h:39
const char * name
std::unordered_map< OString, INetContentTypeParameter > INetContentTypeParameterList
ResultType type
std::unique_ptr< ExecutableBackendDb > m_backendDb