Line data Source code
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 "ConfigurationUpdater.hxx"
22 : #include "ConfigurationTracer.hxx"
23 : #include "ConfigurationClassifier.hxx"
24 : #include "ConfigurationControllerBroadcaster.hxx"
25 : #include "framework/Configuration.hxx"
26 : #include "framework/FrameworkHelper.hxx"
27 :
28 : #include <comphelper/scopeguard.hxx>
29 : #include <tools/diagnose_ex.h>
30 :
31 : #include <boost/bind.hpp>
32 :
33 : using namespace ::com::sun::star;
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::drawing::framework;
36 : using ::sd::framework::FrameworkHelper;
37 : using ::rtl::OUString;
38 : using ::std::vector;
39 :
40 : namespace {
41 : static const sal_Int32 snShortTimeout (100);
42 : static const sal_Int32 snNormalTimeout (1000);
43 : static const sal_Int32 snLongTimeout (10000);
44 : static const sal_Int32 snShortTimeoutCountThreshold (1);
45 : static const sal_Int32 snNormalTimeoutCountThreshold (5);
46 : }
47 :
48 : namespace sd { namespace framework {
49 :
50 :
51 : //===== ConfigurationUpdaterLock ==============================================
52 :
53 : class ConfigurationUpdaterLock
54 : {
55 : public:
56 0 : ConfigurationUpdaterLock (ConfigurationUpdater& rUpdater)
57 0 : : mrUpdater(rUpdater) { mrUpdater.LockUpdates(); }
58 0 : ~ConfigurationUpdaterLock(void) { mrUpdater.UnlockUpdates(); }
59 : private:
60 : ConfigurationUpdater& mrUpdater;
61 : };
62 :
63 :
64 :
65 :
66 : //===== ConfigurationUpdater ==================================================
67 :
68 0 : ConfigurationUpdater::ConfigurationUpdater (
69 : const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster,
70 : const ::boost::shared_ptr<ConfigurationControllerResourceManager>& rpResourceManager,
71 : const Reference<XControllerManager>& rxControllerManager)
72 : : mxControllerManager(),
73 : mpBroadcaster(rpBroadcaster),
74 0 : mxCurrentConfiguration(Reference<XConfiguration>(new Configuration(NULL, false))),
75 : mxRequestedConfiguration(),
76 : mbUpdatePending(false),
77 : mbUpdateBeingProcessed(false),
78 : mnLockCount(0),
79 : maUpdateTimer(),
80 : mnFailedUpdateCount(0),
81 0 : mpResourceManager(rpResourceManager)
82 : {
83 : // Prepare the timer that is started when after an update the current
84 : // and the requested configuration differ. With the timer we try
85 : // updates until the two configurations are the same.
86 0 : maUpdateTimer.SetTimeout(snNormalTimeout);
87 0 : maUpdateTimer.SetTimeoutHdl(LINK(this,ConfigurationUpdater,TimeoutHandler));
88 0 : SetControllerManager(rxControllerManager);
89 0 : }
90 :
91 :
92 :
93 :
94 0 : ConfigurationUpdater::~ConfigurationUpdater (void)
95 : {
96 0 : maUpdateTimer.Stop();
97 0 : }
98 :
99 :
100 :
101 :
102 0 : void ConfigurationUpdater::SetControllerManager(
103 : const Reference<XControllerManager>& rxControllerManager)
104 : {
105 0 : mxControllerManager = rxControllerManager;
106 0 : }
107 :
108 :
109 :
110 :
111 0 : void ConfigurationUpdater::RequestUpdate (
112 : const Reference<XConfiguration>& rxRequestedConfiguration)
113 : {
114 0 : mxRequestedConfiguration = rxRequestedConfiguration;
115 :
116 : // Find out whether we really can update the configuration.
117 0 : if (IsUpdatePossible())
118 : {
119 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": UpdateConfiguration start");
120 :
121 : // Call UpdateConfiguration while that is possible and while someone
122 : // set mbUpdatePending to true in the middle of it.
123 : do
124 : {
125 0 : UpdateConfiguration();
126 :
127 0 : if (mbUpdatePending && IsUpdatePossible())
128 0 : continue;
129 : }
130 : while (false);
131 : }
132 : else
133 : {
134 0 : mbUpdatePending = true;
135 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": scheduling update for later");
136 : }
137 0 : }
138 :
139 :
140 :
141 :
142 0 : Reference<XConfiguration> ConfigurationUpdater::GetCurrentConfiguration (void) const
143 : {
144 0 : return mxCurrentConfiguration;
145 : }
146 :
147 :
148 :
149 :
150 0 : bool ConfigurationUpdater::IsUpdatePossible (void)
151 : {
152 0 : return ! mbUpdateBeingProcessed
153 0 : && mxControllerManager.is()
154 : && mnLockCount==0
155 0 : && mxRequestedConfiguration.is()
156 0 : && mxCurrentConfiguration.is();
157 : }
158 :
159 :
160 :
161 :
162 0 : void ConfigurationUpdater::UpdateConfiguration (void)
163 : {
164 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": UpdateConfiguration update");
165 0 : SetUpdateBeingProcessed(true);
166 : comphelper::ScopeGuard aScopeGuard (
167 0 : ::boost::bind(&ConfigurationUpdater::SetUpdateBeingProcessed, this, false));
168 :
169 : try
170 : {
171 0 : mbUpdatePending = false;
172 :
173 0 : CleanRequestedConfiguration();
174 0 : ConfigurationClassifier aClassifier(mxRequestedConfiguration, mxCurrentConfiguration);
175 0 : if (aClassifier.Partition())
176 : {
177 : #if OSL_DEBUG_LEVEL >= 2
178 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationUpdater::UpdateConfiguration(");
179 : ConfigurationTracer::TraceConfiguration(
180 : mxRequestedConfiguration, "requested configuration");
181 : ConfigurationTracer::TraceConfiguration(
182 : mxCurrentConfiguration, "current configuration");
183 : #endif
184 : // Notify the begining of the update.
185 0 : ConfigurationChangeEvent aEvent;
186 0 : aEvent.Type = FrameworkHelper::msConfigurationUpdateStartEvent;
187 0 : aEvent.Configuration = mxRequestedConfiguration;
188 0 : mpBroadcaster->NotifyListeners(aEvent);
189 :
190 : // Do the actual update. All exceptions are caught and ignored,
191 : // so that the the end of the update is notified always.
192 : try
193 : {
194 0 : if (mnLockCount == 0)
195 0 : UpdateCore(aClassifier);
196 : }
197 0 : catch(const RuntimeException&)
198 : {
199 : }
200 :
201 : // Notify the end of the update.
202 0 : aEvent.Type = FrameworkHelper::msConfigurationUpdateEndEvent;
203 0 : mpBroadcaster->NotifyListeners(aEvent);
204 :
205 0 : CheckUpdateSuccess();
206 : }
207 : else
208 : {
209 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": nothing to do");
210 : #if OSL_DEBUG_LEVEL >= 2
211 : ConfigurationTracer::TraceConfiguration(
212 : mxRequestedConfiguration, "requested configuration");
213 : ConfigurationTracer::TraceConfiguration(
214 : mxCurrentConfiguration, "current configuration");
215 : #endif
216 0 : }
217 : }
218 0 : catch(const RuntimeException &)
219 : {
220 : DBG_UNHANDLED_EXCEPTION();
221 : }
222 :
223 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationUpdater::UpdateConfiguration)");
224 0 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": UpdateConfiguration end");
225 0 : }
226 :
227 :
228 :
229 :
230 0 : void ConfigurationUpdater::CleanRequestedConfiguration (void)
231 : {
232 0 : if (mxControllerManager.is())
233 : {
234 : // Request the deactivation of pure anchors that have no child.
235 0 : vector<Reference<XResourceId> > aResourcesToDeactivate;
236 0 : CheckPureAnchors(mxRequestedConfiguration, aResourcesToDeactivate);
237 0 : if (!aResourcesToDeactivate.empty())
238 : {
239 : Reference<XConfigurationController> xCC (
240 0 : mxControllerManager->getConfigurationController());
241 0 : vector<Reference<XResourceId> >::iterator iId;
242 0 : for (iId=aResourcesToDeactivate.begin(); iId!=aResourcesToDeactivate.end(); ++iId)
243 0 : if (iId->is())
244 0 : xCC->requestResourceDeactivation(*iId);
245 0 : }
246 : }
247 0 : }
248 :
249 :
250 :
251 :
252 0 : void ConfigurationUpdater::CheckUpdateSuccess (void)
253 : {
254 : // When the two configurations differ then start the timer to call
255 : // another update later.
256 0 : if ( ! AreConfigurationsEquivalent(mxCurrentConfiguration, mxRequestedConfiguration))
257 : {
258 0 : if (mnFailedUpdateCount <= snShortTimeoutCountThreshold)
259 0 : maUpdateTimer.SetTimeout(snShortTimeout);
260 0 : else if (mnFailedUpdateCount < snNormalTimeoutCountThreshold)
261 0 : maUpdateTimer.SetTimeout(snNormalTimeout);
262 : else
263 0 : maUpdateTimer.SetTimeout(snLongTimeout);
264 0 : ++mnFailedUpdateCount;
265 0 : maUpdateTimer.Start();
266 : }
267 : else
268 : {
269 : // Update was successfull. Reset the failed update count.
270 0 : mnFailedUpdateCount = 0;
271 : }
272 0 : }
273 :
274 :
275 :
276 :
277 0 : void ConfigurationUpdater::UpdateCore (const ConfigurationClassifier& rClassifier)
278 : {
279 : try
280 : {
281 : #if OSL_DEBUG_LEVEL >= 2
282 : rClassifier.TraceResourceIdVector(
283 : "requested but not current resources:", rClassifier.GetC1minusC2());
284 : rClassifier.TraceResourceIdVector(
285 : "current but not requested resources:", rClassifier.GetC2minusC1());
286 : rClassifier.TraceResourceIdVector(
287 : "requested and current resources:", rClassifier.GetC1andC2());
288 : #endif
289 :
290 : // Updating of the sub controllers is done in two steps. In the
291 : // first the sub controllers typically shut down resources that are
292 : // not requested anymore. In the second the sub controllers
293 : // typically set up resources that have been newly requested.
294 0 : mpResourceManager->DeactivateResources(rClassifier.GetC2minusC1(), mxCurrentConfiguration);
295 0 : mpResourceManager->ActivateResources(rClassifier.GetC1minusC2(), mxCurrentConfiguration);
296 :
297 : #if OSL_DEBUG_LEVEL >= 2
298 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationController::UpdateConfiguration)");
299 : ConfigurationTracer::TraceConfiguration(
300 : mxRequestedConfiguration, "requested configuration");
301 : ConfigurationTracer::TraceConfiguration(
302 : mxCurrentConfiguration, "current configuration");
303 : #endif
304 :
305 : // Deactivate pure anchors that have no child.
306 0 : vector<Reference<XResourceId> > aResourcesToDeactivate;
307 0 : CheckPureAnchors(mxCurrentConfiguration, aResourcesToDeactivate);
308 0 : if (!aResourcesToDeactivate.empty())
309 0 : mpResourceManager->DeactivateResources(aResourcesToDeactivate, mxCurrentConfiguration);
310 : }
311 0 : catch(const RuntimeException&)
312 : {
313 : DBG_UNHANDLED_EXCEPTION();
314 : }
315 0 : }
316 :
317 :
318 :
319 :
320 0 : void ConfigurationUpdater::CheckPureAnchors (
321 : const Reference<XConfiguration>& rxConfiguration,
322 : vector<Reference<XResourceId> >& rResourcesToDeactivate)
323 : {
324 0 : if ( ! rxConfiguration.is())
325 0 : return;
326 :
327 : // Get a list of all resources in the configuration.
328 : Sequence<Reference<XResourceId> > aResources(
329 0 : rxConfiguration->getResources(
330 0 : NULL, OUString(), AnchorBindingMode_INDIRECT));
331 0 : sal_Int32 nCount (aResources.getLength());
332 :
333 : // Prepare the list of pure anchors that have to be deactivated.
334 0 : rResourcesToDeactivate.clear();
335 :
336 : // Iterate over the list in reverse order because when there is a chain
337 : // of pure anchors with only the last one having no child then the whole
338 : // list has to be deactivated.
339 0 : sal_Int32 nIndex (nCount-1);
340 0 : while (nIndex >= 0)
341 : {
342 0 : const Reference<XResourceId> xResourceId (aResources[nIndex]);
343 : const Reference<XResource> xResource (
344 0 : mpResourceManager->GetResource(xResourceId).mxResource);
345 0 : bool bDeactiveCurrentResource (false);
346 :
347 : // Skip all resources that are no pure anchors.
348 0 : if (xResource.is() && xResource->isAnchorOnly())
349 : {
350 : // When xResource is not an anchor of the the next resource in
351 : // the list then it is the anchor of no resource at all.
352 0 : if (nIndex == nCount-1)
353 : {
354 : // No following anchors, deactivate this one, then remove it
355 : // from the list.
356 0 : bDeactiveCurrentResource = true;
357 : }
358 : else
359 : {
360 0 : const Reference<XResourceId> xPrevResourceId (aResources[nIndex+1]);
361 0 : if ( ! xPrevResourceId.is()
362 0 : || ! xPrevResourceId->isBoundTo(xResourceId, AnchorBindingMode_DIRECT))
363 : {
364 : // The previous resource (id) does not exist or is not bound to
365 : // the current anchor.
366 0 : bDeactiveCurrentResource = true;
367 0 : }
368 : }
369 : }
370 :
371 0 : if (bDeactiveCurrentResource)
372 : {
373 : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": deactiving pure anchor " <<
374 : OUStringToOString(
375 : FrameworkHelper::ResourceIdToString(xResourceId),
376 : RTL_TEXTENCODING_UTF8).getStr() << "because it has no children");
377 : // Erase element from current configuration.
378 0 : for (sal_Int32 nI=nIndex; nI<nCount-2; ++nI)
379 0 : aResources[nI] = aResources[nI+1];
380 0 : nCount -= 1;
381 :
382 0 : rResourcesToDeactivate.push_back(xResourceId);
383 : }
384 0 : nIndex -= 1;
385 0 : }
386 : }
387 :
388 :
389 :
390 :
391 0 : void ConfigurationUpdater::LockUpdates (void)
392 : {
393 0 : ++mnLockCount;
394 0 : }
395 :
396 :
397 :
398 :
399 0 : void ConfigurationUpdater::UnlockUpdates (void)
400 : {
401 0 : --mnLockCount;
402 0 : if (mnLockCount == 0 && mbUpdatePending)
403 : {
404 0 : RequestUpdate(mxRequestedConfiguration);
405 : }
406 0 : }
407 :
408 :
409 :
410 :
411 0 : ::boost::shared_ptr<ConfigurationUpdaterLock> ConfigurationUpdater::GetLock (void)
412 : {
413 0 : return ::boost::shared_ptr<ConfigurationUpdaterLock>(new ConfigurationUpdaterLock(*this));
414 : }
415 :
416 :
417 :
418 :
419 0 : void ConfigurationUpdater::SetUpdateBeingProcessed (bool bValue)
420 : {
421 0 : mbUpdateBeingProcessed = bValue;
422 0 : }
423 :
424 :
425 :
426 :
427 0 : IMPL_LINK_NOARG(ConfigurationUpdater, TimeoutHandler)
428 : {
429 : OSL_TRACE("configuration update timer");
430 0 : if ( ! mbUpdateBeingProcessed
431 0 : && mxCurrentConfiguration.is()
432 0 : && mxRequestedConfiguration.is())
433 : {
434 0 : if ( ! AreConfigurationsEquivalent(mxCurrentConfiguration, mxRequestedConfiguration))
435 : {
436 : OSL_TRACE("configurations differ, requesting update");
437 0 : RequestUpdate(mxRequestedConfiguration);
438 : }
439 : }
440 0 : return 0;
441 : }
442 :
443 :
444 9 : } } // end of namespace sd::framework
445 :
446 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|