Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/configmgr/source/configurationprovider.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 <sal/config.h>
21
22
#include <cassert>
23
24
#include <com/sun/star/beans/NamedValue.hpp>
25
#include <com/sun/star/beans/PropertyValue.hpp>
26
#include <com/sun/star/configuration/theDefaultProvider.hpp>
27
#include <com/sun/star/lang/EventObject.hpp>
28
#include <com/sun/star/lang/Locale.hpp>
29
#include <com/sun/star/lang/XLocalizable.hpp>
30
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
31
#include <com/sun/star/lang/XServiceInfo.hpp>
32
#include <com/sun/star/uno/Any.hxx>
33
#include <com/sun/star/uno/Exception.hpp>
34
#include <com/sun/star/uno/Reference.hxx>
35
#include <com/sun/star/uno/Sequence.hxx>
36
#include <com/sun/star/uno/XInterface.hpp>
37
#include <com/sun/star/util/XFlushListener.hpp>
38
#include <com/sun/star/util/XFlushable.hpp>
39
#include <com/sun/star/util/XRefreshListener.hpp>
40
#include <com/sun/star/util/XRefreshable.hpp>
41
#include <comphelper/compbase.hxx>
42
#include <cppuhelper/supportsservice.hxx>
43
#include <cppuhelper/weak.hxx>
44
#include <osl/mutex.hxx>
45
#include <sal/types.h>
46
#include <rtl/ref.hxx>
47
#include <rtl/ustring.hxx>
48
49
#include <i18nlangtag/languagetag.hxx>
50
#include <utility>
51
52
#include "components.hxx"
53
#include "configurationprovider.hxx"
54
#include "lock.hxx"
55
#include "defaultprovider.hxx"
56
#include "rootaccess.hxx"
57
58
namespace configmgr::configuration_provider {
59
60
namespace {
61
62
constexpr OUString accessServiceName =
63
    u"com.sun.star.configuration.ConfigurationAccess"_ustr;
64
constexpr OUString updateAccessServiceName =
65
    u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr;
66
67
0
void badNodePath() {
68
0
    throw css::uno::Exception(
69
0
        (u"com.sun.star.configuration.ConfigurationProvider expects a single,"
70
0
         " non-empty, string nodepath argument"_ustr),
71
0
        nullptr);
72
0
}
73
74
typedef
75
    comphelper::WeakComponentImplHelper<
76
        css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
77
        css::util::XRefreshable, css::util::XFlushable,
78
        css::lang::XLocalizable >
79
    ServiceBase;
80
81
class Service : public ServiceBase
82
{
83
public:
84
    explicit Service(
85
        const css::uno::Reference< css::uno::XComponentContext >& context):
86
14
        context_(context), default_(true),
87
14
        lock_( lock() )
88
14
    {
89
14
        assert(context.is());
90
14
    }
91
92
    Service(
93
        const css::uno::Reference< css::uno::XComponentContext >& context,
94
        OUString locale):
95
0
        context_(context), locale_(std::move(locale)),
96
0
        default_(false),
97
0
        lock_( lock() )
98
0
    {
99
0
        assert(context.is());
100
0
    }
101
102
private:
103
    Service(const Service&) = delete;
104
    Service& operator=(const Service&) = delete;
105
106
0
    virtual ~Service() override {}
107
108
    virtual void disposing(std::unique_lock<std::mutex>& rGuard) override;
109
110
    virtual OUString SAL_CALL getImplementationName() override
111
0
    {
112
0
        return default_
113
0
            ? default_provider::getImplementationName()
114
0
            : u"com.sun.star.comp.configuration.ConfigurationProvider"_ustr;
115
0
    }
116
117
    virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
118
0
    { return cppu::supportsService(this, ServiceName); }
119
120
    virtual css::uno::Sequence< OUString > SAL_CALL
121
    getSupportedServiceNames() override
122
0
    {
123
0
        return default_
124
0
            ? default_provider::getSupportedServiceNames()
125
0
            : css::uno::Sequence<OUString> { u"com.sun.star.configuration.ConfigurationProvider"_ustr };
126
0
    }
127
128
    virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
129
        OUString const & aServiceSpecifier) override;
130
131
    virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
132
    createInstanceWithArguments(
133
        OUString const & ServiceSpecifier,
134
        css::uno::Sequence< css::uno::Any > const & Arguments) override;
135
136
    virtual css::uno::Sequence< OUString > SAL_CALL
137
    getAvailableServiceNames() override;
138
139
    virtual void SAL_CALL refresh() override;
140
141
    virtual void SAL_CALL addRefreshListener(
142
        css::uno::Reference< css::util::XRefreshListener > const & l) override;
143
144
    virtual void SAL_CALL removeRefreshListener(
145
        css::uno::Reference< css::util::XRefreshListener > const & l) override;
146
147
    virtual void SAL_CALL flush() override;
148
149
    virtual void SAL_CALL addFlushListener(
150
        css::uno::Reference< css::util::XFlushListener > const & l) override;
151
152
    virtual void SAL_CALL removeFlushListener(
153
        css::uno::Reference< css::util::XFlushListener > const & l) override;
154
155
    virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale) override;
156
157
    virtual css::lang::Locale SAL_CALL getLocale() override;
158
159
    void flushModifications() const;
160
161
    css::uno::Reference< css::uno::XComponentContext > context_;
162
    OUString locale_;
163
    bool default_;
164
    std::shared_ptr<osl::Mutex> lock_;
165
    comphelper::OInterfaceContainerHelper4<css::util::XRefreshListener> maRefreshListeners;
166
    comphelper::OInterfaceContainerHelper4<css::util::XFlushListener> maFlushListeners;
167
};
168
169
css::uno::Reference< css::uno::XInterface > Service::createInstance(
170
    OUString const & aServiceSpecifier)
