cid#1607171 Data race condition
[LibreOffice.git] / sd / source / ui / framework / configuration / ConfigurationControllerResourceManager.cxx
blob70b308691957e20b285c4825a416b2264b4aae94
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 .
20 #include "ConfigurationControllerResourceManager.hxx"
21 #include "ConfigurationControllerBroadcaster.hxx"
22 #include "ResourceFactoryManager.hxx"
23 #include <framework/FrameworkHelper.hxx>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/drawing/framework/XConfiguration.hpp>
26 #include <com/sun/star/drawing/framework/XResourceFactory.hpp>
27 #include <comphelper/diagnose_ex.hxx>
28 #include <sal/log.hxx>
29 #include <algorithm>
30 #include <utility>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::drawing::framework;
36 namespace sd::framework {
38 //===== ConfigurationControllerResourceManager ================================
40 ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
41 std::shared_ptr<ResourceFactoryManager> pResourceFactoryContainer,
42 std::shared_ptr<ConfigurationControllerBroadcaster> pBroadcaster)
43 : maResourceMap(ResourceComparator()),
44 mpResourceFactoryContainer(std::move(pResourceFactoryContainer)),
45 mpBroadcaster(std::move(pBroadcaster))
49 ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager()
53 ConfigurationControllerResourceManager::ResourceDescriptor
54 ConfigurationControllerResourceManager::GetResource (
55 const Reference<XResourceId>& rxResourceId)
57 ::osl::MutexGuard aGuard (maMutex);
58 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
59 if (iResource != maResourceMap.end())
60 return iResource->second;
61 else
62 return ResourceDescriptor();
65 void ConfigurationControllerResourceManager::ActivateResources (
66 const ::std::vector<Reference<XResourceId> >& rResources,
67 const Reference<XConfiguration>& rxConfiguration)
69 ::osl::MutexGuard aGuard (maMutex);
70 // Iterate in normal order over the resources that are to be
71 // activated so that resources on which others depend are activated
72 // before the depending resources are activated.
73 for (const Reference<XResourceId>& xResource : rResources)
74 ActivateResource(xResource, rxConfiguration);
77 void ConfigurationControllerResourceManager::DeactivateResources (
78 const ::std::vector<Reference<XResourceId> >& rResources,
79 const Reference<XConfiguration>& rxConfiguration)
81 ::osl::MutexGuard aGuard (maMutex);
82 // Iterate in reverse order over the resources that are to be
83 // deactivated so that resources on which others depend are deactivated
84 // only when the depending resources have already been deactivated.
85 ::std::for_each(
86 rResources.rbegin(),
87 rResources.rend(),
88 [&] (Reference<XResourceId> const& xResource) {
89 return DeactivateResource(xResource, rxConfiguration);
90 } );
93 /* In this method we do following steps.
94 1. Get the factory with which the resource will be created.
95 2. Create the resource.
96 3. Add the resource to the URL->Object map of the configuration
97 controller.
98 4. Add the resource id to the current configuration.
99 5. Notify listeners.
101 void ConfigurationControllerResourceManager::ActivateResource (
102 const Reference<XResourceId>& rxResourceId,
103 const Reference<XConfiguration>& rxConfiguration)
105 if ( ! rxResourceId.is())
107 OSL_ASSERT(rxResourceId.is());
108 return;
111 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
112 FrameworkHelper::ResourceIdToString(rxResourceId));
114 // 1. Get the factory.
115 const OUString sResourceURL (rxResourceId->getResourceURL());
116 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
117 if ( ! xFactory.is())
119 SAL_INFO("sd.fwk", __func__ << ": no factory found for " << sResourceURL);
120 return;
125 // 2. Create the resource.
126 Reference<XResource> xResource;
129 xResource = xFactory->createResource(rxResourceId);
131 catch (lang::DisposedException&)
133 // The factory is disposed and can be removed from the list
134 // of registered factories.
135 mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
137 catch (Exception&) {}
139 if (xResource.is())
141 SAL_INFO("sd.fwk", __func__ << ": successfully created");
142 // 3. Add resource to URL->Object map.
143 AddResource(xResource, xFactory);
145 // 4. Add resource id to current configuration.
146 rxConfiguration->addResource(rxResourceId);
148 // 5. Notify the new resource to listeners of the ConfigurationController.
149 mpBroadcaster->NotifyListeners(
150 FrameworkHelper::msResourceActivationEvent,
151 rxResourceId,
152 xResource);
154 else
156 SAL_INFO("sd.fwk", __func__ << ": resource creation failed");
159 catch (RuntimeException&)
161 DBG_UNHANDLED_EXCEPTION("sd");
165 /* In this method we do following steps.
166 1. Remove the resource from the URL->Object map of the configuration
167 controller.
168 2. Notify listeners that deactivation has started.
169 3. Remove the resource id from the current configuration.
170 4. Release the resource.
171 5. Notify listeners about that deactivation is completed.
173 void ConfigurationControllerResourceManager::DeactivateResource (
174 const Reference<XResourceId>& rxResourceId,
175 const Reference<XConfiguration>& rxConfiguration)
177 if ( ! rxResourceId.is())
178 return;
180 #if OSL_DEBUG_LEVEL >= 1
181 bool bSuccess (false);
182 #endif
185 // 1. Remove resource from URL->Object map.
186 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
188 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
190 // 2. Notify listeners that the resource is being deactivated.
191 mpBroadcaster->NotifyListeners(
192 FrameworkHelper::msResourceDeactivationEvent,
193 rxResourceId,
194 aDescriptor.mxResource);
196 // 3. Remove resource id from current configuration.
197 rxConfiguration->removeResource(rxResourceId);
199 // 4. Release the resource.
202 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
204 catch (const lang::DisposedException& rException)
206 if ( ! rException.Context.is()
207 || rException.Context == aDescriptor.mxResourceFactory)
209 // The factory is disposed and can be removed from the
210 // list of registered factories.
211 mpResourceFactoryContainer->RemoveFactoryForReference(
212 aDescriptor.mxResourceFactory);
216 #if OSL_DEBUG_LEVEL >= 1
217 bSuccess = true;
218 #endif
221 catch (RuntimeException&)
223 DBG_UNHANDLED_EXCEPTION("sd");
226 // 5. Notify listeners that the resource is being deactivated.
227 mpBroadcaster->NotifyListeners(
228 FrameworkHelper::msResourceDeactivationEndEvent,
229 rxResourceId,
230 nullptr);
232 #if OSL_DEBUG_LEVEL >= 1
233 if (bSuccess)
234 SAL_INFO("sd.fwk", __func__ << ": successfully deactivated " <<
235 FrameworkHelper::ResourceIdToString(rxResourceId));
236 else
237 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
238 FrameworkHelper::ResourceIdToString(rxResourceId)
239 << " failed");
240 #endif
243 void ConfigurationControllerResourceManager::AddResource (
244 const Reference<XResource>& rxResource,
245 const Reference<XResourceFactory>& rxFactory)
247 if ( ! rxResource.is())
249 OSL_ASSERT(rxResource.is());
250 return;
253 // Add the resource to the resource container.
254 maResourceMap[rxResource->getResourceId()] = ResourceDescriptor{rxResource, rxFactory};
256 #if OSL_DEBUG_LEVEL >= 2
257 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::AddResource(): added " <<
258 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()) <<
259 " -> " << rxResource.get());
260 #endif
263 ConfigurationControllerResourceManager::ResourceDescriptor
264 ConfigurationControllerResourceManager::RemoveResource (
265 const Reference<XResourceId>& rxResourceId)
267 ResourceDescriptor aDescriptor;
269 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
270 if (iResource != maResourceMap.end())
272 #if OSL_DEBUG_LEVEL >= 2
273 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
274 FrameworkHelper::ResourceIdToString(rxResourceId) <<
275 " -> " << &iResource);
276 #endif
278 aDescriptor = iResource->second;
279 maResourceMap.erase(rxResourceId);
282 return aDescriptor;
285 //===== ConfigurationControllerResourceManager::ResourceComparator ============
287 bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
288 const Reference<XResourceId>& rxId1,
289 const Reference<XResourceId>& rxId2) const
291 if (rxId1.is() && rxId2.is())
292 return rxId1->compareTo(rxId2)<0;
293 else if (rxId1.is())
294 return true;
295 else
296 return false;
299 } // end of namespace sd::framework
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */