| 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 "ucpext_provider.hxx" |
| 21 |
#include "ucpext_content.hxx" |
| 22 |
|
| 23 |
#include <com/sun/star/ucb/IllegalIdentifierException.hpp> |
| 24 |
#include <cppuhelper/weak.hxx> |
| 25 |
#include <ucbhelper/contentidentifier.hxx> |
| 26 |
#include <osl/mutex.hxx> |
| 27 |
#include <rtl/ustrbuf.hxx> |
| 28 |
|
| 29 |
|
| 30 |
namespace ucb::ucp::ext |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
using ::com::sun::star::uno::Reference; |
| 35 |
using ::com::sun::star::uno::XInterface; |
| 36 |
using ::com::sun::star::uno::Sequence; |
| 37 |
using ::com::sun::star::ucb::XContentIdentifier; |
| 38 |
using ::com::sun::star::ucb::IllegalIdentifierException; |
| 39 |
using ::com::sun::star::ucb::XContent; |
| 40 |
using ::com::sun::star::uno::XComponentContext; |
| 41 |
|
| 42 |
|
| 43 |
//= ContentProvider |
| 44 |
|
| 45 |
|
| 46 |
ContentProvider::ContentProvider( const Reference< XComponentContext >& rxContext ) |
| 47 |
:ContentProvider_Base( rxContext ) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
ContentProvider::~ContentProvider() |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
OUString SAL_CALL ContentProvider::getImplementationName() |
| 58 |
{ |
| 59 |
return u"org.openoffice.comp.ucp.ext.ContentProvider"_ustr; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
Sequence< OUString > SAL_CALL ContentProvider::getSupportedServiceNames( ) |
| 64 |
{ |
| 65 |
return { u"com.sun.star.ucb.ContentProvider"_ustr, u"com.sun.star.ucb.ExtensionContentProvider"_ustr }; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
OUString ContentProvider::getRootURL() |
| 70 |
{ |
| 71 |
return u"vnd.sun.star.extension://"_ustr; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
OUString ContentProvider::getArtificialNodeContentType() |
| 76 |
{ |
| 77 |
return u"application/vnd.sun.star.extension-content"_ustr; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
namespace |
| 82 |
{ |
| 83 |
void lcl_ensureAndTransfer( std::u16string_view& io_rIdentifierFragment, OUStringBuffer& o_rNormalization, const sal_Unicode i_nLeadingChar ) |
| 84 |
{ |
| 85 |
if ( ( io_rIdentifierFragment.empty() ) || ( io_rIdentifierFragment[0] != i_nLeadingChar ) ) |
| 86 |
throw IllegalIdentifierException(); |
| 87 |
io_rIdentifierFragment = io_rIdentifierFragment.substr( 1 ); |
| 88 |
o_rNormalization.append( i_nLeadingChar ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
Reference< XContent > SAL_CALL ContentProvider::queryContent( const Reference< XContentIdentifier >& i_rIdentifier ) |
| 94 |
{ |
| 95 |
// Check URL scheme... |
| 96 |
static constexpr OUString sScheme( u"vnd.sun.star.extension"_ustr ); |
| 97 |
if ( !i_rIdentifier->getContentProviderScheme().equalsIgnoreAsciiCase( sScheme ) ) |
| 98 |
throw IllegalIdentifierException(); |
| 99 |
|
| 100 |
// normalize the identifier |
| 101 |
const OUString sIdentifier( i_rIdentifier->getContentIdentifier() ); |
| 102 |
|
| 103 |
// the scheme needs to be lower-case |
| 104 |
OUStringBuffer aComposer( sIdentifier.copy( 0, sScheme.getLength() ).toAsciiLowerCase() ); |
| 105 |
|
| 106 |
// one : is required after the scheme |
| 107 |
std::u16string_view sRemaining( sIdentifier.subView( sScheme.getLength() ) ); |
| 108 |
lcl_ensureAndTransfer( sRemaining, aComposer, ':' ); |
| 109 |
|
| 110 |
// and at least one / |
| 111 |
lcl_ensureAndTransfer( sRemaining, aComposer, '/' ); |
| 112 |
|
| 113 |
// the normalized form requires one additional /, but we also accept identifiers which don't have it |
| 114 |
if ( sRemaining.empty() ) |
| 115 |
{ |
| 116 |
// the root content is a special case, it requires / |
| 117 |
aComposer.append( "//" ); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
if ( sRemaining[0] != '/' ) |
| 122 |
{ |
| 123 |
aComposer.append( OUString::Concat("/") + sRemaining ); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
lcl_ensureAndTransfer( sRemaining, aComposer, '/' ); |
| 128 |
// by now, we moved "vnd.sun.star.extension://" from the URL to aComposer |
| 129 |
if ( sRemaining.empty() ) |
| 130 |
{ |
| 131 |
// again, it's the root content, but one / is missing |
| 132 |
aComposer.append( '/' ); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
aComposer.append( sRemaining ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
const Reference< XContentIdentifier > xNormalizedIdentifier( new ::ucbhelper::ContentIdentifier( aComposer.makeStringAndClear() ) ); |
| 141 |
|
| 142 |
::osl::MutexGuard aGuard( m_aMutex ); |
| 143 |
|
| 144 |
// check if a content with given id already exists... |
| 145 |
Reference< XContent > xContent( queryExistingContent( xNormalizedIdentifier ) ); |
| 146 |
if ( xContent.is() ) |
| 147 |
return xContent; |
| 148 |
|
| 149 |
// create a new content |
| 150 |
xContent = new Content( m_xContext, this, xNormalizedIdentifier ); |
| 151 |
if ( !xContent->getIdentifier().is() ) |
| 152 |
throw IllegalIdentifierException(); |
| 153 |
|
| 154 |
registerNewContent( xContent ); |
| 155 |
return xContent; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
} // namespace ucb::ucp::ext |
| 160 |
|
| 161 |
|
| 162 |
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 163 |
ucb_ext_ContentProvider_get_implementation( |
| 164 |
css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&) |
| 165 |
{ |
| 166 |
return cppu::acquire(new ucb::ucp::ext::ContentProvider(context)); |
| 167 |
} |
| 168 |
|
| 169 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |