1 2 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/undo/bookmark_undo_service.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/undo/bookmark_renumber_observer.h"
#include "chrome/browser/undo/bookmark_undo_service_factory.h"
#include "chrome/browser/undo/undo_operation.h"
#include "chrome/grit/generated_resources.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node_data.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/browser/scoped_group_bookmark_actions.h"
using bookmarks::BookmarkNodeData;
namespace {
// BookmarkUndoOperation ------------------------------------------------------
// Base class for all bookmark related UndoOperations that facilitates access to
// the BookmarkUndoService.
class BookmarkUndoOperation : public UndoOperation,
public BookmarkRenumberObserver {
public:
explicit BookmarkUndoOperation(Profile* profile);
~BookmarkUndoOperation() override {}
BookmarkModel* GetBookmarkModel() const;
BookmarkRenumberObserver* GetUndoRenumberObserver() const;
private:
Profile* profile_;
};
BookmarkUndoOperation::BookmarkUndoOperation(Profile* profile)
: profile_(profile) {
}
BookmarkModel* BookmarkUndoOperation::GetBookmarkModel() const {
return BookmarkModelFactory::GetForProfile(profile_);
}
BookmarkRenumberObserver* BookmarkUndoOperation::GetUndoRenumberObserver()
const {
return BookmarkUndoServiceFactory::GetForProfile(profile_);
}
// BookmarkAddOperation -------------------------------------------------------
// Handles the undo of the insertion of a bookmark or folder.
class BookmarkAddOperation : public BookmarkUndoOperation {
public:
BookmarkAddOperation(Profile* profile, const BookmarkNode* parent, int index);
~BookmarkAddOperation() override {}
// UndoOperation:
void Undo() override;
int GetUndoLabelId() const override;
int GetRedoLabelId() const override;
// BookmarkRenumberObserver:
void OnBookmarkRenumbered(int64 old_id, int64 new_id) override;
private:
int64 parent_id_;
const int index_;
DISALLOW_COPY_AND_ASSIGN(BookmarkAddOperation);
};
BookmarkAddOperation::BookmarkAddOperation(Profile* profile,
const BookmarkNode* parent,
int index)
: BookmarkUndoOperation(profile),
parent_id_(parent->id()),
index_(index) {
}
void BookmarkAddOperation::Undo() {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* parent =
bookmarks::GetBookmarkNodeByID(model, parent_id_);
DCHECK(parent);
model->Remove(parent, index_);
}
int BookmarkAddOperation::GetUndoLabelId() const {
return IDS_BOOKMARK_BAR_UNDO_ADD;
}
int BookmarkAddOperation::GetRedoLabelId() const {
return IDS_BOOKMARK_BAR_REDO_DELETE;
}
void BookmarkAddOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) {
if (parent_id_ == old_id)
parent_id_ = new_id;
}
// BookmarkRemoveOperation ----------------------------------------------------
// Handles the undo of the deletion of a bookmark node. For a bookmark folder,
// the information for all descendant bookmark nodes is maintained.
//
// The BookmarkModel allows only single bookmark node to be removed.
class BookmarkRemoveOperation : public BookmarkUndoOperation {
public:
BookmarkRemoveOperation(Profile* profile,
const BookmarkNode* parent,
int old_index,
const BookmarkNode* node);
~BookmarkRemoveOperation() override {}
// UndoOperation:
void Undo() override;
int GetUndoLabelId() const override;
int GetRedoLabelId() const override;
// BookmarkRenumberObserver:
void OnBookmarkRenumbered(int64 old_id, int64 new_id) override;
private:
void UpdateBookmarkIds(const BookmarkNodeData::Element& element,
const BookmarkNode* parent,
int index_added_at) const;
int64 parent_id_;
const int old_index_;
BookmarkNodeData removed_node_;
DISALLOW_COPY_AND_ASSIGN(BookmarkRemoveOperation);
};
BookmarkRemoveOperation::BookmarkRemoveOperation(Profile* profile,
const BookmarkNode* parent,
int old_index,
const BookmarkNode* node)
: BookmarkUndoOperation(profile),
parent_id_(parent->id()),
old_index_(old_index),
removed_node_(node) {
}
void BookmarkRemoveOperation::Undo() {
DCHECK(removed_node_.is_valid());
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* parent =
bookmarks::GetBookmarkNodeByID(model, parent_id_);
DCHECK(parent);
bookmarks::CloneBookmarkNode(
model, removed_node_.elements, parent, old_index_, false);
UpdateBookmarkIds(removed_node_.elements[0], parent, old_index_);
}
int BookmarkRemoveOperation::GetUndoLabelId() const {
return IDS_BOOKMARK_BAR_UNDO_DELETE;
}
int BookmarkRemoveOperation::GetRedoLabelId() const {
return IDS_BOOKMARK_BAR_REDO_ADD;
}
void BookmarkRemoveOperation::UpdateBookmarkIds(
const BookmarkNodeData::Element& element,
const BookmarkNode* parent,
int index_added_at) const {
const BookmarkNode* node = parent->GetChild(index_added_at);
if (element.id() != node->id())
GetUndoRenumberObserver()->OnBookmarkRenumbered(element.id(), node->id());
if (!element.is_url) {
for (int i = 0; i < static_cast<int>(element.children.size()); ++i)
UpdateBookmarkIds(element.children[i], node, 0);
}
}
void BookmarkRemoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) {
if (parent_id_ == old_id)
parent_id_ = new_id;
}
// BookmarkEditOperation ------------------------------------------------------
// Handles the undo of the modification of a bookmark node.
class BookmarkEditOperation : public BookmarkUndoOperation {
public:
BookmarkEditOperation(Profile* profile,
const BookmarkNode* node);
~BookmarkEditOperation() override {}
// UndoOperation:
void Undo() override;
int GetUndoLabelId() const override;
int GetRedoLabelId() const override;
// BookmarkRenumberObserver:
void OnBookmarkRenumbered(int64 old_id, int64 new_id) override;
private:
int64 node_id_;
BookmarkNodeData original_bookmark_;
DISALLOW_COPY_AND_ASSIGN(BookmarkEditOperation);
};
BookmarkEditOperation::BookmarkEditOperation(Profile* profile,
const BookmarkNode* node)
: BookmarkUndoOperation(profile),
node_id_(node->id()),
original_bookmark_(node) {
}
void BookmarkEditOperation::Undo() {
DCHECK(original_bookmark_.is_valid());
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, node_id_);
DCHECK(node);
model->SetTitle(node, original_bookmark_.elements[0].title);
if (original_bookmark_.elements[0].is_url)
model->SetURL(node, original_bookmark_.elements[0].url);
}
int BookmarkEditOperation::GetUndoLabelId() const {
return IDS_BOOKMARK_BAR_UNDO_EDIT;
}
int BookmarkEditOperation::GetRedoLabelId() const {
return IDS_BOOKMARK_BAR_REDO_EDIT;
}
void BookmarkEditOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) {
if (node_id_ == old_id)
node_id_ = new_id;
}
// BookmarkMoveOperation ------------------------------------------------------
// Handles the undo of a bookmark being moved to a new location.
class BookmarkMoveOperation : public BookmarkUndoOperation {
public:
BookmarkMoveOperation(Profile* profile,
const BookmarkNode* old_parent,
int old_index,
const BookmarkNode* new_parent,
int new_index);
~BookmarkMoveOperation() override {}
int GetUndoLabelId() const override;
int GetRedoLabelId() const override;
// UndoOperation:
void Undo() override;
// BookmarkRenumberObserver:
void OnBookmarkRenumbered(int64 old_id, int64 new_id) override;
private:
int64 old_parent_id_;
int64 new_parent_id_;
int old_index_;
int new_index_;
DISALLOW_COPY_AND_ASSIGN(BookmarkMoveOperation);
};
BookmarkMoveOperation::BookmarkMoveOperation(Profile* profile,
const BookmarkNode* old_parent,
int old_index,
const BookmarkNode* new_parent,
int new_index)
: BookmarkUndoOperation(profile),
old_parent_id_(old_parent->id()),
new_parent_id_(new_parent->id()),
old_index_(old_index),
new_index_(new_index) {
}
void BookmarkMoveOperation::Undo() {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* old_parent =
bookmarks::GetBookmarkNodeByID(model, old_parent_id_);
const BookmarkNode* new_parent =
bookmarks::GetBookmarkNodeByID(model, new_parent_id_);
DCHECK(old_parent);
DCHECK(new_parent);
const BookmarkNode* node = new_parent->GetChild(new_index_);
int destination_index = old_index_;
// If the bookmark was moved up within the same parent then the destination
// index needs to be incremented since the old index did not account for the
// moved bookmark.
if (old_parent == new_parent && new_index_ < old_index_)
++destination_index;
model->Move(node, old_parent, destination_index);
}
int BookmarkMoveOperation::GetUndoLabelId() const {
return IDS_BOOKMARK_BAR_UNDO_MOVE;
}
int BookmarkMoveOperation::GetRedoLabelId() const {
return IDS_BOOKMARK_BAR_REDO_MOVE;
}
void BookmarkMoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) {
if (old_parent_id_ == old_id)
old_parent_id_ = new_id;
if (new_parent_id_ == old_id)
new_parent_id_ = new_id;
}
// BookmarkReorderOperation ---------------------------------------------------
// Handle the undo of reordering of bookmarks that can happen as a result of
// sorting a bookmark folder by name or the undo of that operation. The change
// of order is not recursive so only the order of the immediate children of the
// folder need to be restored.
class BookmarkReorderOperation : public BookmarkUndoOperation {
public:
BookmarkReorderOperation(Profile* profile,
const BookmarkNode* parent);
~BookmarkReorderOperation() override;
// UndoOperation:
void Undo() override;
int GetUndoLabelId() const override;
int GetRedoLabelId() const override;
// BookmarkRenumberObserver:
void OnBookmarkRenumbered(int64 old_id, int64 new_id) override;
private:
int64 parent_id_;
std::vector<int64> ordered_bookmarks_;
DISALLOW_COPY_AND_ASSIGN(BookmarkReorderOperation);
};
BookmarkReorderOperation::BookmarkReorderOperation(Profile* profile,
const BookmarkNode* parent)
: BookmarkUndoOperation(profile),
parent_id_(parent->id()) {
ordered_bookmarks_.resize(parent->child_count());
for (int i = 0; i < parent->child_count(); ++i)
ordered_bookmarks_[i] = parent->GetChild(i)->id();
}
BookmarkReorderOperation::~BookmarkReorderOperation() {
}
void BookmarkReorderOperation::Undo() {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* parent =
bookmarks::GetBookmarkNodeByID(model, parent_id_);
DCHECK(parent);
std::vector<const BookmarkNode*> ordered_nodes;
for (size_t i = 0; i < ordered_bookmarks_.size(); ++i) {
ordered_nodes.push_back(
bookmarks::GetBookmarkNodeByID(model, ordered_bookmarks_[i]));
}
model->ReorderChildren(parent, ordered_nodes);
}
int BookmarkReorderOperation::GetUndoLabelId() const {
return IDS_BOOKMARK_BAR_UNDO_REORDER;
}
int BookmarkReorderOperation::GetRedoLabelId() const {
return IDS_BOOKMARK_BAR_REDO_REORDER;
}
void BookmarkReorderOperation::OnBookmarkRenumbered(int64 old_id,
int64 new_id) {
if (parent_id_ == old_id)
parent_id_ = new_id;
for (size_t i = 0; i < ordered_bookmarks_.size(); ++i) {
if (ordered_bookmarks_[i] == old_id)
ordered_bookmarks_[i] = new_id;
}
}
} // namespace
// BookmarkUndoService --------------------------------------------------------
BookmarkUndoService::BookmarkUndoService(Profile* profile) : profile_(profile) {
}
BookmarkUndoService::~BookmarkUndoService() {
BookmarkModelFactory::GetForProfile(profile_)->RemoveObserver(this);
}
void BookmarkUndoService::BookmarkModelLoaded(BookmarkModel* model,
bool ids_reassigned) {
undo_manager_.RemoveAllOperations();
}
void BookmarkUndoService::BookmarkModelBeingDeleted(BookmarkModel* model) {
undo_manager_.RemoveAllOperations();
}
void BookmarkUndoService::BookmarkNodeMoved(BookmarkModel* model,
const BookmarkNode* old_parent,
int old_index,
const BookmarkNode* new_parent,
int new_index) {
scoped_ptr<UndoOperation> op(new BookmarkMoveOperation(profile_,
old_parent,
old_index,
new_parent,
new_index));
undo_manager()->AddUndoOperation(op.Pass());
}
void BookmarkUndoService::BookmarkNodeAdded(BookmarkModel* model,
const BookmarkNode* parent,
int index) {
scoped_ptr<UndoOperation> op(new BookmarkAddOperation(profile_,
parent,
index));
undo_manager()->AddUndoOperation(op.Pass());
}
void BookmarkUndoService::OnWillRemoveBookmarks(BookmarkModel* model,
const BookmarkNode* parent,
int old_index,
const BookmarkNode* node) {
scoped_ptr<UndoOperation> op(new BookmarkRemoveOperation(profile_,
parent,
old_index,
node));
undo_manager()->AddUndoOperation(op.Pass());
}
void BookmarkUndoService::OnWillRemoveAllUserBookmarks(BookmarkModel* model) {
bookmarks::ScopedGroupBookmarkActions merge_removes(model);
for (int i = 0; i < model->root_node()->child_count(); ++i) {
const BookmarkNode* permanent_node = model->root_node()->GetChild(i);
for (int j = permanent_node->child_count() - 1; j >= 0; --j) {
scoped_ptr<UndoOperation> op(new BookmarkRemoveOperation(profile_,
permanent_node, j, permanent_node->GetChild(j)));
undo_manager()->AddUndoOperation(op.Pass());
}
}
}
void BookmarkUndoService::OnWillChangeBookmarkNode(BookmarkModel* model,
const BookmarkNode* node) {
scoped_ptr<UndoOperation> op(new BookmarkEditOperation(profile_, node));
undo_manager()->AddUndoOperation(op.Pass());
}
void BookmarkUndoService::OnWillReorderBookmarkNode(BookmarkModel* model,
const BookmarkNode* node) {
scoped_ptr<UndoOperation> op(new BookmarkReorderOperation(profile_, node));
undo_manager()->AddUndoOperation(op.Pass());
}
void BookmarkUndoService::GroupedBookmarkChangesBeginning(
BookmarkModel* model) {
undo_manager()->StartGroupingActions();
}
void BookmarkUndoService::GroupedBookmarkChangesEnded(BookmarkModel* model) {
undo_manager()->EndGroupingActions();
}
void BookmarkUndoService::OnBookmarkRenumbered(int64 old_id, int64 new_id) {
std::vector<UndoOperation*> all_operations =
undo_manager()->GetAllUndoOperations();
for (std::vector<UndoOperation*>::iterator it = all_operations.begin();
it != all_operations.end(); ++it) {
static_cast<BookmarkUndoOperation*>(*it)->OnBookmarkRenumbered(old_id,
new_id);
}
}
|