/src/libreoffice/desktop/source/deployment/manager/dp_managerfac.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | |
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> |
28 | | |
29 | | using namespace ::dp_misc; |
30 | | using namespace ::com::sun::star; |
31 | | using namespace ::com::sun::star::uno; |
32 | | |
33 | | namespace dp_manager::factory { |
34 | | |
35 | | typedef ::cppu::WeakComponentImplHelper< |
36 | | deployment::XPackageManagerFactory, lang::XServiceInfo > t_pmfac_helper; |
37 | | |
38 | | namespace { |
39 | | |
40 | | class PackageManagerFactoryImpl : private cppu::BaseMutex, public t_pmfac_helper |
41 | | { |
42 | | Reference<XComponentContext> m_xComponentContext; |
43 | | |
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; |
52 | | |
53 | | protected: |
54 | | inline void check(); |
55 | | virtual void SAL_CALL disposing() override; |
56 | | |
57 | | public: |
58 | | explicit PackageManagerFactoryImpl( |
59 | | Reference<XComponentContext> const & xComponentContext ); |
60 | | |
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; |
65 | | |
66 | | // XPackageManagerFactory |
67 | | virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager( |
68 | | OUString const & context ) override; |
69 | | }; |
70 | | |
71 | | } |
72 | | |
73 | | PackageManagerFactoryImpl::PackageManagerFactoryImpl( |
74 | | Reference<XComponentContext> const & xComponentContext ) |
75 | 0 | : t_pmfac_helper( m_aMutex ), |
76 | 0 | m_xComponentContext( xComponentContext ) |
77 | 0 | { |
78 | 0 | } |
79 | | |
80 | | // XServiceInfo |
81 | | OUString PackageManagerFactoryImpl::getImplementationName() |
82 | 0 | { |
83 | 0 | return u"com.sun.star.comp.deployment.PackageManagerFactory"_ustr; |
84 | 0 | } |
85 | | |
86 | | sal_Bool PackageManagerFactoryImpl::supportsService( const OUString& ServiceName ) |
87 | 0 | { |
88 | 0 | return cppu::supportsService(this, ServiceName); |
89 | 0 | } |
90 | | |
91 | | css::uno::Sequence< OUString > PackageManagerFactoryImpl::getSupportedServiceNames() |
92 | 0 | { |
93 | | // a private one: |
94 | 0 | return { u"com.sun.star.comp.deployment.PackageManagerFactory"_ustr }; |
95 | 0 | } |
96 | | |
97 | | inline void PackageManagerFactoryImpl::check() |
98 | 0 | { |
99 | 0 | ::osl::MutexGuard guard( m_aMutex ); |
100 | 0 | if (rBHelper.bInDispose || rBHelper.bDisposed) |
101 | 0 | { |
102 | 0 | throw lang::DisposedException( |
103 | 0 | u"PackageManagerFactory instance has already been disposed!"_ustr, |
104 | 0 | static_cast<OWeakObject *>(this) ); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | |
109 | | void PackageManagerFactoryImpl::disposing() |
110 | 0 | { |
111 | | // dispose all managers: |
112 | 0 | ::osl::MutexGuard guard( m_aMutex ); |
113 | 0 | for (auto const& elem : m_managers) |
114 | 0 | try_dispose( elem.second ); |
115 | 0 | m_managers = t_string2weakref(); |
116 | | // the below are already disposed: |
117 | 0 | m_xUserMgr.clear(); |
118 | 0 | m_xSharedMgr.clear(); |
119 | 0 | m_xBundledMgr.clear(); |
120 | 0 | m_xTmpMgr.clear(); |
121 | 0 | m_xBakMgr.clear(); |
122 | 0 | } |
123 | | |
124 | | // XPackageManagerFactory |
125 | | |
126 | | Reference<deployment::XPackageManager> |
127 | | PackageManagerFactoryImpl::getPackageManager( OUString const & context ) |
128 | 0 | { |
129 | 0 | Reference< deployment::XPackageManager > xRet; |
130 | 0 | ::osl::ResettableMutexGuard guard( m_aMutex ); |
131 | 0 | check(); |
132 | 0 | t_string2weakref::const_iterator const iFind( m_managers.find( context ) ); |
133 | 0 | if (iFind != m_managers.end()) { |
134 | 0 | xRet = iFind->second; |
135 | 0 | if (xRet.is()) |
136 | 0 | return xRet; |
137 | 0 | } |
138 | | |
139 | 0 | guard.clear(); |
140 | 0 | xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) ); |
141 | 0 | guard.reset(); |
142 | 0 | std::pair< t_string2weakref::iterator, bool > insertion( |
143 | 0 | m_managers.emplace( context, xRet ) ); |
144 | 0 | if (insertion.second) |
145 | 0 | { |
146 | 0 | OSL_ASSERT( insertion.first->second.get() == xRet ); |
147 | | // hold user, shared mgrs for whole process: live deployment |
148 | 0 | if ( context == "user" ) |
149 | 0 | m_xUserMgr = xRet; |
150 | 0 | else if ( context == "shared" ) |
151 | 0 | m_xSharedMgr = xRet; |
152 | 0 | else if ( context == "bundled" ) |
153 | 0 | m_xBundledMgr = xRet; |
154 | 0 | else if ( context == "tmp" ) |
155 | 0 | m_xTmpMgr = xRet; |
156 | 0 | else if ( context == "bak" ) |
157 | 0 | m_xBakMgr = xRet; |
158 | 0 | } |
159 | 0 | else |
160 | 0 | { |
161 | 0 | Reference< deployment::XPackageManager > xAlreadyIn( |
162 | 0 | insertion.first->second ); |
163 | 0 | if (xAlreadyIn.is()) |
164 | 0 | { |
165 | 0 | guard.clear(); |
166 | 0 | try_dispose( xRet ); |
167 | 0 | xRet = std::move(xAlreadyIn); |
168 | 0 | } |
169 | 0 | else |
170 | 0 | { |
171 | 0 | insertion.first->second = xRet; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | return xRet; |
175 | 0 | } |
176 | | |
177 | | } // namespace dp_manager::factory |
178 | | |
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& ) |
182 | 0 | { |
183 | 0 | return cppu::acquire(new dp_manager::factory::PackageManagerFactoryImpl(context)); |
184 | 0 | } |
185 | | |
186 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |