| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 
 | // Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/undo/undo_manager.h"
#include <memory>
#include <utility>
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "base/containers/adapters.h"
#include "base/memory/ptr_util.h"
#include "base/observer_list.h"
#include "components/strings/grit/components_strings.h"
#include "components/undo/undo_manager_observer.h"
#include "components/undo/undo_operation.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
// Maximum number of changes that can be undone.
const size_t kMaxUndoGroups = 100;
}  // namespace
// UndoGroup ------------------------------------------------------------------
UndoGroup::UndoGroup()
    : undo_label_id_(IDS_BOOKMARK_BAR_UNDO),
      redo_label_id_(IDS_BOOKMARK_BAR_REDO) {
}
UndoGroup::~UndoGroup() {
}
void UndoGroup::AddOperation(std::unique_ptr<UndoOperation> operation) {
  if (operations_.empty()) {
    set_undo_label_id(operation->GetUndoLabelId());
    set_redo_label_id(operation->GetRedoLabelId());
  }
  operations_.push_back(std::move(operation));
}
void UndoGroup::Undo() {
  for (const std::unique_ptr<UndoOperation>& operation :
       base::Reversed(operations_))
    operation->Undo();
}
// UndoManager ----------------------------------------------------------------
UndoManager::UndoManager()
    : group_actions_count_(0),
      undo_in_progress_action_(nullptr),
      undo_suspended_count_(0),
      performing_undo_(false),
      performing_redo_(false) {}
UndoManager::~UndoManager() {
  DCHECK_EQ(0, group_actions_count_);
  DCHECK_EQ(0, undo_suspended_count_);
  DCHECK(!performing_undo_);
  DCHECK(!performing_redo_);
}
void UndoManager::Undo() {
  Undo(&performing_undo_, &undo_actions_);
}
void UndoManager::Redo() {
  Undo(&performing_redo_, &redo_actions_);
}
std::u16string UndoManager::GetUndoLabel() const {
  return l10n_util::GetStringUTF16(
      undo_actions_.empty() ? IDS_BOOKMARK_BAR_UNDO
                            : undo_actions_.back()->get_undo_label_id());
}
std::u16string UndoManager::GetRedoLabel() const {
  return l10n_util::GetStringUTF16(
      redo_actions_.empty() ? IDS_BOOKMARK_BAR_REDO
                            : redo_actions_.back()->get_redo_label_id());
}
void UndoManager::AddUndoOperation(std::unique_ptr<UndoOperation> operation) {
  if (IsUndoTrakingSuspended()) {
    RemoveAllOperations();
    operation.reset();
    return;
  }
  if (group_actions_count_) {
    pending_grouped_action_->AddOperation(std::move(operation));
  } else {
    auto new_action = std::make_unique<UndoGroup>();
    new_action->AddOperation(std::move(operation));
    AddUndoGroup(std::move(new_action));
  }
}
void UndoManager::StartGroupingActions() {
  if (!group_actions_count_)
    pending_grouped_action_ = std::make_unique<UndoGroup>();
  ++group_actions_count_;
}
void UndoManager::EndGroupingActions() {
  --group_actions_count_;
  if (group_actions_count_ > 0)
    return;
  // Check that StartGroupingActions and EndGroupingActions are paired.
  DCHECK_GE(group_actions_count_, 0);
  bool is_user_action = !performing_undo_ && !performing_redo_;
  if (!pending_grouped_action_->undo_operations().empty()) {
    AddUndoGroup(std::move(pending_grouped_action_));
  } else {
    // No changes were executed since we started grouping actions, so the
    // pending UndoGroup should be discarded.
    pending_grouped_action_.reset();
    // This situation is only expected when it is a user initiated action.
    // Undo/Redo should have at least one operation performed.
    DCHECK(is_user_action);
  }
}
void UndoManager::SuspendUndoTracking() {
  ++undo_suspended_count_;
}
void UndoManager::ResumeUndoTracking() {
  DCHECK_GT(undo_suspended_count_, 0);
  --undo_suspended_count_;
}
bool UndoManager::IsUndoTrakingSuspended() const {
  return undo_suspended_count_ > 0;
}
void UndoManager::RemoveAllOperations() {
  DCHECK(!group_actions_count_);
  undo_actions_.clear();
  redo_actions_.clear();
  observers_.Notify(&UndoManagerObserver::OnUndoManagerStateChange);
}
void UndoManager::AddObserver(UndoManagerObserver* observer) {
  observers_.AddObserver(observer);
}
void UndoManager::RemoveObserver(UndoManagerObserver* observer) {
  observers_.RemoveObserver(observer);
}
void UndoManager::Shutdown() {
  // After the `RemoveAllOperations` call below - this instance won't be
  // notified of `BookmarkModel` destruction. Undo operations keep a pointer to
  // `BookmarkModel` - delete them to avoid dangling pointers.
  RemoveAllOperations();
  observers_.Notify(&UndoManagerObserver::OnUndoManagerShutdown);
  DCHECK(observers_.empty());
}
void UndoManager::Undo(
    bool* performing_indicator,
    std::vector<std::unique_ptr<UndoGroup>>* active_undo_group) {
  // Check that action grouping has been correctly ended.
  DCHECK(!group_actions_count_);
  if (active_undo_group->empty())
    return;
  base::AutoReset<bool> incoming_changes(performing_indicator, true);
  std::unique_ptr<UndoGroup> action = std::move(active_undo_group->back());
  base::AutoReset<raw_ptr<UndoGroup>> action_context(&undo_in_progress_action_,
                                                     action.get());
  active_undo_group->erase(active_undo_group->begin() +
                           active_undo_group->size() - 1);
  StartGroupingActions();
  action->Undo();
  EndGroupingActions();
  observers_.Notify(&UndoManagerObserver::OnUndoManagerStateChange);
}
void UndoManager::AddUndoGroup(std::unique_ptr<UndoGroup> new_undo_group) {
  GetActiveUndoGroup()->push_back(std::move(new_undo_group));
  // User actions invalidate any available redo actions.
  if (is_user_action())
    redo_actions_.clear();
  // Limit the number of undo levels so the undo stack does not grow unbounded.
  if (GetActiveUndoGroup()->size() > kMaxUndoGroups)
    GetActiveUndoGroup()->erase(GetActiveUndoGroup()->begin());
  observers_.Notify(&UndoManagerObserver::OnUndoManagerStateChange);
}
std::vector<std::unique_ptr<UndoGroup>>* UndoManager::GetActiveUndoGroup() {
  return performing_undo_ ? &redo_actions_ : &undo_actions_;
}
 |