cid#1607171 Data race condition
[LibreOffice.git] / framework / source / uiconfiguration / moduleuicfgsupplier.cxx
blob53ef756d9e40e76026be52a6c39185d5eefe9e4f
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::lang;
41 using namespace com::sun::star::container;
42 using namespace com::sun::star::beans;
43 using namespace ::com::sun::star::ui;
44 using namespace ::com::sun::star::frame;
46 namespace {
48 typedef comphelper::WeakComponentImplHelper<
49 css::lang::XServiceInfo,
50 css::ui::XModuleUIConfigurationManagerSupplier >
51 ModuleUIConfigurationManagerSupplier_BASE;
53 class ModuleUIConfigurationManagerSupplier : public ModuleUIConfigurationManagerSupplier_BASE
55 public:
56 explicit ModuleUIConfigurationManagerSupplier( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
57 virtual ~ModuleUIConfigurationManagerSupplier() override;
59 virtual OUString SAL_CALL getImplementationName() override
61 return u"com.sun.star.comp.framework.ModuleUIConfigurationManagerSupplier"_ustr;
64 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
66 return cppu::supportsService(this, ServiceName);
69 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
71 return {u"com.sun.star.ui.ModuleUIConfigurationManagerSupplier"_ustr};
74 // XModuleUIConfigurationManagerSupplier
75 virtual css::uno::Reference< css::ui::XUIConfigurationManager > SAL_CALL getUIConfigurationManager( const OUString& ModuleIdentifier ) override;
77 private:
78 virtual void disposing(std::unique_lock<std::mutex>&) final override;
80 typedef std::unordered_map< OUString, css::uno::Reference< css::ui::XModuleUIConfigurationManager2 > > ModuleToModuleCfgMgr;
82 //TODO_AS void impl_initStorages();
84 ModuleToModuleCfgMgr m_aModuleToModuleUICfgMgrMap;
85 css::uno::Reference< css::frame::XModuleManager2 > m_xModuleMgr;
86 css::uno::Reference< css::uno::XComponentContext > m_xContext;
89 ModuleUIConfigurationManagerSupplier::ModuleUIConfigurationManagerSupplier( const Reference< XComponentContext >& xContext ) :
90 m_xModuleMgr( ModuleManager::create( xContext ) )
91 , m_xContext( xContext )
93 try
95 // Retrieve known modules and insert them into our unordered_map to speed-up access time.
96 Reference< XNameAccess > xNameAccess( m_xModuleMgr, UNO_QUERY_THROW );
97 const Sequence< OUString > aNameSeq = xNameAccess->getElementNames();
98 for ( const OUString& rName : aNameSeq )
99 m_aModuleToModuleUICfgMgrMap.emplace( rName, Reference< XModuleUIConfigurationManager2 >() );
101 catch(...)
106 ModuleUIConfigurationManagerSupplier::~ModuleUIConfigurationManagerSupplier()
108 std::unique_lock g(m_aMutex);
109 disposing(g);
112 void ModuleUIConfigurationManagerSupplier::disposing(std::unique_lock<std::mutex>&)
114 // dispose all our module user interface configuration managers
115 for (auto const& elem : m_aModuleToModuleUICfgMgrMap)
117 Reference< XComponent > xComponent( elem.second, UNO_QUERY );
118 if ( xComponent.is() )
119 xComponent->dispose();
121 m_aModuleToModuleUICfgMgrMap.clear();
122 m_xModuleMgr.clear();
125 // XModuleUIConfigurationManagerSupplier
126 Reference< XUIConfigurationManager > SAL_CALL ModuleUIConfigurationManagerSupplier::getUIConfigurationManager( const OUString& sModuleIdentifier )
128 std::unique_lock g(m_aMutex);
129 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
131 ModuleToModuleCfgMgr::iterator pIter = m_aModuleToModuleUICfgMgrMap.find( sModuleIdentifier );
132 if ( pIter == m_aModuleToModuleUICfgMgrMap.end() )
133 throw NoSuchElementException();
134 //TODO_AS impl_initStorages();
136 // Create instance on demand
137 if ( !pIter->second.is() )
139 OUString sShort;
142 Sequence< PropertyValue > lProps;
143 m_xModuleMgr->getByName(sModuleIdentifier) >>= lProps;
144 for (PropertyValue const& rProp : lProps)
146 if ( rProp.Name == "ooSetupFactoryShortName" )
148 rProp.Value >>= sShort;
149 break;
153 catch( const Exception& )
155 sShort.clear();
158 if (sShort.isEmpty())
159 throw NoSuchElementException();
161 pIter->second = css::ui::ModuleUIConfigurationManager::createDefault(m_xContext, sShort, sModuleIdentifier);
164 return pIter->second;
169 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
170 com_sun_star_comp_framework_ModuleUIConfigurationManagerSupplier_get_implementation(
171 css::uno::XComponentContext *context,
172 css::uno::Sequence<css::uno::Any> const &)
174 return cppu::acquire(new ModuleUIConfigurationManagerSupplier(context));
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */