InspectorHistory.cpp [plain text]
#include "config.h"
#if ENABLE(INSPECTOR)
#include "InspectorHistory.h"
#include "ExceptionCodePlaceholder.h"
#include "Node.h"
namespace WebCore {
namespace {
class UndoableStateMark : public InspectorHistory::Action {
public:
UndoableStateMark() : InspectorHistory::Action("[UndoableState]") { }
virtual bool perform(ExceptionCode&) override { return true; }
virtual bool undo(ExceptionCode&) override { return true; }
virtual bool redo(ExceptionCode&) override { return true; }
virtual bool isUndoableStateMark() override { return true; }
};
}
InspectorHistory::Action::Action(const String& name) : m_name(name)
{
}
InspectorHistory::Action::~Action()
{
}
String InspectorHistory::Action::toString()
{
return m_name;
}
bool InspectorHistory::Action::isUndoableStateMark()
{
return false;
}
String InspectorHistory::Action::mergeId()
{
return "";
}
void InspectorHistory::Action::merge(std::unique_ptr<Action>)
{
}
InspectorHistory::InspectorHistory() : m_afterLastActionIndex(0) { }
InspectorHistory::~InspectorHistory() { }
bool InspectorHistory::perform(std::unique_ptr<Action> action, ExceptionCode& ec)
{
if (!action->perform(ec))
return false;
if (!action->mergeId().isEmpty() && m_afterLastActionIndex > 0 && action->mergeId() == m_history[m_afterLastActionIndex - 1]->mergeId())
m_history[m_afterLastActionIndex - 1]->merge(WTF::move(action));
else {
m_history.resize(m_afterLastActionIndex);
m_history.append(WTF::move(action));
++m_afterLastActionIndex;
}
return true;
}
void InspectorHistory::markUndoableState()
{
perform(std::make_unique<UndoableStateMark>(), IGNORE_EXCEPTION);
}
bool InspectorHistory::undo(ExceptionCode& ec)
{
while (m_afterLastActionIndex > 0 && m_history[m_afterLastActionIndex - 1]->isUndoableStateMark())
--m_afterLastActionIndex;
while (m_afterLastActionIndex > 0) {
Action* action = m_history[m_afterLastActionIndex - 1].get();
if (!action->undo(ec)) {
reset();
return false;
}
--m_afterLastActionIndex;
if (action->isUndoableStateMark())
break;
}
return true;
}
bool InspectorHistory::redo(ExceptionCode& ec)
{
while (m_afterLastActionIndex < m_history.size() && m_history[m_afterLastActionIndex]->isUndoableStateMark())
++m_afterLastActionIndex;
while (m_afterLastActionIndex < m_history.size()) {
Action* action = m_history[m_afterLastActionIndex].get();
if (!action->redo(ec)) {
reset();
return false;
}
++m_afterLastActionIndex;
if (action->isUndoableStateMark())
break;
}
return true;
}
void InspectorHistory::reset()
{
m_afterLastActionIndex = 0;
m_history.clear();
}
}
#endif // ENABLE(INSPECTOR)