cool#7853 sc lok: fix bad view id on hyperlink click

The document had 2 windows. The first window was typing in column A, the
second window ctrl-clicked on a hyperlink in column B. The LOK callback
was emitted in window 1, not in window 2, which is unexpected.

What happens is that ScGlobal::OpenURL() dispatched an async command
when window 2 was active, and we happened to be in window 1 when
processing that user event from the main loop.

Fix the problem by dispatching the URL open command in a synchronous
way, so the LOK view can't be different.

An alternative would be to track the current LOK view id for posted user
events, and set the view back to the one which was current at post-time
before executing the event. We can consider doing that in a follow-up
change, but this fixes the problem at hand and is a safe fix.

Change-Id: I9a3c9fc1b90ad538d8b2510c7844549c9881ca56
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160976
Reviewed-by: Miklos Vajna <[email protected]>
Tested-by: Jenkins
diff --git a/sc/inc/global.hxx b/sc/inc/global.hxx
index 089611e..e140a7f 100644
--- a/sc/inc/global.hxx
+++ b/sc/inc/global.hxx
@@ -575,7 +575,7 @@ public:
     * Open the specified URL.
     * @param bIgnoreSettings - If true, ignore security settings (Ctrl-Click) and just open the URL.
     */
    static void OpenURL(const OUString& rURL, const OUString& rTarget, bool bIgnoreSettings = false);
    SC_DLLPUBLIC static void OpenURL(const OUString& rURL, const OUString& rTarget, bool bIgnoreSettings = false);
    /// Whether the URL can be opened according to current security options (Click/Ctrl-Click)
    static bool                 ShouldOpenURL();
    SC_DLLPUBLIC static OUString            GetAbsDocName( const OUString& rFileName,
diff --git a/sc/qa/unit/tiledrendering/tiledrendering.cxx b/sc/qa/unit/tiledrendering/tiledrendering.cxx
index 2795083..d72cb64 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering.cxx
@@ -435,6 +435,7 @@ public:
    TextSelectionMessage m_aTextSelectionResult;
    OString m_sInvalidateHeader;
    OString m_sInvalidateSheetGeometry;
    OString m_aHyperlinkClicked;
    OString m_ShapeSelection;
    TestLokCallbackWrapper m_callbackWrapper;

@@ -571,6 +572,11 @@ public:
            m_aInvalidateCursorResult.parseMessage(pPayload);
        }
        break;
        case LOK_CALLBACK_HYPERLINK_CLICKED:
        {
            m_aHyperlinkClicked = pPayload;
        }
        break;
        case LOK_CALLBACK_TEXT_SELECTION:
        {
            m_aTextSelectionResult.parseMessage(pPayload);
@@ -3113,6 +3119,28 @@ CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testGetViewRenderState)
    CPPUNIT_ASSERT_EQUAL(";Default"_ostr, pModelObj->getViewRenderState());
}

CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testOpenURL)
{
    // Given a document that has 2 views:
    createDoc("empty.ods");
    int nView1 = SfxLokHelper::getView();
    ViewCallback aView1;
    SfxLokHelper::createView();
    ViewCallback aView2;

    // When clicking on a link in view 2, but switching to view 1 before processing async events:
    ScGlobal::OpenURL(/*aUrl=*/u"http://www.example.com/"_ustr, /*aTarget=*/u""_ustr,
                      /*bIgnoreSettings=*/true);
    SfxLokHelper::setView(nView1);
    Scheduler::ProcessEventsToIdle();

    // Then make sure view 2 gets the callback, not view 1:
    // Without the accompanying fix in place, this test would have failed, view 1 got the hyperlink
    // callback.
    CPPUNIT_ASSERT(aView1.m_aHyperlinkClicked.isEmpty());
    CPPUNIT_ASSERT(!aView2.m_aHyperlinkClicked.isEmpty());
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/global.cxx b/sc/source/core/data/global.cxx
index 29a616c..15fdc7d 100644
--- a/sc/source/core/data/global.cxx
+++ b/sc/source/core/data/global.cxx
@@ -879,8 +879,17 @@ void ScGlobal::OpenURL(const OUString& rURL, const OUString& rTarget, bool bIgno
    SfxBoolItem aBrowsing( SID_BROWSE, true );

    // No SID_SILENT anymore
    SfxCallMode nCall = SfxCallMode::RECORD;
    if (comphelper::LibreOfficeKit::isActive())
    {
        nCall |= SfxCallMode::SYNCHRON;
    }
    else
    {
        nCall |= SfxCallMode::ASYNCHRON;
    }
    pViewFrm->GetDispatcher()->ExecuteList(SID_OPENDOC,
            SfxCallMode::ASYNCHRON | SfxCallMode::RECORD,
            nCall,
            { &aUrl, &aTarget, &aFrm, &aReferer, &aNewView, &aBrowsing });
}