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 | #include "InspectorHistory.h" |
33 | |
34 | #include "Node.h" |
35 | |
36 | namespace WebCore { |
37 | |
38 | class UndoableStateMark : public InspectorHistory::Action { |
39 | private: |
40 | ExceptionOr<void> perform() final { return { }; } |
41 | ExceptionOr<void> undo() final { return { }; } |
42 | ExceptionOr<void> redo() final { return { }; } |
43 | bool isUndoableStateMark() final { return true; } |
44 | }; |
45 | |
46 | ExceptionOr<void> InspectorHistory::perform(std::unique_ptr<Action> action) |
47 | { |
48 | auto performResult = action->perform(); |
49 | if (performResult.hasException()) |
50 | return performResult.releaseException(); |
51 | |
52 | if (!action->mergeId().isEmpty() && m_afterLastActionIndex > 0 && action->mergeId() == m_history[m_afterLastActionIndex - 1]->mergeId()) |
53 | m_history[m_afterLastActionIndex - 1]->merge(WTFMove(action)); |
54 | else { |
55 | m_history.resize(m_afterLastActionIndex); |
56 | m_history.append(WTFMove(action)); |
57 | ++m_afterLastActionIndex; |
58 | } |
59 | return { }; |
60 | } |
61 | |
62 | void InspectorHistory::markUndoableState() |
63 | { |
64 | perform(std::make_unique<UndoableStateMark>()); |
65 | } |
66 | |
67 | ExceptionOr<void> InspectorHistory::undo() |
68 | { |
69 | while (m_afterLastActionIndex > 0 && m_history[m_afterLastActionIndex - 1]->isUndoableStateMark()) |
70 | --m_afterLastActionIndex; |
71 | |
72 | while (m_afterLastActionIndex > 0) { |
73 | Action* action = m_history[m_afterLastActionIndex - 1].get(); |
74 | auto undoResult = action->undo(); |
75 | if (undoResult.hasException()) { |
76 | reset(); |
77 | return undoResult.releaseException(); |
78 | } |
79 | --m_afterLastActionIndex; |
80 | if (action->isUndoableStateMark()) |
81 | break; |
82 | } |
83 | |
84 | return { }; |
85 | } |
86 | |
87 | ExceptionOr<void> InspectorHistory::redo() |
88 | { |
89 | while (m_afterLastActionIndex < m_history.size() && m_history[m_afterLastActionIndex]->isUndoableStateMark()) |
90 | ++m_afterLastActionIndex; |
91 | |
92 | while (m_afterLastActionIndex < m_history.size()) { |
93 | Action* action = m_history[m_afterLastActionIndex].get(); |
94 | auto redoResult = action->redo(); |
95 | if (redoResult.hasException()) { |
96 | reset(); |
97 | return redoResult.releaseException(); |
98 | } |
99 | ++m_afterLastActionIndex; |
100 | if (action->isUndoableStateMark()) |
101 | break; |
102 | } |
103 | return { }; |
104 | } |
105 | |
106 | void InspectorHistory::reset() |
107 | { |
108 | m_afterLastActionIndex = 0; |
109 | m_history.clear(); |
110 | } |
111 | |
112 | } // namespace WebCore |
113 | |