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 <dp_misc.h>
23 #include <cppuhelper/compbase.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 
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 {
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  : t_pmfac_helper( m_aMutex ),
76  m_xComponentContext( xComponentContext )
77 {
78 }
79 
80 // XServiceInfo
81 OUString PackageManagerFactoryImpl::getImplementationName()
82 {
83  return "com.sun.star.comp.deployment.PackageManagerFactory";
84 }
85 
86 sal_Bool PackageManagerFactoryImpl::supportsService( const OUString& ServiceName )
87 {
88  return cppu::supportsService(this, ServiceName);
89 }
90 
91 css::uno::Sequence< OUString > PackageManagerFactoryImpl::getSupportedServiceNames()
92 {
93  // a private one:
94  return { "com.sun.star.comp.deployment.PackageManagerFactory" };
95 }
96 
97 inline void PackageManagerFactoryImpl::check()
98 {
99  ::osl::MutexGuard guard( m_aMutex );
100  if (rBHelper.bInDispose || rBHelper.bDisposed)
101  {
102  throw lang::DisposedException(
103  "PackageManagerFactory instance has already been disposed!",
104  static_cast<OWeakObject *>(this) );
105  }
106 }
107 
108 
109 void PackageManagerFactoryImpl::disposing()
110 {
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();
122 }
123 
124 // XPackageManagerFactory
125 
126 Reference<deployment::XPackageManager>
127 PackageManagerFactoryImpl::getPackageManager( OUString const & context )
128 {
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;
137  }
138 
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)
145  {
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;
158  }
159  else
160  {
161  Reference< deployment::XPackageManager > xAlreadyIn(
162  insertion.first->second );
163  if (xAlreadyIn.is())
164  {
165  guard.clear();
166  try_dispose( xRet );
167  xRet = xAlreadyIn;
168  }
169  else
170  {
171  insertion.first->second = xRet;
172  }
173  }
174  return xRet;
175 }
176 
177 } // namespace dp_manager::factory
178 
179 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
181  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
182 {
183  return cppu::acquire(new dp_manager::factory::PackageManagerFactoryImpl(context));
184 }
185 
186 /* 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)
std::mutex m_aMutex
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:38
Reference< XComponentContext > m_xComponentContext
Reference< deployment::XPackageManager > m_xBakMgr