| 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 |
: m_aName(std::move( aName )), m_xNA(std::move( xNA )), m_pOwner( pOwner ) {} |
| 63 |
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 |
{ return m_xNA->queryInterface( aType ); } |
| 69 |
virtual void SAL_CALL |
| 70 |
acquire() noexcept override |
| 71 |
{ OWeakObject::acquire(); } |
| 72 |
virtual void SAL_CALL |
| 73 |
release() noexcept override |
| 74 |
{ OWeakObject::release(); } |
| 75 |
|
| 76 |
// XHierarchicalNameAccess |
| 77 |
virtual uno::Any SAL_CALL |
| 78 |
getByHierarchicalName( const OUString& aName ) override |
| 79 |
{ return m_xNA->getByHierarchicalName( aName ); } |
| 80 |
virtual sal_Bool SAL_CALL |
| 81 |
hasByHierarchicalName( const OUString& aName ) override |
| 82 |
{ 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 |
: ::ucbhelper::ContentProviderImplHelper( rxContext ) |
| 94 |
{ |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// virtual |
| 99 |
ContentProvider::~ContentProvider() |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
// XInterface methods. |
| 104 |
void SAL_CALL ContentProvider::acquire() |
| 105 |
noexcept |
| 106 |
{ |
| 107 |
OWeakObject::acquire(); |
| 108 |
} |
| 109 |
|
| 110 |
void SAL_CALL ContentProvider::release() |
| 111 |
noexcept |
| 112 |
{ |
| 113 |
OWeakObject::release(); |
| 114 |
} |
| 115 |
|
| 116 |
css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType ) |
| 117 |
{ |
| 118 |
css::uno::Any aRet = cppu::queryInterface( rType, |
| 119 |
static_cast< lang::XTypeProvider* >(this), |
| 120 |
static_cast< lang::XServiceInfo* >(this), |
| 121 |
static_cast< ucb::XContentProvider* >(this) |
| 122 |
); |
| 123 |
return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); |
| 124 |
} |
| 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 |
{ |
| 140 |
return u"com.sun.star.comp.ucb.PackageContentProvider"_ustr; |
| 141 |
} |
| 142 |
|
| 143 |
sal_Bool |
| 144 |
ContentProvider::supportsService(const OUString& s) |
| 145 |
{ |
| 146 |
return cppu::supportsService(this, s); |
| 147 |
} |
| 148 |
|
| 149 |
css::uno::Sequence< OUString > |
| 150 |
ContentProvider::getSupportedServiceNames() |
| 151 |
{ |
| 152 |
return { u"com.sun.star.ucb.PackageContentProvider"_ustr }; |
| 153 |
} |
| 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 |
{ |
| 163 |
if ( !Identifier.is() ) |
| 164 |
return uno::Reference< ucb::XContent >(); |
| 165 |
|
| 166 |
PackageUri aUri( Identifier->getContentIdentifier() ); |
| 167 |
if ( !aUri.isValid() ) |
| 168 |
throw ucb::IllegalIdentifierException(); |
| 169 |
|
| 170 |
// Create a new identifier for the normalized URL returned by |
| 171 |
// PackageUri::getUri(). |
| 172 |
uno::Reference< ucb::XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aUri.getUri() ); |
| 173 |
|
| 174 |
osl::MutexGuard aGuard( m_aMutex ); |
| 175 |
|
| 176 |
// Check, if a content with given id already exists... |
| 177 |
rtl::Reference< ucbhelper::ContentImplHelper > xContent |
| 178 |
= queryExistingContent( xId ); |
| 179 |
if ( xContent.is() ) |
| 180 |
return xContent; |
| 181 |
|
| 182 |
// Create a new content. |
| 183 |
|
| 184 |
xContent = Content::create( m_xContext, this, Identifier ); // not xId!!! |
| 185 |
registerNewContent( xContent ); |
| 186 |
|
| 187 |
if ( xContent.is() && !xContent->getIdentifier().is() ) |
| 188 |
throw ucb::IllegalIdentifierException(); |
| 189 |
|
| 190 |
return xContent; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
// Other methods. |
| 195 |
|
| 196 |
|
| 197 |
uno::Reference< container::XHierarchicalNameAccess > |
| 198 |
ContentProvider::createPackage( const PackageUri & rURI ) |
| 199 |
{ |
| 200 |
osl::MutexGuard aGuard( m_aMutex ); |
| 201 |
|
| 202 |
OUString rURL = rURI.getPackage() + rURI.getParam(); |
| 203 |
|
| 204 |
auto it = m_aPackages.find( rURL ); |
| 205 |
if ( it != m_aPackages.end() ) |
| 206 |
{ |
| 207 |
// Already instantiated. Return package. |
| 208 |
return (*it).second->m_xNA; |
| 209 |
} |
| 210 |
|
| 211 |
// Create new package... |
| 212 |
uno::Sequence< uno::Any > aArguments{ uno::Any(rURL) }; |
| 213 |
uno::Reference< container::XHierarchicalNameAccess > xNameAccess; |
| 214 |
try |
| 215 |
{ |
| 216 |
xNameAccess.set( |
| 217 |
m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( |
| 218 |
u"com.sun.star.packages.comp.ZipPackage"_ustr, |
| 219 |
aArguments, m_xContext ), |
| 220 |
css::uno::UNO_QUERY_THROW ); |
| 221 |
} |
| 222 |
catch ( uno::RuntimeException const & ) |
| 223 |
{ |
| 224 |
throw; |
| 225 |
} |
| 226 |
catch ( uno::Exception const & e ) |
| 227 |
{ |
| 228 |
css::uno::Any anyEx = cppu::getCaughtException(); |
| 229 |
throw css::lang::WrappedTargetRuntimeException( |
| 230 |
e.Message, e.Context, anyEx); |
| 231 |
} |
| 232 |
|
| 233 |
rtl::Reference< Package> xPackage = new Package( rURL, xNameAccess, this ); |
| 234 |
m_aPackages[ rURL ] = xPackage.get(); |
| 235 |
return xPackage; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
void ContentProvider::removePackage( const OUString & rName ) |
| 240 |
{ |
| 241 |
osl::MutexGuard aGuard( m_aMutex ); |
| 242 |
|
| 243 |
m_aPackages.erase( rName ); |
| 244 |
} |
| 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 |
{ |
| 250 |
return cppu::acquire(new ContentProvider(context)); |
| 251 |
} |
| 252 |
|
| 253 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |