Coverage Report

Created: 2026-10-14 09:33

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