LibreOffice Module ucb (master) 1
pkgprovider.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
21/**************************************************************************
22 TODO
23 **************************************************************************
24
25 *************************************************************************/
26
28#include <cppuhelper/weak.hxx>
31#include <ucbhelper/macros.hxx>
32#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
33#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
34#include <com/sun/star/ucb/IllegalIdentifierException.hpp>
35#include <com/sun/star/uno/XComponentContext.hpp>
36#include "pkgprovider.hxx"
37#include "pkgcontent.hxx"
38#include "pkguri.hxx"
39#include <unordered_map>
40#include <utility>
41
42using namespace com::sun::star;
43
44namespace package_ucp
45{
46
47
48
49namespace {
50
51class Package : public cppu::OWeakObject,
52 public container::XHierarchicalNameAccess
53{
55
56 OUString m_aName;
57 uno::Reference< container::XHierarchicalNameAccess > m_xNA;
59
60public:
61 Package( OUString aName,
62 uno::Reference< container::XHierarchicalNameAccess > xNA,
63 ContentProvider* pOwner )
64 : m_aName(std::move( aName )), m_xNA(std::move( xNA )), m_pOwner( pOwner ) {}
65 virtual ~Package() override { m_pOwner->removePackage( m_aName ); }
66
67 // XInterface
68 virtual uno::Any SAL_CALL
69 queryInterface( const uno::Type& aType ) override
70 { return m_xNA->queryInterface( aType ); }
71 virtual void SAL_CALL
72 acquire() noexcept override
73 { OWeakObject::acquire(); }
74 virtual void SAL_CALL
75 release() noexcept override
76 { OWeakObject::release(); }
77
78 // XHierarchicalNameAccess
79 virtual uno::Any SAL_CALL
80 getByHierarchicalName( const OUString& aName ) override
81 { return m_xNA->getByHierarchicalName( aName ); }
82 virtual sal_Bool SAL_CALL
83 hasByHierarchicalName( const OUString& aName ) override
84 { return m_xNA->hasByHierarchicalName( aName ); }
85};
86
87}
88
89class Packages : public std::unordered_map<OUString, Package*> {};
90
91}
92
93using namespace package_ucp;
94
95
96// ContentProvider Implementation.
98 const uno::Reference< uno::XComponentContext >& rxContext )
99: ::ucbhelper::ContentProviderImplHelper( rxContext )
100{
101}
102
103
104// virtual
106{
107}
108
109// XInterface methods.
111 noexcept
112{
113 OWeakObject::acquire();
114}
115
117 noexcept
118{
119 OWeakObject::release();
120}
121
122css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType )
123{
124 css::uno::Any aRet = cppu::queryInterface( rType,
125 static_cast< lang::XTypeProvider* >(this),
126 static_cast< lang::XServiceInfo* >(this),
127 static_cast< ucb::XContentProvider* >(this)
128 );
129 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
130}
131
132// XTypeProvider methods.
133
134
136 lang::XTypeProvider,
137 lang::XServiceInfo,
138 ucb::XContentProvider );
139
140
141// XServiceInfo methods.
142
143OUString
145{
146 return "com.sun.star.comp.ucb.PackageContentProvider";
147}
148
151{
152 return cppu::supportsService(this, s);
153}
154
155css::uno::Sequence< OUString >
157{
158 return { "com.sun.star.ucb.PackageContentProvider" };
159}
160
161
162// XContentProvider methods.
163
164
165// virtual
166uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent(
167 const uno::Reference< ucb::XContentIdentifier >& Identifier )
168{
169 if ( !Identifier.is() )
170 return uno::Reference< ucb::XContent >();
171
172 PackageUri aUri( Identifier->getContentIdentifier() );
173 if ( !aUri.isValid() )
174 throw ucb::IllegalIdentifierException();
175
176 // Create a new identifier for the normalized URL returned by
177 // PackageUri::getUri().
178 uno::Reference< ucb::XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aUri.getUri() );
179
180 osl::MutexGuard aGuard( m_aMutex );
181
182 // Check, if a content with given id already exists...
183 uno::Reference< ucb::XContent > xContent
184 = queryExistingContent( xId );
185 if ( xContent.is() )
186 return xContent;
187
188 // Create a new content.
189
190 xContent = Content::create( m_xContext, this, Identifier ); // not xId!!!
191 registerNewContent( xContent );
192
193 if ( xContent.is() && !xContent->getIdentifier().is() )
194 throw ucb::IllegalIdentifierException();
195
196 return xContent;
197}
198
199
200// Other methods.
201
202
203uno::Reference< container::XHierarchicalNameAccess >
205{
206 osl::MutexGuard aGuard( m_aMutex );
207
208 OUString rURL = rURI.getPackage() + rURI.getParam();
209
210 if ( m_pPackages )
211 {
212 Packages::const_iterator it = m_pPackages->find( rURL );
213 if ( it != m_pPackages->end() )
214 {
215 // Already instantiated. Return package.
216 return (*it).second->m_xNA;
217 }
218 }
219 else
220 m_pPackages.reset( new Packages );
221
222 // Create new package...
223 uno::Sequence< uno::Any > aArguments{ uno::Any(rURL) };
224 uno::Reference< container::XHierarchicalNameAccess > xNameAccess;
225 try
226 {
227 xNameAccess.set(
228 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
229 "com.sun.star.packages.comp.ZipPackage",
231 css::uno::UNO_QUERY_THROW );
232 }
233 catch ( uno::RuntimeException const & )
234 {
235 throw;
236 }
237 catch ( uno::Exception const & e )
238 {
239 css::uno::Any anyEx = cppu::getCaughtException();
240 throw css::lang::WrappedTargetRuntimeException(
241 e.Message, e.Context, anyEx);
242 }
243
244 rtl::Reference< Package> xPackage = new Package( rURL, xNameAccess, this );
245 (*m_pPackages)[ rURL ] = xPackage.get();
246 return xPackage;
247}
248
249
250void ContentProvider::removePackage( const OUString & rName )
251{
252 osl::MutexGuard aGuard( m_aMutex );
253
254 if ( m_pPackages )
255 {
256 Packages::iterator it = m_pPackages->find( rName );
257 if ( it != m_pPackages->end() )
258 {
259 m_pPackages->erase( it );
260 return;
261 }
262 }
263}
264
265extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
267 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
268{
269 return cppu::acquire(new ContentProvider(context));
270}
271
272/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< Packages > m_pPackages
Definition: pkgprovider.hxx:48
virtual void SAL_CALL release() noexcept override
css::uno::Reference< css::container::XHierarchicalNameAccess > createPackage(const PackageUri &rParam)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL acquire() noexcept override
void removePackage(const OUString &rName)
virtual ~ContentProvider() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier) override
static rtl::Reference< Content > create(const css::uno::Reference< css::uno::XComponentContext > &rxContext, ContentProvider *pProvider, const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
const OUString & getParam() const
Definition: pkguri.hxx:72
const OUString & getPackage() const
Definition: pkguri.hxx:63
rtl::Reference< ContentImplHelper > queryExistingContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
css::uno::Reference< css::uno::XComponentContext > m_xContext
Sequence< PropertyValue > aArguments
OUString aName
SvLinkSource * pOwner
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
uno::Reference< container::XHierarchicalNameAccess > m_xNA
Definition: pkgprovider.cxx:57
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_package_ContentProvider_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
OUString m_aName
Definition: pkgprovider.cxx:56
ContentProvider * m_pOwner
Definition: pkgprovider.cxx:58
XTYPEPROVIDER_IMPL_3(ContentProvider, lang::XTypeProvider, lang::XServiceInfo, ucb::XContentProvider)
friend ContentProvider
Definition: pkgprovider.cxx:54
unsigned char sal_Bool