1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dp_registry.hrc"
32*cdf0e10cSrcweir #include "dp_misc.h"
33*cdf0e10cSrcweir #include "dp_resource.h"
34*cdf0e10cSrcweir #include "dp_interact.h"
35*cdf0e10cSrcweir #include "dp_ucb.h"
36*cdf0e10cSrcweir #include "osl/diagnose.h"
37*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
38*cdf0e10cSrcweir #include "rtl/uri.hxx"
39*cdf0e10cSrcweir #include "cppuhelper/compbase2.hxx"
40*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
41*cdf0e10cSrcweir #include "comphelper/sequence.hxx"
42*cdf0e10cSrcweir #include "ucbhelper/content.hxx"
43*cdf0e10cSrcweir #include "com/sun/star/uno/DeploymentException.hpp"
44*cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
45*cdf0e10cSrcweir #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
46*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
47*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp"
48*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleServiceFactory.hpp"
49*cdf0e10cSrcweir #include "com/sun/star/util/XUpdatable.hpp"
50*cdf0e10cSrcweir #include "com/sun/star/container/XContentEnumerationAccess.hpp"
51*cdf0e10cSrcweir #include "com/sun/star/deployment/PackageRegistryBackend.hpp"
52*cdf0e10cSrcweir #include <hash_map>
53*cdf0e10cSrcweir #include <set>
54*cdf0e10cSrcweir #include <hash_set>
55*cdf0e10cSrcweir #include <memory>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::dp_misc;
58*cdf0e10cSrcweir using namespace ::com::sun::star;
59*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
60*cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
61*cdf0e10cSrcweir using ::rtl::OUString;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir namespace dp_registry {
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir namespace backend {
67*cdf0e10cSrcweir namespace bundle {
68*cdf0e10cSrcweir Reference<deployment::XPackageRegistry> create(
69*cdf0e10cSrcweir     Reference<deployment::XPackageRegistry> const & xRootRegistry,
70*cdf0e10cSrcweir     OUString const & context, OUString const & cachePath, bool readOnly,
71*cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext );
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir namespace {
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper2<
78*cdf0e10cSrcweir     deployment::XPackageRegistry, util::XUpdatable > t_helper;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //==============================================================================
81*cdf0e10cSrcweir class PackageRegistryImpl : private MutexHolder, public t_helper
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     struct ci_string_hash {
84*cdf0e10cSrcweir         ::std::size_t operator () ( OUString const & str ) const {
85*cdf0e10cSrcweir             return str.toAsciiLowerCase().hashCode();
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir     };
88*cdf0e10cSrcweir     struct ci_string_equals {
89*cdf0e10cSrcweir         bool operator () ( OUString const & str1, OUString const & str2 ) const{
90*cdf0e10cSrcweir             return str1.equalsIgnoreAsciiCase( str2 );
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir     };
93*cdf0e10cSrcweir     typedef ::std::hash_map<
94*cdf0e10cSrcweir         OUString, Reference<deployment::XPackageRegistry>,
95*cdf0e10cSrcweir         ci_string_hash, ci_string_equals > t_string2registry;
96*cdf0e10cSrcweir     typedef ::std::hash_map<
97*cdf0e10cSrcweir         OUString, OUString,
98*cdf0e10cSrcweir         ci_string_hash, ci_string_equals > t_string2string;
99*cdf0e10cSrcweir     typedef ::std::set<
100*cdf0e10cSrcweir         Reference<deployment::XPackageRegistry> > t_registryset;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     t_string2registry m_mediaType2backend;
103*cdf0e10cSrcweir     t_string2string m_filter2mediaType;
104*cdf0e10cSrcweir     t_registryset m_ambiguousBackends;
105*cdf0e10cSrcweir     t_registryset m_allBackends;
106*cdf0e10cSrcweir     ::std::vector< Reference<deployment::XPackageTypeInfo> > m_typesInfos;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     void insertBackend(
109*cdf0e10cSrcweir         Reference<deployment::XPackageRegistry> const & xBackend );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir protected:
112*cdf0e10cSrcweir     inline void check();
113*cdf0e10cSrcweir     virtual void SAL_CALL disposing();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     virtual ~PackageRegistryImpl();
116*cdf0e10cSrcweir     PackageRegistryImpl() : t_helper( getMutex() ) {}
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir public:
120*cdf0e10cSrcweir     static Reference<deployment::XPackageRegistry> create(
121*cdf0e10cSrcweir         OUString const & context,
122*cdf0e10cSrcweir         OUString const & cachePath, bool readOnly,
123*cdf0e10cSrcweir         Reference<XComponentContext> const & xComponentContext );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     // XUpdatable
126*cdf0e10cSrcweir     virtual void SAL_CALL update() throw (RuntimeException);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     // XPackageRegistry
129*cdf0e10cSrcweir     virtual Reference<deployment::XPackage> SAL_CALL bindPackage(
130*cdf0e10cSrcweir         OUString const & url, OUString const & mediaType, sal_Bool bRemoved,
131*cdf0e10cSrcweir         OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
132*cdf0e10cSrcweir         throw (deployment::DeploymentException,
133*cdf0e10cSrcweir                deployment::InvalidRemovedParameterException,
134*cdf0e10cSrcweir                CommandFailedException,
135*cdf0e10cSrcweir                lang::IllegalArgumentException, RuntimeException);
136*cdf0e10cSrcweir     virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
137*cdf0e10cSrcweir     getSupportedPackageTypes() throw (RuntimeException);
138*cdf0e10cSrcweir     virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType)
139*cdf0e10cSrcweir                 throw (deployment::DeploymentException,
140*cdf0e10cSrcweir                 RuntimeException);
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir };
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //______________________________________________________________________________
145*cdf0e10cSrcweir inline void PackageRegistryImpl::check()
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir     ::osl::MutexGuard guard( getMutex() );
148*cdf0e10cSrcweir     if (rBHelper.bInDispose || rBHelper.bDisposed) {
149*cdf0e10cSrcweir         throw lang::DisposedException(
150*cdf0e10cSrcweir             OUSTR("PackageRegistry instance has already been disposed!"),
151*cdf0e10cSrcweir             static_cast<OWeakObject *>(this) );
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir //______________________________________________________________________________
156*cdf0e10cSrcweir void PackageRegistryImpl::disposing()
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     // dispose all backends:
159*cdf0e10cSrcweir     t_registryset::const_iterator iPos( m_allBackends.begin() );
160*cdf0e10cSrcweir     t_registryset::const_iterator const iEnd( m_allBackends.end() );
161*cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos ) {
162*cdf0e10cSrcweir         try_dispose( *iPos );
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir     m_mediaType2backend = t_string2registry();
165*cdf0e10cSrcweir     m_ambiguousBackends = t_registryset();
166*cdf0e10cSrcweir     m_allBackends = t_registryset();
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     t_helper::disposing();
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir //______________________________________________________________________________
172*cdf0e10cSrcweir PackageRegistryImpl::~PackageRegistryImpl()
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir //______________________________________________________________________________
177*cdf0e10cSrcweir OUString normalizeMediaType( OUString const & mediaType )
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
180*cdf0e10cSrcweir     sal_Int32 index = 0;
181*cdf0e10cSrcweir     for (;;) {
182*cdf0e10cSrcweir         buf.append( mediaType.getToken( 0, '/', index ).trim() );
183*cdf0e10cSrcweir         if (index < 0)
184*cdf0e10cSrcweir             break;
185*cdf0e10cSrcweir         buf.append( static_cast< sal_Unicode >('/') );
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir     return buf.makeStringAndClear();
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //______________________________________________________________________________
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir void PackageRegistryImpl::packageRemoved(
193*cdf0e10cSrcweir     ::rtl::OUString const & url, ::rtl::OUString const & mediaType)
194*cdf0e10cSrcweir     throw (css::deployment::DeploymentException,
195*cdf0e10cSrcweir            css::uno::RuntimeException)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     const t_string2registry::const_iterator i =
198*cdf0e10cSrcweir         m_mediaType2backend.find(mediaType);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     if (i != m_mediaType2backend.end())
201*cdf0e10cSrcweir     {
202*cdf0e10cSrcweir         i->second->packageRemoved(url, mediaType);
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir void PackageRegistryImpl::insertBackend(
207*cdf0e10cSrcweir     Reference<deployment::XPackageRegistry> const & xBackend )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     m_allBackends.insert( xBackend );
210*cdf0e10cSrcweir     typedef ::std::hash_set<OUString, ::rtl::OUStringHash> t_stringset;
211*cdf0e10cSrcweir     t_stringset ambiguousFilters;
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     const Sequence< Reference<deployment::XPackageTypeInfo> > packageTypes(
214*cdf0e10cSrcweir         xBackend->getSupportedPackageTypes() );
215*cdf0e10cSrcweir     for ( sal_Int32 pos = 0; pos < packageTypes.getLength(); ++pos )
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         Reference<deployment::XPackageTypeInfo> const & xPackageType =
218*cdf0e10cSrcweir             packageTypes[ pos ];
219*cdf0e10cSrcweir         m_typesInfos.push_back( xPackageType );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         const OUString mediaType( normalizeMediaType(
222*cdf0e10cSrcweir                                       xPackageType->getMediaType() ) );
223*cdf0e10cSrcweir         ::std::pair<t_string2registry::iterator, bool> mb_insertion(
224*cdf0e10cSrcweir             m_mediaType2backend.insert( t_string2registry::value_type(
225*cdf0e10cSrcweir                                             mediaType, xBackend ) ) );
226*cdf0e10cSrcweir         if (mb_insertion.second)
227*cdf0e10cSrcweir         {
228*cdf0e10cSrcweir             // add parameterless media-type, too:
229*cdf0e10cSrcweir             sal_Int32 semi = mediaType.indexOf( ';' );
230*cdf0e10cSrcweir             if (semi >= 0) {
231*cdf0e10cSrcweir                 m_mediaType2backend.insert(
232*cdf0e10cSrcweir                     t_string2registry::value_type(
233*cdf0e10cSrcweir                         mediaType.copy( 0, semi ), xBackend ) );
234*cdf0e10cSrcweir             }
235*cdf0e10cSrcweir             const OUString fileFilter( xPackageType->getFileFilter() );
236*cdf0e10cSrcweir             //The package backend shall also be called to determine the mediatype
237*cdf0e10cSrcweir             //(XPackageRegistry.bindPackage) when the URL points to a directory.
238*cdf0e10cSrcweir             const bool bExtension = mediaType.equals(OUSTR("application/vnd.sun.star.package-bundle"));
239*cdf0e10cSrcweir             if (fileFilter.getLength() == 0 ||
240*cdf0e10cSrcweir                 fileFilter.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("*.*") ) ||
241*cdf0e10cSrcweir                 fileFilter.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("*") ) ||
242*cdf0e10cSrcweir                 bExtension)
243*cdf0e10cSrcweir             {
244*cdf0e10cSrcweir                 m_ambiguousBackends.insert( xBackend );
245*cdf0e10cSrcweir             }
246*cdf0e10cSrcweir             else
247*cdf0e10cSrcweir             {
248*cdf0e10cSrcweir                 sal_Int32 nIndex = 0;
249*cdf0e10cSrcweir                 do {
250*cdf0e10cSrcweir                     OUString token( fileFilter.getToken( 0, ';', nIndex ) );
251*cdf0e10cSrcweir                     if (token.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("*.") ))
252*cdf0e10cSrcweir                         token = token.copy( 1 );
253*cdf0e10cSrcweir                     if (token.getLength() == 0)
254*cdf0e10cSrcweir                         continue;
255*cdf0e10cSrcweir                     // mark any further wildcards ambig:
256*cdf0e10cSrcweir                     bool ambig = (token.indexOf('*') >= 0 ||
257*cdf0e10cSrcweir                                   token.indexOf('?') >= 0);
258*cdf0e10cSrcweir                     if (! ambig) {
259*cdf0e10cSrcweir                         ::std::pair<t_string2string::iterator, bool> ins(
260*cdf0e10cSrcweir                             m_filter2mediaType.insert(
261*cdf0e10cSrcweir                                 t_string2string::value_type(
262*cdf0e10cSrcweir                                     token, mediaType ) ) );
263*cdf0e10cSrcweir                         ambig = !ins.second;
264*cdf0e10cSrcweir                         if (ambig) {
265*cdf0e10cSrcweir                             // filter has already been in: add previously
266*cdf0e10cSrcweir                             // added backend to ambig set
267*cdf0e10cSrcweir                             const t_string2registry::const_iterator iFind(
268*cdf0e10cSrcweir                                 m_mediaType2backend.find(
269*cdf0e10cSrcweir                                     /* media-type of pr. added backend */
270*cdf0e10cSrcweir                                     ins.first->second ) );
271*cdf0e10cSrcweir                             OSL_ASSERT(
272*cdf0e10cSrcweir                                 iFind != m_mediaType2backend.end() );
273*cdf0e10cSrcweir                             if (iFind != m_mediaType2backend.end())
274*cdf0e10cSrcweir                                 m_ambiguousBackends.insert( iFind->second );
275*cdf0e10cSrcweir                         }
276*cdf0e10cSrcweir                     }
277*cdf0e10cSrcweir                     if (ambig) {
278*cdf0e10cSrcweir                         m_ambiguousBackends.insert( xBackend );
279*cdf0e10cSrcweir                         // mark filter to be removed later from filters map:
280*cdf0e10cSrcweir                         ambiguousFilters.insert( token );
281*cdf0e10cSrcweir                     }
282*cdf0e10cSrcweir                 }
283*cdf0e10cSrcweir                 while (nIndex >= 0);
284*cdf0e10cSrcweir             }
285*cdf0e10cSrcweir         }
286*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
287*cdf0e10cSrcweir         else {
288*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf;
289*cdf0e10cSrcweir             buf.appendAscii(
290*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
291*cdf0e10cSrcweir                     "more than one PackageRegistryBackend for "
292*cdf0e10cSrcweir                     "media-type=\"") );
293*cdf0e10cSrcweir             buf.append( mediaType );
294*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" => ") );
295*cdf0e10cSrcweir             buf.append( Reference<lang::XServiceInfo>(
296*cdf0e10cSrcweir                             xBackend, UNO_QUERY_THROW )->
297*cdf0e10cSrcweir                         getImplementationName() );
298*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\"!") );
299*cdf0e10cSrcweir             OSL_ENSURE( 0, ::rtl::OUStringToOString(
300*cdf0e10cSrcweir                             buf.makeStringAndClear(),
301*cdf0e10cSrcweir                             RTL_TEXTENCODING_UTF8 ) );
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir #endif
304*cdf0e10cSrcweir     }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     // cut out ambiguous filters:
307*cdf0e10cSrcweir     t_stringset::const_iterator iPos( ambiguousFilters.begin() );
308*cdf0e10cSrcweir     const t_stringset::const_iterator iEnd( ambiguousFilters.end() );
309*cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos ) {
310*cdf0e10cSrcweir         m_filter2mediaType.erase( *iPos );
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir //______________________________________________________________________________
315*cdf0e10cSrcweir Reference<deployment::XPackageRegistry> PackageRegistryImpl::create(
316*cdf0e10cSrcweir     OUString const & context,
317*cdf0e10cSrcweir     OUString const & cachePath, bool readOnly,
318*cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext )
319*cdf0e10cSrcweir {
320*cdf0e10cSrcweir     PackageRegistryImpl * that = new PackageRegistryImpl;
321*cdf0e10cSrcweir     Reference<deployment::XPackageRegistry> xRet(that);
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir     // auto-detect all registered package registries:
324*cdf0e10cSrcweir     Reference<container::XEnumeration> xEnum(
325*cdf0e10cSrcweir         Reference<container::XContentEnumerationAccess>(
326*cdf0e10cSrcweir             xComponentContext->getServiceManager(),
327*cdf0e10cSrcweir             UNO_QUERY_THROW )->createContentEnumeration(
328*cdf0e10cSrcweir                 OUSTR("com.sun.star.deployment.PackageRegistryBackend") ) );
329*cdf0e10cSrcweir     if (xEnum.is())
330*cdf0e10cSrcweir     {
331*cdf0e10cSrcweir         while (xEnum->hasMoreElements())
332*cdf0e10cSrcweir         {
333*cdf0e10cSrcweir             Any element( xEnum->nextElement() );
334*cdf0e10cSrcweir             Sequence<Any> registryArgs(
335*cdf0e10cSrcweir                 cachePath.getLength() == 0 ? 1 : 3 );
336*cdf0e10cSrcweir             registryArgs[ 0 ] <<= context;
337*cdf0e10cSrcweir             if (cachePath.getLength() > 0)
338*cdf0e10cSrcweir             {
339*cdf0e10cSrcweir                 Reference<lang::XServiceInfo> xServiceInfo(
340*cdf0e10cSrcweir                     element, UNO_QUERY_THROW );
341*cdf0e10cSrcweir                 OUString registryCachePath(
342*cdf0e10cSrcweir                     makeURL( cachePath,
343*cdf0e10cSrcweir                              ::rtl::Uri::encode(
344*cdf0e10cSrcweir                                  xServiceInfo->getImplementationName(),
345*cdf0e10cSrcweir                                  rtl_UriCharClassPchar,
346*cdf0e10cSrcweir                                  rtl_UriEncodeIgnoreEscapes,
347*cdf0e10cSrcweir                                  RTL_TEXTENCODING_UTF8 ) ) );
348*cdf0e10cSrcweir                 registryArgs[ 1 ] <<= registryCachePath;
349*cdf0e10cSrcweir                 registryArgs[ 2 ] <<= readOnly;
350*cdf0e10cSrcweir                 if (! readOnly)
351*cdf0e10cSrcweir                     create_folder( 0, registryCachePath,
352*cdf0e10cSrcweir                                    Reference<XCommandEnvironment>() );
353*cdf0e10cSrcweir             }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir             Reference<deployment::XPackageRegistry> xBackend;
356*cdf0e10cSrcweir             Reference<lang::XSingleComponentFactory> xFac( element, UNO_QUERY );
357*cdf0e10cSrcweir             if (xFac.is()) {
358*cdf0e10cSrcweir                 xBackend.set(
359*cdf0e10cSrcweir                     xFac->createInstanceWithArgumentsAndContext(
360*cdf0e10cSrcweir                         registryArgs, xComponentContext ), UNO_QUERY );
361*cdf0e10cSrcweir             }
362*cdf0e10cSrcweir             else {
363*cdf0e10cSrcweir                 Reference<lang::XSingleServiceFactory> xSingleServiceFac(
364*cdf0e10cSrcweir                     element, UNO_QUERY_THROW );
365*cdf0e10cSrcweir                 xBackend.set(
366*cdf0e10cSrcweir                     xSingleServiceFac->createInstanceWithArguments(
367*cdf0e10cSrcweir                         registryArgs ), UNO_QUERY );
368*cdf0e10cSrcweir             }
369*cdf0e10cSrcweir             if (! xBackend.is()) {
370*cdf0e10cSrcweir                 throw DeploymentException(
371*cdf0e10cSrcweir                     OUSTR("cannot instantiate PackageRegistryBackend service: ")
372*cdf0e10cSrcweir                     + Reference<lang::XServiceInfo>(
373*cdf0e10cSrcweir                         element, UNO_QUERY_THROW )->getImplementationName(),
374*cdf0e10cSrcweir                     static_cast<OWeakObject *>(that) );
375*cdf0e10cSrcweir             }
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir             that->insertBackend( xBackend );
378*cdf0e10cSrcweir         }
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     // Insert bundle back-end.
382*cdf0e10cSrcweir     // Always register as last, because we want to add extensions also as folders
383*cdf0e10cSrcweir     // and as a default we accept every folder, which was not recognized by the other
384*cdf0e10cSrcweir     // backends.
385*cdf0e10cSrcweir     Reference<deployment::XPackageRegistry> extensionBackend =
386*cdf0e10cSrcweir         ::dp_registry::backend::bundle::create(
387*cdf0e10cSrcweir             that, context, cachePath, readOnly, xComponentContext);
388*cdf0e10cSrcweir     that->insertBackend(extensionBackend);
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir     Reference<lang::XServiceInfo> xServiceInfo(
391*cdf0e10cSrcweir         extensionBackend, UNO_QUERY_THROW );
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir     OSL_ASSERT(xServiceInfo.is());
394*cdf0e10cSrcweir     OUString registryCachePath(
395*cdf0e10cSrcweir         makeURL( cachePath,
396*cdf0e10cSrcweir                  ::rtl::Uri::encode(
397*cdf0e10cSrcweir                      xServiceInfo->getImplementationName(),
398*cdf0e10cSrcweir                      rtl_UriCharClassPchar,
399*cdf0e10cSrcweir                      rtl_UriEncodeIgnoreEscapes,
400*cdf0e10cSrcweir                      RTL_TEXTENCODING_UTF8 ) ) );
401*cdf0e10cSrcweir     create_folder( 0, registryCachePath, Reference<XCommandEnvironment>());
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
405*cdf0e10cSrcweir     // dump tables:
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         t_registryset allBackends;
408*cdf0e10cSrcweir         dp_misc::TRACE("> [dp_registry.cxx] media-type detection:\n\n" );
409*cdf0e10cSrcweir         for ( t_string2string::const_iterator iPos(
410*cdf0e10cSrcweir                   that->m_filter2mediaType.begin() );
411*cdf0e10cSrcweir               iPos != that->m_filter2mediaType.end(); ++iPos )
412*cdf0e10cSrcweir         {
413*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf;
414*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("extension \"") );
415*cdf0e10cSrcweir             buf.append( iPos->first );
416*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
417*cdf0e10cSrcweir                                  "\" maps to media-type \"") );
418*cdf0e10cSrcweir             buf.append( iPos->second );
419*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
420*cdf0e10cSrcweir                                  "\" maps to backend ") );
421*cdf0e10cSrcweir             const Reference<deployment::XPackageRegistry> xBackend(
422*cdf0e10cSrcweir                 that->m_mediaType2backend.find( iPos->second )->second );
423*cdf0e10cSrcweir             allBackends.insert( xBackend );
424*cdf0e10cSrcweir             buf.append( Reference<lang::XServiceInfo>(
425*cdf0e10cSrcweir                             xBackend, UNO_QUERY_THROW )
426*cdf0e10cSrcweir                         ->getImplementationName() );
427*cdf0e10cSrcweir             dp_misc::writeConsole( buf.makeStringAndClear() + OUSTR("\n"));
428*cdf0e10cSrcweir         }
429*cdf0e10cSrcweir         dp_misc::TRACE( "> [dp_registry.cxx] ambiguous backends:\n\n" );
430*cdf0e10cSrcweir         for ( t_registryset::const_iterator iPos(
431*cdf0e10cSrcweir                   that->m_ambiguousBackends.begin() );
432*cdf0e10cSrcweir               iPos != that->m_ambiguousBackends.end(); ++iPos )
433*cdf0e10cSrcweir         {
434*cdf0e10cSrcweir             ::rtl::OUStringBuffer buf;
435*cdf0e10cSrcweir             buf.append(
436*cdf0e10cSrcweir                 Reference<lang::XServiceInfo>(
437*cdf0e10cSrcweir                     *iPos, UNO_QUERY_THROW )->getImplementationName() );
438*cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(": ") );
439*cdf0e10cSrcweir             const Sequence< Reference<deployment::XPackageTypeInfo> > types(
440*cdf0e10cSrcweir                 (*iPos)->getSupportedPackageTypes() );
441*cdf0e10cSrcweir             for ( sal_Int32 pos = 0; pos < types.getLength(); ++pos ) {
442*cdf0e10cSrcweir                 Reference<deployment::XPackageTypeInfo> const & xInfo =
443*cdf0e10cSrcweir                     types[ pos ];
444*cdf0e10cSrcweir                 buf.append( xInfo->getMediaType() );
445*cdf0e10cSrcweir                 const OUString filter( xInfo->getFileFilter() );
446*cdf0e10cSrcweir                 if (filter.getLength() > 0) {
447*cdf0e10cSrcweir                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" (") );
448*cdf0e10cSrcweir                     buf.append( filter );
449*cdf0e10cSrcweir                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
450*cdf0e10cSrcweir                 }
451*cdf0e10cSrcweir                 if (pos < (types.getLength() - 1))
452*cdf0e10cSrcweir                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
453*cdf0e10cSrcweir             }
454*cdf0e10cSrcweir             dp_misc::TRACE(buf.makeStringAndClear() + OUSTR("\n\n"));
455*cdf0e10cSrcweir         }
456*cdf0e10cSrcweir         allBackends.insert( that->m_ambiguousBackends.begin(),
457*cdf0e10cSrcweir                             that->m_ambiguousBackends.end() );
458*cdf0e10cSrcweir         OSL_ASSERT( allBackends == that->m_allBackends );
459*cdf0e10cSrcweir     }
460*cdf0e10cSrcweir #endif
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     return xRet;
463*cdf0e10cSrcweir }
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir // XUpdatable: broadcast to backends
466*cdf0e10cSrcweir //______________________________________________________________________________
467*cdf0e10cSrcweir void PackageRegistryImpl::update() throw (RuntimeException)
468*cdf0e10cSrcweir {
469*cdf0e10cSrcweir     check();
470*cdf0e10cSrcweir     t_registryset::const_iterator iPos( m_allBackends.begin() );
471*cdf0e10cSrcweir     const t_registryset::const_iterator iEnd( m_allBackends.end() );
472*cdf0e10cSrcweir     for ( ; iPos != iEnd; ++iPos ) {
473*cdf0e10cSrcweir         const Reference<util::XUpdatable> xUpdatable( *iPos, UNO_QUERY );
474*cdf0e10cSrcweir         if (xUpdatable.is())
475*cdf0e10cSrcweir             xUpdatable->update();
476*cdf0e10cSrcweir     }
477*cdf0e10cSrcweir }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir // XPackageRegistry
480*cdf0e10cSrcweir //______________________________________________________________________________
481*cdf0e10cSrcweir Reference<deployment::XPackage> PackageRegistryImpl::bindPackage(
482*cdf0e10cSrcweir     OUString const & url, OUString const & mediaType_, sal_Bool bRemoved,
483*cdf0e10cSrcweir     OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
484*cdf0e10cSrcweir     throw (deployment::DeploymentException, deployment::InvalidRemovedParameterException,
485*cdf0e10cSrcweir            CommandFailedException,
486*cdf0e10cSrcweir            lang::IllegalArgumentException, RuntimeException)
487*cdf0e10cSrcweir {
488*cdf0e10cSrcweir     check();
489*cdf0e10cSrcweir     OUString mediaType(mediaType_);
490*cdf0e10cSrcweir     if (mediaType.getLength() == 0)
491*cdf0e10cSrcweir     {
492*cdf0e10cSrcweir         ::ucbhelper::Content ucbContent;
493*cdf0e10cSrcweir         if (create_ucb_content(
494*cdf0e10cSrcweir                 &ucbContent, url, xCmdEnv, false /* no throw */ )
495*cdf0e10cSrcweir                 && !ucbContent.isFolder())
496*cdf0e10cSrcweir         {
497*cdf0e10cSrcweir             OUString title( ucbContent.getPropertyValue(
498*cdf0e10cSrcweir                                 StrTitle::get() ).get<OUString>() );
499*cdf0e10cSrcweir             for (;;)
500*cdf0e10cSrcweir             {
501*cdf0e10cSrcweir                 const t_string2string::const_iterator iFind(
502*cdf0e10cSrcweir                     m_filter2mediaType.find(title) );
503*cdf0e10cSrcweir                 if (iFind != m_filter2mediaType.end()) {
504*cdf0e10cSrcweir                     mediaType = iFind->second;
505*cdf0e10cSrcweir                     break;
506*cdf0e10cSrcweir                 }
507*cdf0e10cSrcweir                 sal_Int32 point = title.indexOf( '.', 1 /* consume . */ );
508*cdf0e10cSrcweir                 if (point < 0)
509*cdf0e10cSrcweir                     break;
510*cdf0e10cSrcweir                 title = title.copy(point);
511*cdf0e10cSrcweir             }
512*cdf0e10cSrcweir         }
513*cdf0e10cSrcweir     }
514*cdf0e10cSrcweir     if (mediaType.getLength() == 0)
515*cdf0e10cSrcweir     {
516*cdf0e10cSrcweir         // try ambiguous backends:
517*cdf0e10cSrcweir         t_registryset::const_iterator iPos( m_ambiguousBackends.begin() );
518*cdf0e10cSrcweir         const t_registryset::const_iterator iEnd( m_ambiguousBackends.end() );
519*cdf0e10cSrcweir         for ( ; iPos != iEnd; ++iPos )
520*cdf0e10cSrcweir         {
521*cdf0e10cSrcweir             try {
522*cdf0e10cSrcweir                 return (*iPos)->bindPackage( url, mediaType, bRemoved,
523*cdf0e10cSrcweir                     identifier, xCmdEnv );
524*cdf0e10cSrcweir             }
525*cdf0e10cSrcweir             catch (lang::IllegalArgumentException &) {
526*cdf0e10cSrcweir             }
527*cdf0e10cSrcweir         }
528*cdf0e10cSrcweir         throw lang::IllegalArgumentException(
529*cdf0e10cSrcweir             getResourceString(RID_STR_CANNOT_DETECT_MEDIA_TYPE) + url,
530*cdf0e10cSrcweir             static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
531*cdf0e10cSrcweir     }
532*cdf0e10cSrcweir     else
533*cdf0e10cSrcweir     {
534*cdf0e10cSrcweir         // get backend by media-type:
535*cdf0e10cSrcweir         t_string2registry::const_iterator iFind(
536*cdf0e10cSrcweir             m_mediaType2backend.find( normalizeMediaType(mediaType) ) );
537*cdf0e10cSrcweir         if (iFind == m_mediaType2backend.end()) {
538*cdf0e10cSrcweir             // xxx todo: more sophisticated media-type argument parsing...
539*cdf0e10cSrcweir             sal_Int32 q = mediaType.indexOf( ';' );
540*cdf0e10cSrcweir             if (q >= 0) {
541*cdf0e10cSrcweir                 iFind = m_mediaType2backend.find(
542*cdf0e10cSrcweir                     normalizeMediaType(
543*cdf0e10cSrcweir                         // cut parameters:
544*cdf0e10cSrcweir                         mediaType.copy( 0, q ) ) );
545*cdf0e10cSrcweir             }
546*cdf0e10cSrcweir         }
547*cdf0e10cSrcweir         if (iFind == m_mediaType2backend.end()) {
548*cdf0e10cSrcweir             throw lang::IllegalArgumentException(
549*cdf0e10cSrcweir                 getResourceString(RID_STR_UNSUPPORTED_MEDIA_TYPE) + mediaType,
550*cdf0e10cSrcweir                 static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
551*cdf0e10cSrcweir         }
552*cdf0e10cSrcweir         return iFind->second->bindPackage( url, mediaType, bRemoved,
553*cdf0e10cSrcweir             identifier, xCmdEnv );
554*cdf0e10cSrcweir     }
555*cdf0e10cSrcweir }
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir //______________________________________________________________________________
558*cdf0e10cSrcweir Sequence< Reference<deployment::XPackageTypeInfo> >
559*cdf0e10cSrcweir PackageRegistryImpl::getSupportedPackageTypes() throw (RuntimeException)
560*cdf0e10cSrcweir {
561*cdf0e10cSrcweir     return comphelper::containerToSequence(m_typesInfos);
562*cdf0e10cSrcweir }
563*cdf0e10cSrcweir } // anon namespace
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir //==============================================================================
566*cdf0e10cSrcweir Reference<deployment::XPackageRegistry> SAL_CALL create(
567*cdf0e10cSrcweir     OUString const & context,
568*cdf0e10cSrcweir     OUString const & cachePath, bool readOnly,
569*cdf0e10cSrcweir     Reference<XComponentContext> const & xComponentContext )
570*cdf0e10cSrcweir {
571*cdf0e10cSrcweir     return PackageRegistryImpl::create(
572*cdf0e10cSrcweir         context, cachePath, readOnly, xComponentContext );
573*cdf0e10cSrcweir }
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir } // namespace dp_registry
576*cdf0e10cSrcweir 
577