Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/undo/undo_manager.h" | 5 #include "components/undo/undo_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "components/undo/undo_manager_observer.h" | 12 #include "components/undo/undo_manager_observer.h" |
| 12 #include "components/undo/undo_operation.h" | 13 #include "components/undo/undo_operation.h" |
| 13 #include "grit/components_strings.h" | 14 #include "grit/components_strings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Maximum number of changes that can be undone. | 19 // Maximum number of changes that can be undone. |
| 19 const size_t kMaxUndoGroups = 100; | 20 const size_t kMaxUndoGroups = 100; |
| 20 | 21 |
| 22 std::vector<UndoOperation*> ConvertToRawPtrVector( | |
| 23 const std::vector<std::unique_ptr<UndoOperation>>& args) { | |
| 24 std::vector<UndoOperation*> args_rawptrs; | |
| 25 for (auto i = args.begin(); i != args.end(); ++i) | |
| 26 args_rawptrs.push_back(i->get()); | |
| 27 return args_rawptrs; | |
| 28 } | |
| 29 | |
| 21 } // namespace | 30 } // namespace |
| 22 | 31 |
| 23 // UndoGroup ------------------------------------------------------------------ | 32 // UndoGroup ------------------------------------------------------------------ |
| 24 | 33 |
| 25 UndoGroup::UndoGroup() | 34 UndoGroup::UndoGroup() |
| 26 : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), | 35 : undo_label_id_(IDS_BOOKMARK_BAR_UNDO), |
| 27 redo_label_id_(IDS_BOOKMARK_BAR_REDO) { | 36 redo_label_id_(IDS_BOOKMARK_BAR_REDO) { |
| 28 } | 37 } |
| 29 | 38 |
| 30 UndoGroup::~UndoGroup() { | 39 UndoGroup::~UndoGroup() { |
| 31 } | 40 } |
| 32 | 41 |
| 33 void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { | 42 void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) { |
| 34 if (operations_.empty()) { | 43 if (operations_.empty()) { |
| 35 set_undo_label_id(operation->GetUndoLabelId()); | 44 set_undo_label_id(operation->GetUndoLabelId()); |
| 36 set_redo_label_id(operation->GetRedoLabelId()); | 45 set_redo_label_id(operation->GetRedoLabelId()); |
| 37 } | 46 } |
| 38 operations_.push_back(operation.release()); | 47 operations_.push_back(std::move(operation)); |
| 39 } | 48 } |
| 40 | 49 |
| 41 void UndoGroup::Undo() { | 50 void UndoGroup::Undo() { |
| 42 for (ScopedVector<UndoOperation>::reverse_iterator ri = operations_.rbegin(); | 51 for (auto ri = operations_.rbegin(); ri != operations_.rend(); ++ri) |
| 43 ri != operations_.rend(); ++ri) { | |
| 44 (*ri)->Undo(); | 52 (*ri)->Undo(); |
| 45 } | |
| 46 } | 53 } |
| 47 | 54 |
| 48 // UndoManager ---------------------------------------------------------------- | 55 // UndoManager ---------------------------------------------------------------- |
| 49 | 56 |
| 50 UndoManager::UndoManager() | 57 UndoManager::UndoManager() |
| 51 : group_actions_count_(0), | 58 : group_actions_count_(0), |
| 52 undo_in_progress_action_(NULL), | 59 undo_in_progress_action_(NULL), |
| 53 undo_suspended_count_(0), | 60 undo_suspended_count_(0), |
| 54 performing_undo_(false), | 61 performing_undo_(false), |
| 55 performing_redo_(false) { | 62 performing_redo_(false) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 | 139 |
| 133 void UndoManager::ResumeUndoTracking() { | 140 void UndoManager::ResumeUndoTracking() { |
| 134 DCHECK_GT(undo_suspended_count_, 0); | 141 DCHECK_GT(undo_suspended_count_, 0); |
| 135 --undo_suspended_count_; | 142 --undo_suspended_count_; |
| 136 } | 143 } |
| 137 | 144 |
| 138 bool UndoManager::IsUndoTrakingSuspended() const { | 145 bool UndoManager::IsUndoTrakingSuspended() const { |
| 139 return undo_suspended_count_ > 0; | 146 return undo_suspended_count_ > 0; |
| 140 } | 147 } |
| 141 | 148 |
| 142 std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { | 149 std::vector<UndoOperation*> UndoManager::GetAllUndoOperations() const { |
|
sky
2017/01/23 16:22:16
This function is only used in tests. Please move t
ke.he
2017/01/24 09:01:13
Done.
ConvertToRawPtrVector() is moved into test f
| |
| 143 std::vector<UndoOperation*> result; | 150 std::vector<UndoOperation*> result; |
| 144 for (size_t i = 0; i < undo_actions_.size(); ++i) { | 151 for (size_t i = 0; i < undo_actions_.size(); ++i) { |
| 145 const std::vector<UndoOperation*>& operations = | 152 const std::vector<UndoOperation*> operations = |
| 146 undo_actions_[i]->undo_operations(); | 153 ConvertToRawPtrVector(undo_actions_[i]->undo_operations()); |
| 147 result.insert(result.end(), operations.begin(), operations.end()); | 154 result.insert(result.end(), operations.begin(), operations.end()); |
| 148 } | 155 } |
| 149 for (size_t i = 0; i < redo_actions_.size(); ++i) { | 156 for (size_t i = 0; i < redo_actions_.size(); ++i) { |
| 150 const std::vector<UndoOperation*>& operations = | 157 const std::vector<UndoOperation*> operations = |
| 151 redo_actions_[i]->undo_operations(); | 158 ConvertToRawPtrVector(redo_actions_[i]->undo_operations()); |
| 152 result.insert(result.end(), operations.begin(), operations.end()); | 159 result.insert(result.end(), operations.begin(), operations.end()); |
| 153 } | 160 } |
| 154 // Ensure that if an Undo is in progress the UndoOperations part of that | 161 // 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 | 162 // UndoGroup are included in the returned set. This will ensure that any |
| 156 // changes (such as renumbering) will be applied to any potentially | 163 // changes (such as renumbering) will be applied to any potentially |
| 157 // unprocessed UndoOperations. | 164 // unprocessed UndoOperations. |
| 158 if (undo_in_progress_action_) { | 165 if (undo_in_progress_action_) { |
| 159 const std::vector<UndoOperation*>& operations = | 166 const std::vector<UndoOperation*> operations = |
| 160 undo_in_progress_action_->undo_operations(); | 167 ConvertToRawPtrVector(undo_in_progress_action_->undo_operations()); |
| 161 result.insert(result.end(), operations.begin(), operations.end()); | 168 result.insert(result.end(), operations.begin(), operations.end()); |
| 162 } | 169 } |
| 163 | 170 |
| 164 return result; | 171 return result; |
| 165 } | 172 } |
| 166 | 173 |
| 167 void UndoManager::RemoveAllOperations() { | 174 void UndoManager::RemoveAllOperations() { |
| 168 DCHECK(!group_actions_count_); | 175 DCHECK(!group_actions_count_); |
| 169 undo_actions_.clear(); | 176 undo_actions_.clear(); |
| 170 redo_actions_.clear(); | 177 redo_actions_.clear(); |
| 171 | 178 |
| 172 NotifyOnUndoManagerStateChange(); | 179 NotifyOnUndoManagerStateChange(); |
| 173 } | 180 } |
| 174 | 181 |
| 175 void UndoManager::AddObserver(UndoManagerObserver* observer) { | 182 void UndoManager::AddObserver(UndoManagerObserver* observer) { |
| 176 observers_.AddObserver(observer); | 183 observers_.AddObserver(observer); |
| 177 } | 184 } |
| 178 | 185 |
| 179 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { | 186 void UndoManager::RemoveObserver(UndoManagerObserver* observer) { |
| 180 observers_.RemoveObserver(observer); | 187 observers_.RemoveObserver(observer); |
| 181 } | 188 } |
| 182 | 189 |
| 183 void UndoManager::Undo(bool* performing_indicator, | 190 void UndoManager::Undo( |
| 184 ScopedVector<UndoGroup>* active_undo_group) { | 191 bool* performing_indicator, |
| 192 std::vector<std::unique_ptr<UndoGroup>>* active_undo_group) { | |
| 185 // Check that action grouping has been correctly ended. | 193 // Check that action grouping has been correctly ended. |
| 186 DCHECK(!group_actions_count_); | 194 DCHECK(!group_actions_count_); |
| 187 | 195 |
| 188 if (active_undo_group->empty()) | 196 if (active_undo_group->empty()) |
| 189 return; | 197 return; |
| 190 | 198 |
| 191 base::AutoReset<bool> incoming_changes(performing_indicator, true); | 199 base::AutoReset<bool> incoming_changes(performing_indicator, true); |
| 192 std::unique_ptr<UndoGroup> action(active_undo_group->back()); | 200 std::unique_ptr<UndoGroup> action = std::move(active_undo_group->back()); |
| 193 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, | 201 base::AutoReset<UndoGroup*> action_context(&undo_in_progress_action_, |
| 194 action.get()); | 202 action.get()); |
| 195 active_undo_group->weak_erase( | 203 active_undo_group->erase(active_undo_group->begin() + |
| 196 active_undo_group->begin() + active_undo_group->size() - 1); | 204 active_undo_group->size() - 1); |
| 197 | 205 |
| 198 StartGroupingActions(); | 206 StartGroupingActions(); |
| 199 action->Undo(); | 207 action->Undo(); |
| 200 EndGroupingActions(); | 208 EndGroupingActions(); |
| 201 | 209 |
| 202 NotifyOnUndoManagerStateChange(); | 210 NotifyOnUndoManagerStateChange(); |
| 203 } | 211 } |
| 204 | 212 |
| 205 void UndoManager::NotifyOnUndoManagerStateChange() { | 213 void UndoManager::NotifyOnUndoManagerStateChange() { |
| 206 for (auto& observer : observers_) | 214 for (auto& observer : observers_) |
| 207 observer.OnUndoManagerStateChange(); | 215 observer.OnUndoManagerStateChange(); |
| 208 } | 216 } |
| 209 | 217 |
| 210 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { | 218 void UndoManager::AddUndoGroup(UndoGroup* new_undo_group) { |
| 211 GetActiveUndoGroup()->push_back(new_undo_group); | 219 GetActiveUndoGroup()->push_back(base::WrapUnique<UndoGroup>(new_undo_group)); |
| 212 | 220 |
| 213 // User actions invalidate any available redo actions. | 221 // User actions invalidate any available redo actions. |
| 214 if (is_user_action()) | 222 if (is_user_action()) |
| 215 redo_actions_.clear(); | 223 redo_actions_.clear(); |
| 216 | 224 |
| 217 // Limit the number of undo levels so the undo stack does not grow unbounded. | 225 // Limit the number of undo levels so the undo stack does not grow unbounded. |
| 218 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) | 226 if (GetActiveUndoGroup()->size() > kMaxUndoGroups) |
| 219 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); | 227 GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin()); |
| 220 | 228 |
| 221 NotifyOnUndoManagerStateChange(); | 229 NotifyOnUndoManagerStateChange(); |
| 222 } | 230 } |
| 223 | 231 |
| 224 ScopedVector<UndoGroup>* UndoManager::GetActiveUndoGroup() { | 232 std::vector<std::unique_ptr<UndoGroup>>* UndoManager::GetActiveUndoGroup() { |
| 225 return performing_undo_ ? &redo_actions_ : &undo_actions_; | 233 return performing_undo_ ? &redo_actions_ : &undo_actions_; |
| 226 } | 234 } |
| OLD | NEW |