| 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/bookmark_undo_service.h" | 5 #include "components/undo/bookmark_undo_service.h" |
| 6 | 6 |
| 7 #include "components/bookmarks/browser/bookmark_model.h" | 7 #include "components/bookmarks/browser/bookmark_model.h" |
| 8 #include "components/bookmarks/browser/bookmark_node_data.h" | 8 #include "components/bookmarks/browser/bookmark_node_data.h" |
| 9 #include "components/bookmarks/browser/bookmark_undo_provider.h" |
| 9 #include "components/bookmarks/browser/bookmark_utils.h" | 10 #include "components/bookmarks/browser/bookmark_utils.h" |
| 10 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 11 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |
| 11 #include "components/undo/bookmark_renumber_observer.h" | |
| 12 #include "components/undo/undo_operation.h" | 12 #include "components/undo/undo_operation.h" |
| 13 #include "grit/components_strings.h" | 13 #include "grit/components_strings.h" |
| 14 | 14 |
| 15 using bookmarks::BookmarkModel; | 15 using bookmarks::BookmarkModel; |
| 16 using bookmarks::BookmarkNode; | 16 using bookmarks::BookmarkNode; |
| 17 using bookmarks::BookmarkNodeData; | 17 using bookmarks::BookmarkNodeData; |
| 18 using bookmarks::BookmarkUndoProvider; |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // BookmarkUndoOperation ------------------------------------------------------ | 22 // BookmarkUndoOperation ------------------------------------------------------ |
| 22 | 23 |
| 23 // Base class for all bookmark related UndoOperations that facilitates access to | 24 // Base class for all bookmark related UndoOperations that facilitates access to |
| 24 // the BookmarkUndoService. | 25 // the BookmarkUndoService. |
| 25 class BookmarkUndoOperation : public UndoOperation, | 26 class BookmarkUndoOperation : public UndoOperation { |
| 26 public BookmarkRenumberObserver { | |
| 27 public: | 27 public: |
| 28 BookmarkUndoOperation(BookmarkModel* bookmark_model, | 28 explicit BookmarkUndoOperation(BookmarkModel* bookmark_model) |
| 29 BookmarkRenumberObserver* undo_renumber_observer) | 29 : bookmark_model_(bookmark_model) {} |
| 30 : bookmark_model_(bookmark_model), | |
| 31 undo_renumber_observer_(undo_renumber_observer) {} | |
| 32 ~BookmarkUndoOperation() override {} | 30 ~BookmarkUndoOperation() override {} |
| 33 | 31 |
| 34 BookmarkModel* bookmark_model() { return bookmark_model_; } | 32 BookmarkModel* bookmark_model() { return bookmark_model_; } |
| 35 | 33 |
| 36 BookmarkRenumberObserver* undo_renumber_observer() { | |
| 37 return undo_renumber_observer_; | |
| 38 } | |
| 39 | |
| 40 private: | 34 private: |
| 41 BookmarkModel* bookmark_model_; | 35 BookmarkModel* bookmark_model_; |
| 42 BookmarkRenumberObserver* undo_renumber_observer_; | |
| 43 }; | 36 }; |
| 44 | 37 |
| 45 // BookmarkAddOperation ------------------------------------------------------- | 38 // BookmarkAddOperation ------------------------------------------------------- |
| 46 | 39 |
| 47 // Handles the undo of the insertion of a bookmark or folder. | 40 // Handles the undo of the insertion of a bookmark or folder. |
| 48 class BookmarkAddOperation : public BookmarkUndoOperation { | 41 class BookmarkAddOperation : public BookmarkUndoOperation { |
| 49 public: | 42 public: |
| 50 BookmarkAddOperation(BookmarkModel* bookmark_model, | 43 BookmarkAddOperation(BookmarkModel* bookmark_model, |
| 51 BookmarkRenumberObserver* undo_renumber_observer, | |
| 52 const BookmarkNode* parent, | 44 const BookmarkNode* parent, |
| 53 int index); | 45 int index); |
| 54 ~BookmarkAddOperation() override {} | 46 ~BookmarkAddOperation() override {} |
| 55 | 47 |
| 56 // UndoOperation: | 48 // UndoOperation: |
| 57 void Undo() override; | 49 void Undo() override; |
| 58 int GetUndoLabelId() const override; | 50 int GetUndoLabelId() const override; |
| 59 int GetRedoLabelId() const override; | 51 int GetRedoLabelId() const override; |
| 60 | 52 |
| 61 // BookmarkRenumberObserver: | |
| 62 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
| 63 | |
| 64 private: | 53 private: |
| 65 int64 parent_id_; | 54 int64 parent_id_; |
| 66 const int index_; | 55 const int index_; |
| 67 | 56 |
| 68 DISALLOW_COPY_AND_ASSIGN(BookmarkAddOperation); | 57 DISALLOW_COPY_AND_ASSIGN(BookmarkAddOperation); |
| 69 }; | 58 }; |
| 70 | 59 |
| 71 BookmarkAddOperation::BookmarkAddOperation( | 60 BookmarkAddOperation::BookmarkAddOperation( |
| 72 BookmarkModel* bookmark_model, | 61 BookmarkModel* bookmark_model, |
| 73 BookmarkRenumberObserver* undo_renumber_observer, | |
| 74 const BookmarkNode* parent, | 62 const BookmarkNode* parent, |
| 75 int index) | 63 int index) |
| 76 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 64 : BookmarkUndoOperation(bookmark_model), |
| 77 parent_id_(parent->id()), | 65 parent_id_(parent->id()), |
| 78 index_(index) { | 66 index_(index) { |
| 79 } | 67 } |
| 80 | 68 |
| 81 void BookmarkAddOperation::Undo() { | 69 void BookmarkAddOperation::Undo() { |
| 82 BookmarkModel* model = bookmark_model(); | 70 BookmarkModel* model = bookmark_model(); |
| 83 const BookmarkNode* parent = | 71 const BookmarkNode* parent = |
| 84 bookmarks::GetBookmarkNodeByID(model, parent_id_); | 72 bookmarks::GetBookmarkNodeByID(model, parent_id_); |
| 85 DCHECK(parent); | 73 DCHECK(parent); |
| 86 | 74 |
| 87 model->Remove(parent->GetChild(index_)); | 75 model->Remove(parent->GetChild(index_)); |
| 88 } | 76 } |
| 89 | 77 |
| 90 int BookmarkAddOperation::GetUndoLabelId() const { | 78 int BookmarkAddOperation::GetUndoLabelId() const { |
| 91 return IDS_BOOKMARK_BAR_UNDO_ADD; | 79 return IDS_BOOKMARK_BAR_UNDO_ADD; |
| 92 } | 80 } |
| 93 | 81 |
| 94 int BookmarkAddOperation::GetRedoLabelId() const { | 82 int BookmarkAddOperation::GetRedoLabelId() const { |
| 95 return IDS_BOOKMARK_BAR_REDO_DELETE; | 83 return IDS_BOOKMARK_BAR_REDO_DELETE; |
| 96 } | 84 } |
| 97 | 85 |
| 98 void BookmarkAddOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
| 99 if (parent_id_ == old_id) | |
| 100 parent_id_ = new_id; | |
| 101 } | |
| 102 | |
| 103 // BookmarkRemoveOperation ---------------------------------------------------- | 86 // BookmarkRemoveOperation ---------------------------------------------------- |
| 104 | 87 |
| 105 // Handles the undo of the deletion of a bookmark node. For a bookmark folder, | 88 // Handles the undo of the deletion of a bookmark node. For a bookmark folder, |
| 106 // the information for all descendant bookmark nodes is maintained. | 89 // the information for all descendant bookmark nodes is maintained. |
| 107 // | 90 // |
| 108 // The BookmarkModel allows only single bookmark node to be removed. | 91 // The BookmarkModel allows only single bookmark node to be removed. |
| 109 class BookmarkRemoveOperation : public BookmarkUndoOperation { | 92 class BookmarkRemoveOperation : public BookmarkUndoOperation { |
| 110 public: | 93 public: |
| 111 BookmarkRemoveOperation(BookmarkModel* bookmark_model, | 94 BookmarkRemoveOperation(BookmarkModel* model, |
| 112 BookmarkRenumberObserver* undo_renumber_observer, | 95 BookmarkUndoProvider* undo_provider, |
| 113 const BookmarkNode* parent, | 96 const BookmarkNode* parent, |
| 114 int old_index, | 97 int index, |
| 115 const BookmarkNode* node); | 98 scoped_ptr<BookmarkNode> node); |
| 116 ~BookmarkRemoveOperation() override {} | 99 ~BookmarkRemoveOperation() override; |
| 117 | 100 |
| 118 // UndoOperation: | 101 // UndoOperation: |
| 119 void Undo() override; | 102 void Undo() override; |
| 120 int GetUndoLabelId() const override; | 103 int GetUndoLabelId() const override; |
| 121 int GetRedoLabelId() const override; | 104 int GetRedoLabelId() const override; |
| 122 | 105 |
| 123 // BookmarkRenumberObserver: | |
| 124 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
| 125 | |
| 126 private: | 106 private: |
| 127 void UpdateBookmarkIds(const BookmarkNodeData::Element& element, | 107 BookmarkUndoProvider* undo_provider_; |
| 128 const BookmarkNode* parent, | 108 const int64_t parent_node_id_; |
| 129 int index_added_at); | 109 const int index_; |
| 130 | 110 scoped_ptr<BookmarkNode> node_; |
| 131 int64 parent_id_; | |
| 132 const int old_index_; | |
| 133 BookmarkNodeData removed_node_; | |
| 134 | 111 |
| 135 DISALLOW_COPY_AND_ASSIGN(BookmarkRemoveOperation); | 112 DISALLOW_COPY_AND_ASSIGN(BookmarkRemoveOperation); |
| 136 }; | 113 }; |
| 137 | 114 |
| 138 BookmarkRemoveOperation::BookmarkRemoveOperation( | 115 BookmarkRemoveOperation::BookmarkRemoveOperation( |
| 139 BookmarkModel* bookmark_model, | 116 BookmarkModel* model, |
| 140 BookmarkRenumberObserver* undo_renumber_observer, | 117 BookmarkUndoProvider* undo_provider, |
| 141 const BookmarkNode* parent, | 118 const BookmarkNode* parent, |
| 142 int old_index, | 119 int index, |
| 143 const BookmarkNode* node) | 120 scoped_ptr<BookmarkNode> node) |
| 144 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 121 : BookmarkUndoOperation(model), |
| 145 parent_id_(parent->id()), | 122 undo_provider_(undo_provider), |
| 146 old_index_(old_index), | 123 parent_node_id_(parent->id()), |
| 147 removed_node_(node) { | 124 index_(index), |
| 125 node_(node.Pass()) { |
| 126 } |
| 127 |
| 128 BookmarkRemoveOperation::~BookmarkRemoveOperation() { |
| 148 } | 129 } |
| 149 | 130 |
| 150 void BookmarkRemoveOperation::Undo() { | 131 void BookmarkRemoveOperation::Undo() { |
| 151 DCHECK(removed_node_.is_valid()); | 132 DCHECK(node_.get()); |
| 152 BookmarkModel* model = bookmark_model(); | 133 |
| 153 const BookmarkNode* parent = | 134 const BookmarkNode* parent = bookmarks::GetBookmarkNodeByID( |
| 154 bookmarks::GetBookmarkNodeByID(model, parent_id_); | 135 bookmark_model(), parent_node_id_); |
| 155 DCHECK(parent); | 136 DCHECK(parent); |
| 156 | 137 |
| 157 bookmarks::CloneBookmarkNode( | 138 undo_provider_->RestoreRemovedNode(parent, index_, node_.Pass()); |
| 158 model, removed_node_.elements, parent, old_index_, false); | |
| 159 UpdateBookmarkIds(removed_node_.elements[0], parent, old_index_); | |
| 160 } | 139 } |
| 161 | 140 |
| 162 int BookmarkRemoveOperation::GetUndoLabelId() const { | 141 int BookmarkRemoveOperation::GetUndoLabelId() const { |
| 163 return IDS_BOOKMARK_BAR_UNDO_DELETE; | 142 return IDS_BOOKMARK_BAR_UNDO_DELETE; |
| 164 } | 143 } |
| 165 | 144 |
| 166 int BookmarkRemoveOperation::GetRedoLabelId() const { | 145 int BookmarkRemoveOperation::GetRedoLabelId() const { |
| 167 return IDS_BOOKMARK_BAR_REDO_ADD; | 146 return IDS_BOOKMARK_BAR_REDO_ADD; |
| 168 } | 147 } |
| 169 | 148 |
| 170 void BookmarkRemoveOperation::UpdateBookmarkIds( | |
| 171 const BookmarkNodeData::Element& element, | |
| 172 const BookmarkNode* parent, | |
| 173 int index_added_at) { | |
| 174 const BookmarkNode* node = parent->GetChild(index_added_at); | |
| 175 if (element.id() != node->id()) | |
| 176 undo_renumber_observer()->OnBookmarkRenumbered(element.id(), node->id()); | |
| 177 if (!element.is_url) { | |
| 178 for (int i = 0; i < static_cast<int>(element.children.size()); ++i) | |
| 179 UpdateBookmarkIds(element.children[i], node, i); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 void BookmarkRemoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
| 184 if (parent_id_ == old_id) | |
| 185 parent_id_ = new_id; | |
| 186 } | |
| 187 | |
| 188 // BookmarkEditOperation ------------------------------------------------------ | 149 // BookmarkEditOperation ------------------------------------------------------ |
| 189 | 150 |
| 190 // Handles the undo of the modification of a bookmark node. | 151 // Handles the undo of the modification of a bookmark node. |
| 191 class BookmarkEditOperation : public BookmarkUndoOperation { | 152 class BookmarkEditOperation : public BookmarkUndoOperation { |
| 192 public: | 153 public: |
| 193 BookmarkEditOperation(BookmarkModel* bookmark_model, | 154 BookmarkEditOperation(BookmarkModel* bookmark_model, |
| 194 BookmarkRenumberObserver* undo_renumber_observer, | |
| 195 const BookmarkNode* node); | 155 const BookmarkNode* node); |
| 196 ~BookmarkEditOperation() override {} | 156 ~BookmarkEditOperation() override {} |
| 197 | 157 |
| 198 // UndoOperation: | 158 // UndoOperation: |
| 199 void Undo() override; | 159 void Undo() override; |
| 200 int GetUndoLabelId() const override; | 160 int GetUndoLabelId() const override; |
| 201 int GetRedoLabelId() const override; | 161 int GetRedoLabelId() const override; |
| 202 | 162 |
| 203 // BookmarkRenumberObserver: | |
| 204 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
| 205 | |
| 206 private: | 163 private: |
| 207 int64 node_id_; | 164 int64 node_id_; |
| 208 BookmarkNodeData original_bookmark_; | 165 BookmarkNodeData original_bookmark_; |
| 209 | 166 |
| 210 DISALLOW_COPY_AND_ASSIGN(BookmarkEditOperation); | 167 DISALLOW_COPY_AND_ASSIGN(BookmarkEditOperation); |
| 211 }; | 168 }; |
| 212 | 169 |
| 213 BookmarkEditOperation::BookmarkEditOperation( | 170 BookmarkEditOperation::BookmarkEditOperation( |
| 214 BookmarkModel* bookmark_model, | 171 BookmarkModel* bookmark_model, |
| 215 BookmarkRenumberObserver* undo_renumber_observer, | |
| 216 const BookmarkNode* node) | 172 const BookmarkNode* node) |
| 217 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 173 : BookmarkUndoOperation(bookmark_model), |
| 218 node_id_(node->id()), | 174 node_id_(node->id()), |
| 219 original_bookmark_(node) { | 175 original_bookmark_(node) { |
| 220 } | 176 } |
| 221 | 177 |
| 222 void BookmarkEditOperation::Undo() { | 178 void BookmarkEditOperation::Undo() { |
| 223 DCHECK(original_bookmark_.is_valid()); | 179 DCHECK(original_bookmark_.is_valid()); |
| 224 BookmarkModel* model = bookmark_model(); | 180 BookmarkModel* model = bookmark_model(); |
| 225 const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, node_id_); | 181 const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, node_id_); |
| 226 DCHECK(node); | 182 DCHECK(node); |
| 227 | 183 |
| 228 model->SetTitle(node, original_bookmark_.elements[0].title); | 184 model->SetTitle(node, original_bookmark_.elements[0].title); |
| 229 if (original_bookmark_.elements[0].is_url) | 185 if (original_bookmark_.elements[0].is_url) |
| 230 model->SetURL(node, original_bookmark_.elements[0].url); | 186 model->SetURL(node, original_bookmark_.elements[0].url); |
| 231 } | 187 } |
| 232 | 188 |
| 233 int BookmarkEditOperation::GetUndoLabelId() const { | 189 int BookmarkEditOperation::GetUndoLabelId() const { |
| 234 return IDS_BOOKMARK_BAR_UNDO_EDIT; | 190 return IDS_BOOKMARK_BAR_UNDO_EDIT; |
| 235 } | 191 } |
| 236 | 192 |
| 237 int BookmarkEditOperation::GetRedoLabelId() const { | 193 int BookmarkEditOperation::GetRedoLabelId() const { |
| 238 return IDS_BOOKMARK_BAR_REDO_EDIT; | 194 return IDS_BOOKMARK_BAR_REDO_EDIT; |
| 239 } | 195 } |
| 240 | 196 |
| 241 void BookmarkEditOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
| 242 if (node_id_ == old_id) | |
| 243 node_id_ = new_id; | |
| 244 } | |
| 245 | |
| 246 // BookmarkMoveOperation ------------------------------------------------------ | 197 // BookmarkMoveOperation ------------------------------------------------------ |
| 247 | 198 |
| 248 // Handles the undo of a bookmark being moved to a new location. | 199 // Handles the undo of a bookmark being moved to a new location. |
| 249 class BookmarkMoveOperation : public BookmarkUndoOperation { | 200 class BookmarkMoveOperation : public BookmarkUndoOperation { |
| 250 public: | 201 public: |
| 251 BookmarkMoveOperation(BookmarkModel* bookmark_model, | 202 BookmarkMoveOperation(BookmarkModel* bookmark_model, |
| 252 BookmarkRenumberObserver* undo_renumber_observer, | |
| 253 const BookmarkNode* old_parent, | 203 const BookmarkNode* old_parent, |
| 254 int old_index, | 204 int old_index, |
| 255 const BookmarkNode* new_parent, | 205 const BookmarkNode* new_parent, |
| 256 int new_index); | 206 int new_index); |
| 257 ~BookmarkMoveOperation() override {} | 207 ~BookmarkMoveOperation() override {} |
| 258 int GetUndoLabelId() const override; | 208 int GetUndoLabelId() const override; |
| 259 int GetRedoLabelId() const override; | 209 int GetRedoLabelId() const override; |
| 260 | 210 |
| 261 // UndoOperation: | 211 // UndoOperation: |
| 262 void Undo() override; | 212 void Undo() override; |
| 263 | 213 |
| 264 // BookmarkRenumberObserver: | |
| 265 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
| 266 | |
| 267 private: | 214 private: |
| 268 int64 old_parent_id_; | 215 int64 old_parent_id_; |
| 269 int64 new_parent_id_; | 216 int64 new_parent_id_; |
| 270 int old_index_; | 217 int old_index_; |
| 271 int new_index_; | 218 int new_index_; |
| 272 | 219 |
| 273 DISALLOW_COPY_AND_ASSIGN(BookmarkMoveOperation); | 220 DISALLOW_COPY_AND_ASSIGN(BookmarkMoveOperation); |
| 274 }; | 221 }; |
| 275 | 222 |
| 276 BookmarkMoveOperation::BookmarkMoveOperation( | 223 BookmarkMoveOperation::BookmarkMoveOperation( |
| 277 BookmarkModel* bookmark_model, | 224 BookmarkModel* bookmark_model, |
| 278 BookmarkRenumberObserver* undo_renumber_observer, | |
| 279 const BookmarkNode* old_parent, | 225 const BookmarkNode* old_parent, |
| 280 int old_index, | 226 int old_index, |
| 281 const BookmarkNode* new_parent, | 227 const BookmarkNode* new_parent, |
| 282 int new_index) | 228 int new_index) |
| 283 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 229 : BookmarkUndoOperation(bookmark_model), |
| 284 old_parent_id_(old_parent->id()), | 230 old_parent_id_(old_parent->id()), |
| 285 new_parent_id_(new_parent->id()), | 231 new_parent_id_(new_parent->id()), |
| 286 old_index_(old_index), | 232 old_index_(old_index), |
| 287 new_index_(new_index) { | 233 new_index_(new_index) { |
| 288 } | 234 } |
| 289 | 235 |
| 290 void BookmarkMoveOperation::Undo() { | 236 void BookmarkMoveOperation::Undo() { |
| 291 BookmarkModel* model = bookmark_model(); | 237 BookmarkModel* model = bookmark_model(); |
| 292 const BookmarkNode* old_parent = | 238 const BookmarkNode* old_parent = |
| 293 bookmarks::GetBookmarkNodeByID(model, old_parent_id_); | 239 bookmarks::GetBookmarkNodeByID(model, old_parent_id_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 309 } | 255 } |
| 310 | 256 |
| 311 int BookmarkMoveOperation::GetUndoLabelId() const { | 257 int BookmarkMoveOperation::GetUndoLabelId() const { |
| 312 return IDS_BOOKMARK_BAR_UNDO_MOVE; | 258 return IDS_BOOKMARK_BAR_UNDO_MOVE; |
| 313 } | 259 } |
| 314 | 260 |
| 315 int BookmarkMoveOperation::GetRedoLabelId() const { | 261 int BookmarkMoveOperation::GetRedoLabelId() const { |
| 316 return IDS_BOOKMARK_BAR_REDO_MOVE; | 262 return IDS_BOOKMARK_BAR_REDO_MOVE; |
| 317 } | 263 } |
| 318 | 264 |
| 319 void BookmarkMoveOperation::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | |
| 320 if (old_parent_id_ == old_id) | |
| 321 old_parent_id_ = new_id; | |
| 322 if (new_parent_id_ == old_id) | |
| 323 new_parent_id_ = new_id; | |
| 324 } | |
| 325 | |
| 326 // BookmarkReorderOperation --------------------------------------------------- | 265 // BookmarkReorderOperation --------------------------------------------------- |
| 327 | 266 |
| 328 // Handle the undo of reordering of bookmarks that can happen as a result of | 267 // Handle the undo of reordering of bookmarks that can happen as a result of |
| 329 // sorting a bookmark folder by name or the undo of that operation. The change | 268 // sorting a bookmark folder by name or the undo of that operation. The change |
| 330 // of order is not recursive so only the order of the immediate children of the | 269 // of order is not recursive so only the order of the immediate children of the |
| 331 // folder need to be restored. | 270 // folder need to be restored. |
| 332 class BookmarkReorderOperation : public BookmarkUndoOperation { | 271 class BookmarkReorderOperation : public BookmarkUndoOperation { |
| 333 public: | 272 public: |
| 334 BookmarkReorderOperation(BookmarkModel* bookmark_model, | 273 BookmarkReorderOperation(BookmarkModel* bookmark_model, |
| 335 BookmarkRenumberObserver* undo_renumber_observer, | |
| 336 const BookmarkNode* parent); | 274 const BookmarkNode* parent); |
| 337 ~BookmarkReorderOperation() override; | 275 ~BookmarkReorderOperation() override; |
| 338 | 276 |
| 339 // UndoOperation: | 277 // UndoOperation: |
| 340 void Undo() override; | 278 void Undo() override; |
| 341 int GetUndoLabelId() const override; | 279 int GetUndoLabelId() const override; |
| 342 int GetRedoLabelId() const override; | 280 int GetRedoLabelId() const override; |
| 343 | 281 |
| 344 // BookmarkRenumberObserver: | |
| 345 void OnBookmarkRenumbered(int64 old_id, int64 new_id) override; | |
| 346 | |
| 347 private: | 282 private: |
| 348 int64 parent_id_; | 283 int64 parent_id_; |
| 349 std::vector<int64> ordered_bookmarks_; | 284 std::vector<int64> ordered_bookmarks_; |
| 350 | 285 |
| 351 DISALLOW_COPY_AND_ASSIGN(BookmarkReorderOperation); | 286 DISALLOW_COPY_AND_ASSIGN(BookmarkReorderOperation); |
| 352 }; | 287 }; |
| 353 | 288 |
| 354 BookmarkReorderOperation::BookmarkReorderOperation( | 289 BookmarkReorderOperation::BookmarkReorderOperation( |
| 355 BookmarkModel* bookmark_model, | 290 BookmarkModel* bookmark_model, |
| 356 BookmarkRenumberObserver* undo_renumber_observer, | |
| 357 const BookmarkNode* parent) | 291 const BookmarkNode* parent) |
| 358 : BookmarkUndoOperation(bookmark_model, undo_renumber_observer), | 292 : BookmarkUndoOperation(bookmark_model), |
| 359 parent_id_(parent->id()) { | 293 parent_id_(parent->id()) { |
| 360 ordered_bookmarks_.resize(parent->child_count()); | 294 ordered_bookmarks_.resize(parent->child_count()); |
| 361 for (int i = 0; i < parent->child_count(); ++i) | 295 for (int i = 0; i < parent->child_count(); ++i) |
| 362 ordered_bookmarks_[i] = parent->GetChild(i)->id(); | 296 ordered_bookmarks_[i] = parent->GetChild(i)->id(); |
| 363 } | 297 } |
| 364 | 298 |
| 365 BookmarkReorderOperation::~BookmarkReorderOperation() { | 299 BookmarkReorderOperation::~BookmarkReorderOperation() { |
| 366 } | 300 } |
| 367 | 301 |
| 368 void BookmarkReorderOperation::Undo() { | 302 void BookmarkReorderOperation::Undo() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 381 } | 315 } |
| 382 | 316 |
| 383 int BookmarkReorderOperation::GetUndoLabelId() const { | 317 int BookmarkReorderOperation::GetUndoLabelId() const { |
| 384 return IDS_BOOKMARK_BAR_UNDO_REORDER; | 318 return IDS_BOOKMARK_BAR_UNDO_REORDER; |
| 385 } | 319 } |
| 386 | 320 |
| 387 int BookmarkReorderOperation::GetRedoLabelId() const { | 321 int BookmarkReorderOperation::GetRedoLabelId() const { |
| 388 return IDS_BOOKMARK_BAR_REDO_REORDER; | 322 return IDS_BOOKMARK_BAR_REDO_REORDER; |
| 389 } | 323 } |
| 390 | 324 |
| 391 void BookmarkReorderOperation::OnBookmarkRenumbered(int64 old_id, | |
| 392 int64 new_id) { | |
| 393 if (parent_id_ == old_id) | |
| 394 parent_id_ = new_id; | |
| 395 for (size_t i = 0; i < ordered_bookmarks_.size(); ++i) { | |
| 396 if (ordered_bookmarks_[i] == old_id) | |
| 397 ordered_bookmarks_[i] = new_id; | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 } // namespace | 325 } // namespace |
| 402 | 326 |
| 403 // BookmarkUndoService -------------------------------------------------------- | 327 // BookmarkUndoService -------------------------------------------------------- |
| 404 | 328 |
| 405 BookmarkUndoService::BookmarkUndoService() : scoped_observer_(this) { | 329 BookmarkUndoService::BookmarkUndoService() |
| 330 : model_(nullptr), scoped_observer_(this) { |
| 406 } | 331 } |
| 407 | 332 |
| 408 BookmarkUndoService::~BookmarkUndoService() { | 333 BookmarkUndoService::~BookmarkUndoService() { |
| 409 } | 334 } |
| 410 | 335 |
| 411 void BookmarkUndoService::Start(BookmarkModel* model) { | 336 void BookmarkUndoService::Start(BookmarkModel* model) { |
| 337 DCHECK(!model_); |
| 338 model_ = model; |
| 412 scoped_observer_.Add(model); | 339 scoped_observer_.Add(model); |
| 340 model->SetUndoDelegate(this); |
| 413 } | 341 } |
| 414 | 342 |
| 415 void BookmarkUndoService::Shutdown() { | 343 void BookmarkUndoService::Shutdown() { |
| 344 DCHECK(model_); |
| 416 scoped_observer_.RemoveAll(); | 345 scoped_observer_.RemoveAll(); |
| 346 model_->SetUndoDelegate(nullptr); |
| 417 } | 347 } |
| 418 | 348 |
| 419 void BookmarkUndoService::BookmarkModelLoaded(BookmarkModel* model, | 349 void BookmarkUndoService::BookmarkModelLoaded(BookmarkModel* model, |
| 420 bool ids_reassigned) { | 350 bool ids_reassigned) { |
| 421 undo_manager_.RemoveAllOperations(); | 351 undo_manager_.RemoveAllOperations(); |
| 422 } | 352 } |
| 423 | 353 |
| 424 void BookmarkUndoService::BookmarkModelBeingDeleted(BookmarkModel* model) { | 354 void BookmarkUndoService::BookmarkModelBeingDeleted(BookmarkModel* model) { |
| 425 undo_manager_.RemoveAllOperations(); | 355 undo_manager_.RemoveAllOperations(); |
| 426 } | 356 } |
| 427 | 357 |
| 428 void BookmarkUndoService::BookmarkNodeMoved(BookmarkModel* model, | 358 void BookmarkUndoService::BookmarkNodeMoved(BookmarkModel* model, |
| 429 const BookmarkNode* old_parent, | 359 const BookmarkNode* old_parent, |
| 430 int old_index, | 360 int old_index, |
| 431 const BookmarkNode* new_parent, | 361 const BookmarkNode* new_parent, |
| 432 int new_index) { | 362 int new_index) { |
| 433 scoped_ptr<UndoOperation> op(new BookmarkMoveOperation( | 363 scoped_ptr<UndoOperation> op(new BookmarkMoveOperation( |
| 434 model, this, old_parent, old_index, new_parent, new_index)); | 364 model, old_parent, old_index, new_parent, new_index)); |
| 435 undo_manager()->AddUndoOperation(op.Pass()); | 365 undo_manager()->AddUndoOperation(op.Pass()); |
| 436 } | 366 } |
| 437 | 367 |
| 438 void BookmarkUndoService::BookmarkNodeAdded(BookmarkModel* model, | 368 void BookmarkUndoService::BookmarkNodeAdded(BookmarkModel* model, |
| 439 const BookmarkNode* parent, | 369 const BookmarkNode* parent, |
| 440 int index) { | 370 int index) { |
| 441 scoped_ptr<UndoOperation> op( | 371 scoped_ptr<UndoOperation> op( |
| 442 new BookmarkAddOperation(model, this, parent, index)); | 372 new BookmarkAddOperation(model, parent, index)); |
| 443 undo_manager()->AddUndoOperation(op.Pass()); | 373 undo_manager()->AddUndoOperation(op.Pass()); |
| 444 } | 374 } |
| 445 | 375 |
| 446 void BookmarkUndoService::OnWillRemoveBookmarks(BookmarkModel* model, | |
| 447 const BookmarkNode* parent, | |
| 448 int old_index, | |
| 449 const BookmarkNode* node) { | |
| 450 scoped_ptr<UndoOperation> op( | |
| 451 new BookmarkRemoveOperation(model, this, parent, old_index, node)); | |
| 452 undo_manager()->AddUndoOperation(op.Pass()); | |
| 453 } | |
| 454 | |
| 455 void BookmarkUndoService::OnWillRemoveAllUserBookmarks(BookmarkModel* model) { | |
| 456 bookmarks::ScopedGroupBookmarkActions merge_removes(model); | |
| 457 for (int i = 0; i < model->root_node()->child_count(); ++i) { | |
| 458 const BookmarkNode* permanent_node = model->root_node()->GetChild(i); | |
| 459 for (int j = permanent_node->child_count() - 1; j >= 0; --j) { | |
| 460 scoped_ptr<UndoOperation> op(new BookmarkRemoveOperation( | |
| 461 model, this, permanent_node, j, permanent_node->GetChild(j))); | |
| 462 undo_manager()->AddUndoOperation(op.Pass()); | |
| 463 } | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 void BookmarkUndoService::OnWillChangeBookmarkNode(BookmarkModel* model, | 376 void BookmarkUndoService::OnWillChangeBookmarkNode(BookmarkModel* model, |
| 468 const BookmarkNode* node) { | 377 const BookmarkNode* node) { |
| 469 scoped_ptr<UndoOperation> op(new BookmarkEditOperation(model, this, node)); | 378 scoped_ptr<UndoOperation> op(new BookmarkEditOperation(model, node)); |
| 470 undo_manager()->AddUndoOperation(op.Pass()); | 379 undo_manager()->AddUndoOperation(op.Pass()); |
| 471 } | 380 } |
| 472 | 381 |
| 473 void BookmarkUndoService::OnWillReorderBookmarkNode(BookmarkModel* model, | 382 void BookmarkUndoService::OnWillReorderBookmarkNode(BookmarkModel* model, |
| 474 const BookmarkNode* node) { | 383 const BookmarkNode* node) { |
| 475 scoped_ptr<UndoOperation> op(new BookmarkReorderOperation(model, this, node)); | 384 scoped_ptr<UndoOperation> op(new BookmarkReorderOperation(model, node)); |
| 476 undo_manager()->AddUndoOperation(op.Pass()); | 385 undo_manager()->AddUndoOperation(op.Pass()); |
| 477 } | 386 } |
| 478 | 387 |
| 479 void BookmarkUndoService::GroupedBookmarkChangesBeginning( | 388 void BookmarkUndoService::GroupedBookmarkChangesBeginning( |
| 480 BookmarkModel* model) { | 389 BookmarkModel* model) { |
| 481 undo_manager()->StartGroupingActions(); | 390 undo_manager()->StartGroupingActions(); |
| 482 } | 391 } |
| 483 | 392 |
| 484 void BookmarkUndoService::GroupedBookmarkChangesEnded(BookmarkModel* model) { | 393 void BookmarkUndoService::GroupedBookmarkChangesEnded(BookmarkModel* model) { |
| 485 undo_manager()->EndGroupingActions(); | 394 undo_manager()->EndGroupingActions(); |
| 486 } | 395 } |
| 487 | 396 |
| 488 void BookmarkUndoService::OnBookmarkRenumbered(int64 old_id, int64 new_id) { | 397 void BookmarkUndoService::SetUndoProvider(BookmarkUndoProvider* undo_provider) { |
| 489 std::vector<UndoOperation*> all_operations = | 398 undo_provider_ = undo_provider; |
| 490 undo_manager()->GetAllUndoOperations(); | |
| 491 for (UndoOperation* op : all_operations) { | |
| 492 static_cast<BookmarkUndoOperation*>(op) | |
| 493 ->OnBookmarkRenumbered(old_id, new_id); | |
| 494 } | |
| 495 } | 399 } |
| 400 |
| 401 void BookmarkUndoService::OnBookmarkNodeRemoved(BookmarkModel* model, |
| 402 const BookmarkNode* parent, |
| 403 int index, |
| 404 scoped_ptr<BookmarkNode> node) { |
| 405 DCHECK(undo_provider_); |
| 406 scoped_ptr<UndoOperation> op(new BookmarkRemoveOperation( |
| 407 model, undo_provider_, parent, index, node.Pass())); |
| 408 undo_manager()->AddUndoOperation(op.Pass()); |
| 409 } |
| OLD | NEW |