Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/services/dispatchhelper.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 <framework/dispatchhelper.hxx>
21
22
#include <com/sun/star/frame/XNotifyingDispatch.hpp>
23
#include <com/sun/star/util/URLTransformer.hpp>
24
#include <com/sun/star/util/XURLTransformer.hpp>
25
26
#include <comphelper/profilezone.hxx>
27
#include <comphelper/sequenceashashmap.hxx>
28
#include <utility>
29
#include <vcl/threadex.hxx>
30
#include <cppuhelper/supportsservice.hxx>
31
32
namespace framework
33
{
34
// XInterface, XTypeProvider, XServiceInfo
35
36
OUString SAL_CALL DispatchHelper::getImplementationName()
37
0
{
38
0
    return u"com.sun.star.comp.framework.services.DispatchHelper"_ustr;
39
0
}
40
41
sal_Bool SAL_CALL DispatchHelper::supportsService(const OUString& sServiceName)
42
0
{
43
0
    return cppu::supportsService(this, sServiceName);
44
0
}
45
46
css::uno::Sequence<OUString> SAL_CALL DispatchHelper::getSupportedServiceNames()
47
0
{
48
0
    return { u"com.sun.star.frame.DispatchHelper"_ustr };
49
0
}
50
51
/** ctor.
52
53
    @param xSMGR    the global uno service manager, which can be used to create own needed services.
54
*/
55
DispatchHelper::DispatchHelper(css::uno::Reference<css::uno::XComponentContext> xContext)
56
0
    : m_xContext(std::move(xContext))
57
0
    , m_aBlockFlag(false)
58
0
{
59
0
}
60
61
/** dtor.
62
*/
63
0
DispatchHelper::~DispatchHelper() {}
64
65
/** capsulate all steps of a dispatch request and provide so an easy way for dispatches.
66
67
    @param xDispatchProvider
68
                identifies the object, which provides may be valid dispatch objects for this execute.
69
70
    @param sURL
71
                describes the requested feature.
72
73
    @param sTargetFrameName
74
                points to the frame, which must be used (or may be created) for this dispatch.
75
76
    @param nSearchFlags
77
                in case the <var>sTargetFrameName</var> isn't unique, these flags regulate further searches.
78
79
    @param lArguments
80
                optional arguments for this request.
81
82
    @return An Any which capsulate a possible result of the internal wrapped dispatch.
83
 */
84
css::uno::Any SAL_CALL DispatchHelper::executeDispatch(
85
    const css::uno::Reference<css::frame::XDispatchProvider>& xDispatchProvider,
86
    const OUString& sURL, const OUString& sTargetFrameName, sal_Int32 nSearchFlags,
87
    const css::uno::Sequence<css::beans::PropertyValue>& lArguments)
88
0
{
89
    // check for valid parameters
90
0
    if ((!xDispatchProvider.is()) || (!m_xContext.is()) || (sURL.isEmpty()))
91
0
    {
92
0
        return css::uno::Any();
93
0
    }
94
95
    // parse given URL
96
0
    css::uno::Reference<css::util::XURLTransformer> xParser;
97
    /* SAFE { */
98
0
    {
99
0
        std::scoped_lock aReadLock(m_mutex);
100
0
        xParser = css::util::URLTransformer::create(m_xContext);
101
0
    }
102
    /* } SAFE */
103
104
0
    css::util::URL aURL;
105
0
    aURL.Complete = sURL;
106
0
    xParser->parseStrict(aURL);
107
108
    // search dispatcher
109
0
    css::uno::Reference<css::frame::XDispatch> xDispatch
110
0
        = xDispatchProvider->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
111
112
0
    comphelper::SequenceAsHashMap aDescriptor(lArguments);
113
0
    bool bOnMainThread = aDescriptor.getUnpackedValueOrDefault(u"OnMainThread"_ustr, false);
114
115
0
    if (bOnMainThread)
116
0
        return vcl::solarthread::syncExecute(
117
0
            [this, &xDispatch, &aURL, &lArguments]() -> css::uno::Any {
118
0
                return executeDispatch(xDispatch, aURL, true, lArguments);
119
0
            });
120
0
    else
121
0
        return executeDispatch(xDispatch, aURL, true, lArguments);
122
0
}
123
124
const css::uno::Any&
125
DispatchHelper::executeDispatch(const css::uno::Reference<css::frame::XDispatch>& xDispatch,
126
                                const css::util::URL& aURL, bool SyncronFlag,
127
                                const css::uno::Sequence<css::beans::PropertyValue>& lArguments)
128
0
{
129
0
    comphelper::ProfileZone aZone("executeDispatch");
130
0
    css::uno::Reference<css::uno::XInterface> xTHIS(static_cast<::cppu::OWeakObject*>(this),
131
0
                                                    css::uno::UNO_QUERY);
132
0
    m_aResult.clear();
133
134
    // check for valid parameters
135
0
    if (xDispatch.is())
136
0
    {
137
0
        css::uno::Reference<css::frame::XNotifyingDispatch> xNotifyDispatch(xDispatch,
138
0
                                                                            css::uno::UNO_QUERY);
139
140
        // make sure that synchronous execution is used (if possible)
141
0
        css::uno::Sequence<css::beans::PropertyValue> aArguments(lArguments);
142
0
        sal_Int32 nLength = lArguments.getLength();
143
0
        aArguments.realloc(nLength + 1);
144
0
        auto pArguments = aArguments.getArray();
145
0
        pArguments[nLength].Name = "SynchronMode";
146
0
        pArguments[nLength].Value <<= SyncronFlag;
147
148
0
        if (xNotifyDispatch.is())
149
0
        {
150
            // dispatch it with guaranteed notification
151
            // Here we can hope for a result ... instead of the normal dispatch.
152
0
            css::uno::Reference<css::frame::XDispatchResultListener> xListener(xTHIS,
153
0
                                                                               css::uno::UNO_QUERY);
154
            /* SAFE { */
155
0
            {
156
0
                std::scoped_lock aWriteLock(m_mutex);
157
0
                m_xBroadcaster = xNotifyDispatch;
158
0
                m_aBlockFlag = false;
159
0
            }
160
            /* } SAFE */
161
162
            // dispatch it and wait for a notification
163
            // TODO/MBA: waiting in main thread?!
164
0
            xNotifyDispatch->dispatchWithNotification(aURL, aArguments, xListener);
165
166
0
            std::unique_lock aWriteLock(m_mutex);
167
0
            m_aBlock.wait(aWriteLock, [this] { return m_aBlockFlag; }); // wait for result
168
0
        }
169
0
        else
170
0
        {
171
            // dispatch it without any chance to get a result
172
0
            xDispatch->dispatch(aURL, aArguments);
173
0
        }
174
0
    }
175
176
0
    return m_aResult;
177
0
}
178
179
/** callback for started dispatch with guaranteed notifications.
180
181
    We must save the result, so the method executeDispatch() can return it.
182
    Further we must release the broadcaster (otherwise it can't die)
183
    and unblock the waiting executeDispatch() request.
184
185
    @param  aResult
186
                describes the result of the dispatch operation
187
 */
188
void SAL_CALL DispatchHelper::dispatchFinished(const css::frame::DispatchResultEvent& aResult)
189
0
{
190
0
    std::scoped_lock g(m_mutex);
191
0
    m_aResult <<= aResult;
192
0
    m_aBlockFlag = true;
193
0
    m_aBlock.notify_one();
194
0
    m_xBroadcaster.clear();
195
0
}
196
197
/** we have to release our broadcaster reference.
198
199
    @param aEvent
200
                describe the source of this event and MUST be our save broadcaster!
201
 */
202
void SAL_CALL DispatchHelper::disposing(const css::lang::EventObject&)
203
0
{
204
0
    std::scoped_lock g(m_mutex);
205
0
    m_aResult.clear();
206
0
    m_aBlockFlag = true;
207
0
    m_aBlock.notify_one();
208
0
    m_xBroadcaster.clear();
209
0
}
210
}
211
212
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
213
framework_DispatchHelper_get_implementation(css::uno::XComponentContext* context,
214
                                            css::uno::Sequence<css::uno::Any> const&)
215
0
{
216
0
    return cppu::acquire(new framework::DispatchHelper(context));
217
0
}
218
219
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */