Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/ucbhelper/source/provider/registerucb.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
#include <ucbhelper/registerucb.hxx>
21
#include <com/sun/star/lang/IllegalArgumentException.hpp>
22
#include <com/sun/star/ucb/DuplicateProviderException.hpp>
23
#include <com/sun/star/ucb/XContentProviderManager.hpp>
24
#include <com/sun/star/ucb/XParameterizedContentProvider.hpp>
25
#include <com/sun/star/ucb/ContentProviderProxyFactory.hpp>
26
#include <com/sun/star/ucb/XContentProviderFactory.hpp>
27
#include <com/sun/star/uno/XComponentContext.hpp>
28
#include <com/sun/star/uno/RuntimeException.hpp>
29
30
#include <osl/diagnose.h>
31
32
using namespace com::sun::star;
33
34
namespace ucbhelper {
35
36
bool
37
registerAtUcb(
38
    uno::Reference< ucb::XContentProviderManager > const & rManager,
39
    uno::Reference< uno::XComponentContext > const & rxContext,
40
    OUString const & rName,
41
    OUString const & rArguments,
42
    OUString const & rTemplate)
43
0
{
44
0
    OSL_ENSURE(rxContext.is(),
45
0
               "ucb::registerAtUcb(): No service factory");
46
47
0
    bool bNoProxy = rArguments.startsWith("{noproxy}");
48
0
    OUString
49
0
        aProviderArguments(bNoProxy ?
50
0
                               rArguments.
51
0
                                   copy(RTL_CONSTASCII_LENGTH("{noproxy}")) :
52
0
                               rArguments);
53
54
0
    uno::Reference< ucb::XContentProvider > xProvider;
55
56
0
    if (!rName.isEmpty())
57
0
    {
58
        // First, try to instantiate proxy for provider:
59
0
        if (!bNoProxy)
60
0
        {
61
0
            uno::Reference< ucb::XContentProviderFactory > xProxyFactory;
62
0
            try
63
0
            {
64
0
                xProxyFactory = ucb::ContentProviderProxyFactory::create( rxContext );
65
0
            }
66
0
            catch (uno::Exception const &) {}
67
0
            OSL_ENSURE(xProxyFactory.is(), "No ContentProviderProxyFactory");
68
0
            if (xProxyFactory.is())
69
0
                xProvider = xProxyFactory->createContentProvider(rName);
70
0
        }
71
72
        // Then, try to instantiate provider directly:
73
0
        if (!xProvider.is())
74
0
            try
75
0
            {
76
0
                xProvider.set(
77
0
                    rxContext->getServiceManager()->createInstanceWithContext(rName, rxContext),
78
0
                    uno::UNO_QUERY);
79
0
            }
80
0
            catch (uno::RuntimeException const &) { throw; }
81
0
            catch (uno::Exception const &) {}
82
0
    }
83
84
0
    uno::Reference< ucb::XParameterizedContentProvider >
85
0
        xParameterized(xProvider, uno::UNO_QUERY);
86
0
    if (xParameterized.is())
87
0
    {
88
0
        uno::Reference< ucb::XContentProvider > xInstance;
89
0
        try
90
0
        {
91
0
            xInstance = xParameterized->registerInstance(rTemplate,
92
0
                                                         aProviderArguments,
93
0
                                                         true);
94
                //@@@ if this call replaces an old instance, the commit-or-
95
                // rollback code below will not work
96
0
        }
97
0
        catch (lang::IllegalArgumentException const &) {}
98
99
0
        if (xInstance.is())
100
0
            xProvider = std::move(xInstance);
101
0
    }
102
103
0
    bool bSuccess = false;
104
0
    if (rManager.is() && (rName.isEmpty() || xProvider.is()))
105
0
    {
106
0
        try
107
0
        {
108
0
            rManager->registerContentProvider(xProvider, rTemplate, true);
109
0
            bSuccess = true;
110
0
        }
111
0
        catch (ucb::DuplicateProviderException const &)
112
0
        {
113
0
            if (xParameterized.is())
114
0
                try
115
0
                {
116
0
                    xParameterized->deregisterInstance(rTemplate,
117
0
                                                       aProviderArguments);
118
0
                }
119
0
                catch (lang::IllegalArgumentException const &) {}
120
0
        }
121
0
        catch (...)
122
0
        {
123
0
            if (xParameterized.is())
124
0
                try
125
0
                {
126
0
                    xParameterized->deregisterInstance(rTemplate,
127
0
                                                       aProviderArguments);
128
0
                }
129
0
                catch (lang::IllegalArgumentException const &) {}
130
0
                catch (uno::RuntimeException const &) {}
131
0
            throw;
132
0
        }
133
0
    }
134
0
    return bSuccess;
135
0
}
136
137
}
138
139
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */