LibreOffice Module toolkit (master) 1
asynccallback.cxx
Go to the documentation of this file.
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 <utility>
23#include <vcl/svapp.hxx>
26#include <com/sun/star/lang/XServiceInfo.hpp>
27#include <com/sun/star/uno/XComponentContext.hpp>
28#include <com/sun/star/awt/XRequestCallback.hpp>
29
31namespace {
32
33class AsyncCallback:
34 public ::cppu::WeakImplHelper<
35 css::lang::XServiceInfo,
36 css::awt::XRequestCallback>
37{
38public:
39 AsyncCallback() {}
40 AsyncCallback(const AsyncCallback&) = delete;
41 AsyncCallback& operator=(const AsyncCallback&) = delete;
42
43 // css::lang::XServiceInfo:
44 virtual OUString SAL_CALL getImplementationName() override;
45 virtual sal_Bool SAL_CALL supportsService(const OUString & ServiceName) override;
46 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
47
48 // css::awt::XRequestCallback:
49 virtual void SAL_CALL addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const css::uno::Any & aData) override;
50
51private:
52
53 struct CallbackData
54 {
55 CallbackData( css::uno::Reference< css::awt::XCallback > _xCallback, css::uno::Any aAny ) :
56 xCallback(std::move( _xCallback )), aData(std::move( aAny )) {}
57
58 css::uno::Reference< css::awt::XCallback > xCallback;
59 css::uno::Any aData;
60 };
61
62 DECL_STATIC_LINK( AsyncCallback, Notify_Impl, void*, void );
63
64 virtual ~AsyncCallback() override {}
65};
66
67// com.sun.star.uno.XServiceInfo:
68OUString SAL_CALL AsyncCallback::getImplementationName()
69{
70 return "com.sun.star.awt.comp.AsyncCallback";
71}
72
73sal_Bool SAL_CALL AsyncCallback::supportsService(OUString const & serviceName)
74{
75 return cppu::supportsService(this, serviceName);
76}
77
78css::uno::Sequence< OUString > SAL_CALL AsyncCallback::getSupportedServiceNames()
79{
80 return { "com.sun.star.awt.AsyncCallback" };
81}
82
83// css::awt::XRequestCallback:
84void SAL_CALL AsyncCallback::addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const css::uno::Any & aData)
85{
87 {
88 // NOTE: We don't need SolarMutexGuard here as Application::PostUserEvent is thread-safe
89 CallbackData* pCallbackData = new CallbackData( xCallback, aData );
90 Application::PostUserEvent( LINK( this, AsyncCallback, Notify_Impl ), pCallbackData );
91 }
92}
93
94// private asynchronous link to call reference to the callback object
95IMPL_STATIC_LINK( AsyncCallback, Notify_Impl, void*, p, void )
96{
97 CallbackData* pCallbackData = static_cast<CallbackData*>(p);
98 try
99 {
100 // Asynchronous execution
101 // Check pointer and reference before!
102 if ( pCallbackData && pCallbackData->xCallback.is() )
103 pCallbackData->xCallback->notify( pCallbackData->aData );
104 }
105 catch ( css::uno::Exception& )
106 {
107 }
108
109 delete pCallbackData;
110}
111
112} // closing anonymous implementation namespace
113
114extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
116 css::uno::XComponentContext *,
117 css::uno::Sequence<css::uno::Any> const &)
118{
119 return cppu::acquire(new AsyncCallback());
120}
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_awt_comp_AsyncCallback_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static bool IsInMain()
void * p
constexpr OUStringLiteral aData
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
IMPL_STATIC_LINK(SwRetrievedInputStreamDataManager, LinkedInputStreamReady, void *, p, void)
unsigned char sal_Bool