1/*
2 * Copyright (C) 2012 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#include "config.h"
32
33#if ENABLE(INSPECTOR)
34
35#include "InspectorHistory.h"
36
37#include "ExceptionCodePlaceholder.h"
38#include "Node.h"
39
40namespace WebCore {
41
42namespace {
43
44class UndoableStateMark : public InspectorHistory::Action {
45public:
46    UndoableStateMark() : InspectorHistory::Action("[UndoableState]") { }
47
48    virtual bool perform(ExceptionCode&) { return true; }
49
50    virtual bool undo(ExceptionCode&) { return true; }
51
52    virtual bool redo(ExceptionCode&) { return true; }
53
54    virtual bool isUndoableStateMark() { return true; }
55};
56
57}
58
59InspectorHistory::Action::Action(const String& name) : m_name(name)
60{
61}
62
63InspectorHistory::Action::~Action()
64{
65}
66
67String InspectorHistory::Action::toString()
68{
69    return m_name;
70}
71
72bool InspectorHistory::Action::isUndoableStateMark()
73{
74    return false;
75}
76
77String InspectorHistory::Action::mergeId()
78{
79    return "";
80}
81
82void InspectorHistory::Action::merge(PassOwnPtr<Action>)
83{
84}
85
86InspectorHistory::InspectorHistory() : m_afterLastActionIndex(0) { }
87
88InspectorHistory::~InspectorHistory() { }
89
90bool InspectorHistory::perform(PassOwnPtr<Action> action, ExceptionCode& ec)
91{
92    if (!action->perform(ec))
93        return false;
94
95    if (!action->mergeId().isEmpty() && m_afterLastActionIndex > 0 && action->mergeId() == m_history[m_afterLastActionIndex - 1]->mergeId())
96        m_history[m_afterLastActionIndex - 1]->merge(action);
97    else {
98        m_history.resize(m_afterLastActionIndex);
99        m_history.append(action);
100        ++m_afterLastActionIndex;
101    }
102    return true;
103}
104
105void InspectorHistory::markUndoableState()
106{
107    perform(adoptPtr(new UndoableStateMark()), IGNORE_EXCEPTION);
108}
109
110bool InspectorHistory::undo(ExceptionCode& ec)
111{
112    while (m_afterLastActionIndex > 0 && m_history[m_afterLastActionIndex - 1]->isUndoableStateMark())
113        --m_afterLastActionIndex;
114
115    while (m_afterLastActionIndex > 0) {
116        Action* action = m_history[m_afterLastActionIndex - 1].get();
117        if (!action->undo(ec)) {
118            reset();
119            return false;
120        }
121        --m_afterLastActionIndex;
122        if (action->isUndoableStateMark())
123            break;
124    }
125
126    return true;
127}
128
129bool InspectorHistory::redo(ExceptionCode& ec)
130{
131    while (m_afterLastActionIndex < m_history.size() && m_history[m_afterLastActionIndex]->isUndoableStateMark())
132        ++m_afterLastActionIndex;
133
134    while (m_afterLastActionIndex < m_history.size()) {
135        Action* action = m_history[m_afterLastActionIndex].get();
136        if (!action->redo(ec)) {
137            reset();
138            return false;
139        }
140        ++m_afterLastActionIndex;
141        if (action->isUndoableStateMark())
142            break;
143    }
144    return true;
145}
146
147void InspectorHistory::reset()
148{
149    m_afterLastActionIndex = 0;
150    m_history.clear();
151}
152
153} // namespace WebCore
154
155#endif // ENABLE(INSPECTOR)
156