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 "core/inspector/InspectorHistory.h" 33 34#include "bindings/core/v8/ExceptionState.h" 35#include "bindings/core/v8/ExceptionStatePlaceholder.h" 36#include "core/dom/Node.h" 37 38namespace blink { 39 40namespace { 41 42class UndoableStateMark FINAL : public InspectorHistory::Action { 43public: 44 UndoableStateMark() : InspectorHistory::Action("[UndoableState]") { } 45 46 virtual bool perform(ExceptionState&) OVERRIDE { return true; } 47 48 virtual bool undo(ExceptionState&) OVERRIDE { return true; } 49 50 virtual bool redo(ExceptionState&) OVERRIDE { return true; } 51 52 virtual bool isUndoableStateMark() OVERRIDE { return true; } 53}; 54 55} 56 57InspectorHistory::Action::Action(const String& name) : m_name(name) 58{ 59} 60 61InspectorHistory::Action::~Action() 62{ 63} 64 65void InspectorHistory::Action::trace(Visitor* visitor) 66{ 67} 68 69String InspectorHistory::Action::toString() 70{ 71 return m_name; 72} 73 74bool InspectorHistory::Action::isUndoableStateMark() 75{ 76 return false; 77} 78 79String InspectorHistory::Action::mergeId() 80{ 81 return ""; 82} 83 84void InspectorHistory::Action::merge(PassRefPtrWillBeRawPtr<Action>) 85{ 86} 87 88InspectorHistory::InspectorHistory() : m_afterLastActionIndex(0) { } 89 90bool InspectorHistory::perform(PassRefPtrWillBeRawPtr<Action> action, ExceptionState& exceptionState) 91{ 92 if (!action->perform(exceptionState)) 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(adoptRefWillBeNoop(new UndoableStateMark()), IGNORE_EXCEPTION); 108} 109 110bool InspectorHistory::undo(ExceptionState& exceptionState) 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(exceptionState)) { 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(ExceptionState& exceptionState) 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(exceptionState)) { 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 153void InspectorHistory::trace(Visitor* visitor) 154{ 155 visitor->trace(m_history); 156} 157 158} // namespace blink 159 160