Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / desktop / source / deployment / manager / dp_managerfac.cxx
blob79e0ea3588dbb4c9b49ae31b291ffea640713d47
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 .
21 #include "dp_manager.h"
22 #include <dp_misc.h>
23 #include <cppuhelper/compbase.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <com/sun/star/deployment/XPackageManagerFactory.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <unordered_map>
29 using namespace ::dp_misc;
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
33 namespace dp_manager::factory {
35 typedef ::cppu::WeakComponentImplHelper<
36 deployment::XPackageManagerFactory, lang::XServiceInfo > t_pmfac_helper;
38 namespace {
40 class PackageManagerFactoryImpl : private cppu::BaseMutex, public t_pmfac_helper
42 Reference<XComponentContext> m_xComponentContext;
44 Reference<deployment::XPackageManager> m_xUserMgr;
45 Reference<deployment::XPackageManager> m_xSharedMgr;
46 Reference<deployment::XPackageManager> m_xBundledMgr;
47 Reference<deployment::XPackageManager> m_xTmpMgr;
48 Reference<deployment::XPackageManager> m_xBakMgr;
49 typedef std::unordered_map<
50 OUString, WeakReference<deployment::XPackageManager> > t_string2weakref;
51 t_string2weakref m_managers;
53 protected:
54 inline void check();
55 virtual void SAL_CALL disposing() override;
57 public:
58 explicit PackageManagerFactoryImpl(
59 Reference<XComponentContext> const & xComponentContext );
61 // XServiceInfo
62 virtual OUString SAL_CALL getImplementationName() override;
63 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
64 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
66 // XPackageManagerFactory
67 virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
68 OUString const & context ) override;
73 PackageManagerFactoryImpl::PackageManagerFactoryImpl(
74 Reference<XComponentContext> const & xComponentContext )
75 : t_pmfac_helper( m_aMutex ),
76 m_xComponentContext( xComponentContext )
80 // XServiceInfo
81 OUString PackageManagerFactoryImpl::getImplementationName()
83 return "com.sun.star.comp.deployment.PackageManagerFactory";
86 sal_Bool PackageManagerFactoryImpl::supportsService( const OUString& ServiceName )
88 return cppu::supportsService(this, ServiceName);
91 css::uno::Sequence< OUString > PackageManagerFactoryImpl::getSupportedServiceNames()
93 // a private one:
94 return { "com.sun.star.comp.deployment.PackageManagerFactory" };
97 inline void PackageManagerFactoryImpl::check()
99 ::osl::MutexGuard guard( m_aMutex );
100 if (rBHelper.bInDispose || rBHelper.bDisposed)
102 throw lang::DisposedException(
103 "PackageManagerFactory instance has already been disposed!",
104 static_cast<OWeakObject *>(this) );
109 void PackageManagerFactoryImpl::disposing()
111 // dispose all managers:
112 ::osl::MutexGuard guard( m_aMutex );
113 for (auto const& elem : m_managers)
114 try_dispose( elem.second );
115 m_managers = t_string2weakref();
116 // the below are already disposed:
117 m_xUserMgr.clear();
118 m_xSharedMgr.clear();
119 m_xBundledMgr.clear();
120 m_xTmpMgr.clear();
121 m_xBakMgr.clear();
124 // XPackageManagerFactory
126 Reference<deployment::XPackageManager>
127 PackageManagerFactoryImpl::getPackageManager( OUString const & context )
129 Reference< deployment::XPackageManager > xRet;
130 ::osl::ResettableMutexGuard guard( m_aMutex );
131 check();
132 t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
133 if (iFind != m_managers.end()) {
134 xRet = iFind->second;
135 if (xRet.is())
136 return xRet;
139 guard.clear();
140 xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
141 guard.reset();
142 std::pair< t_string2weakref::iterator, bool > insertion(
143 m_managers.emplace( context, xRet ) );
144 if (insertion.second)
146 OSL_ASSERT( insertion.first->second.get() == xRet );
147 // hold user, shared mgrs for whole process: live deployment
148 if ( context == "user" )
149 m_xUserMgr = xRet;
150 else if ( context == "shared" )
151 m_xSharedMgr = xRet;
152 else if ( context == "bundled" )
153 m_xBundledMgr = xRet;
154 else if ( context == "tmp" )
155 m_xTmpMgr = xRet;
156 else if ( context == "bak" )
157 m_xBakMgr = xRet;
159 else
161 Reference< deployment::XPackageManager > xAlreadyIn(
162 insertion.first->second );
163 if (xAlreadyIn.is())
165 guard.clear();
166 try_dispose( xRet );
167 xRet = xAlreadyIn;
169 else
171 insertion.first->second = xRet;
174 return xRet;
177 } // namespace dp_manager::factory
179 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
180 com_sun_star_comp_deployment_PackageManagerFactory_get_implementation(
181 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
183 return cppu::acquire(new dp_manager::factory::PackageManagerFactoryImpl(context));
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */