nss: upgrade to release 3.73
[LibreOffice.git] / scripting / source / provider / ProviderCache.cxx
blob4291d811c7a831c0a3ceea55c2f671108d9dc214
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 <comphelper/sequence.hxx>
21 #include <tools/diagnose_ex.h>
22 #include <sal/log.hxx>
24 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
25 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include "ProviderCache.hxx"
29 using namespace com::sun::star;
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::script;
33 namespace func_provider
36 ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) : m_Sctx( scriptContext ), m_xContext( xContext )
38 // initialise m_hProviderDetailsCache with details of ScriptProviders
39 // will use createContentEnumeration
41 m_xMgr = m_xContext->getServiceManager();
42 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
43 populateCache();
47 ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< OUString >& denyList ) : m_sDenyList( denyList ), m_Sctx( scriptContext ), m_xContext( xContext )
50 // initialise m_hProviderDetailsCache with details of ScriptProviders
51 // will use createContentEnumeration
53 m_xMgr = m_xContext->getServiceManager();
54 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
55 populateCache();
58 ProviderCache::~ProviderCache()
62 Reference< provider::XScriptProvider >
63 ProviderCache::getProvider( const OUString& providerName )
65 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
66 Reference< provider::XScriptProvider > provider;
67 ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
68 if ( h_it != m_hProviderDetailsCache.end() )
70 if ( h_it->second.provider.is() )
72 provider = h_it->second.provider;
74 else
76 // need to create provider and insert into hash
77 provider = createProvider( h_it->second );
80 return provider;
83 Sequence < Reference< provider::XScriptProvider > >
84 ProviderCache::getAllProviders()
86 // need to create providers that haven't been created already
87 // so check what providers exist and what ones don't
89 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
90 Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() );
91 // should assert if size !> 0
92 if ( !m_hProviderDetailsCache.empty() )
94 sal_Int32 providerIndex = 0;
95 for (auto& rDetail : m_hProviderDetailsCache)
97 Reference<provider::XScriptProvider> xScriptProvider = rDetail.second.provider;
98 if ( xScriptProvider.is() )
100 providers[ providerIndex++ ] = xScriptProvider;
102 else
104 // create provider
107 xScriptProvider = createProvider(rDetail.second);
108 providers[ providerIndex++ ] = xScriptProvider;
110 catch ( const Exception& )
112 DBG_UNHANDLED_EXCEPTION("scripting");
117 if (providerIndex < providers.getLength())
119 providers.realloc( providerIndex );
123 else
125 SAL_WARN("scripting", "no available providers, something very wrong!!!");
127 return providers;
130 void
131 ProviderCache::populateCache()
133 // wrong name in services.rdb
134 OUString serviceName;
135 ::osl::Guard< osl::Mutex > aGuard( m_mutex );
138 Reference< container::XContentEnumerationAccess > xEnumAccess( m_xMgr, UNO_QUERY_THROW );
139 Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( "com.sun.star.script.provider.LanguageScriptProvider" );
141 while ( xEnum->hasMoreElements() )
144 Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
145 Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
147 Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
149 if ( serviceNames.hasElements() )
151 auto pName = std::find_if(serviceNames.begin(), serviceNames.end(),
152 [this](const OUString& rName) {
153 return rName.startsWith("com.sun.star.script.provider.ScriptProviderFor")
154 && !isInDenyList(rName);
156 if (pName != serviceNames.end())
158 serviceName = *pName;
159 ProviderDetails details;
160 details.factory = factory;
161 m_hProviderDetailsCache[ serviceName ] = details;
166 catch ( const Exception &e )
168 css::uno::Any anyEx = cppu::getCaughtException();
169 throw css::lang::WrappedTargetRuntimeException(
170 "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " + serviceName
171 + " " + e.Message,
172 nullptr, anyEx );
176 Reference< provider::XScriptProvider >
177 ProviderCache::createProvider( ProviderDetails& details )
181 details.provider.set(
182 details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
184 catch ( const Exception& e )
186 css::uno::Any anyEx = cppu::getCaughtException();
187 throw css::lang::WrappedTargetRuntimeException(
188 "ProviderCache::createProvider() Error creating provider from factory. " + e.Message,
189 nullptr, anyEx );
192 return details.provider;
195 bool
196 ProviderCache::isInDenyList( const OUString& serviceName )
198 return comphelper::findValue(m_sDenyList, serviceName) != -1;
200 } //end namespace
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */