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