InspectorHistory.cpp 4.3 KB

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