171
0
{
172
0
    return createInstanceWithArguments(
173
0
        aServiceSpecifier, css::uno::Sequence< css::uno::Any >());
174
0
}
175
176
css::uno::Reference< css::uno::XInterface >
177
Service::createInstanceWithArguments(
178
    OUString const & ServiceSpecifier,
179
    css::uno::Sequence< css::uno::Any > const & Arguments)
180
48.1k
{
181
48.1k
    OUString nodepath;
182
48.1k
    OUString locale;
183
100k
    for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
184
52.3k
        css::beans::NamedValue v1;
185
52.3k
        css::beans::PropertyValue v2;
186
52.3k
        OUString name;
187
52.3k
        css::uno::Any value;
188
52.3k
        if (Arguments[i] >>= v1) {
189
0
            name = v1.Name;
190
0
            value = v1.Value;
191
52.3k
        } else if (Arguments[i] >>= v2) {
192
52.3k
            name = v2.Name;
193
52.3k
            value = v2.Value;
194
52.3k
        } else if (Arguments.getLength() == 1 && (Arguments[i] >>= nodepath)) {
195
            // For backwards compatibility, allow a single string argument that
196
            // denotes nodepath.
197
0
            if (nodepath.isEmpty()) {
198
0
                badNodePath();
199
0
            }
200
0
            break;
201
0
        } else {
202
0
            throw css::uno::Exception(
203
0
                (u"com.sun.star.configuration.ConfigurationProvider expects"
204
0
                 " NamedValue or PropertyValue arguments"_ustr),
205
0
                nullptr);
206
0
        }
207
        // For backwards compatibility, allow "nodepath" and "Locale" in any
208
        // case:
209
52.3k
        if (name.equalsIgnoreAsciiCase("nodepath")) {
210
48.1k
            if (!nodepath.isEmpty() || !(value >>= nodepath) ||
211
48.1k
                nodepath.isEmpty())
212
0
            {
213
0
                badNodePath();
214
0
            }
215
48.1k
        } else if (name.equalsIgnoreAsciiCase("locale")) {
216
0
            if (!locale.isEmpty() || !(value >>= locale) ||
217
0
                locale.isEmpty())
218
0
            {
219
0
                throw css::uno::Exception(
220
0
                    (u"com.sun.star.configuration.ConfigurationProvider expects"
221
0
                     " at most one, non-empty, string Locale argument"_ustr),
222
0
                    nullptr);
223
0
            }
224
0
        }
225
52.3k
    }
