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