1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_executable.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "dp_backend.h"
36 #include "dp_interact.h"
37 #include "rtl/string.hxx"
38 #include "osl/file.hxx"
39 #include "ucbhelper/content.hxx"
40 #include "comphelper/servicedecl.hxx"
41 #include "svtools/inettype.hxx"
42 #include "cppuhelper/implbase1.hxx"
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::ucb
;
47 using ::rtl::OUString
;
49 namespace dp_registry
{
51 namespace executable
{
54 class BackendImpl
: public ::dp_registry::backend::PackageRegistryBackend
56 class ExecutablePackageImpl
: public ::dp_registry::backend::Package
58 BackendImpl
* getMyBackend() const;
61 virtual beans::Optional
< beans::Ambiguous
<sal_Bool
> > isRegistered_(
62 ::osl::ResettableMutexGuard
& guard
,
63 ::rtl::Reference
<dp_misc::AbortChannel
> const & abortChannel
,
64 Reference
<XCommandEnvironment
> const & xCmdEnv
);
65 virtual void processPackage_(
66 ::osl::ResettableMutexGuard
& guard
,
68 ::rtl::Reference
<dp_misc::AbortChannel
> const & abortChannel
,
69 Reference
<XCommandEnvironment
> const & xCmdEnv
);
71 bool getFileAttributes(sal_uInt64
& out_Attributes
);
72 bool isUrlTargetInExtension();
75 inline ExecutablePackageImpl(
76 ::rtl::Reference
<PackageRegistryBackend
> const & myBackend
,
77 OUString
const & url
, OUString
const & name
,
78 Reference
<deployment::XPackageTypeInfo
> const & xPackageType
)
79 : Package( myBackend
, url
, name
, name
/* display-name */,
83 friend class ExecutablePackageImpl
;
85 typedef ::std::hash_map
< OUString
, Reference
<XInterface
>,
86 ::rtl::OUStringHash
> t_string2object
;
88 // PackageRegistryBackend
89 virtual Reference
<deployment::XPackage
> bindPackage_(
90 OUString
const & url
, OUString
const & mediaType
,
91 Reference
<XCommandEnvironment
> const & xCmdEnv
);
93 Reference
<deployment::XPackageTypeInfo
> m_xExecutableTypeInfo
;
96 BackendImpl( Sequence
<Any
> const & args
,
97 Reference
<XComponentContext
> const & xComponentContext
);
100 virtual Sequence
< Reference
<deployment::XPackageTypeInfo
> > SAL_CALL
101 getSupportedPackageTypes() throw (RuntimeException
);
103 using PackageRegistryBackend::disposing
;
107 BackendImpl::BackendImpl(
108 Sequence
<Any
> const & args
,
109 Reference
<XComponentContext
> const & xComponentContext
)
110 : PackageRegistryBackend( args
, xComponentContext
),
111 m_xExecutableTypeInfo(new Package::TypeInfo(
112 OUSTR("application/vnd.sun.star.executable"),
116 RID_IMG_COMPONENT_HC
) )
122 Sequence
< Reference
<deployment::XPackageTypeInfo
> >
123 BackendImpl::getSupportedPackageTypes() throw (RuntimeException
)
125 return Sequence
<Reference
<deployment::XPackageTypeInfo
> >(
126 & m_xExecutableTypeInfo
, 1);
129 // PackageRegistryBackend
130 Reference
<deployment::XPackage
> BackendImpl::bindPackage_(
131 OUString
const & url
, OUString
const & mediaType
,
132 Reference
<XCommandEnvironment
> const & xCmdEnv
)
134 if (mediaType
.getLength() == 0)
136 throw lang::IllegalArgumentException(
137 StrCannotDetectMediaType::get() + url
,
138 static_cast<OWeakObject
*>(this), static_cast<sal_Int16
>(-1) );
141 String type
, subType
;
142 INetContentTypeParameterList params
;
143 if (INetContentTypes::parse( mediaType
, type
, subType
, ¶ms
))
145 if (type
.EqualsIgnoreCaseAscii("application"))
147 ::ucbhelper::Content
ucbContent( url
, xCmdEnv
);
148 const OUString
name( ucbContent
.getPropertyValue(
149 dp_misc::StrTitle::get() ).get
<OUString
>() );
150 if (subType
.EqualsIgnoreCaseAscii("vnd.sun.star.executable"))
152 return new BackendImpl::ExecutablePackageImpl(
153 this, url
, name
, m_xExecutableTypeInfo
);
157 return Reference
<deployment::XPackage
>();
160 //##############################################################################
164 BackendImpl
* BackendImpl::ExecutablePackageImpl::getMyBackend() const
166 BackendImpl
* pBackend
= static_cast<BackendImpl
*>(m_myBackend
.get());
167 if (NULL
== pBackend
)
169 //May throw a DisposedException
171 //We should never get here...
172 throw RuntimeException(
173 OUSTR("Failed to get the BackendImpl"),
174 static_cast<OWeakObject
*>(const_cast<ExecutablePackageImpl
*>(this)));
179 beans::Optional
< beans::Ambiguous
<sal_Bool
> >
180 BackendImpl::ExecutablePackageImpl::isRegistered_(
181 ::osl::ResettableMutexGuard
&,
182 ::rtl::Reference
<dp_misc::AbortChannel
> const &,
183 Reference
<XCommandEnvironment
> const & )
185 //We must return Optional.isPresent = true, otherwise
186 //processPackage is not called.
187 //The user shall not be able to enable/disable the executable. This is not needed since
188 //the executable does not affect the office. The best thing is to show no
189 //status at all. See also BackendImpl::PackageImpl::isRegistered_ (dp_package.cxx)
190 //On Windows there is no executable file attribute. One has to use security API for this.
191 //However, on Windows we do not have the problem, that after unzipping the file cannot be
193 return beans::Optional
< beans::Ambiguous
<sal_Bool
> >(
194 sal_True
/* IsPresent */,
195 beans::Ambiguous
<sal_Bool
>(
196 sal_True
, sal_True
/* IsAmbiguous */ ) );
199 void BackendImpl::ExecutablePackageImpl::processPackage_(
200 ::osl::ResettableMutexGuard
&,
201 bool doRegisterPackage
,
202 ::rtl::Reference
<dp_misc::AbortChannel
> const & abortChannel
,
203 Reference
<XCommandEnvironment
> const & /*xCmdEnv*/ )
205 checkAborted(abortChannel
);
206 if (doRegisterPackage
)
208 if (!isUrlTargetInExtension())
213 sal_uInt64 attributes
= 0;
214 //Setting the executable attribut does not affect executables on Windows
215 if (getFileAttributes(attributes
))
217 if(getMyBackend()->m_context
.equals(OUSTR("user")))
218 attributes
|= osl_File_Attribute_OwnExe
;
219 else if (getMyBackend()->m_context
.equals(OUSTR("shared")))
220 attributes
|= (osl_File_Attribute_OwnExe
| osl_File_Attribute_GrpExe
221 | osl_File_Attribute_OthExe
);
225 //This won't have affect on Windows
226 if (osl::File::E_None
!= osl::File::setAttributes(
227 dp_misc::expandUnoRcUrl(m_url
), attributes
))
228 OSL_ENSURE(0, "Extension Manager: Could not set executable file attribute.");
233 //We currently cannot check if this XPackage represents a content of a particular exension
234 //But we can check if we are within $UNO_USER_PACKAGES_CACHE etc.
235 bool BackendImpl::ExecutablePackageImpl::isUrlTargetInExtension()
237 bool bSuccess
= false;
238 OUString sExtensionDir
;
239 if(getMyBackend()->m_context
.equals(OUSTR("user")))
240 sExtensionDir
= dp_misc::expandUnoRcTerm(OUSTR("$UNO_USER_PACKAGES_CACHE"));
241 else if (getMyBackend()->m_context
.equals(OUSTR("shared")))
242 sExtensionDir
= dp_misc::expandUnoRcTerm(OUSTR("$UNO_SHARED_PACKAGES_CACHE"));
245 //remove file ellipses
246 if (osl::File::E_None
== osl::File::getAbsoluteFileURL(OUString(), sExtensionDir
, sExtensionDir
))
249 if (osl::File::E_None
== osl::File::getAbsoluteFileURL(
250 OUString(), dp_misc::expandUnoRcUrl(m_url
), sFile
))
252 if (sal_True
== sFile
.match(sExtensionDir
, 0))
259 bool BackendImpl::ExecutablePackageImpl::getFileAttributes(sal_uInt64
& out_Attributes
)
261 bool bSuccess
= false;
262 const OUString
url(dp_misc::expandUnoRcUrl(m_url
));
263 osl::DirectoryItem item
;
264 if (osl::FileBase::E_None
== osl::DirectoryItem::get(url
, item
))
266 osl::FileStatus
aStatus(osl_FileStatus_Mask_Attributes
);
267 if( osl::FileBase::E_None
== item
.getFileStatus(aStatus
))
269 out_Attributes
= aStatus
.getAttributes();
276 //##############################################################################
281 namespace sdecl
= comphelper::service_decl
;
282 sdecl::class_
<BackendImpl
, sdecl::with_args
<true> > serviceBI
;
283 extern sdecl::ServiceDecl
const serviceDecl(
285 "com.sun.star.comp.deployment.executable.PackageRegistryBackend",
286 BACKEND_SERVICE_NAME
);
288 } // namespace component
289 } // namespace backend
290 } // namespace dp_registry