1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/instance.hxx>
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
23 #include <uno/dispatcher.hxx>
24 #include <uno/lbnames.h>
25 #include <uno/mapping.hxx>
26 #include <cppuhelper/detail/XExceptionThrower.hpp>
27 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
28 #include <com/sun/star/ucb/NameClashException.hpp>
29 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <cppuhelper/exc_hlp.hxx>
33 using namespace ::osl
;
34 using namespace ::cppu
;
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
41 using cppuhelper::detail::XExceptionThrower
;
44 struct ExceptionThrower
: public uno_Interface
, XExceptionThrower
48 virtual ~ExceptionThrower() {}
50 static Type
const & getCppuType()
52 return cppu::UnoType
<XExceptionThrower
>::get();
56 virtual Any SAL_CALL
queryInterface( Type
const & type
) override
;
57 virtual void SAL_CALL
acquire() throw () override
;
58 virtual void SAL_CALL
release() throw () override
;
61 virtual void SAL_CALL
throwException( Any
const & exc
) override
;
62 virtual void SAL_CALL
rethrowException() override
;
69 void ExceptionThrower_acquire_release_nop(
70 SAL_UNUSED_PARAMETER uno_Interface
* )
74 void ExceptionThrower_dispatch(
75 uno_Interface
* pUnoI
, typelib_TypeDescription
const * pMemberType
,
76 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
78 OSL_ASSERT( pMemberType
->eTypeClass
== typelib_TypeClass_INTERFACE_METHOD
);
80 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription
* >(
81 const_cast< typelib_TypeDescription
* >( pMemberType
) )->
84 case 0: // queryInterface()
86 Type
const & rType_demanded
=
87 *static_cast< Type
const * >( pArgs
[ 0 ] );
88 if (rType_demanded
.equals( cppu::UnoType
<XInterface
>::get() ) ||
89 rType_demanded
.equals( ExceptionThrower::getCppuType() ))
91 typelib_TypeDescription
* pTD
= nullptr;
92 TYPELIB_DANGER_GET( &pTD
, rType_demanded
.getTypeLibType() );
94 static_cast< uno_Any
* >( pReturn
), &pUnoI
, pTD
, nullptr );
95 TYPELIB_DANGER_RELEASE( pTD
);
100 static_cast< uno_Any
* >( pReturn
), nullptr, nullptr, nullptr );
102 *ppException
= nullptr;
107 *ppException
= nullptr;
109 case 3: // throwException()
111 uno_Any
* pAny
= static_cast< uno_Any
* >( pArgs
[ 0 ] );
112 OSL_ASSERT( pAny
->pType
->eTypeClass
== typelib_TypeClass_EXCEPTION
);
113 uno_type_any_construct( *ppException
, pAny
->pData
, pAny
->pType
, nullptr );
119 RuntimeException
exc( "not implemented!" );
120 uno_type_any_construct(
121 *ppException
, &exc
, cppu::UnoType
<decltype(exc
)>::get().getTypeLibType(), nullptr );
130 Any
ExceptionThrower::queryInterface( Type
const & type
)
132 if (type
.equals( cppu::UnoType
<XInterface
>::get() ) ||
133 type
.equals( ExceptionThrower::getCppuType() ))
135 XExceptionThrower
* that
= this;
136 return Any( &that
, type
);
142 void ExceptionThrower::acquire() throw ()
146 void ExceptionThrower::release() throw ()
151 void ExceptionThrower::throwException( Any
const & exc
)
153 OSL_FAIL( "unexpected!" );
154 cppu::throwException( exc
);
158 void ExceptionThrower::rethrowException()
164 ExceptionThrower::ExceptionThrower()
166 uno_Interface::acquire
= ExceptionThrower_acquire_release_nop
;
167 uno_Interface::release
= ExceptionThrower_acquire_release_nop
;
168 uno_Interface::pDispatcher
= ExceptionThrower_dispatch
;
171 class theExceptionThrower
: public rtl::Static
<ExceptionThrower
, theExceptionThrower
> {};
173 #if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
174 // In the native iOS / Android app, where we don't have any Java, Python,
175 // BASIC, or other scripting, the only thing that would use the C++/UNO bridge
176 // functionality that invokes codeSnippet() was cppu::throwException().
178 // codeSnippet() is part of what corresponds to the code that uses
179 // run-time-generated machine code on other platforms. We can't generate code
180 // at run-time on iOS, that has been known forever.
182 // Instead of digging in and trying to understand what is wrong, another
183 // solution was chosen. It turns out that the number of types of exception
184 // objects thrown by cppu::throwException() is fairly small. During startup of
185 // the LibreOffice code, and loading of an .odt document, only one kind of
186 // exception is thrown this way... (The lovely
187 // css::ucb:InteractiveAugmentedIOException.)
189 // So we can simply have code that checks what the type of object being thrown
190 // is, and explicitly throws such an object then with a normal C++ throw
191 // statement. Seems to work.
192 template <class E
> void tryThrow(css::uno::Any
const& aException
)
194 E aSpecificException
;
195 if (aException
>>= aSpecificException
)
196 throw aSpecificException
;
199 void lo_mobile_throwException(css::uno::Any
const& aException
)
201 assert(aException
.getValueTypeClass() == css::uno::TypeClass_EXCEPTION
);
203 tryThrow
<css::ucb::InteractiveAugmentedIOException
>(aException
);
204 tryThrow
<css::ucb::NameClashException
>(aException
);
205 tryThrow
<css::uno::RuntimeException
>(aException
);
207 SAL_WARN("cppuhelper", "lo_mobile_throwException: Unhandled exception type: " << aException
.getValueTypeName());
211 #endif // defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
213 } // anonymous namespace
220 void SAL_CALL
throwException( Any
const & exc
)
222 if (exc
.getValueTypeClass() != TypeClass_EXCEPTION
)
224 throw RuntimeException(
225 "no UNO exception given "
226 "(must be derived from com::sun::star::uno::Exception)!" );
229 #if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
230 lo_mobile_throwException(exc
);
232 Mapping
uno2cpp(Environment(UNO_LB_UNO
), Environment::getCurrent());
235 throw RuntimeException(
236 "cannot get binary UNO to C++ mapping!" );
239 Reference
< XExceptionThrower
> xThrower
;
240 uno2cpp
.mapInterface(
241 reinterpret_cast< void ** >( &xThrower
),
242 static_cast< uno_Interface
* >( &theExceptionThrower::get() ),
243 ExceptionThrower::getCppuType() );
244 OSL_ASSERT( xThrower
.is() );
245 xThrower
->throwException( exc
);
250 Any SAL_CALL
getCaughtException()
252 #if defined(__aarch64__) && defined(ANDROID)
253 // FIXME This stuff works on 32bit ARM, let's use the shortcut only for
257 Mapping
cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO
));
260 throw RuntimeException(
261 "cannot get C++ to binary UNO mapping!" );
263 Mapping
uno2cpp(Environment(UNO_LB_UNO
), Environment::getCurrent());
266 throw RuntimeException(
267 "cannot get binary UNO to C++ mapping!" );
270 typelib_TypeDescription
* pTD
= nullptr;
272 &pTD
, ExceptionThrower::getCppuType().getTypeLibType() );
274 UnoInterfaceReference unoI
;
275 cpp2uno
.mapInterface(
276 reinterpret_cast< void ** >( &unoI
.m_pUnoI
),
277 static_cast< XExceptionThrower
* >( &theExceptionThrower::get() ), pTD
);
278 OSL_ASSERT( unoI
.is() );
280 typelib_TypeDescription
* pMemberTD
= nullptr;
283 reinterpret_cast< typelib_InterfaceTypeDescription
* >( pTD
)->
284 ppMembers
[ 1 ] /* rethrowException() */ );
287 uno_Any
* exc
= &exc_mem
;
288 unoI
.dispatch( pMemberTD
, nullptr, nullptr, &exc
);
290 TYPELIB_DANGER_RELEASE( pMemberTD
);
291 TYPELIB_DANGER_RELEASE( pTD
);
295 throw RuntimeException( "rethrowing C++ exception failed!" );
299 uno_any_destruct( &ret
, reinterpret_cast< uno_ReleaseFunc
>(cpp_release
) );
300 uno_type_any_constructAndConvert(
301 &ret
, exc
->pData
, exc
->pType
, uno2cpp
.get() );
302 uno_any_destruct( exc
, nullptr );
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */