xref: /trunk/main/extensions/source/ole/oleobjw.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
95     // XInvokation
96     virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection(  );
97     virtual Any SAL_CALL invoke( const OUString& aFunctionName,
98                                  const Sequence< Any >& aParams,
99                                  Sequence< sal_Int16 >& aOutParamIndex,
100                                  Sequence< Any >& aOutParam );
101     virtual void SAL_CALL setValue( const OUString& aPropertyName,
102                                     const Any& aValue );
103     virtual Any SAL_CALL getValue( const OUString& aPropertyName );
104     virtual sal_Bool SAL_CALL hasMethod( const OUString& aName );
105     virtual sal_Bool SAL_CALL hasProperty( const OUString& aName );
106 
107     // XBridgeSupplier2
108     // This interface is implemented to provide a safe way to obtain the original
109     // IUnknown or IDispatch within the function anyToVariant. The function asks
110     // every UNO object for its XBridgeSupplier2 and if it is available uses it to convert
111     // the object with its own supplier.
112     virtual Any SAL_CALL createBridge( const Any& modelDepObject,
113                                        const Sequence< sal_Int8 >& aProcessId,
114                                        sal_Int16 sourceModelType,
115                                        sal_Int16 destModelType );
116 
117     // XInitialization
118     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments );
119 
120     // XDefaultProperty
getDefaultPropertyName()121     virtual ::rtl::OUString SAL_CALL getDefaultPropertyName(  ) { return m_sDefaultMember; }
122 
123     // XDefaultMethod
getDefaultMethodName()124     virtual ::rtl::OUString SAL_CALL getDefaultMethodName(  ) { return m_sDefaultMember; }
125 
126     // XDirectInvocation
127     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 );
128     virtual ::sal_Bool SAL_CALL hasMember( const ::rtl::OUString& aName );
129 
130 protected:
131     // ----------------------------------------------------------------------------
132     virtual Any invokeWithDispIdUnoTlb(const OUString& sFunctionName,
133                                        const Sequence< Any >& Params,
134                                        Sequence<sal_Int16 >& OutParamIndex,
135                                        Sequence< Any >& OutParam);
136     // Is used for OleObjectFactory service
137     virtual Any invokeWithDispIdComTlb(const OUString& sFuncName,
138                                        const Sequence< Any >& Params,
139                                        Sequence< sal_Int16 >& OutParamIndex,
140                                        Sequence< Any >& OutParam);
141 
142 //    virtual void setValueWithDispId(DISPID dispID, const Any& Value);
143 
144 //    virtual Any getValueWithDispId(const OUString& sName, DISPID dispID);
145 
146 
147     // UnoConversionUtilities -------------------------------------------------------------------------------
148     virtual Reference<XInterface> createUnoWrapperInstance();
149     virtual Reference<XInterface> createComWrapperInstance();
150 
151     /**Obtains a FUNCDESC structure for a function.
152        Fills the FUNCDESC structure if ITypeInfo provides information for
153        the function of name sFuncName or pFuncDesc will not be filled in.
154        May throw a BridgeRuntimeError.
155      */
156     void getFuncDesc(const OUString & sFuncName, FUNCDESC ** pFuncDesc);
157     /**Obtains a FUNCDESC structures or a VARDESC structure
158        for a property. pFuncDescPut may also contain
159        a structure for a "propertyputref" operation. If pFuncDesc contains a
160        "put ref" or "put" FUNCDESC depends on what was found first in the type
161        description.
162        Fills the FUNCDESC structure if ITypeInfo provides information for
163        the respective property functions or the structures will not be filled in.
164        May throw a BridgeRuntimeError.
165      */
166     void getPropDesc(const OUString & sFuncName, FUNCDESC ** pFuncDescGet,
167                      FUNCDESC** pFuncDescPut, VARDESC ** pVarDesc);
168     // These functions are for the case if an object of this class wraps an IDispatch
169     // object that implements UNO interfaces. In that case the member m_seqTypes
170     // is set through XInitialization::initialize.
171     void getMethodInfo(const OUString& sName, TypeDescription& methodDescription);
172     // After return attributInfo contains typelib_InterfaceAttributeTypeDescription::pAttributeTypeRef
173     void getAttributeInfo(const OUString& sName, TypeDescription& attributeInfo);
174     // used by get MethodInfo
175     TypeDescription  getInterfaceMemberDescOfCurrentCall(const OUString& sName);
176     /** Returns alway a valid ITypeInfo interface or throws a BridgeRuntimeError.
177         The returned interface does not need to be AddRef'ed as long as it is locally
178         used. The interface is kept in the instance of this class.
179      */
180     ITypeInfo*  getTypeInfo();
181 
182     /** Returns the DISPID for a function or property name. If true is returned then
183         id contains a valid DISPID.
184     */
185     bool getDispid(const OUString& sFuncName, DISPID * id);
186 
187     /** Gets the element type in a VARIANT like style. E.g. if desc->lptdesc contains
188         a VT_PTR than it is replaced by VT_BYREF and VT_SAFEARRAY is replaced by VT_ARRAY
189         If the TYPEDESC describes an SAFEARRAY then varType is a combination of VT_ARRAY
190         and the element type.
191         The argument desc must be obtained from FUNCDESC::lprgelemdescParam[i].tdesc where
192         FUNCDESC was obtained from the ITypeInfo belonging to wrapped IDispatch.
193     */
194     VARTYPE getElementTypeDesc( const TYPEDESC *desc);
195     /** Iterates over all functions and put the names and indices into the map
196         m_mapComFunc of type TLBFuncIndexMap.
197         Call the function every time before accessing the map.
198         Throws a BridgeRuntimeError on failure.
199     */
200     void  buildComTlbIndex();
201 
202     /** Returns a FUNCDESC structure which contains type information about the
203         current XInvocation::invoke call. The FUNCDESC either describes a method,
204         a property put or a property get operation.
205         It uses the types  com.sun.star.bridge.oleautomation.PropertyPutArgument
206         which can be
207         contained in the sequence of in-arguments of invoke to determine if the call is
208         a property put or property get operation.
209         If no adequate FUNCDESC was found, an IllegalArgumentException is thrown.
210         Therefore it is safe to assume that the returned FUNCDESC* is not NULL.
211 
212         @exception IllegalArgumentException
213         Thrown if no adequate FUNCDESC could be found.
214     */
215     void getFuncDescForInvoke(const OUString & sFuncName,
216                               const Sequence<Any> & seqArgs, FUNCDESC** pFuncDesc);
217 
218     // Finds out wheter the wrapped IDispatch is an JScript Object. This is is
219     // done by
220     // asking for the property "_environment". If it has the value "JScript"
221     // (case insensitive) then the IDispatch is considered a JScript object.
222     sal_Bool isJScriptObject();
223     // -------------------------------------------------------------------------------
224 
225     // If UNO interfaces are implemented in JScript objects, VB or C++ COM objects
226     // and those are passed as parameter to a UNO interface function, then
227     // the IDispatch* are wrapped by objects of this class. Assuming that the functions
228     // implemented by the IDispatch object returns another UNO interface then
229     // it has to be wrapped to this type. But this is only possible if an object of this
230     // wrapper class knows what type it is represting. The member m_TypeDescription holds this
231     // information.
232     // m_TypeDescription is only useful when an object wraps an IDispatch object that implements
233     // an UNO interface. The value is set during a call to XInitialization::initialize.
234     Sequence<Type> m_seqTypes;
235     CComPtr<IUnknown> m_spUnknown;
236     CComPtr<IDispatch> m_spDispatch;
237         rtl::OUString m_sTypeName; // is "" ( not initialised ), "IDispatch" ( we have no idea ) or "SomeLibrary.SomeTypeName" if we managed to get a type
238     /** This value is set dureing XInitialization::initialize. It indicates that the COM interface
239     was transported as VT_DISPATCH in a VARIANT rather then a VT_UNKNOWN
240     */
241     sal_Bool  m_bOriginalDispatch;
242     DispIdMap           m_dispIdMap;
243     Reference<XIdlClass>*       m_pxIdlClass;
244 
245 
246     // used by isJScriptObject
247     enum JScriptDetermination{ JScriptUndefined=0, NoJScript, IsJScript};
248     JScriptDetermination m_eJScript;
249     // The map is filled by buildComTlbIndex
250     // It maps Uno Function names to an index which is used in ITypeInfo::GetFuncDesc
251     TLBFuncIndexMap m_mapComFunc;
252     // used for synchroizing the computation of the content for m_mapComFunc
253     bool m_bComTlbIndexInit;
254     // Keeps the ITypeInfo obtained from IDispatch::GetTypeInfo
255     CComPtr< ITypeInfo > m_spTypeInfo;
256     rtl::OUString m_sDefaultMember;
257     bool m_bHasDfltMethod;
258     bool m_bHasDfltProperty;
259 };
260 
261 } // end namespace
262 #endif
263