1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #pragma warning(disable : 4917 4555)
24 #include "servprov.hxx"
25 #include "embeddoc.hxx"
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <osl/mutex.hxx>
29 #include <osl/thread.h>
31 using namespace com::sun::star
;
33 const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
34 &OID_WriterTextServer
,
35 &OID_WriterOASISTextServer
,
39 &OID_DrawingOASISServer
,
40 &OID_PresentationServer
,
41 &OID_PresentationOASISServer
,
50 virtual ~CurThreadData();
52 sal_Bool SAL_CALL
setData(void *pData
);
54 void* SAL_CALL
getData();
60 CurThreadData::CurThreadData()
62 m_hKey
= osl_createThreadKey( (oslThreadKeyCallbackFunction
)NULL
);
65 CurThreadData::~CurThreadData()
67 osl_destroyThreadKey(m_hKey
);
70 sal_Bool
CurThreadData::setData(void *pData
)
72 OSL_ENSURE( m_hKey
, "No thread key!\n" );
73 return (osl_setThreadKeyData(m_hKey
, pData
));
76 void *CurThreadData::getData()
78 OSL_ENSURE( m_hKey
, "No thread key!\n" );
79 return (osl_getThreadKeyData(m_hKey
));
84 typedef DECLSPEC_IMPORT
HRESULT (STDAPICALLTYPE
*ptrCoInitEx
)( LPVOID
, DWORD
);
86 typedef DECLSPEC_IMPORT
HRESULT (STDAPICALLTYPE
*ptrCoInit
)( LPVOID
);
88 void o2u_attachCurrentThread()
90 static CurThreadData oleThreadData
;
92 if ( oleThreadData
.getData() != 0 )
94 HINSTANCE inst
= LoadLibrary( _T("ole32.dll"));
98 ptrCoInitEx initFuncEx
= (ptrCoInitEx
)GetProcAddress( inst
, _T("CoInitializeEx"));
100 hr
= initFuncEx( NULL
, COINIT_MULTITHREADED
);
103 ptrCoInit initFunc
= (ptrCoInit
)GetProcAddress( inst
,_T("CoInitialize"));
108 oleThreadData
.setData((void*)sal_True
);
113 //===============================================================================
116 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
):
117 m_xFactory( xFactory
)
119 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
121 m_pOLEFactories
[nInd
] = new EmbedProviderFactory_Impl( m_xFactory
, guidList
[nInd
] );
122 m_pOLEFactories
[nInd
]->registerClass();
126 EmbedServer_Impl::~EmbedServer_Impl()
128 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
130 if ( m_pOLEFactories
[nInd
] )
131 m_pOLEFactories
[nInd
]->deregisterClass();
135 // XInterface --------------------------------------------------
137 EmbedServer_Impl::queryInterface(
138 const uno::Type
& aType
)
140 uno::RuntimeException
144 ::cppu::queryInterface(
145 aType
, static_cast<lang::XTypeProvider
*>(this));
147 return OWeakObject::queryInterface( aType
);
152 void SAL_CALL
EmbedServer_Impl::acquire( ) throw(uno::RuntimeException
)
154 OWeakObject::acquire();
157 void SAL_CALL
EmbedServer_Impl::release( ) throw (uno::RuntimeException
)
159 OWeakObject::release();
163 // XTypeProvider --------------------------------------------------
164 uno::Sequence
< uno::Type
> SAL_CALL
165 EmbedServer_Impl::getTypes( )
167 uno::RuntimeException
170 static ::cppu::OTypeCollection
*pCollection
= 0;
173 ::osl::MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
176 static ::cppu::OTypeCollection
collection(
178 reinterpret_cast<uno::Reference
< uno::XWeak
>*>(0)),
181 uno::Reference
< lang::XTypeProvider
>*>(0)));
182 pCollection
= &collection
;
185 return (*pCollection
).getTypes();
188 uno::Sequence
< sal_Int8
> SAL_CALL
EmbedServer_Impl::getImplementationId() throw(uno::RuntimeException
)
190 static ::cppu::OImplementationId
*pId
= 0;
193 ::osl::MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
196 static ::cppu::OImplementationId
id( sal_False
);
200 return (*pId
).getImplementationId();
203 //===============================================================================
204 // EmbedProviderFactory_Impl
206 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
, const GUID
* pGuid
)
208 , m_xFactory( xFactory
)
213 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
217 sal_Bool
EmbedProviderFactory_Impl::registerClass()
221 o2u_attachCurrentThread();
223 hresult
= CoRegisterClassObject(
230 return (hresult
== NOERROR
);
233 sal_Bool
EmbedProviderFactory_Impl::deregisterClass()
235 HRESULT hresult
= CoRevokeClassObject( m_factoryHandle
);
237 return (hresult
== NOERROR
);
240 STDMETHODIMP
EmbedProviderFactory_Impl::QueryInterface(REFIID riid
, void FAR
* FAR
* ppv
)
242 if(IsEqualIID(riid
, IID_IUnknown
))
245 *ppv
= (IUnknown
*) (IClassFactory
*) this;
248 else if (IsEqualIID(riid
, IID_IClassFactory
))
251 *ppv
= (IClassFactory
*) this;
256 return ResultFromScode(E_NOINTERFACE
);
259 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::AddRef()
261 return osl_atomic_increment( &m_refCount
);
264 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::Release()
266 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex());
267 sal_Int32 nCount
= --m_refCount
;
276 STDMETHODIMP
EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
282 IUnknown
* pEmbedDocument
= (IUnknown
*)(IPersistStorage
*)( new EmbedDocument_Impl( m_xFactory
, &m_guid
) );
284 return pEmbedDocument
->QueryInterface( riid
, ppv
);
287 STDMETHODIMP
EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
292 // Fix strange warnings about some
293 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
294 // warning C4505: 'xxx' : unreferenced local function has been removed
295 #if defined(_MSC_VER)
296 #pragma warning(disable: 4505)
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */