Avoid potential negative array index access to cached text.
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob122401f6b0689cebb721445567da3ff7bfeb0c1f
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 <sal/config.h>
22 #include <cassert>
23 #include <utility>
24 #include <vector>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/lang/EventObject.hpp>
28 #include <com/sun/star/uno/Any.hxx>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <com/sun/star/uno/Type.hxx>
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <com/sun/star/util/ChangesEvent.hpp>
34 #include <com/sun/star/util/ChangesSet.hpp>
35 #include <com/sun/star/util/ElementChange.hpp>
36 #include <com/sun/star/util/XChangesBatch.hpp>
37 #include <com/sun/star/util/XChangesListener.hpp>
38 #include <com/sun/star/util/XChangesNotifier.hpp>
39 #include <comphelper/sequence.hxx>
40 #include <cppu/unotype.hxx>
41 #include <cppuhelper/queryinterface.hxx>
42 #include <cppuhelper/weak.hxx>
43 #include <osl/mutex.hxx>
44 #include <rtl/ref.hxx>
45 #include <rtl/ustring.hxx>
47 #include "broadcaster.hxx"
48 #include "components.hxx"
49 #include "data.hxx"
50 #include "lock.hxx"
51 #include "modifications.hxx"
52 #include "node.hxx"
53 #include "rootaccess.hxx"
55 namespace configmgr {
57 RootAccess::RootAccess(
58 Components & components, OUString pathRepresentation,
59 OUString locale, bool update):
60 Access(components), pathRepresentation_(std::move(pathRepresentation)),
61 locale_(std::move(locale)),
62 lock_( lock() ),
63 update_(update), finalized_(false), alive_(true)
67 std::vector<OUString> RootAccess::getAbsolutePath() {
68 getNode();
69 return path_;
72 void RootAccess::initBroadcaster(
73 Modifications::Node const & modifications, Broadcaster * broadcaster)
75 assert(broadcaster != nullptr);
76 std::vector< css::util::ElementChange > changes;
77 initBroadcasterAndChanges(
78 modifications, broadcaster, changesListeners_.empty() ? nullptr : &changes);
79 if (changes.empty())
80 return;
82 css::util::ChangesSet set(comphelper::containerToSequence(changes));
83 for (auto const& changesListener : changesListeners_)
85 cppu::OWeakObject* pSource = this;
86 css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
87 broadcaster->addChangesNotification(
88 changesListener,
89 css::util::ChangesEvent(
90 pSource, css::uno::Any( xBase ), set), path_.empty());
94 void RootAccess::acquire() noexcept {
95 Access::acquire();
98 void RootAccess::release() noexcept {
99 Access::release();
102 OUString const & RootAccess::getAbsolutePathRepresentation() {
103 getNode(); // turn pathRepresentation_ into canonic form
104 return pathRepresentation_;
108 void RootAccess::setAlive(bool b) {
109 alive_ = b;
112 void RootAccess::addChangesListener(
113 css::uno::Reference< css::util::XChangesListener > const & aListener)
115 assert(thisIs(IS_ANY));
117 osl::MutexGuard g(*lock_);
118 checkLocalizedPropertyAccess();
119 if (!aListener.is()) {
120 throw css::uno::RuntimeException(
121 "null listener", getXWeak());
123 if (!isDisposed()) {
124 changesListeners_.insert(aListener);
125 return;
128 try {
129 aListener->disposing(
130 css::lang::EventObject(getXWeak()));
131 } catch (css::lang::DisposedException &) {}
134 void RootAccess::removeChangesListener(
135 css::uno::Reference< css::util::XChangesListener > const & aListener)
137 assert(thisIs(IS_ANY));
138 osl::MutexGuard g(*lock_);
139 checkLocalizedPropertyAccess();
140 ChangesListeners::iterator i(changesListeners_.find(aListener));
141 if (i != changesListeners_.end()) {
142 changesListeners_.erase(i);
146 void RootAccess::commitChanges()
148 assert(thisIs(IS_UPDATE));
149 if (!alive_)
151 return;
153 Broadcaster bc;
155 osl::MutexGuard g(*lock_);
157 checkLocalizedPropertyAccess();
158 int finalizedLayer;
159 Modifications globalMods;
160 commitChildChanges(
161 ((getComponents().resolvePathRepresentation(
162 pathRepresentation_, nullptr, nullptr, &finalizedLayer)
163 == node_) &&
164 finalizedLayer == Data::NO_LAYER),
165 &globalMods);
166 getComponents().writeModifications();
167 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
169 bc.send();
172 sal_Bool RootAccess::hasPendingChanges() {
173 assert(thisIs(IS_UPDATE));
174 osl::MutexGuard g(*lock_);
175 checkLocalizedPropertyAccess();
176 //TODO: Optimize:
177 std::vector< css::util::ElementChange > changes;
178 reportChildChanges(&changes);
179 return !changes.empty();
182 css::uno::Sequence< ::css::util::ElementChange > RootAccess::getPendingChanges()
184 assert(thisIs(IS_UPDATE));
185 osl::MutexGuard g(*lock_);
186 checkLocalizedPropertyAccess();
187 std::vector< css::util::ElementChange > changes;
188 reportChildChanges(&changes);
189 return comphelper::containerToSequence(changes);
192 RootAccess::~RootAccess()
194 osl::MutexGuard g(*lock_);
195 if (alive_)
196 getComponents().removeRootAccess(this);
199 std::vector<OUString> RootAccess::getRelativePath() {
200 return std::vector<OUString>();
203 OUString RootAccess::getRelativePathRepresentation() {
204 return OUString();
207 const rtl::Reference< Node > & RootAccess::getNode() {
208 if (!node_.is()) {
209 OUString canonic;
210 int finalizedLayer;
211 node_ = getComponents().resolvePathRepresentation(
212 pathRepresentation_, &canonic, &path_, &finalizedLayer);
213 if (!node_.is()) {
214 throw css::uno::RuntimeException(
215 "cannot find " + pathRepresentation_, nullptr);
216 // RootAccess::queryInterface indirectly calls
217 // RootAccess::getNode, so if this RootAccess were passed out in
218 // RuntimeException.Context, client code that called
219 // queryInterface on it would cause trouble; therefore,
220 // RuntimeException.Context is left null here
222 pathRepresentation_ = canonic;
223 assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
224 if (!path_.empty()) {
225 name_ = path_.back();
227 finalized_ = finalizedLayer != Data::NO_LAYER;
229 return node_;
232 bool RootAccess::isFinalized() {
233 getNode();
234 return finalized_;
237 const OUString & RootAccess::getNameInternal() {
238 getNode();
239 return name_;
242 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
243 return this;
246 rtl::Reference< Access > RootAccess::getParentAccess() {
247 return rtl::Reference< Access >();
250 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
251 assert(types != nullptr);
252 types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
253 types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
256 void RootAccess::addSupportedServiceNames(
257 std::vector<OUString> * services)
259 assert(services != nullptr);
260 services->push_back("com.sun.star.configuration.AccessRootElement");
261 if (update_) {
262 services->push_back("com.sun.star.configuration.UpdateRootElement");
266 void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
267 assert(broadcaster != nullptr);
268 for (auto const& changesListener : changesListeners_)
270 broadcaster->addDisposeNotification(
271 changesListener,
272 css::lang::EventObject(getXWeak()));
274 Access::initDisposeBroadcaster(broadcaster);
277 void RootAccess::clearListeners() noexcept {
278 changesListeners_.clear();
279 Access::clearListeners();
282 css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
284 assert(thisIs(IS_ANY));
285 osl::MutexGuard g(*lock_);
286 checkLocalizedPropertyAccess();
287 css::uno::Any res(Access::queryInterface(aType));
288 if (res.hasValue()) {
289 return res;
291 res = cppu::queryInterface(
292 aType, static_cast< css::util::XChangesNotifier * >(this));
293 if (res.hasValue()) {
294 return res;
296 if (!res.hasValue() && update_) {
297 res = cppu::queryInterface(
298 aType, static_cast< css::util::XChangesBatch * >(this));
300 return res;
303 OUString RootAccess::getImplementationName()
305 assert(thisIs(IS_ANY));
306 osl::MutexGuard g(*lock_);
307 checkLocalizedPropertyAccess();
308 return "configmgr.RootAccess";
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */