1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER > 1310)
28*cdf0e10cSrcweir #pragma warning(disable : 4917 4555)
29*cdf0e10cSrcweir #endif
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "stdafx.h"
32*cdf0e10cSrcweir #include "servprov.hxx"
33*cdf0e10cSrcweir #include "embeddoc.hxx"
34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir #include <osl/thread.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace com::sun::star;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir const GUID* guidList[ SUPPORTED_FACTORIES_NUM ] = {
42*cdf0e10cSrcweir 	&OID_WriterTextServer,
43*cdf0e10cSrcweir 	&OID_WriterOASISTextServer,
44*cdf0e10cSrcweir 	&OID_CalcServer,
45*cdf0e10cSrcweir 	&OID_CalcOASISServer,
46*cdf0e10cSrcweir 	&OID_DrawingServer,
47*cdf0e10cSrcweir 	&OID_DrawingOASISServer,
48*cdf0e10cSrcweir 	&OID_PresentationServer,
49*cdf0e10cSrcweir 	&OID_PresentationOASISServer,
50*cdf0e10cSrcweir 	&OID_MathServer,
51*cdf0e10cSrcweir 	&OID_MathOASISServer
52*cdf0e10cSrcweir };
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir class CurThreadData
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	public:
57*cdf0e10cSrcweir 		CurThreadData();
58*cdf0e10cSrcweir 		virtual ~CurThreadData();
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 		sal_Bool SAL_CALL setData(void *pData);
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 		void* SAL_CALL getData();
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	protected:
65*cdf0e10cSrcweir 		oslThreadKey m_hKey;
66*cdf0e10cSrcweir };
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir CurThreadData::CurThreadData()
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir 	m_hKey = osl_createThreadKey( (oslThreadKeyCallbackFunction)NULL );
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir CurThreadData::~CurThreadData()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	osl_destroyThreadKey(m_hKey);
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir sal_Bool CurThreadData::setData(void *pData)
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	OSL_ENSURE( m_hKey, "No thread key!\n" );
81*cdf0e10cSrcweir 	return (osl_setThreadKeyData(m_hKey, pData));
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir void *CurThreadData::getData()
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir 	OSL_ENSURE( m_hKey, "No thread key!\n" );
87*cdf0e10cSrcweir 	return (osl_getThreadKeyData(m_hKey));
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir // CoInitializeEx *
92*cdf0e10cSrcweir typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInitEx)( LPVOID, DWORD);
93*cdf0e10cSrcweir // CoInitialize *
94*cdf0e10cSrcweir typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *ptrCoInit)( LPVOID);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void o2u_attachCurrentThread()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     static CurThreadData oleThreadData;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     if ( oleThreadData.getData() != 0 )
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         HINSTANCE inst=	LoadLibrary( _T("ole32.dll"));
103*cdf0e10cSrcweir         if( inst )
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             HRESULT hr;
106*cdf0e10cSrcweir             ptrCoInitEx initFuncEx= (ptrCoInitEx)GetProcAddress( inst, _T("CoInitializeEx"));
107*cdf0e10cSrcweir             if( initFuncEx)
108*cdf0e10cSrcweir                 hr= initFuncEx( NULL, COINIT_MULTITHREADED);
109*cdf0e10cSrcweir             else
110*cdf0e10cSrcweir             {
111*cdf0e10cSrcweir                 ptrCoInit initFunc= (ptrCoInit)GetProcAddress( inst,_T("CoInitialize"));
112*cdf0e10cSrcweir                 if( initFunc)
113*cdf0e10cSrcweir                     hr= initFunc( NULL);
114*cdf0e10cSrcweir             }
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         oleThreadData.setData((void*)sal_True);
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir //===============================================================================
122*cdf0e10cSrcweir // EmbedServer_Impl
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
125*cdf0e10cSrcweir 	m_xFactory( xFactory)
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir 	for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
128*cdf0e10cSrcweir 	{
129*cdf0e10cSrcweir 		m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
130*cdf0e10cSrcweir 		m_pOLEFactories[nInd]->registerClass();
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir EmbedServer_Impl::~EmbedServer_Impl()
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir 	for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
137*cdf0e10cSrcweir 	{
138*cdf0e10cSrcweir 		if ( m_pOLEFactories[nInd] )
139*cdf0e10cSrcweir 			m_pOLEFactories[nInd]->deregisterClass();
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir // XInterface --------------------------------------------------
144*cdf0e10cSrcweir uno::Any SAL_CALL
145*cdf0e10cSrcweir EmbedServer_Impl::queryInterface(
146*cdf0e10cSrcweir 	const uno::Type& aType )
147*cdf0e10cSrcweir 	throw(
148*cdf0e10cSrcweir 		uno::RuntimeException
149*cdf0e10cSrcweir 	)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	uno::Any a=
152*cdf0e10cSrcweir 		::cppu::queryInterface(
153*cdf0e10cSrcweir 			aType, static_cast<lang::XTypeProvider*>(this));
154*cdf0e10cSrcweir 	if( a == uno::Any())
155*cdf0e10cSrcweir 		return OWeakObject::queryInterface( aType);
156*cdf0e10cSrcweir 	else
157*cdf0e10cSrcweir 		return a;
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir void SAL_CALL EmbedServer_Impl::acquire(  ) throw(uno::RuntimeException)
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir 	OWeakObject::acquire();
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir void SAL_CALL EmbedServer_Impl::release(  ) throw (uno::RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	OWeakObject::release();
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir // XTypeProvider --------------------------------------------------
172*cdf0e10cSrcweir uno::Sequence< uno::Type > SAL_CALL
173*cdf0e10cSrcweir EmbedServer_Impl::getTypes( )
174*cdf0e10cSrcweir 	throw(
175*cdf0e10cSrcweir 		uno::RuntimeException
176*cdf0e10cSrcweir 	)
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	static ::cppu::OTypeCollection *pCollection = 0;
179*cdf0e10cSrcweir 	if( ! pCollection )
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir 		::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
182*cdf0e10cSrcweir 		if( ! pCollection )
183*cdf0e10cSrcweir 		{
184*cdf0e10cSrcweir 			static ::cppu::OTypeCollection collection(
185*cdf0e10cSrcweir 				getCppuType(
186*cdf0e10cSrcweir 					reinterpret_cast<uno::Reference< uno::XWeak>*>(0)),
187*cdf0e10cSrcweir 				getCppuType(
188*cdf0e10cSrcweir 					reinterpret_cast<
189*cdf0e10cSrcweir 					uno::Reference< lang::XTypeProvider>*>(0)));
190*cdf0e10cSrcweir 			pCollection = &collection;
191*cdf0e10cSrcweir 		}
192*cdf0e10cSrcweir 	}
193*cdf0e10cSrcweir 	return (*pCollection).getTypes();
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir uno::Sequence< sal_Int8 > SAL_CALL EmbedServer_Impl::getImplementationId() throw(uno::RuntimeException)
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir 	static ::cppu::OImplementationId *pId = 0;
199*cdf0e10cSrcweir 	if( ! pId )
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 		::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
202*cdf0e10cSrcweir 		if( ! pId )
203*cdf0e10cSrcweir 		{
204*cdf0e10cSrcweir 			static ::cppu::OImplementationId id( sal_False );
205*cdf0e10cSrcweir 			pId = &id;
206*cdf0e10cSrcweir 		}
207*cdf0e10cSrcweir 	}
208*cdf0e10cSrcweir 	return (*pId).getImplementationId();
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir //===============================================================================
212*cdf0e10cSrcweir // EmbedProviderFactory_Impl
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
215*cdf0e10cSrcweir 	: m_refCount( 0L )
216*cdf0e10cSrcweir 	, m_xFactory( xFactory )
217*cdf0e10cSrcweir 	, m_guid( *pGuid )
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir sal_Bool EmbedProviderFactory_Impl::registerClass()
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	HRESULT hresult;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 	o2u_attachCurrentThread();
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	hresult = CoRegisterClassObject(
232*cdf0e10cSrcweir 			m_guid,
233*cdf0e10cSrcweir 			this,
234*cdf0e10cSrcweir 			CLSCTX_LOCAL_SERVER,
235*cdf0e10cSrcweir 			REGCLS_MULTIPLEUSE,
236*cdf0e10cSrcweir 			&m_factoryHandle);
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	return (hresult == NOERROR);
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir sal_Bool EmbedProviderFactory_Impl::deregisterClass()
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir 	HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	return (hresult == NOERROR);
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     if(IsEqualIID(riid, IID_IUnknown))
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		AddRef();
253*cdf0e10cSrcweir 		*ppv = (IUnknown*) (IClassFactory*) this;
254*cdf0e10cSrcweir 		return NOERROR;
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir     else if (IsEqualIID(riid, IID_IClassFactory))
257*cdf0e10cSrcweir 	{
258*cdf0e10cSrcweir 		AddRef();
259*cdf0e10cSrcweir 		*ppv = (IClassFactory*) this;
260*cdf0e10cSrcweir 		return NOERROR;
261*cdf0e10cSrcweir 	}
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir     *ppv = NULL;
264*cdf0e10cSrcweir     return ResultFromScode(E_NOINTERFACE);
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir 	return osl_incrementInterlockedCount( &m_refCount);
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
275*cdf0e10cSrcweir 	sal_Int32 nCount = --m_refCount;
276*cdf0e10cSrcweir 	if ( nCount == 0 )
277*cdf0e10cSrcweir 	{
278*cdf0e10cSrcweir 		delete this;
279*cdf0e10cSrcweir 	}
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     return nCount;
282*cdf0e10cSrcweir }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR* punkOuter,
285*cdf0e10cSrcweir                                                        REFIID riid,
286*cdf0e10cSrcweir                                                        void FAR* FAR* ppv)
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir     punkOuter = NULL;
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     IUnknown* pEmbedDocument = (IUnknown*)(IPersistStorage*)( new EmbedDocument_Impl( m_xFactory, &m_guid ) );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir     return pEmbedDocument->QueryInterface( riid, ppv );
293*cdf0e10cSrcweir }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
296*cdf0e10cSrcweir {
297*cdf0e10cSrcweir     return NOERROR;
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir // Fix strange warnings about some
301*cdf0e10cSrcweir // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
302*cdf0e10cSrcweir // warning C4505: 'xxx' : unreferenced local function has been removed
303*cdf0e10cSrcweir #if defined(_MSC_VER)
304*cdf0e10cSrcweir #pragma warning(disable: 4505)
305*cdf0e10cSrcweir #endif
306