LibreOffice Module desktop (master) 1
dp_sfwk.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <strings.hrc>
23#include <dp_backend.h>
24#include <dp_misc.h>
25#include <dp_ucb.h>
26#include "dp_parceldesc.hxx"
27#include <rtl/uri.hxx>
28#include <ucbhelper/content.hxx>
29#include <svl/inettype.hxx>
30#include <com/sun/star/container/XNameContainer.hpp>
31#include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
32#include <com/sun/star/xml/sax/Parser.hpp>
34#include <utility>
35
36
37using namespace ::dp_misc;
38using namespace ::com::sun::star;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::ucb;
41using namespace ::com::sun::star::script;
42
43
45{
46
47namespace {
48
50{
51 class PackageImpl : public ::dp_registry::backend::Package
52 {
53 BackendImpl * getMyBackend() const;
54
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 ) 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 ) override;
71
72 public:
73 PackageImpl(
74 ::rtl::Reference<BackendImpl> const & myBackend,
75 OUString const & url, OUString libType, bool bRemoved,
76 OUString const & identifier);
77 // XPackage
78 virtual OUString SAL_CALL getDescription() override;
79 virtual OUString SAL_CALL getLicenseText() override;
80 };
81 friend class PackageImpl;
82
83 // PackageRegistryBackend
84 virtual Reference<deployment::XPackage> bindPackage_(
85 OUString const & url, OUString const & mediaType,
86 bool bRemoved, OUString const & identifier,
87 Reference<XCommandEnvironment> const & xCmdEnv ) override;
88
89 const Reference<deployment::XPackageTypeInfo> m_xTypeInfo;
90
91
92public:
93 BackendImpl(
94 Sequence<Any> const & args,
95 Reference<XComponentContext> const & xComponentContext );
96
97 // XServiceInfo
98 virtual OUString SAL_CALL getImplementationName() override;
99 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
100 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
101
102 // XPackageRegistry
103 virtual Sequence< Reference<deployment::XPackageTypeInfo> > SAL_CALL
104 getSupportedPackageTypes() override;
105 virtual void SAL_CALL packageRemoved(OUString const & url, OUString const & mediaType) override;
106};
107
108}
109
110BackendImpl * BackendImpl::PackageImpl::getMyBackend() const
111{
112 BackendImpl * pBackend = static_cast<BackendImpl *>(m_myBackend.get());
113 if (nullptr == pBackend)
114 {
115 //May throw a DisposedException
116 check();
117 //We should never get here...
118 throw RuntimeException("Failed to get the BackendImpl",
119 static_cast<OWeakObject*>(const_cast<PackageImpl *>(this)));
120 }
121 return pBackend;
122}
123
124OUString BackendImpl::PackageImpl::getDescription()
125{
126 if (m_descr.isEmpty())
128 else
129 return m_descr;
130}
131
132OUString BackendImpl::PackageImpl::getLicenseText()
133{
135}
136
137BackendImpl::PackageImpl::PackageImpl(
138 ::rtl::Reference<BackendImpl> const & myBackend,
139 OUString const & url, OUString libType, bool bRemoved,
140 OUString const & identifier)
141 : Package( myBackend, url, OUString(), OUString(),
142 myBackend->m_xTypeInfo, bRemoved, identifier),
143 m_descr(std::move(libType))
144{
145 initPackageHandler();
146
147 sal_Int32 segmEnd = url.getLength();
148 if ( url.endsWith("/") )
149 --segmEnd;
150 sal_Int32 segmStart = url.lastIndexOf( '/', segmEnd ) + 1;
151 if (segmStart < 0)
152 segmStart = 0;
153 // name and display name default the same:
154 m_displayName = ::rtl::Uri::decode(
155 url.copy( segmStart, segmEnd - segmStart ),
156 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
157 m_name = m_displayName;
158
159 dp_misc::TRACE("PackageImpl displayName is " + m_displayName);
160}
161
162
163BackendImpl::BackendImpl(
164 Sequence<Any> const & args,
165 Reference<XComponentContext> const & xComponentContext )
166 : PackageRegistryBackend( args, xComponentContext ),
167 m_xTypeInfo( new Package::TypeInfo(
168 "application/vnd.sun.star.framework-script",
169 OUString() /* no file filter */,
170 "Scripting Framework Script Library"
171 ) )
172{
173}
174
175
176// XServiceInfo
177OUString BackendImpl::getImplementationName()
178{
179 return "com.sun.star.comp.deployment.sfwk.PackageRegistryBackend";
180}
181
182sal_Bool BackendImpl::supportsService( const OUString& ServiceName )
183{
184 return cppu::supportsService(this, ServiceName);
185}
186
187css::uno::Sequence< OUString > BackendImpl::getSupportedServiceNames()
188{
189 return { BACKEND_SERVICE_NAME };
190}
191
192// XPackageRegistry
193
194Sequence< Reference<deployment::XPackageTypeInfo> >
195BackendImpl::getSupportedPackageTypes()
196{
197 return Sequence< Reference<deployment::XPackageTypeInfo> >(&m_xTypeInfo, 1);
198}
199
200void BackendImpl::packageRemoved(OUString const & /*url*/, OUString const & /*mediaType*/)
201{
202}
203
204// PackageRegistryBackend
205
206Reference<deployment::XPackage> BackendImpl::bindPackage_(
207 OUString const & url, OUString const & mediaType_, bool bRemoved,
208 OUString const & identifier, Reference<XCommandEnvironment> const & xCmdEnv )
209{
210 OUString mediaType( mediaType_ );
211 if (mediaType.isEmpty())
212 {
213 // detect media-type:
214 ::ucbhelper::Content ucbContent;
215 if (create_ucb_content( &ucbContent, url, xCmdEnv ) &&
216 ucbContent.isFolder())
217 {
218 // probe for parcel-descriptor.xml:
220 nullptr, makeURL( url, "parcel-descriptor.xml" ),
221 xCmdEnv, false /* no throw */ ))
222 {
223 mediaType = "application/vnd.sun.star.framework-script";
224 }
225 }
226 if (mediaType.isEmpty())
227 throw lang::IllegalArgumentException(
228 StrCannotDetectMediaType() + url,
229 static_cast<OWeakObject *>(this), static_cast<sal_Int16>(-1) );
230 }
231
232 OUString type, subType;
234 if (INetContentTypes::parse( mediaType, type, subType, &params ))
235 {
236 if (type.equalsIgnoreAsciiCase("application"))
237 {
238 if (subType.equalsIgnoreAsciiCase("vnd.sun.star.framework-script"))
239 {
240 OUString lang = "Script";
241 OUString sParcelDescURL = makeURL(
242 url, "parcel-descriptor.xml" );
243
244 ::ucbhelper::Content ucb_content;
245
246 if (create_ucb_content( &ucb_content, sParcelDescURL,
247 xCmdEnv, false /* no throw */ ))
248 {
250 new ParcelDescDocHandler();
251
252 Reference<XComponentContext>
254
255 Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
256
257 xParser->setDocumentHandler( pHandler );
258 xml::sax::InputSource source;
259 source.aInputStream = ucb_content.openStream();
260 source.sSystemId = ucb_content.getURL();
261 xParser->parseStream( source );
262
263 if ( pHandler->isParsed() )
264 {
265 lang = pHandler->getParcelLanguage();
266 }
267 }
268
269 OUString sfwkLibType = DpResId( RID_STR_SFWK_LIB );
270 // replace %MACRONAME placeholder with language name
271 OUString MACRONAME( "%MACROLANG" );
272 sal_Int32 startOfReplace = sfwkLibType.indexOf( MACRONAME );
273 sal_Int32 charsToReplace = MACRONAME.getLength();
274 sfwkLibType = sfwkLibType.replaceAt( startOfReplace, charsToReplace, lang );
275 dp_misc::TRACE("******************************\n");
276 dp_misc::TRACE(" BackEnd detected lang = " + lang + "\n");
277 dp_misc::TRACE(" for url " + sParcelDescURL + "\n");
278 dp_misc::TRACE("******************************\n");
279 return new PackageImpl( this, url, sfwkLibType, bRemoved, identifier);
280 }
281 }
282 }
283 throw lang::IllegalArgumentException(
284 StrUnsupportedMediaType() + mediaType,
285 static_cast<OWeakObject *>(this),
286 static_cast<sal_Int16>(-1) );
287}
288
289
290void BackendImpl::PackageImpl:: initPackageHandler()
291{
292 if (m_xNameCntrPkgHandler.is())
293 return;
294
295 BackendImpl * that = getMyBackend();
296 Any aContext;
297
298 if ( that->m_eContext == Context::User )
299 {
300 aContext <<= OUString("user");
301 }
302 else if ( that->m_eContext == Context::Shared )
303 {
304 aContext <<= OUString("share");
305 }
306 else if ( that->m_eContext == Context::Bundled )
307 {
308 aContext <<= OUString("bundled");
309 }
310 else
311 {
312 OSL_ASSERT( false );
313 // NOT supported at the moment // TODO
314 }
315
316 Reference< provider::XScriptProviderFactory > xFac =
317 provider::theMasterScriptProviderFactory::get( that->getComponentContext() );
318
319 Reference< container::XNameContainer > xName( xFac->createScriptProvider( aContext ), UNO_QUERY );
320 if ( xName.is() )
321 {
322 m_xNameCntrPkgHandler.set( xName );
323 }
324 // TODO what happens if above fails??
325}
326
327// Package
328
329beans::Optional< beans::Ambiguous<sal_Bool> >
330BackendImpl::PackageImpl::isRegistered_(
331 ::osl::ResettableMutexGuard &,
333 Reference<XCommandEnvironment> const & )
334{
335 return beans::Optional< beans::Ambiguous<sal_Bool> >(
336 true /* IsPresent */,
337 beans::Ambiguous<sal_Bool>(
339 m_url ),
340 false /* IsAmbiguous */ ) );
341}
342
343
344void BackendImpl::PackageImpl::processPackage_(
345 ::osl::ResettableMutexGuard &,
346 bool doRegisterPackage,
347 bool /* startup */,
349 Reference<XCommandEnvironment> const & )
350{
351 if ( !m_xNameCntrPkgHandler.is() )
352 {
353 dp_misc::TRACE("no package handler!!!!\n");
354 throw RuntimeException( "No package Handler " );
355 }
356
357 if (doRegisterPackage)
358 {
359 // will throw if it fails
360 m_xNameCntrPkgHandler->insertByName( m_url, Any( Reference< XPackage >(this) ) );
361
362 }
363 else // revokePackage()
364 {
365 m_xNameCntrPkgHandler->removeByName( m_url );
366 }
367}
368
369} // namespace dp_registry::backend::sfwk
370
371extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
373 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
374{
375 return cppu::acquire(new dp_registry::backend::sfwk::BackendImpl(args, context));
376}
377
378/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool parse(OUString const &rMediaType, OUString &rType, OUString &rSubType, INetContentTypeParameterList *pParameters=nullptr)
virtual OUString SAL_CALL getDescription() override
Definition: dp_backend.cxx:479
const OUString & getURL() const
css::uno::Reference< css::io::XInputStream > openStream()
OUString DpResId(TranslateId aId)
Definition: dp_misc.cxx:555
const Reference< deployment::XPackageTypeInfo > m_xTypeInfo
Definition: dp_sfwk.cxx:89
OUString m_descr
Definition: dp_sfwk.cxx:56
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_deployment_sfwk_PackageRegistryBackend_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
Definition: dp_sfwk.cxx:372
Reference< container::XNameContainer > m_xNameCntrPkgHandler
Definition: dp_sfwk.cxx:55
std::unordered_map< OString, INetContentTypeParameter > INetContentTypeParameterList
static uno::Reference< css::uno::XComponentContext > xContext
Definition: init.cxx:2642
OUString m_name
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OUString makeURL(std::u16string_view baseURL, OUString const &relPath_)
appends a relative path to a url.
Definition: dp_misc.cxx:253
void TRACE(OUString const &sText)
print the text to the console in a debug build.
Definition: dp_misc.cxx:486
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC bool create_ucb_content(::ucbhelper::Content *ucb_content, OUString const &url, css::uno::Reference< css::ucb::XCommandEnvironment > const &xCmdEnv, bool throw_exc=true)
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
constexpr OUStringLiteral BACKEND_SERVICE_NAME
Definition: dp_backend.h:41
def check(model)
args
::boost::spirit::classic::rule< ScannerT > identifier
unsigned char sal_Bool
ResultType type