Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uifactory / windowcontentfactorymanager.cxx
blob1918ae6fdd54d3fc8f82445e14e53d1983779695
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 <uifactory/configurationaccessfactorymanager.hxx>
23 #include <helper/mischelper.hxx>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/frame/ModuleManager.hpp>
27 #include <com/sun/star/frame/UnknownModuleException.hpp>
28 #include <com/sun/star/frame/XModuleManager2.hpp>
29 #include <com/sun/star/frame/XFrame.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <comphelper/compbase.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <rtl/ref.hxx>
37 #include <utility>
38 #include <comphelper/diagnose_ex.hxx>
40 using namespace ::com::sun::star;
41 using namespace framework;
43 namespace {
45 typedef comphelper::WeakComponentImplHelper<
46 css::lang::XServiceInfo,
47 css::lang::XSingleComponentFactory > WindowContentFactoryManager_BASE;
49 class WindowContentFactoryManager : public WindowContentFactoryManager_BASE
51 public:
52 explicit WindowContentFactoryManager( css::uno::Reference< css::uno::XComponentContext> xContext );
54 virtual OUString SAL_CALL getImplementationName() override
56 return "com.sun.star.comp.framework.WindowContentFactoryManager";
59 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
61 return cppu::supportsService(this, ServiceName);
64 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
66 return {"com.sun.star.ui.WindowContentFactoryManager"};
69 // XSingleComponentFactory
70 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
71 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const css::uno::Sequence< css::uno::Any >& Arguments, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
73 private:
74 virtual void disposing(std::unique_lock<std::mutex>&) override;
76 css::uno::Reference< css::uno::XComponentContext > m_xContext;
77 bool m_bConfigRead;
78 rtl::Reference<ConfigurationAccess_FactoryManager> m_pConfigAccess;
81 WindowContentFactoryManager::WindowContentFactoryManager( uno::Reference< uno::XComponentContext > xContext ) :
82 m_xContext(std::move( xContext )),
83 m_bConfigRead( false ),
84 m_pConfigAccess(
85 new ConfigurationAccess_FactoryManager(
86 m_xContext,
87 "/org.openoffice.Office.UI.WindowContentFactories/Registered/ContentFactories"))
90 void WindowContentFactoryManager::disposing(std::unique_lock<std::mutex>&)
92 m_pConfigAccess.clear();
95 // XSingleComponentFactory
96 uno::Reference< uno::XInterface > SAL_CALL WindowContentFactoryManager::createInstanceWithContext(
97 const uno::Reference< uno::XComponentContext >& /*xContext*/ )
99 uno::Reference< uno::XInterface > xWindow;
100 return xWindow;
103 uno::Reference< uno::XInterface > SAL_CALL WindowContentFactoryManager::createInstanceWithArgumentsAndContext(
104 const uno::Sequence< uno::Any >& Arguments, const uno::Reference< uno::XComponentContext >& Context )
106 uno::Reference< uno::XInterface > xWindow;
107 uno::Reference< frame::XFrame > xFrame;
108 OUString aResourceURL;
110 for (auto const & arg : Arguments )
112 beans::PropertyValue aPropValue;
113 if ( arg >>= aPropValue )
115 if ( aPropValue.Name == "Frame" )
116 aPropValue.Value >>= xFrame;
117 else if ( aPropValue.Name == "ResourceURL" )
118 aPropValue.Value >>= aResourceURL;
122 // Determine the module identifier
123 OUString aType;
124 OUString aName;
125 OUString aModuleId;
126 uno::Reference< frame::XModuleManager2 > xModuleManager =
127 frame::ModuleManager::create( m_xContext );
130 if ( xFrame.is() && xModuleManager.is() )
131 aModuleId = xModuleManager->identify( uno::Reference< uno::XInterface >( xFrame, uno::UNO_QUERY ) );
133 catch ( const frame::UnknownModuleException& )
137 RetrieveTypeNameFromResourceURL( aResourceURL, aType, aName );
138 if ( !aType.isEmpty() &&
139 !aName.isEmpty() &&
140 !aModuleId.isEmpty() )
142 OUString aImplementationName;
143 uno::Reference< uno::XInterface > xHolder( static_cast<cppu::OWeakObject*>(this), uno::UNO_QUERY );
145 // Determine the implementation name of the window content factory dependent on the
146 // module identifier, user interface element type and name
147 { // SAFE
148 std::unique_lock g(m_aMutex);
149 if (m_bDisposed) {
150 throw css::lang::DisposedException(
151 "disposed", static_cast<OWeakObject *>(this));
153 if ( !m_bConfigRead )
155 m_bConfigRead = true;
156 m_pConfigAccess->readConfigurationData();
158 aImplementationName = m_pConfigAccess->getFactorySpecifierFromTypeNameModule( aType, aName, aModuleId );
159 } // SAFE
161 if ( !aImplementationName.isEmpty() )
163 uno::Reference< lang::XMultiServiceFactory > xServiceManager( Context->getServiceManager(), uno::UNO_QUERY );
164 if ( xServiceManager.is() )
166 uno::Reference< lang::XSingleComponentFactory > xFactory(
167 xServiceManager->createInstance( aImplementationName ), uno::UNO_QUERY );
168 if ( xFactory.is() )
170 // Be careful: We call external code. Therefore here we have to catch all exceptions
173 xWindow = xFactory->createInstanceWithArgumentsAndContext( Arguments, Context );
175 catch ( uno::Exception& )
177 DBG_UNHANDLED_EXCEPTION("fwk");
184 // UNSAFE
185 if ( !xWindow.is())
187 // Fallback: Use internal factory code to create a toolkit dialog as a content window
188 xWindow = createInstanceWithContext(Context);
191 return xWindow;
196 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
197 com_sun_star_comp_framework_WindowContentFactoryManager_get_implementation(
198 css::uno::XComponentContext *context,
199 css::uno::Sequence<css::uno::Any> const &)
201 return cppu::acquire(new WindowContentFactoryManager(context));
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */