[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" |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
thakis | fe8fa0a | 2017-02-23 19:46:36 | [diff] [blame] | 12 | #include "components/strings/grit/components_strings.h" |
sdefresne | c083d1f | 2015-04-17 21:12:18 | [diff] [blame] | 13 | #include "components/undo/undo_manager_observer.h" |
| 14 | #include "components/undo/undo_operation.h" |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 15 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 16 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | // Maximum number of changes that can be undone. |
| 20 | const size_t kMaxUndoGroups = 100; |
| 21 | |
| 22 | } // namespace |
| 23 | |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 24 | // UndoGroup ------------------------------------------------------------------ |
| 25 | |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 26 | UndoGroup::UndoGroup() |
| 27 | : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), |
| 28 | redo_label_id_(IDS_BOOKMARK_BAR_REDO) { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | UndoGroup::~UndoGroup() { |
| 32 | } |
| 33 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 34 | void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 35 | if (operations_.empty()) { |
| 36 | set_undo_label_id(operation->GetUndoLabelId()); |
| 37 | set_redo_label_id(operation->GetRedoLabelId()); |
| 38 | } |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 39 | operations_.push_back(std::move(operation)); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void UndoGroup::Undo() { |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 43 | for (auto ri = operations_.rbegin(); ri != operations_.rend(); ++ri) |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 44 | (*ri)->Undo(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // UndoManager ---------------------------------------------------------------- |
| 48 | |
| 49 | UndoManager::UndoManager() |
| 50 | : group_actions_count_(0), |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 51 | undo_in_progress_action_(nullptr), |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 52 | undo_suspended_count_(0), |
| 53 | performing_undo_(false), |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 54 | performing_redo_(false) {} |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 55 | |
| 56 | UndoManager::~UndoManager() { |
| 57 | DCHECK_EQ(0, group_actions_count_); |
| 58 | DCHECK_EQ(0, undo_suspended_count_); |
| 59 | DCHECK(!performing_undo_); |
| 60 | DCHECK(!performing_redo_); |
| 61 | } |
| 62 | |
| 63 | void UndoManager::Undo() { |
| 64 | Undo(&performing_undo_, &undo_actions_); |
| 65 | } |
| 66 | |
| 67 | void UndoManager::Redo() { |
| 68 | Undo(&performing_redo_, &redo_actions_); |
| 69 | } |
| 70 | |
[email protected] | 7d63a26 | 2013-12-17 17:15:55 | [diff] [blame] | 71 | base::string16 UndoManager::GetUndoLabel() const { |
| 72 | return l10n_util::GetStringUTF16( |
| 73 | undo_actions_.empty() ? IDS_BOOKMARK_BAR_UNDO |
| 74 | : undo_actions_.back()->get_undo_label_id()); |
| 75 | } |
| 76 | |
| 77 | base::string16 UndoManager::GetRedoLabel() const { |
| 78 | return l10n_util::GetStringUTF16( |
| 79 | redo_actions_.empty() ? IDS_BOOKMARK_BAR_REDO |
| 80 | : redo_actions_.back()->get_redo_label_id()); |
| 81 | } |
| 82 | |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 83 | void UndoManager::AddUndoOperation(std::unique_ptr<UndoOperation> operation) { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 84 | if (IsUndoTrakingSuspended()) { |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 85 | RemoveAllOperations(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 86 | operation.reset(); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (group_actions_count_) { |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 91 | pending_grouped_action_->AddOperation(std::move(operation)); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 92 | } else { |
| 93 | UndoGroup* new_action = new UndoGroup(); |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 94 | new_action->AddOperation(std::move(operation)); |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 95 | AddUndoGroup(new_action); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | void UndoManager::StartGroupingActions() { |
| 100 | if (!group_actions_count_) |
| 101 | pending_grouped_action_.reset(new UndoGroup()); |
| 102 | ++group_actions_count_; |
| 103 | } |
| 104 | |
| 105 | void UndoManager::EndGroupingActions() { |
| 106 | --group_actions_count_; |
| 107 | if (group_actions_count_ > 0) |
| 108 | return; |
| 109 | |
| 110 | // Check that StartGroupingActions and EndGroupingActions are paired. |
| 111 | DCHECK_GE(group_actions_count_, 0); |
| 112 | |
| 113 | bool is_user_action = !performing_undo_ && !performing_redo_; |
[email protected] | a175b9d | 2013-07-31 17:44:21 | [diff] [blame] | 114 | if (!pending_grouped_action_->undo_operations().empty()) { |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 115 | AddUndoGroup(pending_grouped_action_.release()); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 116 | } else { |
| 117 | // No changes were executed since we started grouping actions, so the |
| 118 | // pending UndoGroup should be discarded. |
| 119 | pending_grouped_action_.reset(); |
| 120 | |
| 121 | // This situation is only expected when it is a user initiated action. |
| 122 | // Undo/Redo should have at least one operation performed. |
| 123 | DCHECK(is_user_action); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void UndoManager::SuspendUndoTracking() { |
| 128 | ++undo_suspended_count_; |
| 129 | } |
| 130 | |
| 131 | void UndoManager::ResumeUndoTracking() { |
| 132 | DCHECK_GT(undo_suspended_count_, 0); |
| 133 | --undo_suspended_count_; |
| 134 | } |
| 135 | |
| 136 | bool UndoManager::IsUndoTrakingSuspended() const { |
| 137 | return undo_suspended_count_ > 0; |
| 138 | } |
| 139 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 140 | void UndoManager::RemoveAllOperations() { |
| 141 | DCHECK(!group_actions_count_); |
| 142 | undo_actions_.clear(); |
| 143 | redo_actions_.clear(); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 144 | |
| 145 | NotifyOnUndoManagerStateChange(); |
| 146 | } |
| 147 | |
| 148 | void UndoManager::AddObserver(UndoManagerObserver* observer) { |
| 149 | observers_.AddObserver(observer); |
| 150 | } |
| 151 | |
| 152 | void UndoManager::RemoveObserver(UndoManagerObserver* observer) { |
| 153 | observers_.RemoveObserver(observer); |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 154 | } |
| 155 | |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 156 | void UndoManager::Undo( |
| 157 | bool* performing_indicator, |
| 158 | std::vector<std::unique_ptr<UndoGroup>>* active_undo_group) { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 159 | // Check that action grouping has been correctly ended. |
| 160 | DCHECK(!group_actions_count_); |
| 161 | |
| 162 | if (active_undo_group->empty()) |
| 163 | return; |
| 164 | |
| 165 | base::AutoReset<bool> incoming_changes(performing_indicator, true); |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 166 | std::unique_ptr<UndoGroup> action = std::move(active_undo_group->back()); |
[email protected] | cb8f79e | 2013-12-18 20:08:42 | [diff] [blame] | 167 | base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, |
| 168 | action.get()); |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 169 | active_undo_group->erase(active_undo_group->begin() + |
| 170 | active_undo_group->size() - 1); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 171 | |
| 172 | StartGroupingActions(); |
| 173 | action->Undo(); |
| 174 | EndGroupingActions(); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 175 | |
| 176 | NotifyOnUndoManagerStateChange(); |
| 177 | } |
| 178 | |
| 179 | void UndoManager::NotifyOnUndoManagerStateChange() { |
ericwilligers | ff2af33 | 2016-10-19 00:16:50 | [diff] [blame] | 180 | for (auto& observer : observers_) |
| 181 | observer.OnUndoManagerStateChange(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 184 | void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 185 | GetActiveUndoGroup()->push_back(base::WrapUnique<UndoGroup>(new_undo_group)); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 186 | |
[email protected] | f054103 | 2013-07-31 07:27:53 | [diff] [blame] | 187 | // User actions invalidate any available redo actions. |
| 188 | if (is_user_action()) |
| 189 | redo_actions_.clear(); |
| 190 | |
| 191 | // Limit the number of undo levels so the undo stack does not grow unbounded. |
| 192 | if (GetActiveUndoGroup()->size() > kMaxUndoGroups) |
| 193 | GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); |
[email protected] | 0167050e | 2014-04-15 21:14:22 | [diff] [blame] | 194 | |
| 195 | NotifyOnUndoManagerStateChange(); |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 196 | } |
| 197 | |
ke.he | da6df4de | 2017-01-26 00:20:03 | [diff] [blame] | 198 | std::vector<std::unique_ptr<UndoGroup>>* UndoManager::GetActiveUndoGroup() { |
[email protected] | be5cf82 | 2013-07-10 16:36:21 | [diff] [blame] | 199 | return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 200 | } |