Bug 1270832 - Activate standard c++ library hardening r=glandium
[gecko.git] / dom / svg / SVGAnimatedPathSegList.cpp
blobbb617a930193888fc51ab2facd938aa04cd3ab10
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SVGAnimatedPathSegList.h"
9 #include <utility>
11 #include "SVGPathSegListSMILType.h"
12 #include "mozilla/SMILValue.h"
13 #include "mozilla/StaticPrefs_dom.h"
14 #include "mozilla/dom/SVGElement.h"
16 using namespace mozilla::dom;
18 // See the comments in this file's header!
20 namespace mozilla {
22 nsresult SVGAnimatedPathSegList::SetBaseValueString(const nsAString& aValue) {
23 // We don't need to call DidChange* here - we're only called by
24 // SVGElement::ParseAttribute under Element::SetAttr,
25 // which takes care of notifying.
26 return mBaseVal.SetValueFromString(NS_ConvertUTF16toUTF8(aValue));
29 void SVGAnimatedPathSegList::ClearBaseValue() {
30 mBaseVal.Clear();
31 // Caller notifies
34 nsresult SVGAnimatedPathSegList::SetAnimValue(const SVGPathData& aNewAnimValue,
35 SVGElement* aElement) {
36 // Note that a new animation may totally change the number of items in the
37 // animVal list, either replacing what was essentially a mirror of the
38 // baseVal list, or else replacing and overriding an existing animation.
39 // Unfortunately it is not possible for us to reliably distinguish between
40 // calls to this method that are setting a new sample for an existing
41 // animation, and calls that are setting the first sample of an animation
42 // that will override an existing animation.
44 if (!mAnimVal) {
45 mAnimVal = MakeUnique<SVGPathData>();
47 *mAnimVal = aNewAnimValue;
48 aElement->DidAnimatePathSegList();
49 return NS_OK;
52 void SVGAnimatedPathSegList::ClearAnimValue(SVGElement* aElement) {
53 mAnimVal = nullptr;
54 aElement->DidAnimatePathSegList();
57 bool SVGAnimatedPathSegList::IsRendered() const {
58 return mAnimVal ? !mAnimVal->IsEmpty() : !mBaseVal.IsEmpty();
61 UniquePtr<SMILAttr> SVGAnimatedPathSegList::ToSMILAttr(SVGElement* aElement) {
62 return MakeUnique<SMILAnimatedPathSegList>(this, aElement);
65 nsresult SVGAnimatedPathSegList::SMILAnimatedPathSegList::ValueFromString(
66 const nsAString& aStr, const dom::SVGAnimationElement* /*aSrcElement*/,
67 SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
68 SMILValue val(SVGPathSegListSMILType::Singleton());
69 SVGPathDataAndInfo* list = static_cast<SVGPathDataAndInfo*>(val.mU.mPtr);
70 nsresult rv = list->SetValueFromString(NS_ConvertUTF16toUTF8(aStr));
71 if (NS_SUCCEEDED(rv)) {
72 list->SetElement(mElement);
73 aValue = std::move(val);
75 return rv;
78 SMILValue SVGAnimatedPathSegList::SMILAnimatedPathSegList::GetBaseValue()
79 const {
80 // To benefit from Return Value Optimization and avoid copy constructor calls
81 // due to our use of return-by-value, we must return the exact same object
82 // from ALL return points. This function must only return THIS variable:
83 SMILValue tmp(SVGPathSegListSMILType::Singleton());
84 auto* list = static_cast<SVGPathDataAndInfo*>(tmp.mU.mPtr);
85 list->CopyFrom(mVal->mBaseVal);
86 list->SetElement(mElement);
87 return tmp;
90 nsresult SVGAnimatedPathSegList::SMILAnimatedPathSegList::SetAnimValue(
91 const SMILValue& aValue) {
92 NS_ASSERTION(aValue.mType == SVGPathSegListSMILType::Singleton(),
93 "Unexpected type to assign animated value");
94 if (aValue.mType == SVGPathSegListSMILType::Singleton()) {
95 mVal->SetAnimValue(*static_cast<SVGPathDataAndInfo*>(aValue.mU.mPtr),
96 mElement);
98 return NS_OK;
101 void SVGAnimatedPathSegList::SMILAnimatedPathSegList::ClearAnimValue() {
102 if (mVal->mAnimVal) {
103 mVal->ClearAnimValue(mElement);
107 size_t SVGAnimatedPathSegList::SizeOfExcludingThis(
108 MallocSizeOf aMallocSizeOf) const {
109 size_t total = mBaseVal.SizeOfExcludingThis(aMallocSizeOf);
110 if (mAnimVal) {
111 mAnimVal->SizeOfIncludingThis(aMallocSizeOf);
113 return total;
116 } // namespace mozilla