1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #if ! defined INCLUDED_DP_REGISTRY_H
29 #define INCLUDED_DP_REGISTRY_H
30 
31 #include "dp_misc.h"
32 #include "dp_resource.h"
33 #include "dp_interact.h"
34 #include "rtl/ref.hxx"
35 #include "cppuhelper/weakref.hxx"
36 #include "cppuhelper/implbase1.hxx"
37 #include "cppuhelper/compbase1.hxx"
38 #include "cppuhelper/compbase2.hxx"
39 #include "tools/inetmime.hxx"
40 #include "com/sun/star/lang/XEventListener.hpp"
41 #include "com/sun/star/deployment/XPackageRegistry.hpp"
42 #include "com/sun/star/deployment/XPackageManager.hpp"
43 #include "com/sun/star/deployment/InvalidRemovedParameterException.hpp"
44 #include <memory>
45 #include <hash_map>
46 #include <list>
47 #include "dp_registry.hrc"
48 
49 namespace dp_registry
50 {
51 namespace backend
52 {
53 
54 namespace css = ::com::sun::star;
55 
56 class PackageRegistryBackend;
57 
58 char const* const BACKEND_SERVICE_NAME =
59 "com.sun.star.deployment.PackageRegistryBackend";
60 
61 typedef ::cppu::WeakComponentImplHelper1<
62     css::deployment::XPackage > t_PackageBase;
63 
64 //==============================================================================
65 class Package : protected ::dp_misc::MutexHolder, public t_PackageBase
66 {
67     PackageRegistryBackend * getMyBackend() const;
68     void processPackage_impl(
69         bool registerPackage,
70         bool startup,
71         css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
72         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv );
73 
74 protected:
75     ::rtl::Reference<PackageRegistryBackend> m_myBackend;
76     const ::rtl::OUString m_url;
77     ::rtl::OUString m_name;
78     ::rtl::OUString m_displayName;
79     const css::uno::Reference<css::deployment::XPackageTypeInfo> m_xPackageType;
80     const bool m_bRemoved;
81     //Only set if m_bRemoved = true;
82     const ::rtl::OUString m_identifier;
83 
84     void check() const;
85     void fireModified();
86     virtual void SAL_CALL disposing();
87 
88     void checkAborted(
89         ::rtl::Reference< ::dp_misc::AbortChannel > const & abortChannel );
90 
91     // @@@ to be implemented by specific backend:
92     virtual css::beans::Optional< css::beans::Ambiguous<sal_Bool> >
93     isRegistered_(
94         ::osl::ResettableMutexGuard & guard,
95         ::rtl::Reference< ::dp_misc::AbortChannel > const & abortChannel,
96         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
97         = 0;
98     virtual void processPackage_(
99         ::osl::ResettableMutexGuard & guard,
100         bool registerPackage,
101         bool startup,
102         ::rtl::Reference< ::dp_misc::AbortChannel > const & abortChannel,
103         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
104         = 0;
105 
106     virtual ~Package();
107     Package( ::rtl::Reference<PackageRegistryBackend> const & myBackend,
108              ::rtl::OUString const & url,
109              ::rtl::OUString const & name,
110              ::rtl::OUString const & displayName,
111              css::uno::Reference<css::deployment::XPackageTypeInfo> const &
112              xPackageType,
113              bool bRemoved,
114              ::rtl::OUString const & identifier);
115 
116 public:
117 
118     class TypeInfo :
119         public ::cppu::WeakImplHelper1<css::deployment::XPackageTypeInfo>
120     {
121         const ::rtl::OUString m_mediaType;
122         const ::rtl::OUString m_fileFilter;
123         const ::rtl::OUString m_shortDescr;
124         const sal_uInt16 m_smallIcon, m_smallIcon_HC;
125     public:
126         virtual ~TypeInfo();
127         TypeInfo( ::rtl::OUString const & mediaType,
128                   ::rtl::OUString const & fileFilter,
129                   ::rtl::OUString const & shortDescr,
130                   sal_uInt16 smallIcon, sal_uInt16 smallIcon_HC )
131             : m_mediaType(mediaType), m_fileFilter(fileFilter),
132               m_shortDescr(shortDescr),
133               m_smallIcon(smallIcon), m_smallIcon_HC(smallIcon_HC)
134             {}
135         // XPackageTypeInfo
136         virtual ::rtl::OUString SAL_CALL getMediaType()
137             throw (css::uno::RuntimeException);
138         virtual ::rtl::OUString SAL_CALL getDescription()
139             throw (css::deployment::ExtensionRemovedException,
140                    css::uno::RuntimeException);
141         virtual ::rtl::OUString SAL_CALL getShortDescription()
142             throw (css::deployment::ExtensionRemovedException,
143                    css::uno::RuntimeException);
144         virtual ::rtl::OUString SAL_CALL getFileFilter()
145             throw (css::uno::RuntimeException);
146         virtual css::uno::Any SAL_CALL getIcon( sal_Bool highContrast,
147                                                 sal_Bool smallIcon )
148             throw (css::uno::RuntimeException);
149     };
150 
151     // XComponent
152     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException);
153     virtual void SAL_CALL addEventListener(
154         css::uno::Reference<css::lang::XEventListener> const & xListener )
155         throw (css::uno::RuntimeException);
156     virtual void SAL_CALL removeEventListener(
157         css::uno::Reference<css::lang::XEventListener> const & xListener )
158         throw (css::uno::RuntimeException);
159 
160     // XModifyBroadcaster
161     virtual void SAL_CALL addModifyListener(
162         css::uno::Reference<css::util::XModifyListener> const & xListener )
163         throw (css::uno::RuntimeException);
164     virtual void SAL_CALL removeModifyListener(
165         css::uno::Reference<css::util::XModifyListener> const & xListener )
166         throw (css::uno::RuntimeException);
167 
168     // XPackage
169     virtual css::uno::Reference<css::task::XAbortChannel> SAL_CALL
170     createAbortChannel() throw (css::uno::RuntimeException);
171     virtual css::beans::Optional< css::beans::Ambiguous<sal_Bool> >
172     SAL_CALL isRegistered(
173         css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
174         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
175         throw (css::deployment::DeploymentException,
176                css::ucb::CommandFailedException,
177                css::ucb::CommandAbortedException,
178                css::uno::RuntimeException);
179 
180     virtual ::sal_Int32 SAL_CALL checkPrerequisites(
181 		const css::uno::Reference< css::task::XAbortChannel >& xAbortChannel,
182 		const css::uno::Reference< css::ucb::XCommandEnvironment >& xCmdEnv,
183         sal_Bool noLicenseChecking)
184 		throw (css::deployment::DeploymentException,
185                css::deployment::ExtensionRemovedException,
186                css::ucb::CommandFailedException,
187                css::ucb::CommandAbortedException,
188                css::uno::RuntimeException);
189 
190     virtual ::sal_Bool SAL_CALL checkDependencies(
191 		const css::uno::Reference< css::ucb::XCommandEnvironment >& xCmdEnv )
192 		throw (css::deployment::DeploymentException,
193                css::deployment::ExtensionRemovedException,
194                css::ucb::CommandFailedException,
195                css::uno::RuntimeException);
196 
197     virtual void SAL_CALL registerPackage(
198         sal_Bool startup,
199         css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
200         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
201         throw (css::deployment::DeploymentException,
202                css::deployment::ExtensionRemovedException,
203                css::ucb::CommandFailedException,
204                css::ucb::CommandAbortedException,
205                css::lang::IllegalArgumentException, css::uno::RuntimeException);
206     virtual void SAL_CALL revokePackage(
207         css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
208         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
209         throw (css::deployment::DeploymentException,
210                css::ucb::CommandFailedException,
211                css::ucb::CommandAbortedException,
212                css::lang::IllegalArgumentException,
213                css::uno::RuntimeException);
214     virtual sal_Bool SAL_CALL isBundle()
215         throw (css::uno::RuntimeException);
216     virtual css::uno::Sequence< css::uno::Reference<css::deployment::XPackage> >
217     SAL_CALL getBundle(
218         css::uno::Reference<css::task::XAbortChannel> const & xAbortChannel,
219         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
220         throw (css::deployment::DeploymentException,
221                css::ucb::CommandFailedException,
222                css::ucb::CommandAbortedException,
223                css::lang::IllegalArgumentException,
224                css::uno::RuntimeException);
225     virtual ::rtl::OUString SAL_CALL getName()
226         throw (css::uno::RuntimeException);
227     virtual css::beans::Optional< ::rtl::OUString > SAL_CALL getIdentifier()
228         throw (css::uno::RuntimeException);
229     virtual ::rtl::OUString SAL_CALL getVersion()
230         throw (css::deployment::ExtensionRemovedException,
231                css::uno::RuntimeException);
232     virtual ::rtl::OUString SAL_CALL getURL()
233         throw (css::uno::RuntimeException);
234     virtual ::rtl::OUString SAL_CALL getDisplayName()
235         throw (css::deployment::ExtensionRemovedException,
236                css::uno::RuntimeException);
237     virtual ::rtl::OUString SAL_CALL getDescription()
238         throw (css::deployment::ExtensionRemovedException,
239                css::uno::RuntimeException);
240     virtual ::rtl::OUString SAL_CALL getLicenseText()
241         throw (css::deployment::ExtensionRemovedException,
242                css::uno::RuntimeException);
243     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL
244     getUpdateInformationURLs()
245         throw (css::deployment::ExtensionRemovedException,
246                css::uno::RuntimeException);
247     virtual css::beans::StringPair SAL_CALL getPublisherInfo()
248         throw (css::deployment::ExtensionRemovedException,
249                css::uno::RuntimeException);
250     virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL
251     getIcon( sal_Bool bHighContrast )
252         throw (css::deployment::ExtensionRemovedException,
253                css::uno::RuntimeException);
254     virtual css::uno::Reference<css::deployment::XPackageTypeInfo> SAL_CALL
255     getPackageType() throw (css::uno::RuntimeException);
256     virtual void SAL_CALL exportTo(
257         ::rtl::OUString const & destFolderURL,
258         ::rtl::OUString const & newTitle,
259         sal_Int32 nameClashAction,
260         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
261         throw (css::deployment::ExtensionRemovedException,
262                css::ucb::CommandFailedException,
263                css::ucb::CommandAbortedException, css::uno::RuntimeException);
264     virtual ::rtl::OUString SAL_CALL getRepositoryName()
265         throw (css::uno::RuntimeException);
266     virtual css::beans::Optional< ::rtl::OUString > SAL_CALL getRegistrationDataURL()
267         throw (css::deployment::ExtensionRemovedException,
268                css::uno::RuntimeException);
269     virtual sal_Bool SAL_CALL isRemoved()
270         throw (css::uno::RuntimeException);
271 
272 };
273 
274 typedef ::cppu::WeakComponentImplHelper2<
275     css::lang::XEventListener,
276     css::deployment::XPackageRegistry > t_BackendBase;
277 
278 //==============================================================================
279 class PackageRegistryBackend
280     : protected ::dp_misc::MutexHolder, public t_BackendBase
281 {
282     //The map held originally WeakReferences. The map entries are removed in the disposing
283     //function, which is called when the XPackages are destructed or they are
284     //explicitely disposed. The latter happens, for example, when a extension is
285     //removed (see dp_manager.cxx). However, because of how the help systems work, now
286     // XPackageManager::getDeployedPackages is called often. This results in a lot
287     //of bindPackage calls which are costly. Therefore we keep hard references in
288     //the map now.
289     typedef ::std::hash_map<
290         ::rtl::OUString, css::uno::Reference<css::deployment::XPackage>,
291         ::rtl::OUStringHash > t_string2ref;
292     t_string2ref m_bound;
293 
294 protected:
295     ::rtl::OUString m_cachePath;
296     css::uno::Reference<css::uno::XComponentContext> m_xComponentContext;
297 
298     ::rtl::OUString m_context;
299     // currently only for library containers:
300     enum {
301         CONTEXT_UNKNOWN,
302         CONTEXT_USER, CONTEXT_SHARED,CONTEXT_BUNDLED, CONTEXT_TMP, CONTEXT_BUNDLED_PREREG,
303         CONTEXT_DOCUMENT
304     } m_eContext;
305     bool m_readOnly;
306 
307     struct StrCannotDetectMediaType : public ::dp_misc::StaticResourceString<
308         StrCannotDetectMediaType, RID_STR_CANNOT_DETECT_MEDIA_TYPE> {};
309     struct StrUnsupportedMediaType : public ::dp_misc::StaticResourceString<
310         StrUnsupportedMediaType, RID_STR_UNSUPPORTED_MEDIA_TYPE> {};
311 
312     // @@@ to be implemented by specific backend:
313     virtual css::uno::Reference<css::deployment::XPackage> bindPackage_(
314         ::rtl::OUString const & url, ::rtl::OUString const & mediaType,
315         sal_Bool bRemoved, ::rtl::OUString const & identifier,
316         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
317         = 0;
318 
319     void check();
320     virtual void SAL_CALL disposing();
321 
322     virtual ~PackageRegistryBackend();
323     PackageRegistryBackend(
324         css::uno::Sequence<css::uno::Any> const & args,
325         css::uno::Reference<css::uno::XComponentContext> const & xContext );
326 
327     /* creates a folder with a unique name.
328        If url is empty then it is created in the the backend folder, otherwise
329        at a location relative to that folder specified by url.
330     */
331     ::rtl::OUString createFolder(
332         ::rtl::OUString const & relUrl,
333         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv);
334     /* deletes folders and files.
335 
336        All folder all files which end with ".tmp" or ".tmp_" and which are
337        not used are deleted.
338      */
339     void deleteUnusedFolders(
340         ::rtl::OUString const & relUrl,
341         ::std::list< ::rtl::OUString> const & usedFolders);
342     /* deletes one folder with a "temporary" name and the corresponding
343        tmp file, which was used to derive the folder name.
344     */
345     static void deleteTempFolder(
346         ::rtl::OUString const & folderUrl);
347 
348     ::rtl::OUString getSharedRegistrationDataURL(
349         css::uno::Reference<css::deployment::XPackage> const & extension,
350         css::uno::Reference<css::deployment::XPackage> const & item);
351 
352     /* The backends must implement this function, which is called
353        from XPackageRegistry::packageRemoved (also implemented here).
354        This ensure that the backends clean up their registration data
355        when an extension was removed.
356     */
357 //    virtual void deleteDbEntry( ::rtl::OUString const & url) = 0;
358 
359 
360 
361 public:
362     struct StrRegisteringPackage : public ::dp_misc::StaticResourceString<
363         StrRegisteringPackage, RID_STR_REGISTERING_PACKAGE> {};
364     struct StrRevokingPackage : public ::dp_misc::StaticResourceString<
365         StrRevokingPackage, RID_STR_REVOKING_PACKAGE> {};
366 
367     inline css::uno::Reference<css::uno::XComponentContext> const &
368     getComponentContext() const { return m_xComponentContext; }
369 
370     inline ::rtl::OUString const & getCachePath() const { return m_cachePath; }
371     inline bool transientMode() const { return m_cachePath.getLength() == 0; }
372 
373     inline ::rtl::OUString getContext() const {return m_context; }
374 
375     // XEventListener
376     virtual void SAL_CALL disposing( css::lang::EventObject const & evt )
377         throw (css::uno::RuntimeException);
378 
379     // XPackageRegistry
380     virtual css::uno::Reference<css::deployment::XPackage> SAL_CALL bindPackage(
381         ::rtl::OUString const & url, ::rtl::OUString const & mediaType,
382         sal_Bool bRemoved, ::rtl::OUString const & identifier,
383         css::uno::Reference<css::ucb::XCommandEnvironment> const & xCmdEnv )
384         throw (css::deployment::DeploymentException,
385                css::deployment::InvalidRemovedParameterException,
386                css::ucb::CommandFailedException,
387                css::lang::IllegalArgumentException, css::uno::RuntimeException);
388 
389 //     virtual void SAL_CALL packageRemoved(
390 //         ::rtl::OUString const & url, ::rtl::OUString const & mediaType)
391 //         throw (css::deployment::DeploymentException,
392 //                css::uno::RuntimeException);
393 
394 };
395 
396 }
397 }
398 
399 #endif
400 
401