Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / desktop / source / deployment / manager / dp_managerfac.cxx
blob5b7c7e11a3f0607d2735244aa87f70b9b91bf6e1
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_resource.h>
23 #include <dp_services.hxx>
24 #include <cppuhelper/compbase.hxx>
25 #include <comphelper/servicedecl.hxx>
26 #include <com/sun/star/deployment/thePackageManagerFactory.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 {
34 namespace factory {
36 typedef ::cppu::WeakComponentImplHelper<
37 deployment::XPackageManagerFactory > t_pmfac_helper;
40 class PackageManagerFactoryImpl : private MutexHolder, 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 // XPackageManagerFactory
62 virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
63 OUString const & context ) override;
67 namespace sdecl = comphelper::service_decl;
68 sdecl::class_<PackageManagerFactoryImpl> const servicePMFI;
69 sdecl::ServiceDecl const serviceDecl(
70 servicePMFI,
71 // a private one:
72 "com.sun.star.comp.deployment.PackageManagerFactory",
73 "com.sun.star.comp.deployment.PackageManagerFactory" );
76 PackageManagerFactoryImpl::PackageManagerFactoryImpl(
77 Reference<XComponentContext> const & xComponentContext )
78 : t_pmfac_helper( getMutex() ),
79 m_xComponentContext( xComponentContext )
83 inline void PackageManagerFactoryImpl::check()
85 ::osl::MutexGuard guard( getMutex() );
86 if (rBHelper.bInDispose || rBHelper.bDisposed)
88 throw lang::DisposedException(
89 "PackageManagerFactory instance has already been disposed!",
90 static_cast<OWeakObject *>(this) );
95 void PackageManagerFactoryImpl::disposing()
97 // dispose all managers:
98 ::osl::MutexGuard guard( getMutex() );
99 for (auto const& elem : m_managers)
100 try_dispose( elem.second );
101 m_managers = t_string2weakref();
102 // the below are already disposed:
103 m_xUserMgr.clear();
104 m_xSharedMgr.clear();
105 m_xBundledMgr.clear();
106 m_xTmpMgr.clear();
107 m_xBakMgr.clear();
110 // XPackageManagerFactory
112 Reference<deployment::XPackageManager>
113 PackageManagerFactoryImpl::getPackageManager( OUString const & context )
115 Reference< deployment::XPackageManager > xRet;
116 ::osl::ResettableMutexGuard guard( getMutex() );
117 check();
118 t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
119 if (iFind != m_managers.end()) {
120 xRet = iFind->second;
121 if (xRet.is())
122 return xRet;
125 guard.clear();
126 xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
127 guard.reset();
128 std::pair< t_string2weakref::iterator, bool > insertion(
129 m_managers.emplace( context, xRet ) );
130 if (insertion.second)
132 OSL_ASSERT( insertion.first->second.get() == xRet );
133 // hold user, shared mgrs for whole process: live deployment
134 if ( context == "user" )
135 m_xUserMgr = xRet;
136 else if ( context == "shared" )
137 m_xSharedMgr = xRet;
138 else if ( context == "bundled" )
139 m_xBundledMgr = xRet;
140 else if ( context == "tmp" )
141 m_xTmpMgr = xRet;
142 else if ( context == "bak" )
143 m_xBakMgr = xRet;
145 else
147 Reference< deployment::XPackageManager > xAlreadyIn(
148 insertion.first->second );
149 if (xAlreadyIn.is())
151 guard.clear();
152 try_dispose( xRet );
153 xRet = xAlreadyIn;
155 else
157 insertion.first->second = xRet;
160 return xRet;
163 } // namespace factory
164 } // namespace dp_manager
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */