Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uiconfiguration / moduleuicfgsupplier.cxx
blobcdbd647c310871657fd910035e6fa14874e84a01
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 <stdtypes.h>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/frame/ModuleManager.hpp>
26 #include <com/sun/star/io/XOutputStream.hpp>
27 #include <com/sun/star/ui/ModuleUIConfigurationManager.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
30 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
31 #include <com/sun/star/ui/XModuleUIConfigurationManager2.hpp>
32 #include <com/sun/star/frame/XModuleManager2.hpp>
34 #include <comphelper/compbase.hxx>
35 #include <cppuhelper/supportsservice.hxx>
37 #include <unordered_map>
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::io;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::container;
43 using namespace com::sun::star::beans;
44 using namespace com::sun::star::embed;
45 using namespace ::com::sun::star::ui;
46 using namespace ::com::sun::star::frame;
47 using namespace framework;
49 namespace {
51 typedef comphelper::WeakComponentImplHelper<
52 css::lang::XServiceInfo,
53 css::ui::XModuleUIConfigurationManagerSupplier >
54 ModuleUIConfigurationManagerSupplier_BASE;
56 class ModuleUIConfigurationManagerSupplier : public ModuleUIConfigurationManagerSupplier_BASE
58 public:
59 explicit ModuleUIConfigurationManagerSupplier( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
60 virtual ~ModuleUIConfigurationManagerSupplier() override;
62 virtual OUString SAL_CALL getImplementationName() override
64 return "com.sun.star.comp.framework.ModuleUIConfigurationManagerSupplier";
67 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
69 return cppu::supportsService(this, ServiceName);
72 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
74 return {"com.sun.star.ui.ModuleUIConfigurationManagerSupplier"};
77 // XModuleUIConfigurationManagerSupplier
78 virtual css::uno::Reference< css::ui::XUIConfigurationManager > SAL_CALL getUIConfigurationManager( const OUString& ModuleIdentifier ) override;
80 private:
81 virtual void disposing(std::unique_lock<std::mutex>&) final override;
83 typedef std::unordered_map< OUString, css::uno::Reference< css::ui::XModuleUIConfigurationManager2 > > ModuleToModuleCfgMgr;
85 //TODO_AS void impl_initStorages();
87 ModuleToModuleCfgMgr m_aModuleToModuleUICfgMgrMap;
88 css::uno::Reference< css::frame::XModuleManager2 > m_xModuleMgr;
89 css::uno::Reference< css::uno::XComponentContext > m_xContext;
92 ModuleUIConfigurationManagerSupplier::ModuleUIConfigurationManagerSupplier( const Reference< XComponentContext >& xContext ) :
93 m_xModuleMgr( ModuleManager::create( xContext ) )
94 , m_xContext( xContext )
96 try
98 // Retrieve known modules and insert them into our unordered_map to speed-up access time.
99 Reference< XNameAccess > xNameAccess( m_xModuleMgr, UNO_QUERY_THROW );
100 const Sequence< OUString > aNameSeq = xNameAccess->getElementNames();
101 for ( const OUString& rName : aNameSeq )
102 m_aModuleToModuleUICfgMgrMap.emplace( rName, Reference< XModuleUIConfigurationManager2 >() );
104 catch(...)
109 ModuleUIConfigurationManagerSupplier::~ModuleUIConfigurationManagerSupplier()
111 std::unique_lock g(m_aMutex);
112 disposing(g);
115 void ModuleUIConfigurationManagerSupplier::disposing(std::unique_lock<std::mutex>&)
117 // dispose all our module user interface configuration managers
118 for (auto const& elem : m_aModuleToModuleUICfgMgrMap)
120 Reference< XComponent > xComponent( elem.second, UNO_QUERY );
121 if ( xComponent.is() )
122 xComponent->dispose();
124 m_aModuleToModuleUICfgMgrMap.clear();
125 m_xModuleMgr.clear();
128 // XModuleUIConfigurationManagerSupplier
129 Reference< XUIConfigurationManager > SAL_CALL ModuleUIConfigurationManagerSupplier::getUIConfigurationManager( const OUString& sModuleIdentifier )
131 std::unique_lock g(m_aMutex);
132 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
134 ModuleToModuleCfgMgr::iterator pIter = m_aModuleToModuleUICfgMgrMap.find( sModuleIdentifier );
135 if ( pIter == m_aModuleToModuleUICfgMgrMap.end() )
136 throw NoSuchElementException();
137 //TODO_AS impl_initStorages();
139 // Create instance on demand
140 if ( !pIter->second.is() )
142 OUString sShort;
145 Sequence< PropertyValue > lProps;
146 m_xModuleMgr->getByName(sModuleIdentifier) >>= lProps;
147 for (PropertyValue const & rProp : std::as_const(lProps))
149 if ( rProp.Name == "ooSetupFactoryShortName" )
151 rProp.Value >>= sShort;
152 break;
156 catch( const Exception& )
158 sShort.clear();
161 if (sShort.isEmpty())
162 throw NoSuchElementException();
164 pIter->second = css::ui::ModuleUIConfigurationManager::createDefault(m_xContext, sShort, sModuleIdentifier);
167 return pIter->second;
172 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
173 com_sun_star_comp_framework_ModuleUIConfigurationManagerSupplier_get_implementation(
174 css::uno::XComponentContext *context,
175 css::uno::Sequence<css::uno::Any> const &)
177 return cppu::acquire(new ModuleUIConfigurationManagerSupplier(context));
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */