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