Avoid potential negative array index access to cached text.
[LibreOffice.git] / configmgr / source / childaccess.cxx
blobabf3795f1d55dd1ac2fc6ac6fc643099999a6233
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/container/XChild.hpp>
27 #include <com/sun/star/lang/NoSupportException.hpp>
28 #include <com/sun/star/lang/XUnoTunnel.hpp>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/uno/Type.hxx>
33 #include <com/sun/star/uno/XInterface.hpp>
34 #include <cppu/unotype.hxx>
35 #include <cppuhelper/queryinterface.hxx>
36 #include <cppuhelper/weak.hxx>
37 #include <comphelper/servicehelper.hxx>
38 #include <osl/mutex.hxx>
39 #include <rtl/ref.hxx>
40 #include <rtl/ustrbuf.hxx>
41 #include <rtl/ustring.hxx>
42 #include <sal/types.h>
44 #include "access.hxx"
45 #include "childaccess.hxx"
46 #include "components.hxx"
47 #include "data.hxx"
48 #include "localizedpropertynode.hxx"
49 #include "localizedvaluenode.hxx"
50 #include "lock.hxx"
51 #include "modifications.hxx"
52 #include "node.hxx"
53 #include "propertynode.hxx"
54 #include "rootaccess.hxx"
55 #include "type.hxx"
57 namespace configmgr {
59 ChildAccess::ChildAccess(
60 Components & components, rtl::Reference< RootAccess > const & root,
61 rtl::Reference< Access > const & parent, OUString name,
62 rtl::Reference< Node > const & node):
63 Access(components), root_(root), parent_(parent), name_(std::move(name)), node_(node),
64 inTransaction_(false),
65 lock_( lock() )
67 assert(root.is() && parent.is() && node.is());
70 ChildAccess::ChildAccess(
71 Components & components, rtl::Reference< RootAccess > const & root,
72 rtl::Reference< Node > const & node):
73 Access(components), root_(root), node_(node), inTransaction_(false),
74 lock_( lock() )
76 assert(root.is() && node.is());
79 std::vector<OUString> ChildAccess::getAbsolutePath() {
80 rtl::Reference< Access > parent(getParentAccess());
81 assert(parent.is());
82 std::vector<OUString> path(parent->getAbsolutePath());
83 path.push_back(name_);
84 return path;
87 std::vector<OUString> ChildAccess::getRelativePath() {
88 std::vector<OUString> path;
89 rtl::Reference< Access > parent(getParentAccess());
90 if (parent.is()) {
91 path = parent->getRelativePath();
93 path.push_back(name_);
94 return path;
97 OUString ChildAccess::getRelativePathRepresentation() {
98 OUStringBuffer path(128);
99 rtl::Reference< Access > parent(getParentAccess());
100 if (parent.is()) {
101 path.append(parent->getRelativePathRepresentation());
102 if (!path.isEmpty()) {
103 path.append('/');
106 path.append(Data::createSegment(node_->getTemplateName(), name_));
107 return path.makeStringAndClear();
110 const rtl::Reference< Node > & ChildAccess::getNode() {
111 return node_;
114 bool ChildAccess::isFinalized() {
115 return node_->getFinalized() != Data::NO_LAYER ||
116 (parent_.is() && parent_->isFinalized());
119 const OUString & ChildAccess::getNameInternal() {
120 return name_;
123 rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
124 return root_;
127 rtl::Reference< Access > ChildAccess::getParentAccess() {
128 return parent_;
131 void ChildAccess::acquire() noexcept {
132 Access::acquire();
135 void ChildAccess::release() noexcept {
136 Access::release();
139 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
141 assert(thisIs(IS_ANY));
142 osl::MutexGuard g(*lock_);
143 checkLocalizedPropertyAccess();
144 return cppu::getXWeak(parent_.get());
147 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
149 assert(thisIs(IS_ANY));
150 osl::MutexGuard g(*lock_);
151 checkLocalizedPropertyAccess();
152 throw css::lang::NoSupportException(
153 "setParent", getXWeak());
156 void ChildAccess::bind(
157 rtl::Reference< RootAccess > const & root,
158 rtl::Reference< Access > const & parent, OUString const & name)
159 noexcept
161 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
162 root_ = root;
163 parent_ = parent;
164 name_ = name;
167 void ChildAccess::unbind() noexcept {
168 assert(parent_.is());
169 parent_->releaseChild(name_);
170 parent_.clear();
171 inTransaction_ = true;
174 void ChildAccess::committed() {
175 inTransaction_ = false;
178 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
179 node_ = node;
182 void ChildAccess::setProperty(
183 css::uno::Any const & value, Modifications * localModifications)
185 assert(localModifications != nullptr);
186 Type type = TYPE_ERROR;
187 bool isNillable = false;
188 switch (node_->kind()) {
189 case Node::KIND_PROPERTY:
191 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
192 type = prop->getStaticType();
193 isNillable = prop->isNillable();
195 break;
196 case Node::KIND_LOCALIZED_PROPERTY:
198 OUString locale(getRootAccess()->getLocale());
199 if (!Components::allLocales(locale)) {
200 rtl::Reference< ChildAccess > child(getChild(locale));
201 if (child.is()) {
202 child->setProperty(value, localModifications);
203 } else {
204 insertLocalizedValueChild(
205 locale, value, localModifications);
207 return;
210 break;
211 case Node::KIND_LOCALIZED_VALUE:
213 LocalizedPropertyNode * locprop =
214 static_cast< LocalizedPropertyNode * >(getParentNode().get());
215 type = locprop->getStaticType();
216 isNillable = locprop->isNillable();
218 break;
219 default:
220 break;
222 checkValue(value, type, isNillable);
223 getParentAccess()->markChildAsModified(this);
224 changedValue_.emplace(value);
225 localModifications->add(getRelativePath());
229 css::uno::Any ChildAccess::asValue()
231 if (changedValue_)
233 return *changedValue_;
235 css::uno::Any value;
236 if (!asSimpleValue(node_, value, getComponents()))
238 if (node_->kind() == Node::KIND_LOCALIZED_PROPERTY)
240 OUString locale(getRootAccess()->getLocale());
241 if (!Components::allLocales(locale)) {
242 rtl::Reference< ChildAccess > child(getChild("*" + locale));
243 // As a last resort, return a nil value even though it may be
244 // illegal for the given property:
245 return child.is() ? child->asValue() : css::uno::Any();
248 value <<= css::uno::Reference(getXWeak());
250 return value;
253 /// Can we quickly extract a simple value into value ? if so returns true
254 bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
255 css::uno::Any &value,
256 Components &components)
258 switch (rNode->kind()) {
259 case Node::KIND_PROPERTY:
260 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
261 return true;
262 case Node::KIND_LOCALIZED_VALUE:
263 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
264 return true;
265 default:
266 return false;
270 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
272 assert(globalModifications != nullptr);
273 commitChildChanges(valid, globalModifications);
274 if (valid && changedValue_)
276 std::vector<OUString> path(getAbsolutePath());
277 getComponents().addModification(path);
278 globalModifications->add(path);
279 switch (node_->kind()) {
280 case Node::KIND_PROPERTY:
281 static_cast< PropertyNode * >(node_.get())->setValue(
282 Data::NO_LAYER, *changedValue_, true);
283 break;
284 case Node::KIND_LOCALIZED_VALUE:
285 static_cast< LocalizedValueNode * >(node_.get())->setValue(
286 Data::NO_LAYER, *changedValue_, true);
287 break;
288 default:
289 assert(false); // this cannot happen
290 break;
293 changedValue_.reset();
296 ChildAccess::~ChildAccess() {
297 osl::MutexGuard g(*lock_);
298 if (parent_.is()) {
299 parent_->releaseChild(name_);
303 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
304 assert(types != nullptr);
305 types->push_back(cppu::UnoType< css::container::XChild >::get());
306 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
309 void ChildAccess::addSupportedServiceNames(
310 std::vector<OUString> * services)
312 assert(services != nullptr);
313 services->push_back(
314 getParentNode()->kind() == Node::KIND_GROUP
315 ? OUString("com.sun.star.configuration.GroupElement")
316 : OUString("com.sun.star.configuration.SetElement"));
319 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
321 assert(thisIs(IS_ANY));
322 osl::MutexGuard g(*lock_);
323 checkLocalizedPropertyAccess();
324 css::uno::Any res(Access::queryInterface(aType));
325 return res.hasValue()
326 ? res
327 : cppu::queryInterface(
328 aType, static_cast< css::container::XChild * >(this));
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */