Bug Summary

File:home/maarten/src/libreoffice/core/svtools/source/uno/toolboxcontroller.cxx
Warning:line 738, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name toolboxcontroller.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SVT_DLLIMPLEMENTATION -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/svtools/source/inc -I /home/maarten/src/libreoffice/core/svtools/inc -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/svtools/source/uno/toolboxcontroller.cxx

/home/maarten/src/libreoffice/core/svtools/source/uno/toolboxcontroller.cxx

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#include <svtools/toolboxcontroller.hxx>
21#include <com/sun/star/beans/PropertyAttribute.hpp>
22#include <com/sun/star/beans/PropertyValue.hpp>
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/frame/XDispatchProvider.hpp>
25#include <com/sun/star/lang/DisposedException.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27#include <com/sun/star/frame/XLayoutManager.hpp>
28#include <com/sun/star/util/URLTransformer.hpp>
29#include <vcl/svapp.hxx>
30#include <toolkit/helper/vclunohelper.hxx>
31#include <vcl/toolbox.hxx>
32#include <vcl/weldutils.hxx>
33#include <comphelper/processfactory.hxx>
34
35const int TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE = 1;
36const char TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE[] = "SupportsVisible";
37
38
39using namespace ::cppu;
40using namespace css::awt;
41using namespace css::uno;
42using namespace css::util;
43using namespace css::beans;
44using namespace css::lang;
45using namespace css::frame;
46
47namespace svt
48{
49
50ToolboxController::ToolboxController(
51 const Reference< XComponentContext >& rxContext,
52 const Reference< XFrame >& xFrame,
53 const OUString& aCommandURL ) :
54 OPropertyContainer( GetBroadcastHelper() )
55 , m_bSupportVisible( false )
56 , m_bInitialized( false )
57 , m_bDisposed( false )
58 , m_bSidebar( false )
59 , m_nToolBoxId( SAL_MAX_UINT16((sal_uInt16) 0xFFFF) )
60 , m_xFrame( xFrame )
61 , m_xContext( rxContext )
62 , m_aCommandURL( aCommandURL )
63 , m_aListenerContainer( m_aMutex )
64 , m_pToolbar(nullptr)
65 , m_pBuilder(nullptr)
66{
67 OSL_ASSERT( m_xContext.is() )do { if (true && (!(m_xContext.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/svtools/source/uno/toolboxcontroller.cxx"
":" "67" ": "), "OSL_ASSERT: %s", "m_xContext.is()"); } } while
(false)
;
68 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE,
69 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE,
70 css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY,
71 &m_bSupportVisible, cppu::UnoType<decltype(m_bSupportVisible)>::get());
72
73 try
74 {
75 m_xUrlTransformer = URLTransformer::create( rxContext );
76 }
77 catch(const Exception&)
78 {
79 }
80}
81
82ToolboxController::ToolboxController() :
83 OPropertyContainer(GetBroadcastHelper())
84 , m_bSupportVisible(false)
85 , m_bInitialized( false )
86 , m_bDisposed( false )
87 , m_bSidebar( false )
88 , m_nToolBoxId( SAL_MAX_UINT16((sal_uInt16) 0xFFFF) )
89 , m_aListenerContainer( m_aMutex )
90 , m_pToolbar(nullptr)
91 , m_pBuilder(nullptr)
92{
93 registerProperty( TOOLBARCONTROLLER_PROPNAME_SUPPORTSVISIBLE,
94 TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE,
95 css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY,
96 &m_bSupportVisible, cppu::UnoType<decltype(m_bSupportVisible)>::get());
97}
98
99ToolboxController::~ToolboxController()
100{
101}
102
103Reference< XFrame > ToolboxController::getFrameInterface() const
104{
105 SolarMutexGuard aSolarMutexGuard;
106 return m_xFrame;
107}
108
109const Reference< XComponentContext > & ToolboxController::getContext() const
110{
111 SolarMutexGuard aSolarMutexGuard;
112 return m_xContext;
113}
114
115Reference< XLayoutManager > ToolboxController::getLayoutManager() const
116{
117 Reference< XLayoutManager > xLayoutManager;
118 Reference< XPropertySet > xPropSet;
119 {
120 SolarMutexGuard aSolarMutexGuard;
121 xPropSet.set( m_xFrame, UNO_QUERY );
122 }
123
124 if ( xPropSet.is() )
125 {
126 try
127 {
128 xLayoutManager.set(xPropSet->getPropertyValue("LayoutManager"),UNO_QUERY);
129 }
130 catch ( Exception& )
131 {
132 }
133 }
134
135 return xLayoutManager;
136}
137
138// XInterface
139Any SAL_CALL ToolboxController::queryInterface( const Type& rType )
140{
141 css::uno::Any a(ToolboxController_Base::queryInterface(rType));
142 return a.hasValue() ? a : OPropertyContainer::queryInterface(rType);
143}
144
145void SAL_CALL ToolboxController::acquire() throw ()
146{
147 ToolboxController_Base::acquire();
148}
149
150void SAL_CALL ToolboxController::release() throw ()
151{
152 ToolboxController_Base::release();
153}
154
155css::uno::Sequence<css::uno::Type> ToolboxController::getTypes()
156{
157 return comphelper::concatSequences(ToolboxController_Base::getTypes(),
158 getBaseTypes());
159}
160
161void SAL_CALL ToolboxController::initialize( const Sequence< Any >& aArguments )
162{
163 bool bInitialized( true );
164
165 {
166 SolarMutexGuard aSolarMutexGuard;
167
168 if ( m_bDisposed )
169 throw DisposedException();
170
171 bInitialized = m_bInitialized;
172 }
173
174 if ( bInitialized )
175 return;
176
177 SolarMutexGuard aSolarMutexGuard;
178 m_bInitialized = true;
179 m_bSupportVisible = false;
180 PropertyValue aPropValue;
181 for ( const auto& rArgument : aArguments )
182 {
183 if ( rArgument >>= aPropValue )
184 {
185 if ( aPropValue.Name == "Frame" )
186 m_xFrame.set(aPropValue.Value,UNO_QUERY);
187 else if ( aPropValue.Name == "CommandURL" )
188 aPropValue.Value >>= m_aCommandURL;
189 else if ( aPropValue.Name == "ServiceManager" )
190 {
191 Reference<XMultiServiceFactory> xMSF(aPropValue.Value, UNO_QUERY);
192 if (xMSF.is())
193 m_xContext = comphelper::getComponentContext(xMSF);
194 }
195 else if ( aPropValue.Name == "ParentWindow" )
196 m_xParentWindow.set(aPropValue.Value,UNO_QUERY);
197 else if ( aPropValue.Name == "ModuleIdentifier" )
198 aPropValue.Value >>= m_sModuleName;
199 else if ( aPropValue.Name == "Identifier" )
200 aPropValue.Value >>= m_nToolBoxId;
201 else if ( aPropValue.Name == "IsSidebar" )
202 aPropValue.Value >>= m_bSidebar;
203 }
204 }
205
206 try
207 {
208 if ( !m_xUrlTransformer.is() && m_xContext.is() )
209 m_xUrlTransformer = URLTransformer::create( m_xContext );
210 }
211 catch(const Exception&)
212 {
213 }
214
215 if ( !m_aCommandURL.isEmpty() )
216 m_aListenerMap.emplace( m_aCommandURL, Reference< XDispatch >() );
217
218 if (weld::TransportAsXWindow* pTunnel = dynamic_cast<weld::TransportAsXWindow*>(getParent().get()))
219 {
220 m_pToolbar = dynamic_cast<weld::Toolbar*>(pTunnel->getWidget());
221 assert(m_pToolbar && "must be a toolbar")(static_cast <bool> (m_pToolbar && "must be a toolbar"
) ? void (0) : __assert_fail ("m_pToolbar && \"must be a toolbar\""
, "/home/maarten/src/libreoffice/core/svtools/source/uno/toolboxcontroller.cxx"
, 221, __extension__ __PRETTY_FUNCTION__))
;
222 m_pBuilder = pTunnel->getBuilder();
223 }
224}
225
226void SAL_CALL ToolboxController::update()
227{
228 {
229 SolarMutexGuard aSolarMutexGuard;
230 if ( m_bDisposed )
231 throw DisposedException();
232 }
233
234 // Bind all registered listeners to their dispatch objects
235 bindListener();
236}
237
238// XComponent
239void SAL_CALL ToolboxController::dispose()
240{
241 Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
242
243 {
244 SolarMutexGuard aSolarMutexGuard;
245 if ( m_bDisposed )
246 throw DisposedException();
247 }
248
249 css::lang::EventObject aEvent( xThis );
250 m_aListenerContainer.disposeAndClear( aEvent );
251
252 SolarMutexGuard aSolarMutexGuard;
253 Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
254 for (auto const& listener : m_aListenerMap)
255 {
256 try
257 {
258 Reference< XDispatch > xDispatch( listener.second );
259
260 css::util::URL aTargetURL;
261 aTargetURL.Complete = listener.first;
262 if ( m_xUrlTransformer.is() )
263 m_xUrlTransformer->parseStrict( aTargetURL );
264
265 if ( xDispatch.is() && xStatusListener.is() )
266 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
267 }
268 catch ( Exception& )
269 {
270 }
271
272 }
273
274 m_bDisposed = true;
275}
276
277void SAL_CALL ToolboxController::addEventListener( const Reference< XEventListener >& xListener )
278{
279 m_aListenerContainer.addInterface( cppu::UnoType<XEventListener>::get(), xListener );
280}
281
282void SAL_CALL ToolboxController::removeEventListener( const Reference< XEventListener >& aListener )
283{
284 m_aListenerContainer.removeInterface( cppu::UnoType<XEventListener>::get(), aListener );
285}
286
287// XEventListener
288void SAL_CALL ToolboxController::disposing( const EventObject& Source )
289{
290 Reference< XInterface > xSource( Source.Source );
291
292 SolarMutexGuard aSolarMutexGuard;
293
294 if ( m_bDisposed )
295 return;
296
297 for (auto & listener : m_aListenerMap)
298 {
299 // Compare references and release dispatch references if they are equal.
300 Reference< XInterface > xIfac(listener.second, UNO_QUERY);
301 if ( xSource == xIfac )
302 listener.second.clear();
303 }
304
305 Reference< XInterface > xIfac( m_xFrame, UNO_QUERY );
306 if ( xIfac == xSource )
307 m_xFrame.clear();
308}
309
310// XStatusListener
311void SAL_CALL ToolboxController::statusChanged( const FeatureStateEvent& )
312{
313 // must be implemented by sub class
314}
315
316// XToolbarController
317void SAL_CALL ToolboxController::execute( sal_Int16 KeyModifier )
318{
319 Reference< XDispatch > xDispatch;
320 OUString aCommandURL;
321
322 {
323 SolarMutexGuard aSolarMutexGuard;
324
325 if ( m_bDisposed )
326 throw DisposedException();
327
328 if ( m_bInitialized &&
329 m_xFrame.is() &&
330 !m_aCommandURL.isEmpty() )
331 {
332 aCommandURL = m_aCommandURL;
333 URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
334 if ( pIter != m_aListenerMap.end() )
335 xDispatch = pIter->second;
336 }
337 }
338
339 if ( !xDispatch.is() )
340 return;
341
342 try
343 {
344 css::util::URL aTargetURL;
345 Sequence<PropertyValue> aArgs( 1 );
346
347 // Provide key modifier information to dispatch function
348 aArgs[0].Name = "KeyModifier";
349 aArgs[0].Value <<= KeyModifier;
350
351 aTargetURL.Complete = aCommandURL;
352 if ( m_xUrlTransformer.is() )
353 m_xUrlTransformer->parseStrict( aTargetURL );
354 xDispatch->dispatch( aTargetURL, aArgs );
355 }
356 catch ( DisposedException& )
357 {
358 }
359}
360
361void SAL_CALL ToolboxController::click()
362{
363}
364
365void SAL_CALL ToolboxController::doubleClick()
366{
367}
368
369Reference< XWindow > SAL_CALL ToolboxController::createPopupWindow()
370{
371 return Reference< XWindow >();
372}
373
374Reference< XWindow > SAL_CALL ToolboxController::createItemWindow( const Reference< XWindow >& )
375{
376 return Reference< XWindow >();
377}
378
379void ToolboxController::addStatusListener( const OUString& aCommandURL )
380{
381 Reference< XDispatch > xDispatch;
382 Reference< XStatusListener > xStatusListener;
383 css::util::URL aTargetURL;
384
385 {
386 SolarMutexGuard aSolarMutexGuard;
387 URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
388
389 // Already in the list of status listener. Do nothing.
390 if ( pIter != m_aListenerMap.end() )
391 return;
392
393 // Check if we are already initialized. Implementation starts adding itself as status listener when
394 // initialize is called.
395 if ( !m_bInitialized )
396 {
397 // Put into the unordered_map of status listener. Will be activated when initialized is called
398 m_aListenerMap.emplace( aCommandURL, Reference< XDispatch >() );
399 return;
400 }
401 else
402 {
403 // Add status listener directly as initialize has already been called.
404 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
405 if ( m_xContext.is() && xDispatchProvider.is() )
406 {
407 aTargetURL.Complete = aCommandURL;
408 if ( m_xUrlTransformer.is() )
409 m_xUrlTransformer->parseStrict( aTargetURL );
410 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
411
412 xStatusListener.set( static_cast< OWeakObject* >( this ), UNO_QUERY );
413 URLToDispatchMap::iterator aIter = m_aListenerMap.find( aCommandURL );
414 if ( aIter != m_aListenerMap.end() )
415 {
416 Reference< XDispatch > xOldDispatch( aIter->second );
417 aIter->second = xDispatch;
418
419 try
420 {
421 if ( xOldDispatch.is() )
422 xOldDispatch->removeStatusListener( xStatusListener, aTargetURL );
423 }
424 catch ( Exception& )
425 {
426 }
427 }
428 else
429 m_aListenerMap.emplace( aCommandURL, xDispatch );
430 }
431 }
432 }
433
434 // Call without locked mutex as we are called back from dispatch implementation
435 try
436 {
437 if ( xDispatch.is() )
438 xDispatch->addStatusListener( xStatusListener, aTargetURL );
439 }
440 catch ( Exception& )
441 {
442 }
443}
444
445void ToolboxController::removeStatusListener( const OUString& aCommandURL )
446{
447 SolarMutexGuard aSolarMutexGuard;
448
449 URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
450 if ( pIter == m_aListenerMap.end() )
451 return;
452
453 Reference< XDispatch > xDispatch( pIter->second );
454 Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
455 m_aListenerMap.erase( pIter );
456
457 try
458 {
459 css::util::URL aTargetURL;
460 aTargetURL.Complete = aCommandURL;
461 if ( m_xUrlTransformer.is() )
462 m_xUrlTransformer->parseStrict( aTargetURL );
463
464 if ( xDispatch.is() && xStatusListener.is() )
465 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
466 }
467 catch ( Exception& )
468 {
469 }
470}
471
472void ToolboxController::bindListener()
473{
474 std::vector< Listener > aDispatchVector;
475 Reference< XStatusListener > xStatusListener;
476
477 {
478 SolarMutexGuard aSolarMutexGuard;
479
480 if ( !m_bInitialized )
481 return;
482
483 // Collect all registered command URL's and store them temporary
484 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
485 if ( m_xContext.is() && xDispatchProvider.is() )
486 {
487 xStatusListener.set( static_cast< OWeakObject* >( this ), UNO_QUERY );
488 for (auto & listener : m_aListenerMap)
489 {
490 css::util::URL aTargetURL;
491 aTargetURL.Complete = listener.first;
492 if ( m_xUrlTransformer.is() )
493 m_xUrlTransformer->parseStrict( aTargetURL );
494
495 Reference< XDispatch > xDispatch(listener.second);
496 if ( xDispatch.is() )
497 {
498 // We already have a dispatch object => we have to requery.
499 // Release old dispatch object and remove it as listener
500 try
501 {
502 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
503 }
504 catch ( Exception& )
505 {
506 }
507 }
508
509 listener.second.clear();
510 xDispatch.clear();
511
512 // Query for dispatch object. Old dispatch will be released with this, too.
513 try
514 {
515 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
516 }
517 catch ( Exception& )
518 {
519 }
520 listener.second = xDispatch;
521
522 Listener aListener( aTargetURL, xDispatch );
523 aDispatchVector.push_back( aListener );
524 }
525 }
526 }
527
528 // Call without locked mutex as we are called back from dispatch implementation
529 if ( !xStatusListener.is() )
530 return;
531
532 try
533 {
534 for (Listener & rListener : aDispatchVector)
535 {
536 if ( rListener.xDispatch.is() )
537 rListener.xDispatch->addStatusListener( xStatusListener, rListener.aURL );
538 else if ( rListener.aURL.Complete == m_aCommandURL )
539 {
540 try
541 {
542 // Send status changed for the main URL, if we cannot get a valid dispatch object.
543 // UI disables the button. Catch exception as we release our mutex, it is possible
544 // that someone else already disposed this instance!
545 FeatureStateEvent aFeatureStateEvent;
546 aFeatureStateEvent.IsEnabled = false;
547 aFeatureStateEvent.FeatureURL = rListener.aURL;
548 aFeatureStateEvent.State = Any();
549 xStatusListener->statusChanged( aFeatureStateEvent );
550 }
551 catch ( Exception& )
552 {
553 }
554 }
555 }
556 }
557 catch ( Exception& )
558 {
559 }
560}
561
562void ToolboxController::unbindListener()
563{
564 SolarMutexGuard aSolarMutexGuard;
565
566 if ( !m_bInitialized )
567 return;
568
569 // Collect all registered command URL's and store them temporary
570 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
571 if ( !(m_xContext.is() && xDispatchProvider.is()) )
572 return;
573
574 Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
575 for (auto & listener : m_aListenerMap)
576 {
577 css::util::URL aTargetURL;
578 aTargetURL.Complete = listener.first;
579 if ( m_xUrlTransformer.is() )
580 m_xUrlTransformer->parseStrict( aTargetURL );
581
582 Reference< XDispatch > xDispatch(listener.second);
583 if ( xDispatch.is() )
584 {
585 // We already have a dispatch object => we have to requery.
586 // Release old dispatch object and remove it as listener
587 try
588 {
589 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
590 }
591 catch ( Exception& )
592 {
593 }
594 }
595 listener.second.clear();
596 }
597}
598
599void ToolboxController::updateStatus()
600{
601 bindListener();
602}
603
604void ToolboxController::updateStatus( const OUString& aCommandURL )
605{
606 Reference< XDispatch > xDispatch;
607 Reference< XStatusListener > xStatusListener;
608 css::util::URL aTargetURL;
609
610 {
611 SolarMutexGuard aSolarMutexGuard;
612
613 if ( !m_bInitialized )
614 return;
615
616 // Try to find a dispatch object for the requested command URL
617 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
618 xStatusListener.set( static_cast< OWeakObject* >( this ), UNO_QUERY );
619 if ( m_xContext.is() && xDispatchProvider.is() )
620 {
621 aTargetURL.Complete = aCommandURL;
622 if ( m_xUrlTransformer.is() )
623 m_xUrlTransformer->parseStrict( aTargetURL );
624 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
625 }
626 }
627
628 if ( !(xDispatch.is() && xStatusListener.is()) )
629 return;
630
631 // Catch exception as we release our mutex, it is possible that someone else
632 // has already disposed this instance!
633 // Add/remove status listener to get an update status information from the
634 // requested command.
635 try
636 {
637 xDispatch->addStatusListener( xStatusListener, aTargetURL );
638 xDispatch->removeStatusListener( xStatusListener, aTargetURL );
639 }
640 catch ( Exception& )
641 {
642 }
643}
644
645
646void ToolboxController::dispatchCommand( const OUString& sCommandURL, const Sequence< PropertyValue >& rArgs, const OUString &sTarget )
647{
648 try
649 {
650 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY_THROW );
651 URL aURL;
652 aURL.Complete = sCommandURL;
653 getURLTransformer()->parseStrict( aURL );
654
655 Reference< XDispatch > xDispatch( xDispatchProvider->queryDispatch( aURL, sTarget, 0 ), UNO_SET_THROW );
656
657 std::unique_ptr<DispatchInfo> pDispatchInfo(new DispatchInfo( xDispatch, aURL, rArgs ));
658 if ( Application::PostUserEvent( LINK(nullptr, ToolboxController, ExecuteHdl_Impl)::tools::detail::makeLink( ::tools::detail::castTo<ToolboxController
*>(nullptr), &ToolboxController::LinkStubExecuteHdl_Impl
)
,
659 pDispatchInfo.get() ) )
660 pDispatchInfo.release();
661
662 }
663 catch( Exception& )
664 {
665 }
666}
667
668
669css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL ToolboxController::getPropertySetInfo()
670{
671 Reference<XPropertySetInfo> xInfo( createPropertySetInfo( getInfoHelper() ) );
672 return xInfo;
673}
674
675::cppu::IPropertyArrayHelper& ToolboxController::getInfoHelper()
676{
677 return *getArrayHelper();
678}
679
680
681::cppu::IPropertyArrayHelper* ToolboxController::createArrayHelper( ) const
682{
683 css::uno::Sequence< Property > aProps;
684 describeProperties(aProps);
685 return new ::cppu::OPropertyArrayHelper(aProps);
686}
687
688sal_Bool SAL_CALL ToolboxController::convertFastPropertyValue( css::uno::Any& aConvertedValue ,
689 css::uno::Any& aOldValue ,
690 sal_Int32 nHandle ,
691 const css::uno::Any& aValue )
692{
693 switch (nHandle)
694 {
695 case TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE:
696 {
697 bool aNewValue(false);
698 aValue >>= aNewValue;
699 if (aNewValue != m_bSupportVisible)
700 {
701 aConvertedValue <<= aNewValue;
702 aOldValue <<= m_bSupportVisible;
703 return true;
704 }
705 return false;
706 }
707 }
708 return OPropertyContainer::convertFastPropertyValue(aConvertedValue, aOldValue, nHandle, aValue);
709}
710
711void SAL_CALL ToolboxController::setFastPropertyValue_NoBroadcast(
712 sal_Int32 nHandle,
713 const css::uno::Any& aValue )
714{
715 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, aValue);
716 if (TOOLBARCONTROLLER_PROPHANDLE_SUPPORTSVISIBLE == nHandle)
717 {
718 bool rValue(false);
719 if (( aValue >>= rValue ) && m_bInitialized)
720 m_bSupportVisible = rValue;
721 }
722}
723
724
725IMPL_STATIC_LINK( ToolboxController, ExecuteHdl_Impl, void*, p, void )void ToolboxController::LinkStubExecuteHdl_Impl(void * instance
, void* data) { return ExecuteHdl_Impl(static_cast<ToolboxController
*>(instance), data); } void ToolboxController::ExecuteHdl_Impl
(__attribute__ ((unused)) ToolboxController *, void* p)
726{
727 DispatchInfo* pDispatchInfo = static_cast<DispatchInfo*>(p);
728 pDispatchInfo->mxDispatch->dispatch( pDispatchInfo->maURL, pDispatchInfo->maArgs );
729 delete pDispatchInfo;
730}
731
732void ToolboxController::enable( bool bEnable )
733{
734 ToolBox* pToolBox = nullptr;
735 sal_uInt16 nItemId = 0;
736 if( getToolboxId( nItemId, &pToolBox ) )
1
Calling 'ToolboxController::getToolboxId'
16
Returning; memory was released via 2nd parameter
17
Taking true branch
737 {
738 pToolBox->EnableItem( nItemId, bEnable );
18
Use of memory after it is freed
739 }
740}
741
742bool ToolboxController::getToolboxId( sal_uInt16& rItemId, ToolBox** ppToolBox )
743{
744 if( (m_nToolBoxId != SAL_MAX_UINT16((sal_uInt16) 0xFFFF)) && (ppToolBox == nullptr) )
2
Assuming field 'm_nToolBoxId' is not equal to SAL_MAX_UINT16
3
Taking false branch
745 return m_nToolBoxId;
746
747 ToolBox* pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ).get() );
4
Calling implicit destructor for 'VclPtr<vcl::Window>'
5
Calling '~Reference'
13
Returning from '~Reference'
14
Returning from destructor for 'VclPtr<vcl::Window>'
748
749 if( (m_nToolBoxId
14.1
Field 'm_nToolBoxId' is not equal to SAL_MAX_UINT16
14.1
Field 'm_nToolBoxId' is not equal to SAL_MAX_UINT16
14.1
Field 'm_nToolBoxId' is not equal to SAL_MAX_UINT16
== SAL_MAX_UINT16((sal_uInt16) 0xFFFF)) && pToolBox )
750 {
751 const ToolBox::ImplToolItems::size_type nCount = pToolBox->GetItemCount();
752 for ( ToolBox::ImplToolItems::size_type nPos = 0; nPos < nCount; ++nPos )
753 {
754 const sal_uInt16 nItemId = pToolBox->GetItemId( nPos );
755 if ( pToolBox->GetItemCommand( nItemId ) == m_aCommandURL )
756 {
757 m_nToolBoxId = nItemId;
758 break;
759 }
760 }
761 }
762
763 if( ppToolBox
14.2
'ppToolBox' is non-null
14.2
'ppToolBox' is non-null
14.2
'ppToolBox' is non-null
)
15
Taking true branch
764 *ppToolBox = pToolBox;
765
766 rItemId = m_nToolBoxId;
767
768 return (rItemId
15.1
'rItemId' is not equal to SAL_MAX_UINT16
15.1
'rItemId' is not equal to SAL_MAX_UINT16
15.1
'rItemId' is not equal to SAL_MAX_UINT16
!= SAL_MAX_UINT16((sal_uInt16) 0xFFFF)) && (( ppToolBox == nullptr) || (*ppToolBox != nullptr) );
769}
770//end
771
772} // svt
773
774/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
6
Assuming field 'm_pBody' is non-null
7
Taking true branch
113 m_pBody->release();
8
Calling 'VclReferenceBase::release'
12
Returning; memory was released
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody)
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
9
Assuming the condition is true
10
Taking true branch
40 delete this;
11
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif