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/supportsservice.hxx>
28 #include <osl/diagnose.h>
29 #include <osl/mutex.hxx>
30 #include <osl/thread.h>
32 using namespace com::sun::star
;
34 const GUID
* guidList
[ SUPPORTED_FACTORIES_NUM
] = {
35 &OID_WriterTextServer
,
36 &OID_WriterOASISTextServer
,
40 &OID_DrawingOASISServer
,
41 &OID_PresentationServer
,
42 &OID_PresentationOASISServer
,
51 virtual ~CurThreadData();
53 sal_Bool SAL_CALL
setData(void *pData
);
55 void* SAL_CALL
getData();
61 CurThreadData::CurThreadData()
63 m_hKey
= osl_createThreadKey( (oslThreadKeyCallbackFunction
)NULL
);
66 CurThreadData::~CurThreadData()
68 osl_destroyThreadKey(m_hKey
);
71 sal_Bool
CurThreadData::setData(void *pData
)
73 OSL_ENSURE( m_hKey
, "No thread key!\n" );
74 return (osl_setThreadKeyData(m_hKey
, pData
));
77 void *CurThreadData::getData()
79 OSL_ENSURE( m_hKey
, "No thread key!\n" );
80 return (osl_getThreadKeyData(m_hKey
));
83 void o2u_attachCurrentThread()
85 static CurThreadData oleThreadData
;
87 if ( oleThreadData
.getData() != 0 )
89 HRESULT hr
= CoInitializeEx(0, COINIT_MULTITHREADED
);
91 { // FIXME: is it a problem that this ends up in STA currently?
92 assert(RPC_E_CHANGED_MODE
== hr
);
93 SAL_INFO("embedserv.ole",
94 "CoInitializeEx fail: probably thread is in STA already?");
96 oleThreadData
.setData((void*)sal_True
);
104 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
):
105 m_xFactory( xFactory
)
107 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
109 m_pOLEFactories
[nInd
] = new EmbedProviderFactory_Impl( m_xFactory
, guidList
[nInd
] );
110 m_pOLEFactories
[nInd
]->registerClass();
114 EmbedServer_Impl::~EmbedServer_Impl()
116 for( int nInd
= 0; nInd
< SUPPORTED_FACTORIES_NUM
; nInd
++ )
118 if ( m_pOLEFactories
[nInd
] )
119 m_pOLEFactories
[nInd
]->deregisterClass();
123 OUString
EmbedServer_Impl::getImplementationName()
124 throw (css::uno::RuntimeException
, std::exception
)
126 return OUString("com.sun.star.comp.ole.EmbedServer");
129 sal_Bool
EmbedServer_Impl::supportsService(OUString
const & ServiceName
)
130 throw (css::uno::RuntimeException
, std::exception
)
132 return cppu::supportsService(this, ServiceName
);
135 css::uno::Sequence
<OUString
> EmbedServer_Impl::getSupportedServiceNames()
136 throw (css::uno::RuntimeException
, std::exception
)
138 return css::uno::Sequence
<OUString
>{
139 "com.sun.star.document.OleEmbeddedServerRegistration"};
142 // EmbedProviderFactory_Impl
144 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference
<lang::XMultiServiceFactory
>& xFactory
, const GUID
* pGuid
)
146 , m_xFactory( xFactory
)
151 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
155 sal_Bool
EmbedProviderFactory_Impl::registerClass()
159 o2u_attachCurrentThread();
161 hresult
= CoRegisterClassObject(
168 return (hresult
== NOERROR
);
171 sal_Bool
EmbedProviderFactory_Impl::deregisterClass()
173 HRESULT hresult
= CoRevokeClassObject( m_factoryHandle
);
175 return (hresult
== NOERROR
);
178 STDMETHODIMP
EmbedProviderFactory_Impl::QueryInterface(REFIID riid
, void FAR
* FAR
* ppv
)
180 if(IsEqualIID(riid
, IID_IUnknown
))
183 *ppv
= (IUnknown
*) (IClassFactory
*) this;
186 else if (IsEqualIID(riid
, IID_IClassFactory
))
189 *ppv
= (IClassFactory
*) this;
194 return ResultFromScode(E_NOINTERFACE
);
197 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::AddRef()
199 return osl_atomic_increment( &m_refCount
);
202 STDMETHODIMP_(ULONG
) EmbedProviderFactory_Impl::Release()
204 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex());
205 sal_Int32 nCount
= --m_refCount
;
214 STDMETHODIMP
EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR
* punkOuter
,
220 IUnknown
* pEmbedDocument
= (IUnknown
*)(IPersistStorage
*)( new EmbedDocument_Impl( m_xFactory
, &m_guid
) );
222 return pEmbedDocument
->QueryInterface( riid
, ppv
);
225 STDMETHODIMP
EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */