Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / embedserv / source / embed / servprov.cxx
blob209163d1dad29ef249e7ffeddc9cb14d8a2c0a12
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 .
19 #ifdef _MSC_VER
20 #pragma warning(disable : 4917 4555)
21 #endif
23 #include "stdafx.h"
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,
37 &OID_CalcServer,
38 &OID_CalcOASISServer,
39 &OID_DrawingServer,
40 &OID_DrawingOASISServer,
41 &OID_PresentationServer,
42 &OID_PresentationOASISServer,
43 &OID_MathServer,
44 &OID_MathOASISServer
47 class CurThreadData
49 public:
50 CurThreadData();
51 virtual ~CurThreadData();
53 sal_Bool SAL_CALL setData(void *pData);
55 void* SAL_CALL getData();
57 protected:
58 oslThreadKey m_hKey;
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);
90 if (!SUCCEEDED(hr))
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);
101 // EmbedServer_Impl
103 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
104 m_xFactory( xFactory)
106 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
108 m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
109 m_pOLEFactories[nInd]->registerClass();
113 EmbedServer_Impl::~EmbedServer_Impl()
115 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
117 if ( m_pOLEFactories[nInd] )
118 m_pOLEFactories[nInd]->deregisterClass();
122 OUString EmbedServer_Impl::getImplementationName()
123 throw (css::uno::RuntimeException, std::exception)
125 return OUString("com.sun.star.comp.ole.EmbedServer");
128 sal_Bool EmbedServer_Impl::supportsService(OUString const & ServiceName)
129 throw (css::uno::RuntimeException, std::exception)
131 return cppu::supportsService(this, ServiceName);
134 css::uno::Sequence<OUString> EmbedServer_Impl::getSupportedServiceNames()
135 throw (css::uno::RuntimeException, std::exception)
137 return css::uno::Sequence<OUString>{
138 "com.sun.star.document.OleEmbeddedServerRegistration"};
141 // EmbedProviderFactory_Impl
143 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
144 : m_refCount( 0L )
145 , m_guid( *pGuid )
146 , m_xFactory( xFactory )
150 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
154 sal_Bool EmbedProviderFactory_Impl::registerClass()
156 HRESULT hresult;
158 o2u_attachCurrentThread();
160 hresult = CoRegisterClassObject(
161 m_guid,
162 this,
163 CLSCTX_LOCAL_SERVER,
164 REGCLS_MULTIPLEUSE,
165 &m_factoryHandle);
167 return (hresult == NOERROR);
170 sal_Bool EmbedProviderFactory_Impl::deregisterClass()
172 HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
174 return (hresult == NOERROR);
177 STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void FAR* FAR* ppv)
179 if(IsEqualIID(riid, IID_IUnknown))
181 AddRef();
182 *ppv = (IUnknown*) (IClassFactory*) this;
183 return NOERROR;
185 else if (IsEqualIID(riid, IID_IClassFactory))
187 AddRef();
188 *ppv = (IClassFactory*) this;
189 return NOERROR;
192 *ppv = NULL;
193 return ResultFromScode(E_NOINTERFACE);
196 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
198 return osl_atomic_increment( &m_refCount);
201 STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
203 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
204 sal_Int32 nCount = --m_refCount;
205 if ( nCount == 0 )
207 delete this;
210 return nCount;
213 STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown FAR* punkOuter,
214 REFIID riid,
215 void FAR* FAR* ppv)
217 punkOuter = NULL;
219 IUnknown* pEmbedDocument = (IUnknown*)(IPersistStorage*)( new EmbedDocument_Impl( m_xFactory, &m_guid ) );
221 return pEmbedDocument->QueryInterface( riid, ppv );
224 STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
226 return NOERROR;
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */