LibreOffice Module desktop (master)  1
dp_managerfac.cxx
Go to the documentation of this file.
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 <cppuhelper/compbase.hxx>
24 #include <com/sun/star/deployment/XPackageManagerFactory.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <unordered_map>
27 
28 using namespace ::dp_misc;
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 
33 
34 typedef ::cppu::WeakComponentImplHelper<
35  deployment::XPackageManagerFactory, lang::XServiceInfo > t_pmfac_helper;
36 
37 namespace {
38 
39 class PackageManagerFactoryImpl : private MutexHolder, public t_pmfac_helper
40 {
42 
43  Reference<deployment::XPackageManager> m_xUserMgr;
44  Reference<deployment::XPackageManager> m_xSharedMgr;
45  Reference<deployment::XPackageManager> m_xBundledMgr;
46  Reference<deployment::XPackageManager> m_xTmpMgr;
47  Reference<deployment::XPackageManager> m_xBakMgr;
48  typedef std::unordered_map<
49  OUString, WeakReference<deployment::XPackageManager> > t_string2weakref;
50  t_string2weakref m_managers;
51 
52 protected:
53  inline void check();
54  virtual void SAL_CALL disposing() override;
55 
56 public:
57  explicit PackageManagerFactoryImpl(
58  Reference<XComponentContext> const & xComponentContext );
59 
60  // XServiceInfo
61  virtual OUString SAL_CALL getImplementationName() override;
62  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
63  virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
64 
65  // XPackageManagerFactory
66  virtual Reference<deployment::XPackageManager> SAL_CALL getPackageManager(
67  OUString const & context ) override;
68 };
69 
70 }
71 
72 PackageManagerFactoryImpl::PackageManagerFactoryImpl(
73  Reference<XComponentContext> const & xComponentContext )
74  : t_pmfac_helper( getMutex() ),
75  m_xComponentContext( xComponentContext )
76 {
77 }
78 
79 // XServiceInfo
80 OUString PackageManagerFactoryImpl::getImplementationName()
81 {
82  return "com.sun.star.comp.deployment.PackageManagerFactory";
83 }
84 
85 sal_Bool PackageManagerFactoryImpl::supportsService( const OUString& ServiceName )
86 {
87  return cppu::supportsService(this, ServiceName);
88 }
89 
90 css::uno::Sequence< OUString > PackageManagerFactoryImpl::getSupportedServiceNames()
91 {
92  // a private one:
93  return { "com.sun.star.comp.deployment.PackageManagerFactory" };
94 }
95 
96 inline void PackageManagerFactoryImpl::check()
97 {
98  ::osl::MutexGuard guard( getMutex() );
99  if (rBHelper.bInDispose || rBHelper.bDisposed)
100  {
101  throw lang::DisposedException(
102  "PackageManagerFactory instance has already been disposed!",
103  static_cast<OWeakObject *>(this) );
104  }
105 }
106 
107 
108 void PackageManagerFactoryImpl::disposing()
109 {
110  // dispose all managers:
111  ::osl::MutexGuard guard( getMutex() );
112  for (auto const& elem : m_managers)
113  try_dispose( elem.second );
114  m_managers = t_string2weakref();
115  // the below are already disposed:
116  m_xUserMgr.clear();
117  m_xSharedMgr.clear();
118  m_xBundledMgr.clear();
119  m_xTmpMgr.clear();
120  m_xBakMgr.clear();
121 }
122 
123 // XPackageManagerFactory
124 
125 Reference<deployment::XPackageManager>
126 PackageManagerFactoryImpl::getPackageManager( OUString const & context )
127 {
128  Reference< deployment::XPackageManager > xRet;
129  ::osl::ResettableMutexGuard guard( getMutex() );
130  check();
131  t_string2weakref::const_iterator const iFind( m_managers.find( context ) );
132  if (iFind != m_managers.end()) {
133  xRet = iFind->second;
134  if (xRet.is())
135  return xRet;
136  }
137 
138  guard.clear();
139  xRet.set( PackageManagerImpl::create( m_xComponentContext, context ) );
140  guard.reset();
141  std::pair< t_string2weakref::iterator, bool > insertion(
142  m_managers.emplace( context, xRet ) );
143  if (insertion.second)
144  {
145  OSL_ASSERT( insertion.first->second.get() == xRet );
146  // hold user, shared mgrs for whole process: live deployment
147  if ( context == "user" )
148  m_xUserMgr = xRet;
149  else if ( context == "shared" )
150  m_xSharedMgr = xRet;
151  else if ( context == "bundled" )
152  m_xBundledMgr = xRet;
153  else if ( context == "tmp" )
154  m_xTmpMgr = xRet;
155  else if ( context == "bak" )
156  m_xBakMgr = xRet;
157  }
158  else
159  {
160  Reference< deployment::XPackageManager > xAlreadyIn(
161  insertion.first->second );
162  if (xAlreadyIn.is())
163  {
164  guard.clear();
165  try_dispose( xRet );
166  xRet = xAlreadyIn;
167  }
168  else
169  {
170  insertion.first->second = xRet;
171  }
172  }
173  return xRet;
174 }
175 
176 } // namespace dp_manager::factory
177 
178 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
180  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
181 {
182  return cppu::acquire(new dp_manager::factory::PackageManagerFactoryImpl(context));
183 }
184 
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::cppu::WeakComponentImplHelper< deployment::XPackageManagerFactory, lang::XServiceInfo > t_pmfac_helper
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
def check(model)
Reference< deployment::XPackageManager > m_xUserMgr
Reference< deployment::XPackageManager > m_xTmpMgr
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_deployment_PackageManagerFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
t_string2weakref m_managers
unsigned char sal_Bool
Reference< deployment::XPackageManager > m_xSharedMgr
Reference< deployment::XPackageManager > m_xBundledMgr
void try_dispose(css::uno::Reference< css::uno::XInterface > const &x)
Definition: dp_misc.h:43
Reference< XComponentContext > m_xComponentContext
Reference< deployment::XPackageManager > m_xBakMgr
::osl::Mutex & getMutex()