Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / embedserv / source / embed / servprov.cxx
blob09ef6211724e066132dccc8986ab5ad4ceb7e788
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <stdafx.h>
21 #include <servprov.hxx>
22 #include <embeddoc.hxx>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <osl/diagnose.h>
26 #include <osl/mutex.hxx>
27 #include <osl/thread.h>
28 #include <sal/log.hxx>
30 using namespace com::sun::star;
32 const GUID* const guidList[ SUPPORTED_FACTORIES_NUM ] = {
33 &OID_WriterTextServer,
34 &OID_WriterOASISTextServer,
35 &OID_CalcServer,
36 &OID_CalcOASISServer,
37 &OID_DrawingServer,
38 &OID_DrawingOASISServer,
39 &OID_PresentationServer,
40 &OID_PresentationOASISServer,
41 &OID_MathServer,
42 &OID_MathOASISServer
45 class CurThreadData
47 public:
48 CurThreadData();
49 virtual ~CurThreadData();
51 bool setData(void *pData);
53 void* getData();
55 protected:
56 oslThreadKey m_hKey;
59 CurThreadData::CurThreadData()
61 m_hKey = osl_createThreadKey( nullptr );
64 CurThreadData::~CurThreadData()
66 osl_destroyThreadKey(m_hKey);
69 bool CurThreadData::setData(void *pData)
71 OSL_ENSURE( m_hKey, "No thread key!" );
72 return osl_setThreadKeyData(m_hKey, pData);
75 void *CurThreadData::getData()
77 OSL_ENSURE( m_hKey, "No thread key!" );
78 return osl_getThreadKeyData(m_hKey);
81 static void o2u_attachCurrentThread()
83 static CurThreadData oleThreadData;
85 if ( oleThreadData.getData() != nullptr )
87 HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
88 if (!SUCCEEDED(hr))
89 { // FIXME: is it a problem that this ends up in STA currently?
90 assert(RPC_E_CHANGED_MODE == hr);
91 SAL_INFO("embedserv.ole",
92 "CoInitializeEx fail: probably thread is in STA already?");
94 oleThreadData.setData(reinterpret_cast<void*>(true));
99 // EmbedServer_Impl
101 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
102 m_xFactory( xFactory)
104 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
106 m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
107 m_pOLEFactories[nInd]->registerClass();
111 EmbedServer_Impl::~EmbedServer_Impl()
113 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
115 if ( m_pOLEFactories[nInd] )
116 m_pOLEFactories[nInd]->deregisterClass();
120 OUString EmbedServer_Impl::getImplementationName()
122 return "com.sun.star.comp.ole.EmbedServer";
125 sal_Bool EmbedServer_Impl::supportsService(OUString const & ServiceName)
127 return cppu::supportsService(this, ServiceName);
130 css::uno::Sequence<OUString> EmbedServer_Impl::getSupportedServiceNames()
132 return css::uno::Sequence<OUString>{
133 "com.sun.star.document.OleEmbeddedServerRegistration"};
136 // EmbedProviderFactory_Impl
138 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
139 : m_refCount( 0 )
140 , m_guid( *pGuid )
141 , m_xFactory( xFactory )
145 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
149 bool EmbedProviderFactory_Impl::registerClass()
151 HRESULT hresult;
153 o2u_attachCurrentThread();
155 hresult = CoRegisterClassObject(
156 m_guid,
157 this,
158 CLSCTX_LOCAL_SERVER,
159 REGCLS_MULTIPLEUSE,
160 &m_factoryHandle);
162 return (hresult == NOERROR);
165 bool EmbedProviderFactory_Impl::deregisterClass()
167 HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
169 return (hresult == NOERROR);
172 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
174 if(IsEqualIID(riid, IID_IUnknown))
176 AddRef();
177 *ppv = static_cast<IUnknown*>(static_cast<IClassFactory*>(this));
178 return NOERROR;
180 else if (IsEqualIID(riid, IID_IClassFactory))
182 AddRef();
183 *ppv = static_cast<IClassFactory*>(this);
184 return NOERROR;
187 *ppv = nullptr;
188 return ResultFromScode(E_NOINTERFACE);
191 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
193 return osl_atomic_increment( &m_refCount);
196 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
198 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
199 sal_Int32 nCount = --m_refCount;
200 if ( nCount == 0 )
202 delete this;
205 return nCount;
208 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR* punkOuter,
209 REFIID riid,
210 void FAR* FAR* ppv)
212 punkOuter = nullptr;
214 IUnknown* pEmbedDocument = static_cast<IUnknown*>(static_cast<IPersistStorage*>( new EmbedDocument_Impl( m_xFactory, &m_guid ) ));
216 return pEmbedDocument->QueryInterface( riid, ppv );
219 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
221 return NOERROR;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */