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