1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef SVGPathSegList_h
32#define SVGPathSegList_h
33
34#include "core/svg/SVGPathByteStream.h"
35#include "core/svg/SVGPathSeg.h"
36#include "core/svg/properties/SVGAnimatedProperty.h"
37#include "core/svg/properties/SVGListPropertyHelper.h"
38#include "wtf/WeakPtr.h"
39
40namespace WebCore {
41
42class SVGPathElement;
43class SVGPathSegListTearOff;
44
45class SVGPathSegList : public SVGListPropertyHelper<SVGPathSegList, SVGPathSeg> {
46public:
47    typedef void PrimitiveType;
48    typedef SVGPathSeg ItemPropertyType;
49    typedef SVGPathSegListTearOff TearOffType;
50    typedef SVGListPropertyHelper<SVGPathSegList, SVGPathSeg> Base;
51
52    static PassRefPtr<SVGPathSegList> create(SVGPathElement* contextElement, SVGPathSegRole role = PathSegUndefinedRole)
53    {
54        return adoptRef(new SVGPathSegList(contextElement, role));
55    }
56
57    virtual ~SVGPathSegList();
58
59    const SVGPathByteStream* byteStream() const;
60    void clearByteStream() { m_byteStream.clear(); }
61
62    // SVGListPropertyHelper methods with |m_byteStream| sync:
63
64    ItemPropertyType* at(size_t index)
65    {
66        updateListFromByteStream();
67        return Base::at(index);
68    }
69
70    size_t length()
71    {
72        updateListFromByteStream();
73        return Base::length();
74    }
75
76    bool isEmpty() const
77    {
78        if (m_listSyncedToByteStream)
79            return Base::isEmpty();
80
81        return !m_byteStream || m_byteStream->isEmpty();
82    }
83
84    void clear()
85    {
86        clearByteStream();
87        Base::clear();
88    }
89
90    void append(PassRefPtr<ItemPropertyType> passNewItem)
91    {
92        updateListFromByteStream();
93        clearByteStream();
94        Base::append(passNewItem);
95    }
96
97    PassRefPtr<ItemPropertyType> initialize(PassRefPtr<ItemPropertyType> passItem)
98    {
99        clearByteStream();
100        return Base::initialize(passItem);
101    }
102
103    PassRefPtr<ItemPropertyType> getItem(size_t index, ExceptionState& exceptionState)
104    {
105        updateListFromByteStream();
106        return Base::getItem(index, exceptionState);
107    }
108
109    PassRefPtr<ItemPropertyType> insertItemBefore(PassRefPtr<ItemPropertyType> passItem, size_t index)
110    {
111        updateListFromByteStream();
112        clearByteStream();
113        return Base::insertItemBefore(passItem, index);
114    }
115
116    PassRefPtr<ItemPropertyType> replaceItem(PassRefPtr<ItemPropertyType> passItem, size_t index, ExceptionState& exceptionState)
117    {
118        updateListFromByteStream();
119        clearByteStream();
120        return Base::replaceItem(passItem, index, exceptionState);
121    }
122
123    PassRefPtr<ItemPropertyType> removeItem(size_t index, ExceptionState& exceptionState)
124    {
125        updateListFromByteStream();
126        clearByteStream();
127        return Base::removeItem(index, exceptionState);
128    }
129
130    PassRefPtr<ItemPropertyType> appendItem(PassRefPtr<ItemPropertyType> passItem);
131
132    // SVGPropertyBase:
133    PassRefPtr<SVGPathSegList> clone();
134    virtual PassRefPtr<SVGPropertyBase> cloneForAnimation(const String&) const OVERRIDE;
135    virtual String valueAsString() const OVERRIDE;
136    void setValueAsString(const String&, ExceptionState&);
137
138    virtual void add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*) OVERRIDE;
139    virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*) OVERRIDE;
140    virtual float calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*) OVERRIDE;
141
142    static AnimatedPropertyType classType() { return AnimatedPath; }
143
144private:
145    SVGPathSegList(SVGPathElement*, SVGPathSegRole);
146    SVGPathSegList(SVGPathElement*, SVGPathSegRole, PassOwnPtr<SVGPathByteStream>);
147
148    friend class SVGPathSegListBuilder;
149    // This is only to be called from SVGPathSegListBuilder.
150    void appendWithoutByteStreamSync(PassRefPtr<ItemPropertyType> passNewItem)
151    {
152        Base::append(passNewItem);
153    }
154
155    void updateListFromByteStream();
156    void invalidateList();
157
158    // FIXME: This pointer should be removed after SVGPathSeg has a tear-off.
159    // FIXME: oilpan: This is raw-ptr to avoid reference cycles.
160    //        SVGPathSegList is either owned by SVGAnimatedPath or
161    //        SVGPathSegListTearOff. Both keep |contextElement| alive,
162    //        so this ptr is always valid.
163    SVGPathElement* m_contextElement;
164
165    SVGPathSegRole m_role;
166    mutable OwnPtr<SVGPathByteStream> m_byteStream;
167    bool m_listSyncedToByteStream;
168};
169
170inline PassRefPtr<SVGPathSegList> toSVGPathSegList(PassRefPtr<SVGPropertyBase> passBase)
171{
172    RefPtr<SVGPropertyBase> base = passBase;
173    ASSERT(base->type() == SVGPathSegList::classType());
174    return static_pointer_cast<SVGPathSegList>(base.release());
175}
176
177} // namespace WebCore
178
179#endif
180