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