226
48.1k
    if (nodepath.isEmpty()) {
227
0
        badNodePath();
228
0
    }
229
    // For backwards compatibility, allow a nodepath that misses the leading
230
    // slash:
231
48.1k
    if (nodepath[0] != '/') {
232
11.3k
        nodepath = "/" + nodepath;
233
11.3k
    }
234
48.1k
    if (locale.isEmpty()) {
235
        //TODO: should the Access use the dynamically changing locale_ instead?
236
48.1k
        locale = locale_;
237
48.1k
        if (locale.isEmpty()) {
238
48.1k
            locale = "en-US";
239
48.1k
        }
240
48.1k
    }
241
48.1k
    bool update;
242
48.1k
    if (ServiceSpecifier == accessServiceName) {
243
47.5k
        update = false;
244
47.5k
    } else if (ServiceSpecifier == updateAccessServiceName) {
245
542
        update = true;
246
542
    } else {
247
0
        throw css::uno::Exception(
248
0
            ("com.sun.star.configuration.ConfigurationProvider does not support"
249
0
             " service " + ServiceSpecifier),
250
0
            getXWeak());
251
0
    }
252
48.1k
    osl::MutexGuard guard(*lock_);
253
48.1k
    Components & components = Components::getSingleton(context_);
254
48.1k
    rtl::Reference root(
255
48.1k
        new RootAccess(components, nodepath, locale, update));
256
48.1k
    if (root->isValue()) {
257
0
        throw css::uno::Exception(
258
0
            ("com.sun.star.configuration.ConfigurationProvider: there is a leaf"
259
0
             " value at nodepath " + nodepath),
260
0
            getXWeak());
261
0
    }
262
48.1k
    components.addRootAccess(root);
263
48.1k
    return root->getXWeak();
264
48.1k
}
265
266
css::uno::Sequence< OUString > Service::getAvailableServiceNames()
267
0
{
268
0
    return { accessServiceName, updateAccessServiceName };
269
0
}
270
271
0
void Service::refresh() {
272
    //TODO
273
0
    std::unique_lock g(m_aMutex);
274
0
    if (maRefreshListeners.getLength(g)) {
275
0
        css::lang::EventObject ev(getXWeak());
276
0
        maRefreshListeners.notifyEach(g, &css::util::XRefreshListener::refreshed, ev);
277
0
    }
278
0
}
279
280
void Service::addRefreshListener(
281
    css::uno::Reference< css::util::XRefreshListener > const & l)
282
0
{
283
0
    std::unique_lock g(m_aMutex);
284
0
    maRefreshListeners.addInterface(g, l);
285
0
}
286
287
void Service::removeRefreshListener(
288
    css::uno::Reference< css::util::XRefreshListener > const & l)
289
0
{
290
0
    std::unique_lock g(m_aMutex);
291
0
    maRefreshListeners.removeInterface(g, l);
292
0
}
293
294
0
void Service::flush() {
295
0
    flushModifications();
296
0
    std::unique_lock g(m_aMutex);
297
0
    if (maFlushListeners.getLength(g)) {
298
0
        css::lang::EventObject ev(getXWeak());
299
0
        maFlushListeners.notifyEach(g, &css::util::XFlushListener::flushed, ev);
300
0
    }
301
0
}
302
303
void Service::addFlushListener(
304
    css::uno::Reference< css::util::XFlushListener > const & l)
