xref: /trunk/main/desktop/source/deployment/registry/executable/dp_executable.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include "dp_misc.h"
28 #include "dp_backend.h"
29 #include "dp_ucb.h"
30 #include "dp_interact.h"
31 #include "rtl/string.hxx"
32 #include "osl/file.hxx"
33 #include "ucbhelper/content.hxx"
34 #include "comphelper/servicedecl.hxx"
35 #include "svl/inettype.hxx"
36 #include "cppuhelper/implbase1.hxx"
37 #include "dp_executablebackenddb.hxx"
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::ucb;
42 using namespace dp_misc;
43 using ::rtl::OUString;
44 
45 namespace dp_registry {
46 namespace backend {
47 namespace executable {
48 namespace {
49 
50 class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
51 {
52     class ExecutablePackageImpl : public ::dp_registry::backend::Package
53     {
54         BackendImpl * getMyBackend() const;
55 
56         // Package
57         virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
58             ::osl::ResettableMutexGuard & guard,
59             ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
60             Reference<XCommandEnvironment> const & xCmdEnv );
61         virtual void processPackage_(
62             ::osl::ResettableMutexGuard & guard,
63             bool registerPackage,
64             bool startup,
65             ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
66             Reference<XCommandEnvironment> const & xCmdEnv );
67 
68         bool getFileAttributes(sal_uInt64& out_Attributes);
69         bool isUrlTargetInExtension();
70 
71     public:
ExecutablePackageImpl(::rtl::Reference<PackageRegistryBackend> const & myBackend,OUString const & url,OUString const & name,Reference<deployment::XPackageTypeInfo> const & xPackageType,bool bRemoved,OUString const & identifier)72         inline ExecutablePackageImpl(
73             ::rtl::Reference<PackageRegistryBackend> const & myBackend,
74             OUString const & url, OUString const & name,
75             Reference<deployment::XPackageTypeInfo> const & xPackageType,
76             bool bRemoved, OUString const & identifier)
77             : Package( myBackend, url, name, name /* display-name */,
78                        xPackageType, bRemoved, identifier)
79             {}
80     };
81     friend class ExecutablePackageImpl;
82 
83     typedef ::std::hash_map< OUString, Reference<XInterface>,
84                              ::rtl::OUStringHash > t_string2object;
85 
86     // PackageRegistryBackend
87     virtual Reference<deployment::XPackage> bindPackage_(
88         OUString const & url, OUString const & mediaType, sal_Bool bRemoved,
89         OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv );
90 
91     void addDataToDb(OUString const & url);
92     bool hasActiveEntry(OUString const & url);
93     void revokeEntryFromDb(OUString const & url);
94 
95     Reference<deployment::XPackageTypeInfo> m_xExecutableTypeInfo;
96     std::auto_ptr<ExecutableBackendDb> m_backendDb;
97 public:
98     BackendImpl( Sequence<Any> const & args,
99                  Reference<XComponentContext> const & xComponentContext );
100 
101     // XPackageRegistry
102     virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
103     getSupportedPackageTypes();
104     virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType);
105 
106     using PackageRegistryBackend::disposing;
107 };
108 
109 
BackendImpl(Sequence<Any> const & args,Reference<XComponentContext> const & xComponentContext)110 BackendImpl::BackendImpl(
111     Sequence<Any> const & args,
112     Reference<XComponentContext> const & xComponentContext )
113     : PackageRegistryBackend( args, xComponentContext ),
114       m_xExecutableTypeInfo(new Package::TypeInfo(
115                                 OUSTR("application/vnd.sun.star.executable"),
116                                 OUSTR(""),
117                                 OUSTR("Executable"),
118                                 RID_IMG_COMPONENT,
119                                 RID_IMG_COMPONENT_HC ) )
120 {
121     if (!transientMode())
122     {
123         OUString dbFile = makeURL(getCachePath(), OUSTR("backenddb.xml"));
124         m_backendDb.reset(
125             new ExecutableBackendDb(getComponentContext(), dbFile));
126    }
127 }
128 
addDataToDb(OUString const & url)129 void BackendImpl::addDataToDb(OUString const & url)
130 {
131     if (m_backendDb.get())
132         m_backendDb->addEntry(url);
133 }
134 
revokeEntryFromDb(OUString const & url)135 void BackendImpl::revokeEntryFromDb(OUString const & url)
136 {
137     if (m_backendDb.get())
138         m_backendDb->revokeEntry(url);
139 }
140 
hasActiveEntry(OUString const & url)141 bool BackendImpl::hasActiveEntry(OUString const & url)
142 {
143     if (m_backendDb.get())
144         return m_backendDb->hasActiveEntry(url);
145     return false;
146 }
147 
148 
149 // XPackageRegistry
150 Sequence< Reference<deployment::XPackageTypeInfo> >
getSupportedPackageTypes()151 BackendImpl::getSupportedPackageTypes()
152 {
153     return Sequence<Reference<deployment::XPackageTypeInfo> >(
154         & m_xExecutableTypeInfo, 1);
155 }
156 
packageRemoved(OUString const & url,OUString const &)157 void BackendImpl::packageRemoved(OUString const & url, OUString const & /*mediaType*/)
158 {
159     if (m_backendDb.get())
160         m_backendDb->removeEntry(url);
161 }
162 
163 // PackageRegistryBackend
bindPackage_(OUString const & url,OUString const & mediaType,sal_Bool bRemoved,OUString const & identifier,Reference<XCommandEnvironment> const & xCmdEnv)164 Reference<deployment::XPackage> BackendImpl::bindPackage_(
165     OUString const & url, OUString const & mediaType, sal_Bool bRemoved,
166     OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
167 {
168     if (mediaType.getLength() == 0)
169     {
170         throw lang::IllegalArgumentException(
171             StrCannotDetectMediaType::get() + url,
172             static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
173     }
174 
175     String type, subType;
176     INetContentTypeParameterList params;
177     if (INetContentTypes::parse( mediaType, type, subType, &params ))
178     {
179         if (type.EqualsIgnoreCaseAscii("application"))
180         {
181             OUString name;
182             if (!bRemoved)
183             {
184                 ::ucbhelper::Content ucbContent( url, xCmdEnv );
185                 name = ucbContent.getPropertyValue(
186                     dp_misc::StrTitle::get() ).get<OUString>();
187             }
188             if (subType.EqualsIgnoreCaseAscii("vnd.sun.star.executable"))
189             {
190                 return new BackendImpl::ExecutablePackageImpl(
191                     this, url, name,  m_xExecutableTypeInfo, bRemoved,
192                     identifier);
193             }
194         }
195     }
196     return Reference<deployment::XPackage>();
197 }
198 
199 //##############################################################################
200 
201 
202 // Package
getMyBackend() const203 BackendImpl * BackendImpl::ExecutablePackageImpl::getMyBackend() const
204 {
205     BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
206     if (NULL == pBackend)
207     {
208         //May throw a DisposedException
209         check();
210         //We should never get here...
211         throw RuntimeException(
212             OUSTR("Failed to get the BackendImpl"),
213             static_cast<OWeakObject*>(const_cast<ExecutablePackageImpl *>(this)));
214     }
215     return pBackend;
216 }
217 
218 beans::Optional< beans::Ambiguous<sal_Bool> >
isRegistered_(::osl::ResettableMutexGuard &,::rtl::Reference<dp_misc::AbortChannel> const &,Reference<XCommandEnvironment> const &)219 BackendImpl::ExecutablePackageImpl::isRegistered_(
220     ::osl::ResettableMutexGuard &,
221     ::rtl::Reference<dp_misc::AbortChannel> const &,
222     Reference<XCommandEnvironment> const & )
223 {
224     bool registered = getMyBackend()->hasActiveEntry(getURL());
225     return beans::Optional< beans::Ambiguous<sal_Bool> >(
226             sal_True /* IsPresent */,
227                 beans::Ambiguous<sal_Bool>(
228                     registered, sal_False /* IsAmbiguous */ ) );
229 }
230 
processPackage_(::osl::ResettableMutexGuard &,bool doRegisterPackage,bool,::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,Reference<XCommandEnvironment> const &)231 void BackendImpl::ExecutablePackageImpl::processPackage_(
232     ::osl::ResettableMutexGuard &,
233     bool doRegisterPackage,
234     bool /*startup*/,
235     ::rtl::Reference<dp_misc::AbortChannel> const & abortChannel,
236     Reference<XCommandEnvironment> const & /*xCmdEnv*/ )
237 {
238     checkAborted(abortChannel);
239     if (doRegisterPackage)
240     {
241         if (!isUrlTargetInExtension())
242         {
243             OSL_ASSERT(0);
244             return;
245         }
246         sal_uInt64 attributes = 0;
247         //Setting the executable attribut does not affect executables on Windows
248         if (getFileAttributes(attributes))
249         {
250             if(getMyBackend()->m_context.equals(OUSTR("user")))
251                 attributes |= osl_File_Attribute_OwnExe;
252             else if (getMyBackend()->m_context.equals(OUSTR("shared")))
253                 attributes |= (osl_File_Attribute_OwnExe | osl_File_Attribute_GrpExe
254                                | osl_File_Attribute_OthExe);
255             else if (!getMyBackend()->m_context.equals(OUSTR("bundled"))
256                 && !getMyBackend()->m_context.equals(OUSTR("bundled_prereg")))
257                 //Bundled extension are required to be in the properly
258                 //installed. That is an executable must have the right flags
259                 OSL_ASSERT(0);
260 
261             //This won't have affect on Windows
262             osl::File::setAttributes(
263                     dp_misc::expandUnoRcUrl(m_url), attributes);
264         }
265         getMyBackend()->addDataToDb(getURL());
266     }
267     else
268     {
269         getMyBackend()->revokeEntryFromDb(getURL());
270     }
271 }
272 
273 //We currently cannot check if this XPackage represents a content of a particular extension
274 //But we can check if we are within $UNO_USER_PACKAGES_CACHE etc.
275 //Done for security reasons. For example an extension manifest could contain a path to
276 //an executable outside the extension.
isUrlTargetInExtension()277 bool BackendImpl::ExecutablePackageImpl::isUrlTargetInExtension()
278 {
279     bool bSuccess = false;
280     OUString sExtensionDir;
281     if(getMyBackend()->m_context.equals(OUSTR("user")))
282         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$UNO_USER_PACKAGES_CACHE"));
283     else if (getMyBackend()->m_context.equals(OUSTR("shared")))
284         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$UNO_SHARED_PACKAGES_CACHE"));
285     else if (getMyBackend()->m_context.equals(OUSTR("bundled"))
286         || getMyBackend()->m_context.equals(OUSTR("bundled_prereg")))
287         sExtensionDir = dp_misc::expandUnoRcTerm(OUSTR("$BUNDLED_EXTENSIONS"));
288     else
289         OSL_ASSERT(0);
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 (sal_True == sFile.match(sExtensionDir, 0))
298                 bSuccess = true;
299         }
300     }
301     return bSuccess;
302 }
303 
getFileAttributes(sal_uInt64 & out_Attributes)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 
323 
324 } // anon namespace
325 
326 namespace sdecl = comphelper::service_decl;
327 sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
328 extern sdecl::ServiceDecl const serviceDecl(
329     serviceBI,
330     "com.sun.star.comp.deployment.executable.PackageRegistryBackend",
331     BACKEND_SERVICE_NAME );
332 
333 } // namespace component
334 } // namespace backend
335 } // namespace dp_registry
336