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