305
0
{
306
0
    std::unique_lock g(m_aMutex);
307
0
    maFlushListeners.addInterface(g, l);
308
0
}
309
310
void Service::removeFlushListener(
311
    css::uno::Reference< css::util::XFlushListener > const & l)
312
0
{
313
0
    std::unique_lock g(m_aMutex);
314
0
    maFlushListeners.removeInterface(g, l);
315
0
}
316
317
void Service::setLocale(css::lang::Locale const & eLocale)
318
0
{
319
0
    osl::MutexGuard guard(*lock_);
320
0
    locale_ = LanguageTag::convertToBcp47( eLocale, false);
321
0
}
322
323
48.9k
css::lang::Locale Service::getLocale() {
324
48.9k
    osl::MutexGuard guard(*lock_);
325
48.9k
    css::lang::Locale loc;
326
48.9k
    if (! locale_.isEmpty()) {
327
0
        loc = LanguageTag::convertToLocale( locale_, false);
328
0
    }
329
48.9k
    return loc;
330
48.9k
}
331
332
0
void Service::disposing(std::unique_lock<std::mutex>& rGuard) {
333
0
    rGuard.unlock(); // just in case we call back into Service during dispose()
334
0
    flushModifications();
335
0
    rGuard.lock();
336
0
}
337
338
0
void Service::flushModifications() const {
339
0
    Components * components;
340
0
    {
341
0
        osl::MutexGuard guard(*lock_);
342
0
        components = &Components::getSingleton(context_);
343
0
    }
344
0
    components->flushModifications();
345
0
}
346
347
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
348
com_sun_star_comp_configuration_ConfigurationProvider_get_implementation(
349
    css::uno::XComponentContext* Context, css::uno::Sequence<css::uno::Any> const& Arguments)
350
0
{
351
0
    if (!Arguments.hasElements()) {
352
0
        auto p = css::configuration::theDefaultProvider::get(Context);
353
0
        p->acquire();
354
0
        return p.get();
355
0
    } else {
356
0
        OUString locale;
357
0
        for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
358
0
            css::beans::NamedValue v1;
359
0
            css::beans::PropertyValue v2;
360
0
            OUString name;
361
0
            css::uno::Any value;
362
0
            if (Arguments[i] >>= v1) {
363
0
                name = v1.Name;
364
0
                value = v1.Value;
365
0
            } else if (Arguments[i] >>= v2) {
366
0
                name = v2.Name;
367
0
                value = v2.Value;
368
0
            } else {
369
0
                throw css::uno::Exception(
370
0
                    (u"com.sun.star.configuration.ConfigurationProvider factory"
371
0
                     " expects NamedValue or PropertyValue arguments"_ustr),
372
0
                    nullptr);
373
0
            }
374
            // For backwards compatibility, allow "Locale" and (ignored)
375
            // "EnableAsync" in any case:
376
0
            if (name.equalsIgnoreAsciiCase("locale")) {
377
0
                if (!locale.isEmpty() || !(value >>= locale) ||
378
0
                    locale.isEmpty())
379
0
                {
380
0
                    throw css::uno::Exception(
381
0
                        (u"com.sun.star.configuration.ConfigurationProvider"
382
0
                         " factory expects at most one, non-empty, string"
383
0
                         " Locale argument"_ustr),
384
0
                        nullptr);
385
0
                }
386
0
            } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
387
0
                throw css::uno::Exception(
388
0
                    ("com.sun.star.configuration.ConfigurationProvider factory:"
389
0
                     " unknown argument " + name),
390
0
                    nullptr);
391
0
            }
392
0
        }
393
0
        return cppu::acquire(new Service(Context, locale));
394
0
    }
395
0
}
396
397
}
398
399
css::uno::Reference< css::uno::XInterface > createDefault(
400
    css::uno::Reference< css::uno::XComponentContext > const & context)
401
14
{
402
14
    return getXWeak(new Service(context));
403
14
}
404
405
}
406
407
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */