Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "dp_sfwk.hrc"
21 : #include "dp_backend.h"
22 : #include "dp_ucb.h"
23 : #include "dp_parceldesc.hxx"
24 : #include <rtl/uri.hxx>
25 : #include <ucbhelper/content.hxx>
26 : #include <cppuhelper/exc_hlp.hxx>
27 : #include <comphelper/servicedecl.hxx>
28 : #include <svl/inettype.hxx>
29 : #include <com/sun/star/container/XNameContainer.hpp>
30 : #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
31 : #include <com/sun/star/xml/sax/Parser.hpp>
32 :
33 :
34 : using namespace ::dp_misc;
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::ucb;
38 : using namespace ::com::sun::star::script;
39 :
40 :
41 : namespace dp_registry
42 : {
43 : namespace backend
44 : {
45 : namespace sfwk
46 : {
47 :
48 :
49 490 : class BackendImpl : public ::dp_registry::backend::PackageRegistryBackend
50 : {
51 0 : class PackageImpl : public ::dp_registry::backend::Package
52 : {
53 : BackendImpl * getMyBackend() const;
54 :
55 : Reference< container::XNameContainer > m_xNameCntrPkgHandler;
56 : OUString m_descr;
57 :
58 : void initPackageHandler();
59 :
60 : // Package
61 : virtual beans::Optional< beans::Ambiguous<sal_Bool> > isRegistered_(
62 : ::osl::ResettableMutexGuard & guard,
63 : ::rtl::Reference<AbortChannel> const & abortChannel,
64 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
65 : virtual void processPackage_(
66 : ::osl::ResettableMutexGuard & guard,
67 : bool registerPackage,
68 : bool startup,
69 : ::rtl::Reference<AbortChannel> const & abortChannel,
70 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
71 :
72 : public:
73 : PackageImpl(
74 : ::rtl::Reference<BackendImpl> const & myBackend,
75 : OUString const & url, OUString const & libType, bool bRemoved,
76 : OUString const & identifier);
77 : // XPackage
78 : virtual OUString SAL_CALL getDescription()
79 : throw (deployment::ExtensionRemovedException,
80 : RuntimeException, std::exception) SAL_OVERRIDE;
81 : virtual OUString SAL_CALL getLicenseText()
82 : throw (deployment::ExtensionRemovedException,
83 : RuntimeException, std::exception) SAL_OVERRIDE;
84 : };
85 : friend class PackageImpl;
86 :
87 : // PackageRegistryBackend
88 : virtual Reference<deployment::XPackage> bindPackage_(
89 : OUString const & url, OUString const & mediaType,
90 : bool bRemoved, OUString const & identifier,
91 : Reference<XCommandEnvironment> const & xCmdEnv ) SAL_OVERRIDE;
92 :
93 : const Reference<deployment::XPackageTypeInfo> m_xTypeInfo;
94 :
95 :
96 : public:
97 : BackendImpl(
98 : Sequence<Any> const & args,
99 : Reference<XComponentContext> const & xComponentContext );
100 :
101 : // XPackageRegistry
102 : virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
103 : getSupportedPackageTypes() throw (RuntimeException, std::exception) SAL_OVERRIDE;
104 : virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType)
105 : throw (deployment::DeploymentException,
106 : uno::RuntimeException, std::exception) SAL_OVERRIDE;
107 : };
108 :
109 0 : BackendImpl * BackendImpl::PackageImpl::getMyBackend() const
110 : {
111 0 : BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
112 0 : if (NULL == pBackend)
113 : {
114 : //May throw a DisposedException
115 0 : check();
116 : //We should never get here...
117 : throw RuntimeException("Failed to get the BackendImpl",
118 0 : static_cast<OWeakObject*>(const_cast<PackageImpl *>(this)));
119 : }
120 0 : return pBackend;
121 : }
122 :
123 0 : OUString BackendImpl::PackageImpl::getDescription()
124 : throw (deployment::ExtensionRemovedException,
125 : RuntimeException, std::exception)
126 : {
127 0 : if (m_descr.isEmpty())
128 0 : return Package::getDescription();
129 : else
130 0 : return m_descr;
131 : }
132 :
133 0 : OUString BackendImpl::PackageImpl::getLicenseText()
134 : throw (deployment::ExtensionRemovedException,
135 : RuntimeException, std::exception)
136 : {
137 0 : return Package::getDescription();
138 : }
139 :
140 0 : BackendImpl::PackageImpl::PackageImpl(
141 : ::rtl::Reference<BackendImpl> const & myBackend,
142 : OUString const & url, OUString const & libType, bool bRemoved,
143 : OUString const & identifier)
144 0 : : Package( myBackend.get(), url, OUString(), OUString(),
145 0 : myBackend->m_xTypeInfo, bRemoved, identifier),
146 0 : m_descr(libType)
147 : {
148 0 : initPackageHandler();
149 :
150 0 : sal_Int32 segmEnd = url.getLength();
151 0 : if ( url.endsWith("/") )
152 0 : --segmEnd;
153 0 : sal_Int32 segmStart = (url.lastIndexOf( '/', segmEnd ) + 1);
154 0 : if (segmStart < 0)
155 0 : segmStart = 0;
156 : // name and display name default the same:
157 0 : m_displayName = ::rtl::Uri::decode(
158 : url.copy( segmStart, segmEnd - segmStart ),
159 0 : rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
160 0 : m_name = m_displayName;
161 :
162 0 : dp_misc::TRACE("PakageImpl displayName is " + m_displayName);
163 0 : }
164 :
165 :
166 490 : BackendImpl::BackendImpl(
167 : Sequence<Any> const & args,
168 : Reference<XComponentContext> const & xComponentContext )
169 : : PackageRegistryBackend( args, xComponentContext ),
170 : m_xTypeInfo( new Package::TypeInfo(
171 : "application/vnd.sun.star.framework-script",
172 : OUString() /* no file filter */,
173 : "Scripting Framework Script Library",
174 490 : RID_IMG_SCRIPTLIB ) )
175 : {
176 490 : if (! transientMode())
177 : {
178 : }
179 490 : }
180 :
181 :
182 :
183 : // XPackageRegistry
184 :
185 : Sequence< Reference<deployment::XPackageTypeInfo> >
186 490 : BackendImpl::getSupportedPackageTypes() throw (RuntimeException, std::exception)
187 : {
188 490 : return Sequence< Reference<deployment::XPackageTypeInfo> >(&m_xTypeInfo, 1);
189 : }
190 :
191 0 : void BackendImpl::packageRemoved(OUString const & /*url*/, OUString const & /*mediaType*/)
192 : throw (deployment::DeploymentException,
193 : uno::RuntimeException, std::exception)
194 : {
195 0 : }
196 :
197 : // PackageRegistryBackend
198 :
199 6 : Reference<deployment::XPackage> BackendImpl::bindPackage_(
200 : OUString const & url, OUString const & mediaType_, bool bRemoved,
201 : OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
202 : {
203 6 : OUString mediaType( mediaType_ );
204 6 : if (mediaType.isEmpty())
205 : {
206 : // detect media-type:
207 6 : ::ucbhelper::Content ucbContent;
208 12 : if (create_ucb_content( &ucbContent, url, xCmdEnv ) &&
209 6 : ucbContent.isFolder())
210 : {
211 : // probe for parcel-descriptor.xml:
212 4 : if (create_ucb_content(
213 : 0, makeURL( url, "parcel-descriptor.xml" ),
214 4 : xCmdEnv, false /* no throw */ ))
215 : {
216 0 : mediaType = "application/vnd.sun.star.framework-script";
217 : }
218 : }
219 6 : if (mediaType.isEmpty())
220 : throw lang::IllegalArgumentException(
221 12 : StrCannotDetectMediaType::get() + url,
222 18 : static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
223 : }
224 :
225 0 : OUString type, subType;
226 0 : INetContentTypeParameterList params;
227 0 : if (INetContentTypes::parse( mediaType, type, subType, ¶ms ))
228 : {
229 0 : if (type.equalsIgnoreAsciiCase("application"))
230 : {
231 0 : if (subType.equalsIgnoreAsciiCase("vnd.sun.star.framework-script"))
232 : {
233 0 : OUString lang = "Script";
234 : OUString sParcelDescURL = makeURL(
235 0 : url, "parcel-descriptor.xml" );
236 :
237 0 : ::ucbhelper::Content ucb_content;
238 :
239 0 : if (create_ucb_content( &ucb_content, sParcelDescURL,
240 : xCmdEnv, false /* no throw */ ))
241 : {
242 : ParcelDescDocHandler* pHandler =
243 0 : new ParcelDescDocHandler();
244 : Reference< xml::sax::XDocumentHandler >
245 0 : xDocHandler = pHandler;
246 :
247 : Reference<XComponentContext>
248 0 : xContext( getComponentContext() );
249 :
250 0 : Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
251 :
252 0 : xParser->setDocumentHandler( xDocHandler );
253 0 : xml::sax::InputSource source;
254 0 : source.aInputStream = ucb_content.openStream();
255 0 : source.sSystemId = ucb_content.getURL();
256 0 : xParser->parseStream( source );
257 :
258 0 : if ( pHandler->isParsed() )
259 : {
260 0 : lang = pHandler->getParcelLanguage();
261 0 : }
262 : }
263 :
264 0 : OUString sfwkLibType = getResourceString( RID_STR_SFWK_LIB );
265 : // replace %MACRONAME placeholder with language name
266 0 : OUString MACRONAME( "%MACROLANG" );
267 0 : sal_Int32 startOfReplace = sfwkLibType.indexOf( MACRONAME );
268 0 : sal_Int32 charsToReplace = MACRONAME.getLength();
269 0 : sfwkLibType = sfwkLibType.replaceAt( startOfReplace, charsToReplace, lang );
270 0 : dp_misc::TRACE("******************************\n");
271 0 : dp_misc::TRACE(" BackEnd detected lang = " + lang + "\n");
272 0 : dp_misc::TRACE(" for url " + sParcelDescURL + "\n");
273 0 : dp_misc::TRACE("******************************\n");
274 0 : return new PackageImpl( this, url, sfwkLibType, bRemoved, identifier);
275 : }
276 : }
277 : }
278 : throw lang::IllegalArgumentException(
279 0 : StrUnsupportedMediaType::get() + mediaType,
280 : static_cast<OWeakObject *>(this),
281 6 : static_cast<sal_Int16>(-1) );
282 : }
283 :
284 :
285 0 : void BackendImpl::PackageImpl:: initPackageHandler()
286 : {
287 0 : if (m_xNameCntrPkgHandler.is())
288 0 : return;
289 :
290 0 : BackendImpl * that = getMyBackend();
291 0 : Any aContext;
292 :
293 0 : if ( that->m_eContext == CONTEXT_USER )
294 : {
295 0 : aContext <<= OUString("user");
296 : }
297 0 : else if ( that->m_eContext == CONTEXT_SHARED )
298 : {
299 0 : aContext <<= OUString("share");
300 : }
301 0 : else if ( that->m_eContext == CONTEXT_BUNDLED )
302 : {
303 0 : aContext <<= OUString("bundled");
304 : }
305 : else
306 : {
307 : OSL_ASSERT( false );
308 : // NOT supported at the momemtn // TODO
309 : }
310 :
311 : Reference< provider::XScriptProviderFactory > xFac =
312 0 : provider::theMasterScriptProviderFactory::get( that->getComponentContext() );
313 :
314 0 : Reference< container::XNameContainer > xName( xFac->createScriptProvider( aContext ), UNO_QUERY );
315 0 : if ( xName.is() )
316 : {
317 0 : m_xNameCntrPkgHandler.set( xName );
318 0 : }
319 : // TODO what happens if above fails??
320 : }
321 :
322 : // Package
323 :
324 : beans::Optional< beans::Ambiguous<sal_Bool> >
325 0 : BackendImpl::PackageImpl::isRegistered_(
326 : ::osl::ResettableMutexGuard &,
327 : ::rtl::Reference<AbortChannel> const &,
328 : Reference<XCommandEnvironment> const & )
329 : {
330 : return beans::Optional< beans::Ambiguous<sal_Bool> >(
331 : true /* IsPresent */,
332 : beans::Ambiguous<sal_Bool>(
333 0 : m_xNameCntrPkgHandler.is() && m_xNameCntrPkgHandler->hasByName(
334 0 : m_url ),
335 0 : false /* IsAmbiguous */ ) );
336 : }
337 :
338 :
339 0 : void BackendImpl::PackageImpl::processPackage_(
340 : ::osl::ResettableMutexGuard &,
341 : bool doRegisterPackage,
342 : bool /* startup */,
343 : ::rtl::Reference<AbortChannel> const &,
344 : Reference<XCommandEnvironment> const & )
345 : {
346 0 : if ( !m_xNameCntrPkgHandler.is() )
347 : {
348 0 : dp_misc::TRACE("no package handler!!!!\n");
349 0 : throw RuntimeException( "No package Handler " );
350 : }
351 :
352 0 : if (doRegisterPackage)
353 : {
354 : // will throw if it fails
355 0 : m_xNameCntrPkgHandler->insertByName( m_url, makeAny( Reference< XPackage >(this) ) );
356 :
357 : }
358 : else // revokePackage()
359 : {
360 0 : m_xNameCntrPkgHandler->removeByName( m_url );
361 : }
362 0 : }
363 :
364 : namespace sdecl = comphelper::service_decl;
365 140 : sdecl::class_<BackendImpl, sdecl::with_args<true> > serviceBI;
366 140 : extern sdecl::ServiceDecl const serviceDecl(
367 : serviceBI,
368 : "com.sun.star.comp.deployment.sfwk.PackageRegistryBackend",
369 : BACKEND_SERVICE_NAME );
370 :
371 : } // namespace sfwk
372 : } // namespace backend
373 420 : } // namespace dp_registry
374 :
375 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|