Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / oframes.cxx
blob2fd43c8a52a31be530358ac9c3624fc1d65b0fcb
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 <helper/oframes.hxx>
22 #include <com/sun/star/frame/FrameSearchFlag.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <vcl/svapp.hxx>
25 #include <sal/log.hxx>
27 namespace framework{
29 using namespace ::com::sun::star::container;
30 using namespace ::com::sun::star::frame;
31 using namespace ::com::sun::star::lang;
32 using namespace ::com::sun::star::uno;
33 using namespace ::cppu;
34 using namespace ::osl;
36 // constructor
38 OFrames::OFrames( const css::uno::Reference< XFrame >& xOwner ,
39 FrameContainer* pFrameContainer )
40 : m_xOwner ( xOwner )
41 , m_pFrameContainer ( pFrameContainer )
42 , m_bRecursiveSearchProtection( false )
44 // An instance of this class can only work with valid initialization.
45 // We share the mutex with our owner class, need a valid factory to instantiate new services and
46 // use the access to our owner for some operations.
47 SAL_WARN_IF( !xOwner.is() || !pFrameContainer, "fwk", "OFrames::OFrames(): Invalid parameter detected!" );
50 // (protected!) destructor
52 OFrames::~OFrames()
54 // Reset instance, free memory...
55 impl_resetObject();
58 // XFrames
59 void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame )
61 SolarMutexGuard g;
63 // Safe impossible cases
64 // Method is not defined for ALL incoming parameters!
65 SAL_WARN_IF( !xFrame.is(), "fwk", "OFrames::append(): Invalid parameter detected!" );
67 // Do the follow only, if owner instance valid!
68 // Lock owner for follow operations - make a "hard reference"!
69 css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
70 if ( xOwner.is() )
72 // Append frame to the end of the container ...
73 m_pFrameContainer->append( xFrame );
74 // Set owner of this instance as parent of the new frame in container!
75 xFrame->setCreator( xOwner );
77 // Else; Do nothing! Our owner is dead.
78 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
81 // XFrames
82 void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame )
84 SolarMutexGuard g;
86 // Safe impossible cases
87 // Method is not defined for ALL incoming parameters!
88 SAL_WARN_IF( !xFrame.is(), "fwk", "OFrames::remove(): Invalid parameter detected!" );
90 // Do the follow only, if owner instance valid!
91 // Lock owner for follow operations - make a "hard reference"!
92 css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
93 if ( xOwner.is() )
95 // Search frame and remove it from container ...
96 m_pFrameContainer->remove( xFrame );
97 // Don't reset owner-property of removed frame!
98 // This must do the caller of this method himself.
99 // See documentation of interface XFrames for further information.
101 // Else; Do nothing! Our owner is dead.
102 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
105 // XFrames
106 Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags )
108 SolarMutexGuard g;
110 // Safe impossible cases
111 // Method is not defined for ALL incoming parameters!
112 SAL_WARN_IF( !impldbg_checkParameter_queryFrames( nSearchFlags ), "fwk", "OFrames::queryFrames(): Invalid parameter detected!" );
114 // Set default return value. (empty sequence)
115 Sequence< css::uno::Reference< XFrame > > seqFrames;
117 // Do the follow only, if owner instance valid.
118 // Lock owner for follow operations - make a "hard reference"!
119 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
120 if ( xOwner.is() )
122 // Work only, if search was not started here ...!
123 if( !m_bRecursiveSearchProtection )
125 // This class is a helper for services, which must implement XFrames.
126 // His parent and children are MY parent and children to.
127 // All searchflags are supported by this implementation!
128 // If some flags should not be supported - don't call me with this flags!!!
130 // Search with AUTO-flag is not supported yet!
131 // We think about right implementation.
132 SAL_WARN_IF( (nSearchFlags & FrameSearchFlag::AUTO), "fwk", "OFrames::queryFrames(): Search with AUTO-flag is not supported yet!" );
134 // Search for ALL and GLOBAL is superfluous!
135 // We support all necessary flags, from which these two flags are derived.
136 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
137 // GLOBAL = ALL + TASKS
139 // Add parent to list ... if any exist!
140 if( nSearchFlags & FrameSearchFlag::PARENT )
142 css::uno::Reference< XFrame > xParent = xOwner->getCreator();
143 if( xParent.is() )
145 impl_appendSequence( seqFrames, { xParent } );
149 // Add owner to list if SELF is searched.
150 if( nSearchFlags & FrameSearchFlag::SELF )
152 impl_appendSequence( seqFrames, { xOwner } );
155 // Add SIBLINGS to list.
156 if( nSearchFlags & FrameSearchFlag::SIBLINGS )
158 // Else; start a new search.
159 // Protect this instance against recursive calls from parents.
160 m_bRecursiveSearchProtection = true;
161 // Ask parent of my owner for frames and append results to return list.
162 css::uno::Reference< XFramesSupplier > xParent = xOwner->getCreator();
163 // If a parent exist ...
164 if ( xParent.is() )
166 // ... ask him for right frames.
167 impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
169 // We have all searched information.
170 // Reset protection-mode.
171 m_bRecursiveSearchProtection = false;
174 // If searched for children, step over all elements in container and collect the information.
175 if ( nSearchFlags & FrameSearchFlag::CHILDREN )
177 // Don't search for parents, siblings and self at children!
178 // These things are supported by this instance himself.
179 sal_Int32 const nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
180 // Step over all items of container and ask children for frames.
181 sal_uInt32 nCount = m_pFrameContainer->getCount();
182 for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
184 // We don't must control this conversion.
185 // We have done this at append()!
186 css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
187 impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
192 // Else; Do nothing! Our owner is dead.
193 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::queryFrames(): Our owner is dead - you can't query for frames ...!" );
195 // Return result of this operation.
196 return seqFrames;
199 // XIndexAccess
200 sal_Int32 SAL_CALL OFrames::getCount()
202 SolarMutexGuard g;
204 // Set default return value.
205 sal_Int32 nCount = 0;
207 // Do the follow only, if owner instance valid.
208 // Lock owner for follow operations - make a "hard reference"!
209 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
210 if ( xOwner.is() )
212 // Set CURRENT size of container for return.
213 nCount = m_pFrameContainer->getCount();
216 // Return result.
217 return nCount;
220 // XIndexAccess
222 Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex )
224 SolarMutexGuard g;
226 sal_uInt32 nCount = m_pFrameContainer->getCount();
227 if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
228 throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
229 static_cast<OWeakObject *>(this) );
231 // Set default return value.
232 Any aReturnValue;
234 // Do the follow only, if owner instance valid.
235 // Lock owner for follow operations - make a "hard reference"!
236 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
237 if ( xOwner.is() )
239 // Get element form container.
240 // (If index not valid, FrameContainer return NULL!)
241 aReturnValue <<= (*m_pFrameContainer)[nIndex];
244 // Return result of this operation.
245 return aReturnValue;
248 // XElementAccess
249 Type SAL_CALL OFrames::getElementType()
251 // This "container" support XFrame-interfaces only!
252 return cppu::UnoType<XFrame>::get();
255 // XElementAccess
256 sal_Bool SAL_CALL OFrames::hasElements()
258 SolarMutexGuard g;
260 // Set default return value.
261 bool bHasElements = false;
262 // Do the follow only, if owner instance valid.
263 // Lock owner for follow operations - make a "hard reference"!
264 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
265 if ( xOwner.is() )
267 // If some elements exist ...
268 if ( m_pFrameContainer->getCount() > 0 )
270 // ... change this state value!
271 bHasElements = true;
274 // Return result of this operation.
275 return bHasElements;
278 // protected method
280 void OFrames::impl_resetObject()
282 // Attention:
283 // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
284 // It exist two ways to call this method. From destructor and from disposing().
285 // I can't say, which one is the first. Normally the disposing-call - but other way...
287 // This instance can't work if the weakreference to owner is invalid!
288 // Destroy this to reset this object.
289 m_xOwner.clear();
290 // Reset pointer to shared container to!
291 m_pFrameContainer = nullptr;
294 void OFrames::impl_appendSequence( Sequence< css::uno::Reference< XFrame > >& seqDestination ,
295 const Sequence< css::uno::Reference< XFrame > >& seqSource )
297 // Get some information about the sequences.
298 sal_Int32 nSourceCount = seqSource.getLength();
299 sal_Int32 nDestinationCount = seqDestination.getLength();
300 const css::uno::Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
301 css::uno::Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
303 // Get memory for result list.
304 Sequence< css::uno::Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
305 css::uno::Reference< XFrame >* pResultAccess = seqResult.getArray();
306 sal_Int32 nResultPosition = 0;
308 // Copy all items from first sequence.
309 for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
311 pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
312 ++nResultPosition;
315 // Don't manipulate nResultPosition between these two loops!
316 // It's the current position in the result list.
318 // Copy all items from second sequence.
319 for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
321 pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
322 ++nResultPosition;
325 // Return result of this operation.
326 seqDestination.realloc( 0 );
327 seqDestination = seqResult;
330 // debug methods
332 /*-----------------------------------------------------------------------------------------------------------------
333 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
334 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
336 ATTENTION
338 If you miss a test for one of this parameters, contact the author or add it himself !(?)
339 But ... look for right testing! See using of this methods!
340 -----------------------------------------------------------------------------------------------------------------*/
342 // A search for frames must initiate with right flags.
343 // Some one are superfluous and not supported yet. But here we control only the range of incoming parameter!
344 bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
346 // Set default return value.
347 bool bOK = true;
348 // Check parameter.
349 if (
350 ( nSearchFlags != FrameSearchFlag::AUTO ) &&
351 ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
352 ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
353 ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
354 ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
355 ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
356 ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
357 ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
358 ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
361 bOK = false;
363 // Return result of check.
364 return bOK;
367 } // namespace framework
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */