Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / ole / unoobjw.hxx
blob603027fccdbf097c87eee4547e0bd01f38dfc886
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_UNOOBJW_HXX
21 #define INCLUDED_EXTENSIONS_SOURCE_OLE_UNOOBJW_HXX
23 #include <com/sun/star/bridge/XBridgeSupplier2.hpp>
24 #include <com/sun/star/beans/XExactName.hpp>
25 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <com/sun/star/script/InvocationInfo.hpp>
27 #include <salhelper/simplereferenceobject.hxx>
28 #include <cppuhelper/implbase.hxx>
30 #include "comifaces.hxx"
31 #include "ole2uno.hxx"
32 #include "unoconversionutilities.hxx"
34 #define JSCRIPT_VALUE_FUNC L"_GetValueObject"
35 #define GET_STRUCT_FUNC L"_GetStruct"
36 #define BRIDGE_VALUE_FUNC L"Bridge_GetValueObject"
37 #define BRIDGE_GET_STRUCT_FUNC L"Bridge_GetStruct"
38 #define BRIDGE_CREATE_TYPE_FUNC L"Bridge_CreateType"
40 #define DISPID_JSCRIPT_VALUE_FUNC -10l
41 #define DISPID_GET_STRUCT_FUNC -102
42 #define DISPID_CREATE_TYPE_FUNC -103
44 using namespace std;
45 using namespace cppu;
46 using namespace com::sun::star::bridge;
47 using namespace com::sun::star::script;
49 struct MemberInfo
51 MemberInfo() : flags(0), name() {}
52 MemberInfo(WORD f, const OUString& n) : flags(f), name(n) {}
54 WORD flags;
55 OUString name;
58 typedef std::unordered_map
60 OUString,
61 DISPID
62 > NameToIdMap;
64 typedef std::unordered_map
66 OUString,
67 bool
68 > BadNameMap;
70 typedef std::unordered_map
72 DISPID,
73 MemberInfo
74 > IdToMemberInfoMap;
76 // An InterfaceOleWrapper object can wrap either a UNO struct or a UNO
77 // interface as a COM IDispatchEx and IUnoObjectWrapper.
79 class InterfaceOleWrapper : public WeakImplHelper<XBridgeSupplier2, XInitialization>,
80 public IDispatchEx,
81 public IProvideClassInfo,
82 public IConnectionPointContainer,
83 public UnoConversionUtilities<InterfaceOleWrapper>,
84 public IUnoObjectWrapper
86 public:
87 InterfaceOleWrapper(Reference<XMultiServiceFactory> const & xFactory, sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass);
88 ~InterfaceOleWrapper() override;
90 // IUnknown
91 STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObj) override;
92 STDMETHOD_(ULONG, AddRef)() override;
93 STDMETHOD_(ULONG, Release)() override;
95 // IDispatch
96 STDMETHOD( GetTypeInfoCount )( UINT * pctinfo ) override;
97 STDMETHOD( GetTypeInfo )( UINT itinfo, LCID lcid, ITypeInfo ** pptinfo ) override;
98 STDMETHOD( GetIDsOfNames )( REFIID riid, LPOLESTR * rgszNames, UINT cNames,
99 LCID lcid, DISPID * rgdispid ) override;
100 STDMETHOD( Invoke )( DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
101 DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo,
102 UINT * puArgErr ) override;
104 // IDispatchEx
105 virtual HRESULT STDMETHODCALLTYPE GetDispID(
106 /* [in] */ BSTR bstrName,
107 /* [in] */ DWORD grfdex,
108 /* [out] */ DISPID __RPC_FAR *pid) override;
110 virtual /* [local] */ HRESULT STDMETHODCALLTYPE InvokeEx(
111 /* [in] */ DISPID id,
112 /* [in] */ LCID lcid,
113 /* [in] */ WORD wFlags,
114 /* [in] */ DISPPARAMS __RPC_FAR *pdp,
115 /* [out] */ VARIANT __RPC_FAR *pvarRes,
116 /* [out] */ EXCEPINFO __RPC_FAR *pei,
117 /* [unique][in] */ IServiceProvider __RPC_FAR *pspCaller) override;
119 virtual HRESULT STDMETHODCALLTYPE DeleteMemberByName(
120 /* [in] */ BSTR bstr,
121 /* [in] */ DWORD grfdex) override;
123 virtual HRESULT STDMETHODCALLTYPE DeleteMemberByDispID(
124 /* [in] */ DISPID id) override;
126 virtual HRESULT STDMETHODCALLTYPE GetMemberProperties(
127 /* [in] */ DISPID id,
128 /* [in] */ DWORD grfdexFetch,
129 /* [out] */ DWORD __RPC_FAR *pgrfdex) override;
131 virtual HRESULT STDMETHODCALLTYPE GetMemberName(
132 /* [in] */ DISPID id,
133 /* [out] */ BSTR __RPC_FAR *pbstrName) override;
135 virtual HRESULT STDMETHODCALLTYPE GetNextDispID(
136 /* [in] */ DWORD grfdex,
137 /* [in] */ DISPID id,
138 /* [out] */ DISPID __RPC_FAR *pid) override;
140 virtual HRESULT STDMETHODCALLTYPE GetNameSpaceParent(
141 /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunk) override;
143 // IProvideClassInfo
144 virtual HRESULT STDMETHODCALLTYPE GetClassInfo(
145 /* [out] */ ITypeInfo **ppTI) override;
147 // IConnectionPointContainer
148 virtual HRESULT STDMETHODCALLTYPE EnumConnectionPoints(
149 /* [out] */ IEnumConnectionPoints **ppEnum) override;
150 virtual HRESULT STDMETHODCALLTYPE FindConnectionPoint(
151 /* [in] */ REFIID riid,
152 /* [out] */ IConnectionPoint **ppCP) override;
154 // XBridgeSupplier2
155 virtual Any SAL_CALL createBridge(const Any& modelDepObject,
156 const Sequence<sal_Int8>& ProcessId,
157 sal_Int16 sourceModelType,
158 sal_Int16 destModelType) override;
160 // XInitialization
161 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) override;
163 // IUnoObjectWrapper
164 STDMETHOD( getWrapperXInterface)( Reference<XInterface>* pXInt) override;
165 STDMETHOD( getOriginalUnoObject)( Reference<XInterface>* pXInt) override;
166 STDMETHOD( getOriginalUnoStruct)( Any * pStruct) override;
168 // UnoConversionUtility
169 virtual Reference< XInterface > createUnoWrapperInstance() override;
170 virtual Reference< XInterface > createComWrapperInstance() override;
172 const OUString& getImplementationName() const
174 return m_sImplementationName;
177 protected:
178 virtual HRESULT doInvoke( DISPPARAMS * pdispparams, VARIANT * pvarResult,
179 EXCEPINFO * pexcepinfo, unsigned int * puArgErr, OUString & name, Sequence<Any>& params);
181 virtual HRESULT doGetProperty( DISPPARAMS * pdispparams, VARIANT * pvarResult,
182 EXCEPINFO * pexcepinfo, OUString & name );
184 virtual HRESULT doSetProperty( DISPPARAMS * pdispparams, VARIANT * pvarResult,
185 EXCEPINFO * pexcepinfo, unsigned int * puArgErr, OUString & name, Sequence<Any> const & params);
187 virtual HRESULT InvokeGeneral( DISPID dispidMember, unsigned short wFlags,
188 DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo,
189 unsigned int * puArgErr, bool& bHandled);
191 void convertDispparamsArgs( DISPID id, unsigned short wFlags, DISPPARAMS* pdispparams,
192 Sequence<Any>& rSeq);
194 bool getInvocationInfoForCall(DISPID id, InvocationInfo& info);
196 Reference<XInvocation> m_xInvocation;
197 Reference<XExactName> m_xExactName;
198 Reference<XInterface> m_xOrigin;
199 NameToIdMap m_nameToDispIdMap;
200 vector<MemberInfo> m_MemberInfos;
201 // This member is used to determine the default value
202 // denoted by DISPID_VALUE (0). For proper results in JavaScript
203 // we have to return the default value when we write an object
204 // as out parameter. That is, we get a JScript Array as parameter
205 // and put a wrapped object on index null. The array object tries
206 // to detect the default value. The wrapped object must then return
207 // its own IDispatch* otherwise we cannot access it within the script.
208 // see InterfaceOleWrapper::Invoke
209 VARTYPE m_defaultValueType;
211 // The name of the implementation. Can be empty if unknown.
212 OUString m_sImplementationName;
215 /*****************************************************************************
217 UnoObjectWrapperRemoteOpt = Uno Object Wrapper Remote Optimized
219 This is the UNO wrapper used in the service com.sun.star.bridge.OleBridgeSupplierVar1.
220 Key features:
221 DISPIDs are passed out blindly. That is in GetIDsOfNames is no name checking carried out.
222 Only if Invoke fails the name is being checked. Moreover Invoke tries to figure out
223 if a call is made to a property or method if the flags are DISPATCH_METHOD | DISPATCH_PROPERTYPUT.
224 If something has been found out about a property or member than it is saved
225 in a MemberInfo structure hold by an IdToMemberInfoMap stl map.
227 *****************************************************************************/
228 class UnoObjectWrapperRemoteOpt: public InterfaceOleWrapper
230 public:
231 UnoObjectWrapperRemoteOpt( Reference<XMultiServiceFactory> const & aFactory, sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass);
232 ~UnoObjectWrapperRemoteOpt() override;
234 STDMETHOD( GetIDsOfNames )( REFIID riid, LPOLESTR * rgszNames, UINT cNames,
235 LCID lcid, DISPID * rgdispid ) override;
236 STDMETHOD( Invoke )( DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
237 DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo,
238 UINT * puArgErr ) override;
240 // UnoConversionUtility
241 // If UNO interfaces are converted in methods of this class then
242 // they are always wrapped with instances of this class
243 virtual Reference< XInterface > createUnoWrapperInstance() override;
245 protected:
247 static HRESULT methodInvoke( DISPID dispidMember, DISPPARAMS * pdispparams, VARIANT * pvarResult,
248 EXCEPINFO * pexcepinfo, unsigned int * puArgErr, Sequence<Any> const & params);
249 // In GetIDsOfNames are blindly passed out, that is without verifying
250 // the name. If two names are passed in during different calls to
251 // GetIDsOfNames and the names differ only in their cases then different
252 // id's are passed out ( e.g. "doSomethingMethod" or "dosomethingmethod").
253 // In Invoke the DISPID is remapped to the name passed to GetIDsOfNames
254 // and the name is used as parameter for XInvocation::invoke. If invoke
255 // fails because of a wrong name, then m_xExactName ( XExactName) is used
256 // to verify the name. The correct name is then inserted to m_MemberInfos
257 // ( vector<MemberInfo> ). During the next call to Invoke the right name
258 // is used. .
261 BadNameMap m_badNameMap;
263 IdToMemberInfoMap m_idToMemberInfoMap;
265 DISPID m_currentId;
270 #endif
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */