xref: /aoo42x/main/extensions/source/ole/oleobjw.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __OLEOBJW_HXX
29 #define __OLEOBJW_HXX
30 #include "ole2uno.hxx"
31 
32 #ifdef _MSC_VER
33 #pragma warning (push,1)
34 #pragma warning (disable:4548)
35 #endif
36 
37 #include <tools/presys.h>
38 #define _WIN32_WINNT 0x0400
39 
40 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
41 #undef _DEBUG
42 #endif
43 #include <atlbase.h>
44 #include <vector>
45 #include <hash_map>
46 #include <tools/postsys.h>
47 
48 #ifdef _MSC_VER
49 #pragma warning (pop)
50 #endif
51 #include <cppuhelper/implbase3.hxx>
52 #include <cppuhelper/implbase4.hxx>
53 #include <cppuhelper/implbase7.hxx>
54 
55 #include <com/sun/star/lang/XInitialization.hpp>
56 #include <com/sun/star/bridge/oleautomation/XAutomationObject.hpp>
57 #include <rtl/ustring.hxx>
58 
59 #include <com/sun/star/script/XDefaultProperty.hpp>
60 #include <com/sun/star/script/XDefaultMethod.hpp>
61 #include <com/sun/star/script/XDirectInvocation.hpp>
62 
63 #include <typelib/typedescription.hxx>
64 #include "unoconversionutilities.hxx"
65 #include "windata.hxx"
66 using namespace cppu;
67 using namespace rtl;
68 using namespace std;
69 using namespace com::sun::star::lang;
70 using namespace com::sun::star::bridge;
71 using namespace com::sun::star::bridge::oleautomation;
72 
73 namespace ole_adapter
74 {
75 
76 
77 
78 typedef hash_map<OUString, pair<DISPID, unsigned short>, hashOUString_Impl, equalOUString_Impl> DispIdMap;
79 
80 typedef hash_multimap<OUString, unsigned int, hashOUString_Impl, equalOUString_Impl> TLBFuncIndexMap;
81 
82 // This class wraps an IDispatch and maps XInvocation calls to IDispatch calls on the wrapped object.
83 // If m_TypeDescription is set then this class represents an UNO interface implemented in a COM component.
84 // The interface is not a real interface in terms of an abstract class but is realized through IDispatch.
85 class IUnknownWrapper_Impl : public WeakImplHelper7< XInvocation, XBridgeSupplier2, XInitialization, XAutomationObject, XDefaultProperty, XDefaultMethod, XDirectInvocation >,
86 
87 							 public UnoConversionUtilities<IUnknownWrapper_Impl>
88 
89 {
90 public:
91 	IUnknownWrapper_Impl(Reference<XMultiServiceFactory> &xFactory,
92                          sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass);
93 
94 	~IUnknownWrapper_Impl();
95 
96     //XInterface
97 	Any SAL_CALL queryInterface(const Type& t)
98         throw (RuntimeException);
99 
100 	// XInvokation
101 	virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection(  )
102         throw(RuntimeException);
103     virtual Any SAL_CALL invoke( const OUString& aFunctionName,
104                                  const Sequence< Any >& aParams,
105                                  Sequence< sal_Int16 >& aOutParamIndex,
106                                  Sequence< Any >& aOutParam )
107         throw(IllegalArgumentException, CannotConvertException,
108               InvocationTargetException, RuntimeException);
109     virtual void SAL_CALL setValue( const OUString& aPropertyName,
110                                     const Any& aValue )
111         throw(UnknownPropertyException, CannotConvertException,
112               InvocationTargetException, RuntimeException);
113     virtual Any SAL_CALL getValue( const OUString& aPropertyName )
114         throw(UnknownPropertyException, RuntimeException);
115     virtual sal_Bool SAL_CALL hasMethod( const OUString& aName )
116         throw(RuntimeException);
117     virtual sal_Bool SAL_CALL hasProperty( const OUString& aName )
118         throw(RuntimeException);
119 
120 	// XBridgeSupplier2
121 	// This interface is implemented to provide a safe way to obtain the original
122 	// IUnknown or IDispatch within the function anyToVariant. The function asks
123 	// every UNO object for its XBridgeSupplier2 and if it is available uses it to convert
124 	// the object with its own supplier.
125 	virtual Any SAL_CALL createBridge( const Any& modelDepObject,
126                                        const Sequence< sal_Int8 >& aProcessId,
127                                        sal_Int16 sourceModelType,
128                                        sal_Int16 destModelType )
129         throw(IllegalArgumentException, RuntimeException);
130 
131 	// XInitialization
132     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
133         throw(Exception, RuntimeException);
134 
135     // XDefaultProperty
136     virtual ::rtl::OUString SAL_CALL getDefaultPropertyName(  ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
137 
138     // XDefaultMethod
139     virtual ::rtl::OUString SAL_CALL getDefaultMethodName(  ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
140 
141     // XDirectInvocation
142     virtual ::com::sun::star::uno::Any SAL_CALL directInvoke( const ::rtl::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);
143     virtual ::sal_Bool SAL_CALL hasMember( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
144 
145 protected:
146 	// ----------------------------------------------------------------------------
147     virtual Any invokeWithDispIdUnoTlb(const OUString& sFunctionName,
148                                        const Sequence< Any >& Params,
149                                        Sequence<sal_Int16 >& OutParamIndex,
150                                        Sequence< Any >& OutParam);
151 	// Is used for OleObjectFactory service
152 	virtual Any invokeWithDispIdComTlb(const OUString& sFuncName,
153                                        const Sequence< Any >& Params,
154                                        Sequence< sal_Int16 >& OutParamIndex,
155                                        Sequence< Any >& OutParam);
156 
157 //    virtual void setValueWithDispId(DISPID dispID, const Any& Value);
158 
159 //    virtual Any getValueWithDispId(const OUString& sName, DISPID dispID);
160 
161 
162 	// UnoConversionUtilities -------------------------------------------------------------------------------
163 	virtual	Reference<XInterface> createUnoWrapperInstance();
164 	virtual Reference<XInterface> createComWrapperInstance();
165 
166     /**Obtains a FUNCDESC structure for a function.
167        Fills the FUNCDESC structure if ITypeInfo provides information for
168        the function of name sFuncName or pFuncDesc will not be filled in.
169        May throw a BridgeRuntimeError.
170      */
171     void getFuncDesc(const OUString & sFuncName, FUNCDESC ** pFuncDesc);
172     /**Obtains a FUNCDESC structures or a VARDESC structure
173        for a property. pFuncDescPut may also contain
174        a structure for a "propertyputref" operation. If pFuncDesc contains a
175        "put ref" or "put" FUNCDESC depends on what was found first in the type
176        description.
177        Fills the FUNCDESC structure if ITypeInfo provides information for
178        the respective property functions or the structures will not be filled in.
179        May throw a BridgeRuntimeError.
180      */
181     void getPropDesc(const OUString & sFuncName, FUNCDESC ** pFuncDescGet,
182                      FUNCDESC** pFuncDescPut, VARDESC ** pVarDesc);
183 	// These functions are for the case if an object of this class wraps an IDispatch
184 	// object that implements UNO interfaces. In that case the member m_seqTypes
185 	// is set through XInitialization::initialize.
186 	void getMethodInfo(const OUString& sName, TypeDescription& methodDescription);
187 	// After return attributInfo contains typelib_InterfaceAttributeTypeDescription::pAttributeTypeRef
188 	void getAttributeInfo(const OUString& sName, TypeDescription& attributeInfo);
189 	// used by get MethodInfo
190 	TypeDescription	 getInterfaceMemberDescOfCurrentCall(const OUString& sName);
191     /** Returns alway a valid ITypeInfo interface or throws a BridgeRuntimeError.
192         The returned interface does not need to be AddRef'ed as long as it is locally
193         used. The interface is kept in the instance of this class.
194      */
195 	ITypeInfo*	getTypeInfo();
196 
197 	/** Returns the DISPID for a function or property name. If true is returned then
198 		id contains a valid DISPID.
199 	*/
200 	bool getDispid(const OUString& sFuncName, DISPID * id);
201 
202 	/** Gets the element type in a VARIANT like style. E.g. if desc->lptdesc contains
203 		a VT_PTR than it is replaced by VT_BYREF and VT_SAFEARRAY is replaced by VT_ARRAY
204 		If the TYPEDESC describes an SAFEARRAY then varType is a combination of VT_ARRAY
205 		and the element type.
206 		The argument desc must be obtained from FUNCDESC::lprgelemdescParam[i].tdesc where
207 		FUNCDESC was obtained from the ITypeInfo belonging to wrapped IDispatch.
208 	*/
209 	VARTYPE getElementTypeDesc( const TYPEDESC *desc);
210 	/** Iterates over all functions and put the names and indices into the map
211         m_mapComFunc of type TLBFuncIndexMap.
212         Call the function every time before accessing the map.
213         Throws a BridgeRuntimeError on failure.
214     */
215 	void  buildComTlbIndex();
216 
217     /** Returns a FUNCDESC structure which contains type information about the
218         current XInvocation::invoke call. The FUNCDESC either describes a method,
219         a property put or a property get operation.
220         It uses the types  com.sun.star.bridge.oleautomation.PropertyPutArgument
221         which can be
222         contained in the sequence of in-arguments of invoke to determine if the call is
223         a property put or property get operation.
224         If no adequate FUNCDESC was found, an IllegalArgumentException is thrown.
225         Therefore it is safe to assume that the returned FUNCDESC* is not NULL.
226 
227         @exception IllegalArgumentException
228         Thrown if no adequate FUNCDESC could be found.
229     */
230     void getFuncDescForInvoke(const OUString & sFuncName,
231                               const Sequence<Any> & seqArgs, FUNCDESC** pFuncDesc);
232 
233     // Finds out wheter the wrapped IDispatch is an JScript Object. This is is
234 	// done by
235 	// asking for the property "_environment". If it has the value "JScript"
236 	// (case insensitive) then the IDispatch is considered a JScript object.
237 	sal_Bool isJScriptObject();
238 	// -------------------------------------------------------------------------------
239 
240 	// If UNO interfaces are implemented in JScript objects, VB or C++ COM objects
241 	// and those are passed as parameter to a UNO interface function, then
242 	// the IDispatch* are wrapped by objects of this class. Assuming that the functions
243 	// implemented by the IDispatch object returns another UNO interface then
244 	// it has to be wrapped to this type. But this is only possible if an object of this
245 	// wrapper class knows what type it is represting. The member m_TypeDescription holds this
246 	// information.
247 	// m_TypeDescription is only useful when an object wraps an IDispatch object that implements
248 	// an UNO interface. The value is set during a call to XInitialization::initialize.
249 	Sequence<Type> m_seqTypes;
250 	CComPtr<IUnknown> m_spUnknown;
251 	CComPtr<IDispatch> m_spDispatch;
252         rtl::OUString m_sTypeName; // is "" ( not initialised ), "IDispatch" ( we have no idea ) or "SomeLibrary.SomeTypeName" if we managed to get a type
253     /** This value is set dureing XInitialization::initialize. It indicates that the COM interface
254     was transported as VT_DISPATCH in a VARIANT rather then a VT_UNKNOWN
255     */
256     sal_Bool  m_bOriginalDispatch;
257 	DispIdMap 			m_dispIdMap;
258 	Reference<XIdlClass>*		m_pxIdlClass;
259 
260 
261 	// used by isJScriptObject
262 	enum JScriptDetermination{ JScriptUndefined=0, NoJScript, IsJScript};
263 	JScriptDetermination m_eJScript;
264 	// The map is filled by buildComTlbIndex
265     // It maps Uno Function names to an index which is used in ITypeInfo::GetFuncDesc
266 	TLBFuncIndexMap m_mapComFunc;
267     // used for synchroizing the computation of the content for m_mapComFunc
268     bool m_bComTlbIndexInit;
269 	// Keeps the ITypeInfo obtained from IDispatch::GetTypeInfo
270 	CComPtr< ITypeInfo > m_spTypeInfo;
271     rtl::OUString m_sDefaultMember;
272     bool m_bHasDfltMethod;
273     bool m_bHasDfltProperty;
274 };
275 
276 } // end namespace
277 #endif
278 
279