Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/appl/openuriexternally.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
10
#include <sal/config.h>
11
12
#include <com/sun/star/lang/IllegalArgumentException.hpp>
13
#include <com/sun/star/security/AccessControlException.hpp>
14
#include <com/sun/star/system/SystemShellExecute.hpp>
15
#include <com/sun/star/system/SystemShellExecuteException.hpp>
16
#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
17
#include <com/sun/star/uno/Reference.hxx>
18
#include <com/sun/star/uno/RuntimeException.hpp>
19
#include <comphelper/processfactory.hxx>
20
#include <rtl/ustring.hxx>
21
#include <sfx2/sfxresid.hxx>
22
#include <tools/urlobj.hxx>
23
#include <vcl/svapp.hxx>
24
#include <vcl/timer.hxx>
25
#include <vcl/vclenum.hxx>
26
#include <vcl/weld/MessageDialog.hxx>
27
#include <vcl/weld/weld.hxx>
28
#include <openuriexternally.hxx>
29
#include <comphelper/lok.hxx>
30
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
31
32
#include <sfx2/viewsh.hxx>
33
#include <sfx2/strings.hrc>
34
35
namespace {
36
37
class URITools
38
{
39
private:
40
    Timer aOpenURITimer { "sfx2::openUriExternallyTimer" };
41
    OUString msURI;
42
    weld::Widget* mpDialogParent;
43
    bool mbHandleSystemShellExecuteException;
44
    DECL_LINK(onOpenURI, Timer*, void);
45
46
public:
47
    URITools(weld::Widget* pDialogParent)
48
0
        : mpDialogParent(pDialogParent)
49
0
        , mbHandleSystemShellExecuteException(false)
50
0
    {
51
0
    }
52
    void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
53
};
54
55
}
56
57
void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
58
0
{
59
0
    if (comphelper::LibreOfficeKit::isActive())
60
0
    {
61
0
        if (SfxViewShell* pViewShell = SfxViewShell::Current())
62
0
        {
63
0
            pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
64
0
                                                   sURI.toUtf8());
65
0
        }
66
0
        delete this;
67
0
        return;
68
0
    }
69
70
0
    mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
71
0
    msURI = sURI;
72
73
    // tdf#116305 Workaround: Use timer to bring browsers to the front
74
0
    aOpenURITimer.SetInvokeHandler(LINK(this, URITools, onOpenURI));
75
#ifdef _WIN32
76
    // 200ms seems to be the best compromise between responsiveness and success rate
77
    aOpenURITimer.SetTimeout(200);
78
#else
79
0
    aOpenURITimer.SetTimeout(0);
80
0
#endif
81
0
    aOpenURITimer.Start();
82
0
}
83
84
IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
85
0
{
86
0
    std::unique_ptr<URITools> guard(this);
87
0
    css::uno::Reference< css::system::XSystemShellExecute > exec(
88
0
        css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
89
0
    for (sal_Int32 flags = css::system::SystemShellExecuteFlags::URIS_ONLY;;) {
90
0
        try {
91
0
            exec->execute(msURI, OUString(), flags);
92
0
        } catch (css::security::AccessControlException & e) {
93
0
            if (e.LackingPermission.hasValue() || flags == 0) {
94
0
                throw css::uno::RuntimeException(
95
0
                    "unexpected AccessControlException: " + e.Message);
96
0
            }
97
0
            SolarMutexGuard g;
98
0
            std::unique_ptr<weld::MessageDialog> eb(
99
0
                Application::CreateMessageDialog(
100
0
                    mpDialogParent, VclMessageType::Warning, VclButtonsType::YesNo,
101
0
                    SfxResId(STR_DANGEROUS_TO_OPEN)));
102
0
            eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
103
0
            eb->set_default_response(RET_NO);
104
0
            if (eb->run() == RET_YES) {
105
0
                flags = 0;
106
0
                continue;
107
0
            }
108
0
        } catch (css::lang::IllegalArgumentException & e) {
109
0
            if (e.ArgumentPosition != 0) {
110
0
                throw css::uno::RuntimeException(
111
0
                    "unexpected IllegalArgumentException: " + e.Message);
112
0
            }
113
0
            SolarMutexGuard g;
114
0
            std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
115
0
                                                                     VclMessageType::Warning, VclButtonsType::Ok,
116
0
                                                                     SfxResId(STR_NO_ABS_URI_REF)));
117
0
            eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
118
0
            eb->run();
119
0
        } catch (css::system::SystemShellExecuteException & e) {
120
0
            if (!mbHandleSystemShellExecuteException) {
121
0
                throw;
122
0
            }
123
0
            SolarMutexGuard g;
124
0
            std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
125
0
                                                                     VclMessageType::Warning, VclButtonsType::Ok,
126
0
                                                                     SfxResId(STR_NO_WEBBROWSER_FOUND)));
127
0
            eb->set_primary_text(
128
0
                eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
129
0
                .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
130
0
                .replaceFirst("$(ARG3)", e.Message));
131
                //TODO: avoid subsequent replaceFirst acting on previous replacement
132
0
            eb->run();
133
0
        }
134
0
        break;
135
0
    }
136
0
}
137
138
void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException, weld::Widget* pDialogParent)
139
0
{
140
0
    URITools* uriTools = new URITools(pDialogParent);
141
0
    uriTools->openURI(sURI, bHandleSystemShellExecuteException);
142
0
} //-V773
143
144
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */