Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / statusindicatorfactory.cxx
blob879bd8597b33e77328bdea81bcd7f50bc7d48232
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 <algorithm>
21 #include <utility>
22 #include <helper/statusindicatorfactory.hxx>
23 #include <helper/statusindicator.hxx>
24 #include <helper/vclstatusindicator.hxx>
25 #include <properties.h>
27 #include <com/sun/star/awt/XWindow2.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/frame/XLayoutManager2.hpp>
31 #include <toolkit/helper/vclunohelper.hxx>
33 #include <comphelper/sequenceashashmap.hxx>
34 #include <unotools/mediadescriptor.hxx>
35 #include <vcl/svapp.hxx>
36 #include <mutex>
37 #include <rtl/ref.hxx>
39 #include <officecfg/Office/Common.hxx>
41 namespace framework{
43 sal_Int32 StatusIndicatorFactory::m_nInReschedule = 0; ///< static counter for rescheduling
45 constexpr OUString PROGRESS_RESOURCE = u"private:resource/progressbar/progressbar"_ustr;
47 StatusIndicatorFactory::StatusIndicatorFactory(css::uno::Reference< css::uno::XComponentContext > xContext)
48 : m_xContext (std::move(xContext ))
49 , m_bAllowReschedule (false)
50 , m_bAllowParentShow (false)
51 , m_bDisableReschedule(false)
55 StatusIndicatorFactory::~StatusIndicatorFactory()
57 impl_stopWakeUpThread();
60 void SAL_CALL StatusIndicatorFactory::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
62 if (lArguments.hasElements()) {
63 std::scoped_lock g(m_mutex);
65 css::uno::Reference< css::frame::XFrame > xTmpFrame;
66 css::uno::Reference< css::awt::XWindow > xTmpWindow;
67 bool b1 = lArguments[0] >>= xTmpFrame;
68 bool b2 = lArguments[0] >>= xTmpWindow;
69 if (lArguments.getLength() == 3 && b1) {
70 // it's the first service constructor "createWithFrame"
71 m_xFrame = xTmpFrame;
72 lArguments[1] >>= m_bDisableReschedule;
73 lArguments[2] >>= m_bAllowParentShow;
74 } else if (lArguments.getLength() == 3 && b2) {
75 // it's the second service constructor "createWithWindow"
76 m_xPluggWindow = xTmpWindow;
77 lArguments[1] >>= m_bDisableReschedule;
78 lArguments[2] >>= m_bAllowParentShow;
79 } else {
80 // it's an old-style initialisation using properties
81 ::comphelper::SequenceAsHashMap lArgs(lArguments);
83 m_xFrame = lArgs.getUnpackedValueOrDefault("Frame" , css::uno::Reference< css::frame::XFrame >());
84 m_xPluggWindow = lArgs.getUnpackedValueOrDefault("Window" , css::uno::Reference< css::awt::XWindow >() );
85 m_bAllowParentShow = lArgs.getUnpackedValueOrDefault("AllowParentShow" , false );
86 m_bDisableReschedule = lArgs.getUnpackedValueOrDefault("DisableReschedule", false );
90 #ifdef EMSCRIPTEN
91 m_bDisableReschedule = true;
92 #endif
93 impl_createProgress();
96 css::uno::Reference< css::task::XStatusIndicator > SAL_CALL StatusIndicatorFactory::createStatusIndicator()
98 return new StatusIndicator(this);
101 void SAL_CALL StatusIndicatorFactory::update()
103 std::scoped_lock g(m_mutex);
104 m_bAllowReschedule = true;
107 void StatusIndicatorFactory::start(const css::uno::Reference< css::task::XStatusIndicator >& xChild,
108 const OUString& sText ,
109 sal_Int32 nRange)
111 css::uno::Reference< css::task::XStatusIndicator > xProgress;
112 // SAFE -> ----------------------------------
114 std::scoped_lock aWriteLock(m_mutex);
116 // create new info structure for this child or move it to the front of our stack
117 IndicatorStack::iterator pItem = ::std::find(m_aStack.begin(), m_aStack.end(), xChild);
118 if (pItem != m_aStack.end())
119 m_aStack.erase(pItem);
120 IndicatorInfo aInfo(xChild, sText);
121 m_aStack.push_back (aInfo );
123 m_xActiveChild = xChild;
124 xProgress = m_xProgress;
126 // <- SAFE ----------------------------------
128 implts_makeParentVisibleIfAllowed();
130 if (xProgress.is())
131 xProgress->start(sText, nRange);
133 impl_startWakeUpThread();
134 impl_reschedule(true);
137 void StatusIndicatorFactory::reset(const css::uno::Reference< css::task::XStatusIndicator >& xChild)
139 css::uno::Reference< css::task::XStatusIndicator > xActive;
140 css::uno::Reference< css::task::XStatusIndicator > xProgress;
141 // SAFE -> ----------------------------------
143 std::scoped_lock aReadLock(m_mutex);
145 // reset the internal info structure related to this child
146 IndicatorStack::iterator pItem = ::std::find(m_aStack.begin(), m_aStack.end(), xChild);
147 if (pItem != m_aStack.end())
149 pItem->m_nValue = 0;
150 pItem->m_sText.clear();
153 xActive = m_xActiveChild;
154 xProgress = m_xProgress;
156 // <- SAFE ----------------------------------
158 // not the top most child => don't change UI
159 // But don't forget Reschedule!
160 if (
161 (xChild == xActive) &&
162 (xProgress.is() )
164 xProgress->reset();
166 impl_reschedule(true);
169 void StatusIndicatorFactory::end(const css::uno::Reference< css::task::XStatusIndicator >& xChild)
171 css::uno::Reference< css::task::XStatusIndicator > xActive;
172 css::uno::Reference< css::task::XStatusIndicator > xProgress;
173 OUString sText;
174 sal_Int32 nValue = 0;
175 // SAFE -> ----------------------------------
177 std::scoped_lock aWriteLock(m_mutex);
179 // remove this child from our stack
180 IndicatorStack::iterator pItem = ::std::find(m_aStack.begin(), m_aStack.end(), xChild);
181 if (pItem != m_aStack.end())
182 m_aStack.erase(pItem);
184 // activate next child ... or finish the progress if there is no further one.
185 m_xActiveChild.clear();
186 IndicatorStack::reverse_iterator pNext = m_aStack.rbegin();
187 if (pNext != m_aStack.rend())
189 m_xActiveChild = pNext->m_xIndicator;
190 sText = pNext->m_sText;
191 nValue = pNext->m_nValue;
194 xActive = m_xActiveChild;
195 xProgress = m_xProgress;
197 // <- SAFE ----------------------------------
199 if (xActive.is())
201 // There is at least one further child indicator.
202 // Actualize our progress, so it shows these values from now.
203 if (xProgress.is())
205 xProgress->setText (sText );
206 xProgress->setValue(nValue);
209 else
211 // Our stack is empty. No further child exists.
212 // Se we must "end" our progress really
213 if (xProgress.is())
214 xProgress->end();
215 // Now hide the progress bar again.
216 impl_hideProgress();
218 impl_stopWakeUpThread();
221 impl_reschedule(true);
224 void StatusIndicatorFactory::setText(const css::uno::Reference< css::task::XStatusIndicator >& xChild,
225 const OUString& sText )
227 css::uno::Reference< css::task::XStatusIndicator > xActive;
228 css::uno::Reference< css::task::XStatusIndicator > xProgress;
229 // SAFE -> ----------------------------------
231 std::scoped_lock aWriteLock(m_mutex);
233 IndicatorStack::iterator pItem = ::std::find(m_aStack.begin(), m_aStack.end(), xChild);
234 if (pItem != m_aStack.end())
235 pItem->m_sText = sText;
237 xActive = m_xActiveChild;
238 xProgress = m_xProgress;
240 // SAFE -> ----------------------------------
242 // paint only the top most indicator
243 // but don't forget to Reschedule!
244 if (
245 (xChild == xActive) &&
246 (xProgress.is() )
249 xProgress->setText(sText);
252 impl_reschedule(true);
255 void StatusIndicatorFactory::setValue( const css::uno::Reference< css::task::XStatusIndicator >& xChild ,
256 sal_Int32 nValue )
258 sal_Int32 nOldValue = 0;
259 css::uno::Reference< css::task::XStatusIndicator > xActive;
260 css::uno::Reference< css::task::XStatusIndicator > xProgress;
261 // SAFE -> ----------------------------------
263 std::scoped_lock aWriteLock(m_mutex);
265 IndicatorStack::iterator pItem = ::std::find(m_aStack.begin(), m_aStack.end(), xChild);
266 if (pItem != m_aStack.end())
268 nOldValue = pItem->m_nValue;
269 pItem->m_nValue = nValue;
272 xActive = m_xActiveChild;
273 xProgress = m_xProgress;
275 // SAFE -> ----------------------------------
277 if (
278 (xChild == xActive) &&
279 (nOldValue != nValue ) &&
280 (xProgress.is() )
283 xProgress->setValue(nValue);
286 impl_reschedule(false);
289 void StatusIndicatorFactory::implts_makeParentVisibleIfAllowed()
291 css::uno::Reference< css::frame::XFrame > xFrame;
292 css::uno::Reference< css::awt::XWindow > xPluggWindow;
293 css::uno::Reference< css::uno::XComponentContext > xContext;
294 // SAFE -> ----------------------------------
296 std::scoped_lock aReadLock(m_mutex);
298 if (!m_bAllowParentShow)
299 return;
301 xFrame = m_xFrame;
302 xPluggWindow = m_xPluggWindow;
303 xContext = m_xContext;
305 // <- SAFE ----------------------------------
307 css::uno::Reference< css::awt::XWindow > xParentWindow;
308 if (xFrame.is())
309 xParentWindow = xFrame->getContainerWindow();
310 else
311 xParentWindow = xPluggWindow;
313 // don't disturb user in case he put the loading document into the background!
314 // Suppress any setVisible() or toFront() call in case the initial show was
315 // already made.
316 css::uno::Reference< css::awt::XWindow2 > xVisibleCheck(xParentWindow, css::uno::UNO_QUERY);
317 bool bIsVisible = false;
318 if (xVisibleCheck.is())
319 bIsVisible = xVisibleCheck->isVisible();
321 if (bIsVisible)
323 impl_showProgress();
324 return;
327 // Check if the layout manager has been set to invisible state. It this case we are also
328 // not allowed to set the frame visible!
329 css::uno::Reference< css::beans::XPropertySet > xPropSet(xFrame, css::uno::UNO_QUERY);
330 if (xPropSet.is())
332 css::uno::Reference< css::frame::XLayoutManager2 > xLayoutManager;
333 xPropSet->getPropertyValue(FRAME_PROPNAME_ASCII_LAYOUTMANAGER) >>= xLayoutManager;
334 if (xLayoutManager.is())
336 if ( !xLayoutManager->isVisible() )
337 return;
341 // Ok the window should be made visible... because it is not currently visible.
342 // BUT..!
343 // We need a Hack for our applications: They get her progress from the frame directly
344 // on saving documents. Because there is no progress set on the MediaDescriptor.
345 // But that's wrong. In case the document was opened hidden, they should not use any progress .-(
346 // They only possible workaround: don't show the parent window here, if the document was opened hidden.
347 bool bHiddenDoc = false;
348 if (xFrame.is())
350 css::uno::Reference< css::frame::XController > xController;
351 css::uno::Reference< css::frame::XModel > xModel;
352 xController = xFrame->getController();
353 if (xController.is())
354 xModel = xController->getModel();
355 if (xModel.is())
357 utl::MediaDescriptor lDocArgs(xModel->getArgs());
358 bHiddenDoc = lDocArgs.getUnpackedValueOrDefault(
359 utl::MediaDescriptor::PROP_HIDDEN,
360 false);
364 if (bHiddenDoc)
365 return;
367 // OK: The document was not opened in hidden mode ...
368 // and the window isn't already visible.
369 // Show it and bring it to front.
370 // But before we have to be sure, that our internal used helper progress
371 // is visible too.
372 impl_showProgress();
374 SolarMutexGuard aSolarGuard;
375 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow(xParentWindow);
376 if ( pWindow )
378 bool bForceFrontAndFocus(officecfg::Office::Common::View::NewDocumentHandling::ForceFocusAndToFront::get());
379 pWindow->Show(true, bForceFrontAndFocus ? ShowFlags::ForegroundTask : ShowFlags::NONE );
384 void StatusIndicatorFactory::impl_createProgress()
386 css::uno::Reference< css::frame::XFrame > xFrame;
387 css::uno::Reference< css::awt::XWindow > xWindow;
388 // SAFE -> ----------------------------------
390 std::scoped_lock aReadLock(m_mutex);
392 xFrame = m_xFrame;
393 xWindow = m_xPluggWindow;
395 // <- SAFE ----------------------------------
397 css::uno::Reference< css::task::XStatusIndicator > xProgress;
399 if (xWindow.is())
401 // use vcl based progress implementation in plugged mode
402 xProgress = new VCLStatusIndicator(xWindow);
404 else if (xFrame.is())
406 // use frame layouted progress implementation
407 css::uno::Reference< css::beans::XPropertySet > xPropSet(xFrame, css::uno::UNO_QUERY);
408 if (xPropSet.is())
410 css::uno::Reference< css::frame::XLayoutManager2 > xLayoutManager;
411 xPropSet->getPropertyValue(FRAME_PROPNAME_ASCII_LAYOUTMANAGER) >>= xLayoutManager;
412 if (xLayoutManager.is())
414 xLayoutManager->lock();
415 OUString sPROGRESS_RESOURCE(PROGRESS_RESOURCE);
416 xLayoutManager->createElement( sPROGRESS_RESOURCE );
417 xLayoutManager->hideElement( sPROGRESS_RESOURCE );
419 css::uno::Reference< css::ui::XUIElement > xProgressBar = xLayoutManager->getElement(sPROGRESS_RESOURCE);
420 if (xProgressBar.is())
421 xProgress.set(xProgressBar->getRealInterface(), css::uno::UNO_QUERY);
422 xLayoutManager->unlock();
427 std::scoped_lock g(m_mutex);
428 m_xProgress = xProgress;
431 void StatusIndicatorFactory::impl_showProgress()
433 css::uno::Reference< css::frame::XFrame > xFrame;
434 // SAFE -> ----------------------------------
436 std::scoped_lock aReadLock(m_mutex);
438 xFrame = m_xFrame;
440 // <- SAFE ----------------------------------
442 css::uno::Reference< css::task::XStatusIndicator > xProgress;
444 if (!xFrame.is())
445 return;
447 // use frame layouted progress implementation
448 css::uno::Reference< css::beans::XPropertySet > xPropSet(xFrame, css::uno::UNO_QUERY);
449 if (xPropSet.is())
451 css::uno::Reference< css::frame::XLayoutManager2 > xLayoutManager;
452 xPropSet->getPropertyValue(FRAME_PROPNAME_ASCII_LAYOUTMANAGER) >>= xLayoutManager;
453 if (xLayoutManager.is())
455 // Be sure that we have always a progress. It can be that our frame
456 // was recycled and therefore the progress was destroyed!
457 // CreateElement does nothing if there is already a valid progress.
458 OUString sPROGRESS_RESOURCE(PROGRESS_RESOURCE);
459 xLayoutManager->createElement( sPROGRESS_RESOURCE );
460 xLayoutManager->showElement( sPROGRESS_RESOURCE );
462 css::uno::Reference< css::ui::XUIElement > xProgressBar = xLayoutManager->getElement(sPROGRESS_RESOURCE);
463 if (xProgressBar.is())
464 xProgress.set(xProgressBar->getRealInterface(), css::uno::UNO_QUERY);
468 std::scoped_lock g(m_mutex);
469 m_xProgress = xProgress;
472 void StatusIndicatorFactory::impl_hideProgress()
474 css::uno::Reference< css::frame::XFrame > xFrame;
475 // SAFE -> ----------------------------------
477 std::scoped_lock aReadLock(m_mutex);
479 xFrame = m_xFrame;
481 // <- SAFE ----------------------------------
483 if (xFrame.is())
485 // use frame layouted progress implementation
486 css::uno::Reference< css::beans::XPropertySet > xPropSet(xFrame, css::uno::UNO_QUERY);
487 if (xPropSet.is())
489 css::uno::Reference< css::frame::XLayoutManager2 > xLayoutManager;
490 xPropSet->getPropertyValue(FRAME_PROPNAME_ASCII_LAYOUTMANAGER) >>= xLayoutManager;
491 if (xLayoutManager.is())
492 xLayoutManager->hideElement( PROGRESS_RESOURCE );
497 void StatusIndicatorFactory::impl_reschedule(bool bForce)
499 // SAFE ->
501 std::scoped_lock aReadLock(m_mutex);
502 if (m_bDisableReschedule)
503 return;
505 // <- SAFE
507 bool bReschedule = bForce;
508 if (!bReschedule)
510 std::scoped_lock g(m_mutex);
511 bReschedule = m_bAllowReschedule;
512 m_bAllowReschedule = false;
515 if (!bReschedule)
516 return;
518 static std::mutex rescheduleLock;
519 // SAFE ->
520 std::unique_lock aRescheduleGuard(rescheduleLock);
522 if (m_nInReschedule != 0)
523 return;
525 // coverity[missing_lock: FALSE] - coverity fails to see the aRescheduleGuard ctor as taking a lock
526 ++m_nInReschedule;
527 aRescheduleGuard.unlock();
528 // <- SAFE
531 SolarMutexGuard g;
532 Application::Reschedule(true);
535 // SAFE ->
536 aRescheduleGuard.lock();
537 --m_nInReschedule;
540 void StatusIndicatorFactory::impl_startWakeUpThread()
542 std::scoped_lock g(m_mutex);
544 if (m_bDisableReschedule)
545 return;
547 if (!m_pWakeUp)
548 m_pWakeUp.reset(new WakeUpThread(this));
551 void StatusIndicatorFactory::impl_stopWakeUpThread()
553 std::unique_ptr<WakeUpThread> wakeUp;
555 std::scoped_lock g(m_mutex);
556 std::swap(wakeUp, m_pWakeUp);
558 if (wakeUp)
559 wakeUp->stop();
562 } // namespace framework
564 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
565 com_sun_star_comp_framework_StatusIndicatorFactory_get_implementation(
566 css::uno::XComponentContext *context,
567 css::uno::Sequence<css::uno::Any> const &)
569 return cppu::acquire(new framework::StatusIndicatorFactory(context));
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */