Avoid potential negative array index access to cached text.
[LibreOffice.git] / embedserv / source / embed / servprov.cxx
blob44eab63dd8b76ffd6a6d0eadb23a3c8914d5c5a1
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 <com/sun/star/uno/XComponentContext.hpp>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
28 #include <osl/thread.h>
29 #include <sal/log.hxx>
31 using namespace com::sun::star;
33 const GUID* const guidList[ SUPPORTED_FACTORIES_NUM ] = {
34 &OID_WriterTextServer,
35 &OID_WriterOASISTextServer,
36 &OID_CalcServer,
37 &OID_CalcOASISServer,
38 &OID_DrawingServer,
39 &OID_DrawingOASISServer,
40 &OID_PresentationServer,
41 &OID_PresentationOASISServer,
42 &OID_MathServer,
43 &OID_MathOASISServer
46 static void o2u_attachCurrentThread()
48 [[maybe_unused]] static thread_local bool aInit = []
50 HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
51 if (!SUCCEEDED(hr))
52 { // FIXME: is it a problem that this ends up in STA currently?
53 assert(RPC_E_CHANGED_MODE == hr);
54 SAL_INFO("embedserv.ole",
55 "CoInitializeEx fail: probably thread is in STA already?");
57 return SUCCEEDED(hr);
58 }();
62 // EmbedServer_Impl
64 EmbedServer_Impl::EmbedServer_Impl( const uno::Reference<lang::XMultiServiceFactory>& xFactory):
65 m_xFactory( xFactory)
67 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
69 m_pOLEFactories[nInd] = new EmbedProviderFactory_Impl( m_xFactory, guidList[nInd] );
70 m_pOLEFactories[nInd]->registerClass();
74 EmbedServer_Impl::~EmbedServer_Impl()
76 for( int nInd = 0; nInd < SUPPORTED_FACTORIES_NUM; nInd++ )
78 if ( m_pOLEFactories[nInd] )
79 m_pOLEFactories[nInd]->deregisterClass();
83 OUString EmbedServer_Impl::getImplementationName()
85 return "com.sun.star.comp.ole.EmbedServer";
88 sal_Bool EmbedServer_Impl::supportsService(OUString const & ServiceName)
90 return cppu::supportsService(this, ServiceName);
93 css::uno::Sequence<OUString> EmbedServer_Impl::getSupportedServiceNames()
95 return css::uno::Sequence<OUString>{
96 "com.sun.star.document.OleEmbeddedServerRegistration"};
99 // EmbedProviderFactory_Impl
101 EmbedProviderFactory_Impl::EmbedProviderFactory_Impl(const uno::Reference<lang::XMultiServiceFactory>& xFactory, const GUID* pGuid)
102 : m_refCount( 0 )
103 , m_guid( *pGuid )
104 , m_xFactory( xFactory )
108 EmbedProviderFactory_Impl::~EmbedProviderFactory_Impl()
112 bool EmbedProviderFactory_Impl::registerClass()
114 HRESULT hresult;
116 o2u_attachCurrentThread();
118 hresult = CoRegisterClassObject(
119 m_guid,
120 this,
121 CLSCTX_LOCAL_SERVER,
122 REGCLS_MULTIPLEUSE,
123 &m_factoryHandle);
125 return (hresult == NOERROR);
128 bool EmbedProviderFactory_Impl::deregisterClass()
130 HRESULT hresult = CoRevokeClassObject( m_factoryHandle );
132 return (hresult == NOERROR);
135 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::QueryInterface(REFIID riid, void** ppv)
137 if(IsEqualIID(riid, IID_IUnknown))
139 AddRef();
140 *ppv = static_cast<IUnknown*>(static_cast<IClassFactory*>(this));
141 return NOERROR;
143 else if (IsEqualIID(riid, IID_IClassFactory))
145 AddRef();
146 *ppv = static_cast<IClassFactory*>(this);
147 return NOERROR;
150 *ppv = nullptr;
151 return ResultFromScode(E_NOINTERFACE);
154 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::AddRef()
156 return osl_atomic_increment( &m_refCount);
159 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) EmbedProviderFactory_Impl::Release()
161 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
162 sal_Int32 nCount = --m_refCount;
163 if ( nCount == 0 )
165 delete this;
168 return nCount;
171 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::CreateInstance(IUnknown*,
172 REFIID riid, void** ppv)
174 IUnknown* pEmbedDocument = static_cast<IPersistStorage*>( new EmbedDocument_Impl( m_xFactory, &m_guid ) );
176 return pEmbedDocument->QueryInterface( riid, ppv );
179 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedProviderFactory_Impl::LockServer( int /*fLock*/ )
181 return NOERROR;
185 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
186 embedserv_EmbedServer(
187 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
189 auto msf = uno::Reference<lang::XMultiServiceFactory>(context->getServiceManager(), css::uno::UNO_QUERY_THROW);
190 return cppu::acquire(new EmbedServer_Impl(msf));
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */