Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uifactory / uicontrollerfactory.cxx
blobfa7c76d6114409132a0dbe8462b38e3bb11d6e7b
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 <sal/config.h>
22 #include <string_view>
24 #include <uifactory/factoryconfiguration.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/frame/XUIControllerFactory.hpp>
30 #include <rtl/ref.hxx>
31 #include <comphelper/compbase.hxx>
32 #include <cppuhelper/supportsservice.hxx>
34 using namespace css::uno;
35 using namespace css::lang;
36 using namespace css::beans;
37 using namespace css::container;
38 using namespace css::frame;
39 using namespace framework;
41 namespace {
43 typedef comphelper::WeakComponentImplHelper<
44 css::lang::XServiceInfo,
45 css::frame::XUIControllerFactory > UIControllerFactory_BASE;
47 class UIControllerFactory : public UIControllerFactory_BASE
49 public:
50 virtual ~UIControllerFactory() override;
52 // XMultiComponentFactory
53 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( const OUString& aServiceSpecifier, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
54 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const OUString& ServiceSpecifier, const css::uno::Sequence< css::uno::Any >& Arguments, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
55 virtual css::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames() override;
57 // XUIControllerRegistration
58 virtual sal_Bool SAL_CALL hasController( const OUString& aCommandURL, const OUString& aModuleName ) override;
59 virtual void SAL_CALL registerController( const OUString& aCommandURL, const OUString& aModuleName, const OUString& aControllerImplementationName ) override;
60 virtual void SAL_CALL deregisterController( const OUString& aCommandURL, const OUString& aModuleName ) override;
62 protected:
63 UIControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext, std::u16string_view rUINode );
64 bool m_bConfigRead;
65 css::uno::Reference< css::uno::XComponentContext > m_xContext;
66 rtl::Reference<ConfigurationAccess_ControllerFactory> m_pConfigAccess;
68 private:
69 virtual void disposing(std::unique_lock<std::mutex>&) final override;
72 UIControllerFactory::UIControllerFactory(
73 const Reference< XComponentContext >& xContext,
74 std::u16string_view rConfigurationNode )
75 : m_bConfigRead( false )
76 , m_xContext( xContext )
78 m_pConfigAccess = new ConfigurationAccess_ControllerFactory(m_xContext,
79 OUString::Concat("/org.openoffice.Office.UI.Controller/Registered/")
80 + rConfigurationNode);
83 UIControllerFactory::~UIControllerFactory()
85 std::unique_lock g(m_aMutex);
86 disposing(g);
89 void UIControllerFactory::disposing(std::unique_lock<std::mutex>&)
91 m_pConfigAccess.clear();
94 // XMultiComponentFactory
95 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
96 const OUString& aServiceSpecifier,
97 const Reference< XComponentContext >& )
99 // SAFE
100 std::unique_lock g(m_aMutex);
102 if ( !m_bConfigRead )
104 m_bConfigRead = true;
105 m_pConfigAccess->readConfigurationData();
108 OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, std::u16string_view() );
109 if ( !aServiceName.isEmpty() )
110 return m_xContext->getServiceManager()->createInstanceWithContext( aServiceName, m_xContext );
111 else
112 return Reference< XInterface >();
113 // SAFE
116 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithArgumentsAndContext(
117 const OUString& ServiceSpecifier,
118 const Sequence< Any >& Arguments,
119 const Reference< XComponentContext >& )
121 static constexpr OUStringLiteral aPropModuleName( u"ModuleIdentifier" );
123 OUString aPropName;
124 PropertyValue aPropValue;
126 // Retrieve the optional module name from the Arguments sequence. It is used as a part of
127 // the hash map key to support different controller implementation for the same URL but different
128 // module!!
129 for ( Any const & arg : Arguments )
131 if (( arg >>= aPropValue ) && ( aPropValue.Name == aPropModuleName ))
133 aPropValue.Value >>= aPropName;
134 break;
138 Sequence< Any > aNewArgs( Arguments );
140 sal_Int32 nAppendIndex = aNewArgs.getLength();
141 aNewArgs.realloc( aNewArgs.getLength() + 2 );
142 auto pNewArgs = aNewArgs.getArray();
144 // Append the command URL to the Arguments sequence so that one controller can be
145 // used for more than one command URL.
146 aPropValue.Name = "CommandURL";
147 aPropValue.Value <<= ServiceSpecifier;
148 pNewArgs[nAppendIndex] <<= aPropValue;
150 // Append the optional value argument. It's an empty string if no additional info
151 // is provided to the controller.
152 OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
153 aPropValue.Name = "Value";
154 aPropValue.Value <<= aValue;
155 pNewArgs[nAppendIndex+1] <<= aPropValue;
158 OUString aServiceName;
159 { // SAFE
160 std::unique_lock g(m_aMutex);
162 if ( !m_bConfigRead )
164 m_bConfigRead = true;
165 m_pConfigAccess->readConfigurationData();
168 aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
169 } // SAFE
171 if ( !aServiceName.isEmpty() )
172 return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, m_xContext );
173 else
174 return Reference< XInterface >();
178 Sequence< OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
180 return Sequence< OUString >();
183 // XUIControllerRegistration
184 sal_Bool SAL_CALL UIControllerFactory::hasController(
185 const OUString& aCommandURL,
186 const OUString& aModuleName )
188 std::unique_lock g(m_aMutex);
190 if ( !m_bConfigRead )
192 m_bConfigRead = true;
193 m_pConfigAccess->readConfigurationData();
196 return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
199 void SAL_CALL UIControllerFactory::registerController(
200 const OUString& aCommandURL,
201 const OUString& aModuleName,
202 const OUString& aControllerImplementationName )
204 // SAFE
205 std::unique_lock g(m_aMutex);
207 if ( !m_bConfigRead )
209 m_bConfigRead = true;
210 m_pConfigAccess->readConfigurationData();
213 m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
214 // SAFE
217 void SAL_CALL UIControllerFactory::deregisterController(
218 const OUString& aCommandURL,
219 const OUString& aModuleName )
221 // SAFE
222 std::unique_lock g(m_aMutex);
224 if ( !m_bConfigRead )
226 m_bConfigRead = true;
227 m_pConfigAccess->readConfigurationData();
230 m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
231 // SAFE
234 class PopupMenuControllerFactory : public UIControllerFactory
236 public:
237 explicit PopupMenuControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
239 virtual OUString SAL_CALL getImplementationName() override
241 return "com.sun.star.comp.framework.PopupMenuControllerFactory";
244 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
246 return cppu::supportsService(this, ServiceName);
249 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
251 return {"com.sun.star.frame.PopupMenuControllerFactory"};
256 PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
257 UIControllerFactory( xContext, u"PopupMenu" )
261 class ToolbarControllerFactory : public UIControllerFactory
263 public:
264 explicit ToolbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
266 virtual OUString SAL_CALL getImplementationName() override
268 return "com.sun.star.comp.framework.ToolBarControllerFactory";
271 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
273 return cppu::supportsService(this, ServiceName);
276 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
278 return {"com.sun.star.frame.ToolbarControllerFactory"};
283 ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
284 UIControllerFactory( xContext, u"ToolBar" )
288 class StatusbarControllerFactory : public UIControllerFactory
290 public:
291 explicit StatusbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
293 virtual OUString SAL_CALL getImplementationName() override
295 return "com.sun.star.comp.framework.StatusBarControllerFactory";
298 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
300 return cppu::supportsService(this, ServiceName);
303 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
305 return {"com.sun.star.frame.StatusbarControllerFactory"};
310 StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
311 UIControllerFactory( xContext, u"StatusBar" )
317 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
318 com_sun_star_comp_framework_PopupMenuControllerFactory_get_implementation(
319 css::uno::XComponentContext *context,
320 css::uno::Sequence<css::uno::Any> const &)
322 return cppu::acquire(new PopupMenuControllerFactory(context));
325 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
326 com_sun_star_comp_framework_ToolBarControllerFactory_get_implementation(
327 css::uno::XComponentContext *context,
328 css::uno::Sequence<css::uno::Any> const &)
330 return cppu::acquire(new ToolbarControllerFactory(context));
333 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
334 com_sun_star_comp_framework_StatusBarControllerFactory_get_implementation(
335 css::uno::XComponentContext *context,
336 css::uno::Sequence<css::uno::Any> const &)
338 return cppu::acquire(new StatusbarControllerFactory(context));
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */