[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sdefresne | c083d1f | 2015-04-17 21:12:18 | [diff] [blame] | 5 | #include "components/undo/undo_manager.h" |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 6 | |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
| 10 | #include "base/logging.h" |
sdefresne | c083d1f | 2015-04-17 21:12:18 | [diff] [blame] | 11 | #include "components/undo/undo_manager_observer.h" |
| 12 | #include "components/undo/undo_operation.h" |
sdefresne | 5ec59f44 | 2015-04-17 07:42:24 | [diff] [blame] | 13 | #include "grit/components_strings.h" |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 14 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 15 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // Maximum number of changes that can be undone. |
| 19 | const size_t kMaxUndoGroups = 100; |
| 20 | |
| 21 | } // namespace |
| 22 | |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 23 | // UndoGroup ------------------------------------------------------------------ |
| 24 | |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 25 | UndoGroup::UndoGroup() |
| 26 | : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), |
| 27 | redo_label_id_(IDS_BOOKMARK_BAR_REDO) { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | UndoGroup::~UndoGroup() { |
| 31 | } |
| 32 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 33 | void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 34 | if (operations_.empty()) { |
| 35 | set_undo_label_id(operation->GetUndoLabelId()); |
| 36 | set_redo_label_id(operation->GetRedoLabelId()); |
| 37 | } |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 38 | operations_.push_back(operation.release()); |
| 39 | } |
| 40 | |
| 41 | void UndoGroup::Undo() { |
| 42 | for (ScopedVector<UndoOperation>::reverse_iterator ri = operations_.rbegin(); |
| 43 | ri != operations_.rend(); ++ri) { |
| 44 | (*ri)->Undo(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // UndoManager ---------------------------------------------------------------- |
| 49 | |
| 50 | UndoManager::UndoManager() |
| 51 | : group_actions_count_(0), |
[email protected] | cb8f79e | 2013-12-18 20:08:42 | [diff] [blame] | 52 | undo_in_progress_action_(NULL), |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 53 | undo_suspended_count_(0), |
| 54 | performing_undo_(false), |
| 55 | performing_redo_(false) { |
| 56 | } |
| 57 | |
| 58 | UndoManager::~UndoManager() { |
| 59 | DCHECK_EQ(0, group_actions_count_); |
| 60 | DCHECK_EQ(0, undo_suspended_count_); |
| 61 | DCHECK(!performing_undo_); |
| 62 | DCHECK(!performing_redo_); |
| 63 | } |
| 64 | |
| 65 | void UndoManager::Undo() { |
| 66 | Undo(&performing_undo_, &undo_actions_); |
| 67 | } |
| 68 | |
| 69 | void UndoManager::Redo() { |
| 70 | Undo(&performing_redo_, &redo_actions_); |
| 71 | } |
| 72 | |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 73 | base::string16 UndoManager::GetUndoLabel() const { |
| 74 | return l10n_util::GetStringUTF16( |
| 75 | undo_actions_.empty() ? IDS_BOOKMARK_BAR_UNDO |
| 76 | : undo_actions_.back()->get_undo_label_id()); |
| 77 | } |
| 78 | |
| 79 | base::string16 UndoManager::GetRedoLabel() const { |
| 80 | return l10n_util::GetStringUTF16( |
| 81 | redo_actions_.empty() ? IDS_BOOKMARK_BAR_REDO |
| 82 | : redo_actions_.back()->get_redo_label_id()); |
| 83 | } |
| 84 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 85 | void UndoManager::AddUndoOperation(std::unique_ptr<UndoOperation> operation) { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 86 | if (IsUndoTrakingSuspended()) { |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 87 | RemoveAllOperations(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 88 | operation.reset(); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (group_actions_count_) { |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 93 | pending_grouped_action_->AddOperation(std::move(operation)); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 94 | } else { |
| 95 | UndoGroup* new_action = new UndoGroup(); |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 96 | new_action->AddOperation(std::move(operation)); |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 97 | AddUndoGroup(new_action); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | void UndoManager::StartGroupingActions() { |
| 102 | if (!group_actions_count_) |
| 103 | pending_grouped_action_.reset(new UndoGroup()); |
| 104 | ++group_actions_count_; |
| 105 | } |
| 106 | |
| 107 | void UndoManager::EndGroupingActions() { |
| 108 | --group_actions_count_; |
| 109 | if (group_actions_count_ > 0) |
| 110 | return; |
| 111 | |
| 112 | // Check that StartGroupingActions and EndGroupingActions are paired. |
| 113 | DCHECK_GE(group_actions_count_, 0); |
| 114 | |
| 115 | bool is_user_action = !performing_undo_ && !performing_redo_; |
[email protected] | a175b9d | 2013-07-31 17:44:21 | [diff] [blame] | 116 | if (!pending_grouped_action_->undo_operations().empty()) { |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 117 | AddUndoGroup(pending_grouped_action_.release()); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 118 | } else { |
| 119 | // No changes were executed since we started grouping actions, so the |
| 120 | // pending UndoGroup should be discarded. |
| 121 | pending_grouped_action_.reset(); |
| 122 | |
| 123 | // This situation is only expected when it is a user initiated action. |
| 124 | // Undo/Redo should have at least one operation performed. |
| 125 | DCHECK(is_user_action); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void UndoManager::SuspendUndoTracking() { |
| 130 | ++undo_suspended_count_; |
| 131 | } |
| 132 | |
| 133 | void UndoManager::ResumeUndoTracking() { |
| 134 | DCHECK_GT(undo_suspended_count_, 0); |
| 135 | --undo_suspended_count_; |
| 136 | } |
| 137 | |
| 138 | bool UndoManager::IsUndoTrakingSuspended() const { |
| 139 | return undo_suspended_count_ > 0; |
| 140 | } |
| 141 | |
[email protected] | a175b9d | 2013-07-31 17:44:21 | [diff] [blame] | 142 | std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { |
| 143 | std::vector<UndoOperation*> result; |
| 144 | for (size_t i = 0; i < undo_actions_.size(); ++i) { |
| 145 | const std::vector<UndoOperation*>& operations = |
| 146 | undo_actions_[i]->undo_operations(); |
| 147 | result.insert(result.end(), operations.begin(), operations.end()); |
| 148 | } |
| 149 | for (size_t i = 0; i < redo_actions_.size(); ++i) { |
| 150 | const std::vector<UndoOperation*>& operations = |
| 151 | redo_actions_[i]->undo_operations(); |
| 152 | result.insert(result.end(), operations.begin(), operations.end()); |
| 153 | } |
[email protected] | cb8f79e | 2013-12-18 20:08:42 | [diff] [blame] | 154 | // Ensure that if an Undo is in progress the UndoOperations part of that |
| 155 | // UndoGroup are included in the returned set. This will ensure that any |
| 156 | // changes (such as renumbering) will be applied to any potentially |
| 157 | // unprocessed UndoOperations. |
| 158 | if (undo_in_progress_action_) { |
| 159 | const std::vector<UndoOperation*>& operations = |
| 160 | undo_in_progress_action_->undo_operations(); |
| 161 | result.insert(result.end(), operations.begin(), operations.end()); |
| 162 | } |
[email protected] | a175b9d | 2013-07-31 17:44:21 | [diff] [blame] | 163 | |
| 164 | return result; |
| 165 | } |
| 166 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 167 | void UndoManager::RemoveAllOperations() { |
| 168 | DCHECK(!group_actions_count_); |
| 169 | undo_actions_.clear(); |
| 170 | redo_actions_.clear(); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 171 | |
| 172 | NotifyOnUndoManagerStateChange(); |
| 173 | } |
| 174 | |
| 175 | void UndoManager::AddObserver(UndoManagerObserver* observer) { |
| 176 | observers_.AddObserver(observer); |
| 177 | } |
| 178 | |
| 179 | void UndoManager::RemoveObserver(UndoManagerObserver* observer) { |
| 180 | observers_.RemoveObserver(observer); |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 183 | void UndoManager::Undo(bool* performing_indicator, |
| 184 | ScopedVector<UndoGroup>* active_undo_group) { |
| 185 | // Check that action grouping has been correctly ended. |
| 186 | DCHECK(!group_actions_count_); |
| 187 | |
| 188 | if (active_undo_group->empty()) |
| 189 | return; |
| 190 | |
| 191 | base::AutoReset<bool> incoming_changes(performing_indicator, true); |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 192 | std::unique_ptr<UndoGroup> action(active_undo_group->back()); |
[email protected] | cb8f79e | 2013-12-18 20:08:42 | [diff] [blame] | 193 | base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, |
| 194 | action.get()); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 195 | active_undo_group->weak_erase( |
| 196 | active_undo_group->begin() + active_undo_group->size() - 1); |
| 197 | |
| 198 | StartGroupingActions(); |
| 199 | action->Undo(); |
| 200 | EndGroupingActions(); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 201 | |
| 202 | NotifyOnUndoManagerStateChange(); |
| 203 | } |
| 204 | |
| 205 | void UndoManager::NotifyOnUndoManagerStateChange() { |
ericwilligers | ff2af33 | 2016-10-19 00:16:50 | [diff] [blame^] | 206 | for (auto& observer : observers_) |
| 207 | observer.OnUndoManagerStateChange(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 210 | void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { |
| 211 | GetActiveUndoGroup()->push_back(new_undo_group); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 212 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 213 | // User actions invalidate any available redo actions. |
| 214 | if (is_user_action()) |
| 215 | redo_actions_.clear(); |
| 216 | |
| 217 | // Limit the number of undo levels so the undo stack does not grow unbounded. |
| 218 | if (GetActiveUndoGroup()->size() > kMaxUndoGroups) |
| 219 | GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 220 | |
| 221 | NotifyOnUndoManagerStateChange(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { |
| 225 | return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 226 | } |