bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / ole / oleobjw.hxx
blob96011012477f8e528885842e28a3657d7c5d493f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_EXTENSIONS_SOURCE_OLE_OLEOBJW_HXX
21 #define INCLUDED_EXTENSIONS_SOURCE_OLE_OLEOBJW_HXX
23 #include "ole2uno.hxx"
24 #include "wincrap.hxx"
26 #include <unordered_map>
27 #include <vector>
29 #include <cppuhelper/implbase3.hxx>
30 #include <cppuhelper/implbase4.hxx>
31 #include <cppuhelper/implbase7.hxx>
33 #include <com/sun/star/lang/XInitialization.hpp>
34 #include <com/sun/star/bridge/oleautomation/XAutomationObject.hpp>
35 #include <com/sun/star/script//XAutomationInvocation.hpp>
36 #include <rtl/ustring.hxx>
38 #include <com/sun/star/script/XDefaultProperty.hpp>
39 #include <com/sun/star/script/XDefaultMethod.hpp>
40 #include <com/sun/star/script/XDirectInvocation.hpp>
42 #include <typelib/typedescription.hxx>
43 #include "unoconversionutilities.hxx"
44 #include "windata.hxx"
45 using namespace cppu;
46 using namespace std;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::bridge;
49 using namespace com::sun::star::bridge::oleautomation;
51 namespace ole_adapter
56 typedef std::unordered_map<OUString, pair<DISPID, unsigned short>, OUStringHash> DispIdMap;
58 typedef std::unordered_multimap<OUString, unsigned int, OUStringHash> TLBFuncIndexMap;
60 // This class wraps an IDispatch and maps XInvocation calls to IDispatch calls on the wrapped object.
61 // If m_TypeDescription is set then this class represents an UNO interface implemented in a COM component.
62 // The interface is not a real interface in terms of an abstract class but is realized through IDispatch.
63 class IUnknownWrapper_Impl : public WeakImplHelper7< XBridgeSupplier2, XInitialization, XAutomationObject, XDefaultProperty, XDefaultMethod, XDirectInvocation, XAutomationInvocation >,
65 public UnoConversionUtilities<IUnknownWrapper_Impl>
68 public:
69 IUnknownWrapper_Impl(Reference<XMultiServiceFactory> &xFactory,
70 sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass);
72 ~IUnknownWrapper_Impl();
74 //XInterface
75 Any SAL_CALL queryInterface(const Type& t)
76 throw (RuntimeException);
78 // XInvokation
79 virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection( )
80 throw(RuntimeException);
81 virtual Any SAL_CALL invoke( const OUString& aFunctionName,
82 const Sequence< Any >& aParams,
83 Sequence< sal_Int16 >& aOutParamIndex,
84 Sequence< Any >& aOutParam )
85 throw(IllegalArgumentException, CannotConvertException,
86 InvocationTargetException, RuntimeException);
87 virtual void SAL_CALL setValue( const OUString& aPropertyName,
88 const Any& aValue )
89 throw(UnknownPropertyException, CannotConvertException,
90 InvocationTargetException, RuntimeException);
91 virtual Any SAL_CALL getValue( const OUString& aPropertyName )
92 throw(UnknownPropertyException, RuntimeException);
93 virtual sal_Bool SAL_CALL hasMethod( const OUString& aName )
94 throw(RuntimeException);
95 virtual sal_Bool SAL_CALL hasProperty( const OUString& aName )
96 throw(RuntimeException);
98 // XBridgeSupplier2
99 // This interface is implemented to provide a safe way to obtain the original
100 // IUnknown or IDispatch within the function anyToVariant. The function asks
101 // every UNO object for its XBridgeSupplier2 and if it is available uses it to convert
102 // the object with its own supplier.
103 virtual Any SAL_CALL createBridge( const Any& modelDepObject,
104 const Sequence< sal_Int8 >& aProcessId,
105 sal_Int16 sourceModelType,
106 sal_Int16 destModelType )
107 throw(IllegalArgumentException, RuntimeException);
109 // XInitialization
110 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
111 throw(Exception, RuntimeException);
113 // XDefaultProperty
114 virtual OUString SAL_CALL getDefaultPropertyName( ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
116 // XDefaultMethod
117 virtual OUString SAL_CALL getDefaultMethodName( ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
119 virtual ::com::sun::star::uno::Any SAL_CALL invokeGetProperty( const OUString& aFunctionName, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams, ::com::sun::star::uno::Sequence< ::sal_Int16 >& aOutParamIndex, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::script::CannotConvertException, ::com::sun::star::reflection::InvocationTargetException, ::com::sun::star::uno::RuntimeException);
120 virtual ::com::sun::star::uno::Any SAL_CALL invokePutProperty( const OUString& aFunctionName, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams, ::com::sun::star::uno::Sequence< ::sal_Int16 >& aOutParamIndex, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aOutParam ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::script::CannotConvertException, ::com::sun::star::reflection::InvocationTargetException, ::com::sun::star::uno::RuntimeException);
122 // XDirectInvocation
123 virtual ::com::sun::star::uno::Any SAL_CALL directInvoke( const OUString& aName, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::script::CannotConvertException, ::com::sun::star::reflection::InvocationTargetException, ::com::sun::star::uno::RuntimeException);
124 virtual sal_Bool SAL_CALL hasMember( const OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
127 Any invokeWithDispIdComTlb(FuncDesc& aFuncDesc,
128 const OUString& sFuncName,
129 const Sequence< Any >& Params,
130 Sequence< sal_Int16 >& OutParamIndex,
131 Sequence< Any >& OutParam);
134 protected:
136 virtual Any invokeWithDispIdUnoTlb(const OUString& sFunctionName,
137 const Sequence< Any >& Params,
138 Sequence<sal_Int16 >& OutParamIndex,
139 Sequence< Any >& OutParam);
140 // Is used for OleObjectFactory service
141 virtual Any invokeWithDispIdComTlb(const OUString& sFuncName,
142 const Sequence< Any >& Params,
143 Sequence< sal_Int16 >& OutParamIndex,
144 Sequence< Any >& OutParam);
146 // UnoConversionUtilities -------------------------------------------------------------------------------
147 virtual Reference<XInterface> createUnoWrapperInstance();
148 virtual Reference<XInterface> createComWrapperInstance();
150 /**Obtains a FUNCDESC structure for a function.
151 Fills the FUNCDESC structure if ITypeInfo provides information for
152 the function of name sFuncName or pFuncDesc will not be filled in.
153 May throw a BridgeRuntimeError.
155 void getFuncDesc(const OUString & sFuncName, FUNCDESC ** pFuncDesc);
156 /**Obtains a FUNCDESC structures or a VARDESC structure
157 for a property. pFuncDescPut may also contain
158 a structure for a "propertyputref" operation. If pFuncDesc contains a
159 "put ref" or "put" FUNCDESC depends on what was found first in the type
160 description.
161 Fills the FUNCDESC structure if ITypeInfo provides information for
162 the respective property functions or the structures will not be filled in.
163 May throw a BridgeRuntimeError.
165 void getPropDesc(const OUString & sFuncName, FUNCDESC ** pFuncDescGet,
166 FUNCDESC** pFuncDescPut, VARDESC ** pVarDesc);
167 // These functions are for the case if an object of this class wraps an IDispatch
168 // object that implements UNO interfaces. In that case the member m_seqTypes
169 // is set through XInitialization::initialize.
170 void getMethodInfo(const OUString& sName, TypeDescription& methodDescription);
171 // After return attributInfo contains typelib_InterfaceAttributeTypeDescription::pAttributeTypeRef
172 void getAttributeInfo(const OUString& sName, TypeDescription& attributeInfo);
173 // used by get MethodInfo
174 TypeDescription getInterfaceMemberDescOfCurrentCall(const OUString& sName);
175 /** Returns always a valid ITypeInfo interface or throws a BridgeRuntimeError.
176 The returned interface does not need to be AddRef'ed as long as it is locally
177 used. The interface is kept in the instance of this class.
179 ITypeInfo* getTypeInfo();
181 /** Returns the DISPID for a function or property name. If true is returned then
182 id contains a valid DISPID.
185 bool getDispid(const OUString& sFuncName, DISPID * id);
187 VARTYPE getUserDefinedElementType( ITypeInfo* pTypeInfo, const DWORD nHrefType );
189 /** Gets the element type in a VARIANT like style. E.g. if desc->lptdesc contains
190 a VT_PTR than it is replaced by VT_BYREF and VT_SAFEARRAY is replaced by VT_ARRAY
191 If the TYPEDESC describes an SAFEARRAY then varType is a combination of VT_ARRAY
192 and the element type.
193 The argument desc must be obtained from FUNCDESC::lprgelemdescParam[i].tdesc where
194 FUNCDESC was obtained from the ITypeInfo belonging to wrapped IDispatch.
196 VARTYPE getElementTypeDesc( const TYPEDESC *desc);
197 /** Iterates over all functions and put the names and indices into the map
198 m_mapComFunc of type TLBFuncIndexMap.
199 Call the function every time before accessing the map.
200 Throws a BridgeRuntimeError on failure.
202 void buildComTlbIndex();
204 /** Returns a FUNCDESC structure which contains type information about the
205 current XInvocation::invoke call. The FUNCDESC either describes a method,
206 a property put or a property get operation.
207 It uses the types com.sun.star.bridge.oleautomation.PropertyPutArgument
208 which can be
209 contained in the sequence of in-arguments of invoke to determine if the call is
210 a property put or property get operation.
211 If no adequate FUNCDESC was found, an IllegalArgumentException is thrown.
212 Therefore it is safe to assume that the returned FUNCDESC* is not NULL.
214 @exception IllegalArgumentException
215 Thrown if no adequate FUNCDESC could be found.
217 void getFuncDescForInvoke(const OUString & sFuncName,
218 const Sequence<Any> & seqArgs, FUNCDESC** pFuncDesc);
220 // Finds out whether the wrapped IDispatch is an JScript Object. This is is
221 // done by
222 // asking for the property "_environment". If it has the value "JScript"
223 // (case insensitive) then the IDispatch is considered a JScript object.
224 sal_Bool isJScriptObject();
227 // If UNO interfaces are implemented in JScript objects, VB or C++ COM objects
228 // and those are passed as parameter to a UNO interface function, then
229 // the IDispatch* are wrapped by objects of this class. Assuming that the functions
230 // implemented by the IDispatch object returns another UNO interface then
231 // it has to be wrapped to this type. But this is only possible if an object of this
232 // wrapper class knows what type it is represting. The member m_TypeDescription holds this
233 // information.
234 // m_TypeDescription is only useful when an object wraps an IDispatch object that implements
235 // an UNO interface. The value is set during a call to XInitialization::initialize.
236 Sequence<Type> m_seqTypes;
237 CComPtr<IUnknown> m_spUnknown;
238 CComPtr<IDispatch> m_spDispatch;
239 OUString m_sTypeName; // is "" ( not initialised ), "IDispatch" ( we have no idea ) or "SomeLibrary.SomeTypeName" if we managed to get a type
240 /** This value is set dureing XInitialization::initialize. It indicates that the COM interface
241 was transported as VT_DISPATCH in a VARIANT rather then a VT_UNKNOWN
243 sal_Bool m_bOriginalDispatch;
244 DispIdMap m_dispIdMap;
245 Reference<XIdlClass>* m_pxIdlClass;
248 // used by isJScriptObject
249 enum JScriptDetermination{ JScriptUndefined=0, NoJScript, IsJScript};
250 JScriptDetermination m_eJScript;
251 // The map is filled by buildComTlbIndex
252 // It maps Uno Function names to an index which is used in ITypeInfo::GetFuncDesc
253 TLBFuncIndexMap m_mapComFunc;
254 // used for synchroizing the computation of the content for m_mapComFunc
255 bool m_bComTlbIndexInit;
256 // Keeps the ITypeInfo obtained from IDispatch::GetTypeInfo
257 CComPtr< ITypeInfo > m_spTypeInfo;
258 OUString m_sDefaultMember;
259 bool m_bHasDfltMethod;
260 bool m_bHasDfltProperty;
263 } // end namespace
265 #endif
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */