1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: servprov.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if defined(_MSC_VER) && (_MSC_VER > 1310)
31 #pragma warning(disable : 4917 4555)
35 #include "servprov.hxx"
36 #include "embeddoc.hxx"
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <osl/mutex.hxx>
40 #include <osl/thread.h>
42 using namespace com::sun::star
;
44 const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
45 &OID_WriterTextServer
,
46 &OID_WriterOASISTextServer
,
50 &OID_DrawingOASISServer
,
51 &OID_PresentationServer
,
52 &OID_PresentationOASISServer
,
61 virtual ~CurThreadData();
63 sal_Bool SAL_CALL
setData(void *pData
);
65 void* SAL_CALL
getData();
71 CurThreadData::CurThreadData()
73 m_hKey
= osl_createThreadKey( (oslThreadKeyCallbackFunction
)NULL
);
76 CurThreadData::~CurThreadData()
78 osl_destroyThreadKey(m_hKey
);
81 sal_Bool
CurThreadData::setData(void *pData
)
83 OSL_ENSURE( m_hKey
, "No thread key!\n" );
84 return (osl_setThreadKeyData(m_hKey
, pData
));
87 void *CurThreadData::getData()
89 OSL_ENSURE( m_hKey
, "No thread key!\n" );
90 return (osl_getThreadKeyData(m_hKey
));
95 typedef DECLSPEC_IMPORT
HRESULT (STDAPICALLTYPE
*ptrCoInitEx
)( LPVOID
, DWORD
);
97 typedef DECLSPEC_IMPORT
HRESULT (STDAPICALLTYPE
*ptrCoInit
)( LPVOID
);
99 void o2u_attachCurrentThread()
101 static CurThreadData oleThreadData
;
103 if ( oleThreadData
.getData() != 0 )
105 HINSTANCE inst
= LoadLibrary( _T("ole32.dll"));
109 ptrCoInitEx initFuncEx
= (ptrCoInitEx
)GetProcAddress( inst
, _T("CoInitializeEx"));
111 hr
= initFuncEx( NULL
, COINIT_MULTITHREADED
);
114 ptrCoInit initFunc
= (ptrCoInit
)GetProcAddress( inst
,_T("CoInitialize"));
119 oleThreadData
.setData((void*)sal_True
);
124 //===============================================================================
127 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
):
128 m_xFactory( xFactory
)
130 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
132 m_pOLEFactories
[nInd
] = new EmbedProviderFactory_Impl( m_xFactory
, guidList
[nInd
] );
133 m_pOLEFactories
[nInd
]->registerClass();
137 EmbedServer_Impl::~EmbedServer_Impl()
139 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
141 if ( m_pOLEFactories
[nInd
] )
142 m_pOLEFactories
[nInd
]->deregisterClass();
146 // XInterface --------------------------------------------------
148 EmbedServer_Impl::queryInterface(
149 const uno::Type
& aType
)
151 uno::RuntimeException
155 ::cppu::queryInterface(
156 aType
, static_cast<lang::XTypeProvider
*>(this));
158 return OWeakObject::queryInterface( aType
);
163 void SAL_CALL
EmbedServer_Impl::acquire( ) throw(uno::RuntimeException
)
165 OWeakObject::acquire();
168 void SAL_CALL
EmbedServer_Impl::release( ) throw (uno::RuntimeException
)
170 OWeakObject::release();
174 // XTypeProvider --------------------------------------------------
175 uno::Sequence
< uno::Type
> SAL_CALL
176 EmbedServer_Impl::getTypes( )
178 uno::RuntimeException
181 static ::cppu::OTypeCollection
*pCollection
= 0;
184 ::osl::MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
187 static ::cppu::OTypeCollection
collection(
189 reinterpret_cast<uno::Reference
< uno::XWeak
>*>(0)),
192 uno::Reference
< lang::XTypeProvider
>*>(0)));
193 pCollection
= &collection
;
196 return (*pCollection
).getTypes();
199 uno::Sequence
< sal_Int8
> SAL_CALL
EmbedServer_Impl::getImplementationId() throw(uno::RuntimeException
)
201 static ::cppu::OImplementationId
*pId
= 0;
204 ::osl::MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
207 static ::cppu::OImplementationId
id( sal_False
);
211 return (*pId
).getImplementationId();
214 //===============================================================================
215 // EmbedProviderFactory_Impl
217 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
, const GUID
* pGuid
)
219 , m_xFactory( xFactory
)
224 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
228 sal_Bool
EmbedProviderFactory_Impl::registerClass()
232 o2u_attachCurrentThread();
234 hresult
= CoRegisterClassObject(
241 return (hresult
== NOERROR
);
244 sal_Bool
EmbedProviderFactory_Impl::deregisterClass()
246 HRESULT hresult
= CoRevokeClassObject( m_factoryHandle
);
248 return (hresult
== NOERROR
);
251 STDMETHODIMP
EmbedProviderFactory_Impl::QueryInterface(REFIID riid
, void FAR
* FAR
* ppv
)
253 if(IsEqualIID(riid
, IID_IUnknown
))
256 *ppv
= (IUnknown
*) (IClassFactory
*) this;
259 else if (IsEqualIID(riid
, IID_IClassFactory
))
262 *ppv
= (IClassFactory
*) this;
267 return ResultFromScode(E_NOINTERFACE
);
270 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::AddRef()
272 return osl_incrementInterlockedCount( &m_refCount
);
275 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::Release()
277 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex());
278 sal_Int32 nCount
= --m_refCount
;
287 STDMETHODIMP
EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
293 IUnknown
* pEmbedDocument
= (IUnknown
*)(IPersistStorage
*)( new EmbedDocument_Impl( m_xFactory
, &m_guid
) );
295 return pEmbedDocument
->QueryInterface( riid
, ppv
);
298 STDMETHODIMP
EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
303 // Fix strange warnings about some
304 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
305 // warning C4505: 'xxx' : unreferenced local function has been removed
306 #if defined(_MSC_VER)
307 #pragma warning(disable: 4505)